From ad23b6ecee58d4e474ed577085925626ba677653 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Mon, 25 Sep 2023 13:04:02 +0200 Subject: [PATCH] Check power usage --- resources/js/main.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/resources/js/main.js b/resources/js/main.js index c1fddec..f80df31 100644 --- a/resources/js/main.js +++ b/resources/js/main.js @@ -109,7 +109,7 @@ async function startup() { device_power_state_entry.appendChild(device_power_state); - if (devices[i][2] == true) { + if (devices[i][2] == true && device_state["power_draw"] < 15) { let device_power_on = document.createElement('button'); device_power_on.innerText = "On" device_power_on.onclick = async () => { await power_on(device_id) }; @@ -179,7 +179,19 @@ async function power_on(plug) { } async function power_off(plug) { - await change_plug_state(plug, "power", "off") + // get plug status + const device_status_response = await fetch( + "/plug_state/" + device_id, + { + method: "GET" + } + ); + + let device_state = JSON.parse(await device_status_response.json()); + + if (device_state["power_draw"] < 15) { + await change_plug_state(plug, "power", "off") + } } async function change_device_name(plug, name) {