This content originally appeared on DEV Community and was authored by DEV Community
Over 6 months have passed since I first made this indicator light, and while it was good during previous outbreaks, the increasing availability of vaccinations in Australia means runs of 0 cases is less important. More important is getting jabbed! So I updated my light to track daily jabs!
The New Colour Code:
- No data yet: White
- Under 25,000 jabs in NSW today: Red
- Under 80,000 jabs: Blue
- Under 80,000 jabs: Green
- 100,000 plus jabs: Party Mode!
Finding The Data
Just like the original build, finding a data source to use was critical to making it work. Data NSW didn't have vaccination data available that I could find and I didn't want to go down the path of searching for different API's hoping that one would have the data I wanted.
The COVID LIVE website has a page with NSW Daily vaccinations on it in table form! There's also a nice JSON file with all the data that it's creator Migga told me about when he came across my indicator light the first time. The Electric Imp agent has a 2048kB memory limit though, so I can't load the full JSON file in. The page with the table on it will have to do!
The source code of the website makes it easy to extract the number I want to make the light colour decision on. I need to find the last column in the first table row (not table header) and understand that number.
Making It Light Up
I used the code from the previous version that tracked daily cases of known local transmission as the starting point. It has code on the device to set the lights colour based on the number it received. And the agent has code to make a web request and massage data in the response to determine the number and send it oto the device.
I started with the device code (code that runs on the hardware), changing the numbers for each colour. Then I reversed the order of the logic. This meant it would check if it was going to be party mode first, then work back towards 0 for red, and finally treat any other response (i.e. negative numbers) as an indication to show white. The main driver behind reversing the order of the code was to make it so I only needed to disable party mode once, instead of disabling it for each colour. Once done this was the device code:
Once I had the device code updated (and had tested it by feeding it some hardcoded data) I moved on to writing the agent code (code that runs in the cloud).
I renamed variables and functions to represent what the code would now be doing, then started writing code to parse the HTML that I was extracting the data from. To extract the data I:
- Found the first line of the table rows
- Found the end of the opening tag for the net change column
- Removed everything prior to that
- Found the first closing tag (which could either be the a span or a td tag)
- Removed it and everything after it
- Checked if there was still an opening tag that needed removing
- Removed it if there was
- Checked if all I had left was a "-" character (indicating that there was no data for the day yet)
- If there wasn't, removed all commas from the string and turned it into an integer
I tried thinking of other ways I could simplify the code for extracting this data, but there weren't any libraries or functions I could see that would help. This was the agent code once I was finished:
Closing
Now that I have it updated and have tested that it is working (and fixed the edge cases I didn't think of when I was first coding it) I have it sitting somewhere a bit more visible on my desk, waiting for the day it first goes multicoloured.
Personally, I'm hoping it does that on tomorrow because I am getting jabbed today (and that's when today's numbers will be shown).
This content originally appeared on DEV Community and was authored by DEV Community
DEV Community | Sciencx (2021-08-08T23:45:00+00:00) Updating My “Get On The Beers” Indicator To Track Vaccinations. Retrieved from https://www.scien.cx/2021/08/08/updating-my-get-on-the-beers-indicator-to-track-vaccinations/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.