Created
November 12, 2023 10:46
-
-
Save Dzhuneyt/21e4826c05b8c587ac6bb58b1d486fb0 to your computer and use it in GitHub Desktop.
Revisions
-
Dzhuneyt created this gist
Nov 12, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,65 @@ 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 for the relay to be off toggleTime: 30, //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 || error_code === -104) { print("Failed to fetch ", CONFIG.endpoints[endpointIdx]); failCounter++; print("Rotating through endpoints"); endpointIdx++; endpointIdx = endpointIdx % CONFIG.endpoints.length; } else { failCounter = 0; print("Ping succeeded") } if (failCounter >= CONFIG.numberOfFails) { print("Too many fails, resetting..."); failCounter = 0; Timer.clear(pingTimer); //set the output with toggling back Shelly.call("Shelly.Reboot"); return; } } ); } print("Start watchdog timer"); pingTimer = Timer.set(CONFIG.pingTime * 1000, true, pingEndpoints); Shelly.addStatusHandler(function (status) { //is the component a switch if(status.name !== "switch") return; //is it the one with id 0 if(status.id !== 0) return; //does it have a delta.source property if(typeof status.delta.source === "undefined") return; //is the source a timer if(status.delta.source !== "timer") return; //is it turned on if(status.delta.output !== true) return; //start the loop to ping the endpoints again pingTimer = Timer.set(CONFIG.pingTime * 1000, true, pingEndpoints); });