Skip to content

Instantly share code, notes, and snippets.

@mschmitt
Last active September 26, 2024 19:16
Show Gist options
  • Select an option

  • Save mschmitt/19acc2c2503eccea021f67a3ac1cb56d to your computer and use it in GitHub Desktop.

Select an option

Save mschmitt/19acc2c2503eccea021f67a3ac1cb56d to your computer and use it in GitHub Desktop.
/*
Casually working on a shelly script to automatically restart
an unstable connection based on something.
In this case, floating average of ping measurements from an URL.
Very early prototype.
*/
const cycle_if_over_ms = 4.0;
const leave_off_for_secs = 60;
const check_interval_secs = 3600;
const mock_ms_url = "http://192.168.1.11/shelly-test/test.json";
let current_ms = 0.0;
function check_need_toggle(result, error_code, error_message) {
print("check_need_toggle");
print("HTTP status code and message:", result.code, result.message);
if(result.code !== 200) {
print("Refusing to act on non-OK/200 HTTP status");
return;
}
print("Response body:");
print(result.body);
let current_ms = (JSON.parse(result.body)).ms;
print("current_ms:", current_ms);
print("cycle_if_over_ms:", cycle_if_over_ms);
if (current_ms > cycle_if_over_ms) {
print("Toggling power");
Shelly.call("Switch.Set", {id: 0, on: false, toggle_after: leave_off_for_secs});
}else{
print("Not acting, unmet condition");
}
}
function get_status() {
print("get_status", mock_ms_url);
Shelly.call("HTTP.GET", {url: mock_ms_url}, check_need_toggle);
}
print("Start timer");
let timer = Timer.set(check_interval_secs * 1000, true, get_status);
print("Initial invocation");
get_status();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment