-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxdrip-get-entries.sh
executable file
·169 lines (134 loc) · 5.36 KB
/
xdrip-get-entries.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash
# requirements
# bt-device --> bluez-tools
# sudo apt-get install bluez-tools
# xdrip-js
#
cd /root/src/xdrip-js-logger
echo "Starting xdrip-get-entries.sh"
date
if [ -e "./entry.json" ] ; then
lastGlucose=$(cat ./entry.json | jq -M '.[0].unfiltered')
lastUnfiltered=$(cat ./entry.json | jq -M '.[0].unfiltered')
mv ./entry.json ./last-entry.json
fi
transmitter=$1
id2=$(echo "${transmitter: -2}")
id="Dexcom${id2}"
echo "Removing existing Dexcom bluetooth connection = ${id}"
bt-device -r $id
echo "Calling xdrip-js ... node logger $transmitter"
timeout 360s node logger $transmitter
#DEBUG=transmitter,bluetooth-manager node logger 410BFE
echo "after xdrip-js bg record below ..."
cat ./entry.json
glucose=$(cat ./entry.json | jq -M '.[0].unfiltered')
echo
if [ "${glucose}" == "" ] ; then
echo "Invalid response from g5 transmitter"
ls -al ./entry.json
cat ./entry.json
rm ./entry.json
else
dg=`expr $glucose - $lastGlucose`
# begin try out averaging last two entries ...
da=${dg}
if [ ${da} -lt 0 ]; then
da=`expr 0 - $da`
fi
if [ ${da} -lt 45 -a ${da} -gt 6 ]; then
echo "Before Average last 2 entries - lastGlucose=$lastGlucose, dg=$dg, glucose=${glucose}"
glucose=`expr $glucose + $lastGlucose`
glucose=`expr $glucose / 2`
dg=`expr $glucose - $lastGlucose`
echo "After Average last 2 entries - lastGlucose=$lastGlucose, dg=$dg, glucose=${glucose}"
fi
# end average last two entries if noise code
# log to csv file for research g5 outputs
unfiltered=$(cat ./entry.json | jq -M '.[0].unfiltered')
filtered=$(cat ./entry.json | jq -M '.[0].filtered')
glucoseg5=$(cat ./entry.json | jq -M '.[0].glucose')
# logdate=$(cat ./entry.json | jq -M '.[0].dateString')
datetime=$(date +"%Y-%m-%d %H:%M")
# end log to csv file logic for g5 outputs
# begin calibration logic - look for calibration from NS, use existing calibration or none
calibration=0
ns_url="${NIGHTSCOUT_HOST}"
METERBG_NS_RAW="meterbg_ns_raw.json"
CALIBRATION_STORAGE="calibration.json"
curl -m 30 "${ns_url}/api/v1/treatments.json?find\[created_at\]\[\$gte\]=$(date -d "7 minutes ago" -Iminutes -u)&find\[eventType\]\[\$regex\]=Check" 2>/dev/null > $METERBG_NS_RAW
meterbgunits=$(cat $METERBG_NS_RAW | jq -M '.[0] | .units')
meterbg=$(cat $METERBG_NS_RAW | jq -M '.[0] | .glucose')
meterbg="${meterbg%\"}"
meterbg="${meterbg#\"}"
if [ "$meterbgunits" == "mmol" ]; then
meterbg=$(echo $meterbg "*18" | bc)
fi
if [ "$meterbg" == "null" ]; then
:
else
if [ $meterbg -lt 400 -a $meterbg -gt 40 ]; then
calibration=`expr $meterbg - $glucose`
echo "calibration=$calibration, meterbg=$meterbg, glucose=$glucose"
if [ $calibration -lt 30 -a $calibration -gt -50 ]; then
# another safety check, but this is a good calibration
echo "[{\"calibration\":${calibration}}]" > $CALIBRATION_STORAGE
cp $METERBG_NS_RAW meterbg-ns-backup.json
fi
fi
fi
if [ -e $CALIBRATION_STORAGE ]; then
calibration=$(cat $CALIBRATION_STORAGE | jq -M '.[0] | .calibration')
calibratedglucose=`expr $glucose + $calibration`
echo "After calibration calibratedglucose =$calibratedglucose"
fi
cp entry.json entry-before-calibration.json
tmp=$(mktemp)
jq ".[0].glucose = $calibratedglucose" entry.json > "$tmp" && mv "$tmp" entry.json
tmp=$(mktemp)
jq ".[0].sgv = $calibratedglucose" entry.json > "$tmp" && mv "$tmp" entry.json
tmp=$(mktemp)
jq ".[0].device = \"${transmitter}\"" entry.json > "$tmp" && mv "$tmp" entry.json
# end calibration logic
direction='NONE'
echo "Valid response from g5 transmitter"
if [ ${dg} -lt -10 ]; then
direction='DoubleDown'
elif [ ${dg} -lt -7 ]; then
direction='SingleDown'
elif [ ${dg} -lt -3 ]; then
direction='FortyFiveDown'
elif [ ${dg} -lt 3 ]; then
direction='Flat'
elif [ ${dg} -lt 7 ]; then
direction='FortyFiveUp'
elif [ ${dg} -lt 10 ]; then
direction='SingleUp'
elif [ ${dg} -lt 50 ]; then
direction='DoubleUp'
fi
echo "Gluc=${glucose}, last=${lastGlucose}, diff=${dg}, dir=${direction}"
cat entry.json | jq ".[0].direction = \"$direction\"" > entry-xdrip.json
if [ ! -f "/var/log/openaps/g5.csv" ]; then
echo "datetime,unfiltered,filtered,glucoseg5,glucose,calibratedglucose,direction,calibration" > /var/log/openaps/g5.csv
fi
echo "${datetime},${unfiltered},${filtered},${glucoseg5},${glucose},${calibratedglucose},${direction},${calibration}" >> /var/log/openaps/g5.csv
echo "Posting glucose record to xdripAPS"
./post-xdripAPS.sh ./entry-xdrip.json
if [ -e "./entry-backfill.json" ] ; then
# In this case backfill records not yet sent to Nightscout
jq -s add ./entry-xdrip.json ./entry-backfill.json > ./entry-ns.json
cp ./entry-ns.json ./entry-backfill.json
echo "entry-backfill.json exists, so setting up for backfill"
else
echo "entry-backfill.json does not exist so no backfill"
cp ./entry-xdrip.json ./entry-ns.json
fi
echo "Posting blood glucose record(s) to NightScout"
./post-ns.sh ./entry-ns.json && (echo; echo "Upload to NightScout of xdrip entry worked ... removing ./entry-backfill.json"; rm ./entry-backfill.json) || (echo; echo "Upload to NS of xdrip entry did not work ... saving for upload when network is restored"; cp ./entry-ns.json ./entry-backfill.json)
echo
fi
bt-device -r $id
echo "Finished xdrip-get-entries.sh"
date
echo