Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: simplified and updated

...

These rulesets each depend on the one listed before it. The source code for these rulesets is maintained in Pico Labs' GitHub Wovyn repository.

Problem

Over time, the pico has accumulated a very large number of (date-stamped) readings in the levels ruleset. As of this writing, it is holding 68,712 such readings, in an array:

...

Code Block
linenumberstrue
function doPost(e) {
  recordReading(e.parameter);
}

function recordReading(data) {
  var ss = SpreadsheetApp.openById('1i2wraAkeFiQWP...SHAmN3bbo'getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet1');
  sheet.appendRow([data.timestamp,data.concentration]);
}

...

The recordReading function, lines 5-9, identifies our spreadsheet from its (redacted) identifier uses the active spreadsheet (line 6), obtains the sheet in the tab named "Sheet1" (line 7), and appends the reading as a row (line 8).

...

Code Block
linenumberstrue
ruleset wovyn_co2_recorder {
  global {
    url = "https://script.google.com/macros/s/AKfycby7bk...j0oVR_yn-u/exec"
  }
  rule record_co2_level_to_sheet {
    select when wovyn co2_level_recorded where ent:url
    pre {
      ts = time:strftime(event:attr("timestamp"), "%F %T");
      data = {"timestamp": ts, "concentration": event:attr("concentration")}
    }
    http:post(ent:url,qs=data) setting(response)
    fired {
      ent:lastData := data;
      ent:lastResponse := response;
      raise wovyn event "co2_level_recorded_to_sheet" attributes event:attrs()
    }
  }
}

Once First, we install this ruleset is installed in our "Wovyn CO2" pico. Notice that the rule record_co2_level_to_sheet will not be selected until we have provided the URL given to us when we deployed the Google script. We do this by sending our pico an event furnishing the URL, as described in the Keeping keys "safe" in a different way page. As soon as the ruleset has the value for ent:url, it will begin posting a reading to the spreadsheet each time the wovyn_co2_levels ruleset records it internally.

The (redacted) URL given to us when we deployed the Google script is bound to the name url in line 3.

Each time a CO₂ reading is recorded internally to the pico, the record_co2_level_to_sheet rule (lines 52-1714) will select (as declared by line 63). It will throw away the millisecond information from the timestamp and format it in a way acceptable to the spreadsheet (line 85), package it up with the concentration number (line 96), and post it to the Google script (line 118). For debugging purposes, the rule saves the latest data and HTTP reponse (lines 1310-1411). Significantly, the rule raises a (new) wovyn:co2_level_recorded_to_sheet event (line 1512), which we can use in a future rule or ruleset to do other things, such as, for example, prune data held within the pico.

...