Browse Source

Benutze keine anonymen Funktionen mehr um möglicherweise ein Abstürzen zu verhindern

master
Philipp 3 years ago
parent
commit
2e4164d3cf
  1. 38
      switch.js

38
switch.js

@ -1,11 +1,4 @@
function runLoop() { function processHttpResponse(res, error_code, error_msg, ud) {
let alertTimer = Timer.set(
CONFIG.pollingInterval,
true, // Run periodically
function (ud) {
Shelly.call("HTTP.REQUEST",
{ method: "POST", url: CONFIG.pollingUrl, body: '{"801":{"170":null}}', timeout: 10 },
function (res, error_code, error_msg, ud) {
print("errors-http-post:", error_code, error_msg); print("errors-http-post:", error_code, error_msg);
if (error_code === 0) { // // do this so that during timeout no crash if (error_code === 0) { // // do this so that during timeout no crash
let parsedBody = JSON.parse(res.body); let parsedBody = JSON.parse(res.body);
@ -19,7 +12,7 @@ function runLoop() {
threshold = consumption + CONFIG.chargingConsumption; threshold = consumption + CONFIG.chargingConsumption;
} }
// print(threshold); // print(threshold);
if (production > threshold) { if (production> threshold) {
if (!switchIsOpen) { if (!switchIsOpen) {
setSwitch(true); setSwitch(true);
} }
@ -29,23 +22,36 @@ function runLoop() {
} }
} }
} }
}, }
function processTick(ud) {
Shelly.call("HTTP.REQUEST",
{ method: "POST", url: CONFIG.pollingUrl, body: '{"801":{"170":null}}', timeout: 10 },
processHttpResponse,
null null
); );
}, }
function runLoop() {
let alertTimer = Timer.set(
CONFIG.pollingInterval,
true, // Run periodically
processTick,
null null
); );
} }
function setSwitch(on) { function processSwitchSet(res, error_code, error_msg, ud) {
Shelly.call("Switch.Set",
{ id: CONFIG.switchId, on: on },
function (res, error_code, error_msg, ud) {
print("errors-switch-set:", error_code, error_msg); print("errors-switch-set:", error_code, error_msg);
let newSwitchState = !res["was_on"]; let newSwitchState = !res["was_on"];
switchIsOpen = newSwitchState; switchIsOpen = newSwitchState;
print("Changed switch state to:", newSwitchState); print("Changed switch state to:", newSwitchState);
}, }
function setSwitch(on) {
Shelly.call("Switch.Set",
{ id: CONFIG.switchId, on: on },
processSwitchSet,
null null
); );
} }

Loading…
Cancel
Save