Nightscout Plugin (#108)
This commit is contained in:
34
source/plugins/nightscout/README.md
Normal file
34
source/plugins/nightscout/README.md
Normal file
@@ -0,0 +1,34 @@
|
||||
### 💉 Nightscout
|
||||
|
||||
The *Nightscout* plugin lets you display blood sugar values from a [Nightscout](http://nightscout.info) site.
|
||||
|
||||
<table>
|
||||
<td align="center">
|
||||
<img src="https://github.com/legoandmars/legoandmars/blob/master/metrics.plugin.nightscout.svg">
|
||||
<img width="900" height="1" alt="">
|
||||
</td>
|
||||
</table>
|
||||
|
||||
<details>
|
||||
<summary>💬 Setting up a nightscout site</summary>
|
||||
|
||||
The [nightscout website](http://www.nightscout.info/) details how to self-host a nightscout site. Check out the instructions there.
|
||||
|
||||
</details>
|
||||
|
||||
#### ℹ️ Examples workflows
|
||||
|
||||
[➡️ Available options for this plugin](metadata.yml)
|
||||
|
||||
```yaml
|
||||
- uses: lowlighter/metrics@master
|
||||
with:
|
||||
# ... other options
|
||||
plugin_nightscout: yes
|
||||
plugin_nightscout_url: ${{ secrets.NIGHTSCOUT_URL }} # Use the github actions "NIGHTSCOUT_URL" secret as your nightscout site
|
||||
plugin_nightscout_datapoints: 12 # Use the latest 12 blood sugar datapoints to create a graph
|
||||
plugin_nightscout_lowalert: 80 # Blood sugars below 80 will be considered low
|
||||
plugin_nightscout_highalert: 180 # Blood sugars above 180 will be considered high
|
||||
plugin_nightscout_urgentlowalert: 50 # Blood sugars below 50 will be considered urgently low
|
||||
plugin_nightscout_urgenthighalert: 250 # Blood sugars above 250 will be considered urgently high
|
||||
```
|
||||
83
source/plugins/nightscout/index.mjs
Normal file
83
source/plugins/nightscout/index.mjs
Normal file
@@ -0,0 +1,83 @@
|
||||
//Setup
|
||||
export default async function({q, imports, data, account}, {enabled = false} = {}) {
|
||||
//Plugin execution
|
||||
try {
|
||||
//Check if plugin is enabled and requirements are met
|
||||
if ((!enabled)||(!q.nightscout))
|
||||
return null
|
||||
|
||||
//Load inputs
|
||||
let {url, datapoints, lowalert, highalert, urgentlowalert, urgenthighalert} = imports.metadata.plugins.nightscout.inputs({data, account, q})
|
||||
|
||||
if (!url || url === "https://example.herokuapp.com") throw "Nightscout site URL isn't set!"
|
||||
if (url.substring(url.length - 1) !== "/") url += "/"
|
||||
if (url.substring(0, 7) === "http://") url = `https://${url.substring(7)}`
|
||||
if (url.substring(0, 8) !== "https://") url = `https://${url}`
|
||||
if (datapoints <= 0) datapoints = 1
|
||||
//Get nightscout data from axios
|
||||
const resp = await imports.axios.get(`${url}api/v1/entries.json?count=${datapoints}`)
|
||||
for (let i = 0; i < resp.data.length; i++){
|
||||
const {sgv} = resp.data[i]
|
||||
//Add human readable timestamps and arrows
|
||||
const date = new Date(resp.data[i].dateString)
|
||||
resp.data[i].arrowHumanReadable = directionArrow(resp.data[i].direction)
|
||||
resp.data[i].timeUTCHumanReadable = `${addZero(date.getUTCHours())}:${addZero(date.getUTCMinutes())}`
|
||||
/*
|
||||
* Add colors and alert names
|
||||
* TODO: Maybe make colors better themed instead of just the "github style" - red and yellow could fit better than darker shades of green
|
||||
*/
|
||||
let color = "#40c463"
|
||||
let alertName = "Normal"
|
||||
if (sgv >= urgenthighalert || sgv <= urgentlowalert){
|
||||
color = "#216e39"
|
||||
alertName = sgv >= urgenthighalert ? "Urgent High" : "Urgent Low"
|
||||
}
|
||||
else if (sgv >= highalert || sgv <= lowalert){
|
||||
color = "#30a14e"
|
||||
alertName = sgv >= highalert ? "High" : "Low"
|
||||
}
|
||||
resp.data[i].color = color
|
||||
resp.data[i].alert = alertName
|
||||
}
|
||||
return {data:resp.data.reverse()}
|
||||
}
|
||||
//Handle errors
|
||||
catch (error) {
|
||||
throw {error:{message:"An error occured", instance:error}}
|
||||
}
|
||||
}
|
||||
|
||||
function addZero(i) {
|
||||
if (i < 10)
|
||||
i = `0${i}`
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
function directionArrow(direction) {
|
||||
const dir = direction.toUpperCase()
|
||||
switch (dir) {
|
||||
case "NONE":
|
||||
return ""
|
||||
case "DOUBLEUP":
|
||||
return "↑↑"
|
||||
case "SINGLEUP":
|
||||
return "↑"
|
||||
case "FORTYFIVEUP":
|
||||
return "↗"
|
||||
case "FLAT":
|
||||
return "→"
|
||||
case "FORTYFIVEDOWN":
|
||||
return "↘"
|
||||
case "SINGLEDOWN":
|
||||
return "↓"
|
||||
case "DOUBLEDOWN":
|
||||
return "↓↓"
|
||||
case "NOT COMPUTABLE":
|
||||
return ""
|
||||
case "RATE OUT OF RANGE":
|
||||
return ""
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
53
source/plugins/nightscout/metadata.yml
Normal file
53
source/plugins/nightscout/metadata.yml
Normal file
@@ -0,0 +1,53 @@
|
||||
name: "💉 Nightscout"
|
||||
cost: N/A
|
||||
supports:
|
||||
- user
|
||||
- organization
|
||||
inputs:
|
||||
|
||||
# Enable or disable plugin
|
||||
plugin_nightscout:
|
||||
description: Displays Blood Glucose
|
||||
type: boolean
|
||||
default: no
|
||||
|
||||
# Nightscout site URL
|
||||
plugin_nightscout_url:
|
||||
description: Your Nightscout site URL
|
||||
type: string
|
||||
default: https://example.herokuapp.com
|
||||
|
||||
# Controls how big the graph is
|
||||
plugin_nightscout_datapoints:
|
||||
description: How many datapoints to show on the graph. 0 and 1 disable the graph.
|
||||
type: number
|
||||
default: 12
|
||||
min: 0
|
||||
|
||||
# Low value used for colors and text alerts
|
||||
plugin_nightscout_lowalert:
|
||||
description: When the blood sugar is considered low
|
||||
type: number
|
||||
default: 80
|
||||
min: 0
|
||||
|
||||
# High value used for colors and text alerts
|
||||
plugin_nightscout_highalert:
|
||||
description: When the blood sugar is considered high
|
||||
type: number
|
||||
default: 180
|
||||
min: 0
|
||||
|
||||
# Urgent low value used for colors and text alerts
|
||||
plugin_nightscout_urgentlowalert:
|
||||
description: When the blood sugar is considered urgently low
|
||||
type: number
|
||||
default: 50
|
||||
min: 0
|
||||
|
||||
# Urgent high value used for colors and text alerts
|
||||
plugin_nightscout_urgenthighalert:
|
||||
description: When the blood sugar is considered urgently high
|
||||
type: number
|
||||
default: 250
|
||||
min: 0
|
||||
14
source/plugins/nightscout/tests.yml
Normal file
14
source/plugins/nightscout/tests.yml
Normal file
@@ -0,0 +1,14 @@
|
||||
- name: Nightscout plugin (default)
|
||||
uses: lowlighter/metrics@master
|
||||
with:
|
||||
token: NOT_NEEDED
|
||||
plugin_nightscout: yes
|
||||
plugin_nightscout_url: https://testapp.herokuapp.com/
|
||||
|
||||
- name: Nightscout plugin (without graph)
|
||||
uses: lowlighter/metrics@master
|
||||
with:
|
||||
token: NOT_NEEDED
|
||||
plugin_nightscout: yes
|
||||
plugin_nightscout_url: https://testapp.herokuapp.com/
|
||||
plugin_nightscout_datapoints: 0
|
||||
Reference in New Issue
Block a user