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.

Revisions

  1. mschmitt revised this gist Sep 26, 2024. 1 changed file with 20 additions and 19 deletions.
    39 changes: 20 additions & 19 deletions auto-cycle.js
    Original file line number Diff line number Diff line change
    @@ -7,14 +7,22 @@
    Very early prototype.
    */

    const cycle_if_over_ms = 4.0;
    const leave_off_for_secs = 60;
    const check_interval_secs = 3600;
    const act_if_over_ms = 4.0;
    const leave_off_for_secs = 2;
    const check_interval_secs = 10;
    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");
    function act_on_measurement(current_ms) {
    print("function: act_on_measurement()");
    print("current_ms:", current_ms, "- act_if_over_ms:", act_if_over_ms);
    if (current_ms > act_if_over_ms) {
    print("Cycling power");
    Shelly.call("Switch.Set", {id: 0, on: false, toggle_after: leave_off_for_secs});
    }
    }

    function extract_measurement(result, error_code, error_message) {
    print("function: extract_status");
    print("HTTP status code and message:", result.code, result.message);
    if(result.code !== 200) {
    print("Refusing to act on non-OK/200 HTTP status");
    @@ -23,22 +31,15 @@ function check_need_toggle(result, error_code, error_message) {
    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");
    }
    act_on_measurement(current_ms);
    }

    function get_status() {
    print("get_status", mock_ms_url);
    Shelly.call("HTTP.GET", {url: mock_ms_url}, check_need_toggle);
    function retrieve_measurement() {
    print("function: retrieve_measurement - ", mock_ms_url);
    Shelly.call("HTTP.GET", {url: mock_ms_url}, extract_measurement);
    }

    print("Start timer");
    let timer = Timer.set(check_interval_secs * 1000, true, get_status);
    let timer = Timer.set(check_interval_secs * 1000, true, retrieve_measurement);
    print("Initial invocation");
    get_status();
    retrieve_measurement();
  2. mschmitt created this gist Sep 25, 2024.
    44 changes: 44 additions & 0 deletions auto-cycle.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    /*
    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();