-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnightscout.1m.sh
executable file
·72 lines (60 loc) · 1.88 KB
/
nightscout.1m.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
# <bitbar.title>Nightscout Reader</bitbar.title>
# <bitbar.version>0.2.0</bitbar.version>
# <bitbar.desc>For Diabetics using Nightscout to track CGM data: Display current Blood Sugar data and trend from Nightscout</bitbar.desc>
# <bitbar.dependencies>bash, curl, bc, jq</bitbar.dependencies>
# <bitbar.author>Jeremy Hay Draude</bitbar.author>
# <bitbar.author.github>jhaydraude</bitbar.author.github>
# <bitbar.image>https://raw.githubusercontent.com/jhaydraude/NightscoutBitBar/master/Preview.png</bitbar.image>
# <bitbar.abouturl>https://github.com/jhaydraude/NightscoutBitBar/blob/master/README.md</bitbar.abouturl>
export PATH="/usr/local/bin:/usr/bin:$PATH"
if ! [ -x "$(command -v jq)" ]; then
echo 'Error: jq is not installed.'
echo '---'
echo 'Click here to install jq | href=https://stedolan.github.io/jq/download/'
exit 1
fi
NSURL=https://YOURNIGHTSCOUTURL.herokuapp.com # Add your own Nightscout URL here
USEMMOL=true # true if you use mmol/l units. false if you use mg/dl
JSONOUT=$(curl --silent $NSURL/api/v1/entries/current.json)
JSDATE=$(jq '.[0].date' <<< "$JSONOUT")
#NS Returns date as ms since epoch. BASH likes Seconds since epoch
EPOCHTS=$(($JSDATE / 1000))
TIMESTRING=$(date -r $EPOCHTS)
EPOCHNOW=$(date +%s) # Convert current time to epoch time
TIMEDIFF=$(((EPOCHNOW - EPOCHTS)/60)) #calculate the difference
BG=$(jq '.[0].sgv' <<< "$JSONOUT")
TRENDSTR=$(jq -r '.[0].direction' <<< "$JSONOUT")
if $USEMMOL ; then
BG=$(echo "scale=1; $BG / 18" | bc) #Convert mg/dl to mmol/l
fi
case $TRENDSTR in
FortyFiveUp)
TREND='↗'
;;
FortyFiveDown)
TREND='↘'
;;
SingleUp)
TREND='↑'
;;
SingleDown)
TREND='↓'
;;
Flat)
TREND='→'
;;
DoubleUp)
TREND='⇈'
;;
DoubleDown)
TREND='⇊'
;;
*)
TREND=$TRENDSTR
;;
esac
echo "$BG $TREND (${TIMEDIFF}m ago)"
echo "---"
echo "Go to Nightscout | href=$NSURL"
echo "Reading taken: $TIMESTRING"