/agent.nut Secret
Last active
August 8, 2021 09:50
Get On The Beers Light updated to track jabs administered
This file contains 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 characters
dataUrl <- "https://covidlive.com.au/report/daily-vaccinations/nsw"; | |
// COVID LIVE is CC BY 4.0 https://creativecommons.org/licenses/by/4.0/ | |
// Function that will run when the device requests an update | |
function updateDevice(data) { | |
server.log("Update Requested."); | |
local jabs = jabsAdministeredToday(); | |
device.send("update", jabs); | |
} | |
function jabsAdministeredToday() { | |
local request = http.get(dataUrl); | |
local response = request.sendsync(); | |
server.log("Response Status: " + response.statuscode); | |
if (response.statuscode != 200) { | |
server.log("Error: " + response.body); | |
return -1; | |
} | |
local lines = split(response.body, "\r\n"); | |
foreach (line in lines) { | |
// Uncomment the next line to test zero jabs administered | |
// line = "<tr class=\"odd\"><td class=\"COL1 DATE\">16 Feb <span>21</span></td><td class=\"COL2 DOSES\">0</td><td class=\"COL3 VAR\"><span class=\"zero\"> </span></td><td class=\"COL4 NET\"><span>0</span></td></tr>"; | |
if (line.find("<tr ") == 0) { | |
server.log(line); | |
// <tr class="even"><td class="COL1 DATE">06 Aug <span>21</span></td><td class="COL2 DOSES">4,289,913</td><td class="COL3 VAR"><span class="green-up"> </span></td><td class="COL4 NET"><span class="green">93,626</span></td></tr> | |
local netChangeStart = line.find("NET\">") + 5; // Find where the net change start column is | |
line = line.slice(netChangeStart); // Get rid of all data before that | |
local firstCloseTag = line.find("</"); // Find the first close tag now (could be </span> or </td>) | |
line = line.slice(0, firstCloseTag); // Remove everything including and after it | |
local openTagEnd = line.find(">"); // Check to see if we have an open tag at the start | |
if (openTagEnd != null) { | |
line = line.slice(openTagEnd + 1); // if we do it is likely a span tag and needs removing | |
} | |
local jabs = -1; | |
if (line != "-") { | |
// Remove all commas and turn it into a number | |
jabs = split(line, ",").reduce(function(previousValue, currentValue){ | |
return (previousValue + currentValue); | |
}).tointeger(); | |
} | |
server.log("Got " + jabs + " from CovidLive"); | |
return jabs; | |
} | |
} | |
return -1; | |
} | |
device.on("requestUpdate", updateDevice); |
This file contains 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 characters
// Configure the LEDs | |
#require "WS2812.class.nut:3.0.0" | |
spi <- hardware.spi257; | |
pixels <- WS2812(spi, 1); | |
hardware.pin1.configure(DIGITAL_OUT, 1); | |
function updateLight(jabsAdministered) { | |
server.log("Update Recieved: " + jabsAdministered + " Jabs Administered"); | |
if (jabsAdministered >= 100000) { | |
if (!rainbowMode) { // Only enable rainbow mode once | |
server.log("Starting rainbow mode"); | |
rainbowMode = true; // Enable rainbow mode | |
rainbow(); // Start the rainbow loop | |
} | |
} | |
else { | |
rainbowMode = false; // Ensure rainbow mode is disabled | |
if (jabsAdministered >= 80000) { | |
pixels.set(0, [0, 255, 0]).draw(); // Green | |
} | |
else if (jabsAdministered >= 25000) { | |
pixels.set(0, [0, 0, 255]).draw(); // Blue | |
} | |
else if (jabsAdministered >= 0) { | |
pixels.set(0, [255, 0, 0]).draw(); // Red | |
} | |
else { | |
// Something has likely gone wrong default to white | |
pixels.set(0, [255, 255, 255]).draw(); // White | |
} | |
} | |
} | |
// Used for making the light change colours through a rainbow! | |
rainbowMode <- false; // If set to false, rainbow mode will do nothing | |
rainbowStep <- 0; // The current step in the colour wheel | |
function rainbow() { | |
if (rainbowMode) { | |
local colour; | |
// This determines the colour the LED should be set to based on the current step | |
if(rainbowStep < 85) { | |
colour = [rainbowStep * 3, 255 - (rainbowStep * 3), 0]; | |
} | |
else if(rainbowStep < 170) { | |
local diff = rainbowStep - 85; | |
colour = [255 - (diff * 3), 0, diff * 3]; | |
} | |
else { | |
local diff = rainbowStep - 170; | |
colour = [0, diff * 3, 255 - (diff * 3)]; | |
} | |
pixels.set(0, colour).draw(); // Set the LED colour | |
rainbowStep += 5; // Incriment the step count | |
rainbowStep = rainbowStep % 256; // Ensure we cycle back to 0 once we reach the end | |
imp.wakeup(0.05, rainbow); // Briefly wait and then change colours again | |
} | |
} | |
// Call this function when we want to ask for updated numbers | |
function requestUpdate() { | |
agent.send("requestUpdate", null); | |
imp.wakeup(1800, requestUpdate); // After 1800 seconds, request another update | |
} | |
server.log("Device Started"); | |
agent.on("update", updateLight); // when we recieve new numbers update the light | |
requestUpdate(); // Request our first update |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment