Hi,
at first I wanted to say, I'm not a coder by any means
So my goal is to have a script which checks if a Shelly is able to reach the Internet and if not, Shelly should be restarted.
I have tried to modify existing script and my outcome looks like:
Code
// This script tries to execute HTTP GET requests within a set time, against a set of endpoints
// After certain number of failures the script resets the shelly
let CONFIG = {
endpoints: [
"https://global.gcping.com/ping",
"https://us-central1-5tkroniexa-uc.a.run.app/ping",
],
//number of failures that trigger the reset
numberOfFails: 5,
//time in seconds after which the http request is considered failed
httpTimeout: 10,
//time in seconds to retry a "ping"
pingTime: 60,
};
let endpointIdx = 0;
let failCounter = 0;
let pingTimer = null;
function pingEndpoints() {
Shelly.call(
"http.get",
{ url: CONFIG.endpoints[endpointIdx], timeout: CONFIG.httpTimeout },
function (response, error_code, error_message) {
//http timeout, magic number, not yet documented
if (error_code === -114) {
print("Failed to fetch ", CONFIG.endpoints[endpointIdx]);
failCounter++;
print("Rotating through endpoints");
endpointIdx++;
endpointIdx = endpointIdx % CONFIG.endpoints.length;
} else {
failCounter = 0;
}
if (failCounter >= CONFIG.numberOfFails) {
print("Too many fails, resetting...");
failCounter = 0;
Timer.clear(pingTimer);
Shelly.call("Shelly.Reboot");
}
}
);
}
print("Start watchdog timer");
pingTimer = Timer.set(CONFIG.pingTime * 1000, true, pingEndpoints);
Alles anzeigen
I have checked and it looks like the script works. Maybe someone could check/advise if it is ok. Maybe some improvement could be made.
Thanks in advance.