Hello all -
I'm having a hell of a time trying to get a Shelly script to work with Timers. I know what I'm after, but it does seem like the asynchronous execution of this JavaScript is biting me. It's a very simple script, but every time I add another timer it seems like the call stack just balloons into infinity and then I "run out of" timers. Basically, my script needs to:
- Turn the Shelly on.
- Wait two minutes.
- Use the HTTP.Get to check if a certain webpage is up, and print to the console the number of reboots, the number of successes (times it got the webpage) and the number of failures (times it failed to load the webpage).
- Turn the Shelly off.
- Repeat.
In order to repeat this, I use a five second timer to repeat the process... but I'm obviously ALSO using a timer to delay firing off the HTTP.Get part of the script. How do I solve this? If this were a synchronous script, this would be cake, but... it isn't, and async code is something I'm MUCH less experienced with (and extremely frustrated by, but I'm here to learn!).
JavaScript
/*
SHELLY REBOOT STABILITY TEST SCRIPT
This script is intended to test the stability of the example units by
subjecting them to a long series of constant rebooting, checking to see HTTP
data returned successfully after each reboot. We are, obviously, striving for
a "failCount" of zero after a two-week period of testing.
*/
// Basic configuration of the Shelly Plug Plus US
let CONFIG = {
// The endpoint of the unit to test against.
endpoint: 'http://192.168.1.5',
// Time in seconds for the relay to be off
rebootToggleTime: 30
}
// The number of failures it has encountered
let failCount = 0
// The number of reboots it has performed
let rebootCount = 0
function powerOn() {
// Turn the Shelly on
Shelly.call (
"Switch.Set",
{ id: 0, on: true },
);
}
function httpGetAndReboot() {
Shelly.call (
"http.get",
{ url: CONFIG.endpoint, timeout: CONFIG.httpTimeout },
function (response, error_code, error_message) {
//http timeout, magic number, not yet documented
if (error_code === -114 || error_code === -104) {
print("Failed to fetch ", CONFIG.endpoint);
failCount++;
} else {
rebootCount++;
print("Number of Reboots: " + rebootCount);
print("Number of Failures: " + failCount);
print("");
print("Resetting!");
print("");
Shelly.call (
"Switch.Set",
{ id: 0, on: false },
function () {}
);
return;
}
}
);
}
function rebootCycleTest() {
powerOn();
Timer.set(
115000,
false,
httpGetAndReboot
);
}
Timer.set(
5000,
true,
rebootCycleTest
);
Alles anzeigen