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