-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnightscout.py
More file actions
229 lines (205 loc) · 7.14 KB
/
nightscout.py
File metadata and controls
229 lines (205 loc) · 7.14 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import json
import sys
import time
import urllib.request
import urllib.error
from sys import platform
def run(sUrl, iUnit=0, iDisplay=0, bAlert=False, bSpeech=False, bWait=False):
"""
Run(sUrl, iDisplay, bAlert, bSpeech, bWait)
--------------------------------------------------------------------------------------
Keyword arguments:
--------------------------------------------------------------------------------------
sUrl -- String -- Nightscout url
iUnit -- Integer -- 0=Auto, 1=mg/dl, 2=mmol
iDisplay -- Integer -- Option of print glucose data (0=All, 1=Glucose, 2=Direction, 3=Delta, 4=Minutes)
bAlert -- Boolean -- Alarm option (True or False)
bSpeech -- Boolean -- Speech option, with wait every zero min. (True or False)
bWait -- Boolean -- Wait for glucose update (True or False)
--------------------------------------------------------------------------------------
Example: while 1:
ns.run('https://account.herokuapp.com', 0, True, True, True)
--------------------------------------------------------------------------------------
"""
# Load JSON from url
try:
urlEntries = urllib.request.urlopen(sUrl + str('/api/v1/entries/sgv.json?count=2'))
jsonEntries = json.load(urlEntries)
urlStatus = urllib.request.urlopen(sUrl + str('/api/v1/status.json'))
jsonStatus = json.load(urlStatus)
except urllib.error.HTTPError as e:
print(str('HTTP-Error: ') + str(e.code))
sys.exit()
except urllib.error.URLError as e:
print(str('URL-Error: ') + str(e.args))
sys.exit()
# Read glucose values
iSgv = int(jsonEntries[0]['sgv'])
iLastSgv = int(jsonEntries[1]['sgv'])
# Read unix date values diff
iDate = int(jsonEntries[0]['date'])
iLastDate = int(jsonEntries[1]['date'])
iServerTimeEpoch = int(jsonStatus['serverTimeEpoch'])
# Calculate
iMsServerTimeEpochDate = int(iServerTimeEpoch - iDate)
iMsInterval = int(iDate - iLastDate)
iMinInterval = int(round(iMsInterval / 60000, 0))
iMinSecondInterval = int(iMinInterval * 2)
iMin = int(iMsServerTimeEpochDate / 60000)
# Check glucose every minute
fMin = float(iMsServerTimeEpochDate / 60000)
fMinDiff = float(fMin - iMin)
fSeconds = float(60 - fMinDiff * 36)
# Alarm with int values
iBgHigh = int(jsonStatus['settings']['thresholds']['bgHigh'])
iBgTargetTop = int(jsonStatus['settings']['thresholds']['bgTargetTop'])
iBgTargetBottom = int(jsonStatus['settings']['thresholds']['bgTargetBottom'])
iBgLow = int(jsonStatus['settings']['thresholds']['bgLow'])
# String values
sStatus = str(jsonStatus['status'])
sUnits = str(jsonStatus['settings']['units'])
# Check status of Nightscout
if str('ok') not in sStatus:
print(str('Status: [' + sStatus + ']'))
# iUnit
i_fSgv = ''
i_fLastSgv = ''
fCalcSgvMmol = round(float(iSgv * 0.0555), 1)
fCalcLastSgvMmol = round(float(iLastSgv * 0.0555), 1)
if iUnit == 0:
if sUnits == str('mmol'):
i_fSgv = fCalcSgvMmol
i_fLastSgv = round(float(iLastSgv * 0.0555), 1)
else:
i_fSgv = iSgv
i_fLastSgv = iLastSgv
if iUnit == 1:
i_fSgv = iSgv
i_fLastSgv = iLastSgv
if iUnit == 2:
i_fSgv = fCalcSgvMmol
i_fLastSgv = fCalcLastSgvMmol
# Calculate delta
sTmpDelta = ''
i_fDelta = float(i_fSgv - i_fLastSgv)
sTmpDelta = str(i_fDelta)
sDirection = str(jsonEntries[0]['direction'])
if i_fSgv < i_fLastSgv:
if sDirection == str('FortyFiveUp') or sDirection == str('SingleUp') or sDirection == str('DoubleUp'):
sTmpDelta = sTmpDelta.replace('-', '')
i_fDelta = float(sTmpDelta)
sDelta = ''
if i_fDelta > 0:
sDelta = str('+')
if i_fDelta == 0:
sDelta = str('±')
if sUnits == str('mg/dl'):
i_fDelta = int(i_fDelta)
else:
i_fDelta = round(i_fDelta, 1)
# Direction
sTrend = ''
sSpeechTrend = ''
if sDirection == str("DoubleUp"):
sTrend = str("⇈")
sSpeechTrend = str('Tendency: rising fast')
if sDirection == str("Flat"):
sTrend = str("→︎")
sSpeechTrend = str('Tendency: constant')
if sDirection == str("SingleUp"):
sTrend = str("↑")
sSpeechTrend = str('Tendency: rising')
if sDirection == str("FortyFiveUp"):
sTrend = str("↗")
sSpeechTrend = str('Tendency: slightly rising')
if sDirection == str("FortyFiveDown"):
sTrend = str("↘")
sSpeechTrend = str('Tendency: sinks slightly')
if sDirection == str("SingleDown"):
sTrend = str("↓")
sSpeechTrend = str('Tendency: sinks')
if sDirection == str("DoubleDown"):
sTrend = str("⇊")
sSpeechTrend = str('Tendency: sinks quickly')
# Notification
sMin = str(iMin) + str(' min')
sDelta = sDelta + str(i_fDelta)
sGlucoseData = str(i_fSgv) + str(' • ') + sTrend + str(' • ') + sDelta + str(' • ') + sMin
# Logic for display glucose and speech
sDisplay = ''
if iDisplay == int(0):
sDisplay = sGlucoseData
print(sDisplay)
if bSpeech == True:
sDisplay = sDisplay.replace(sTrend, sSpeechTrend)
if iDisplay == int(1):
sDisplay = str(i_fSgv)
print(sDisplay)
if iDisplay == int(2):
sDisplay = sTrend
print(sDisplay)
if bSpeech == True:
sDisplay = sSpeechTrend
if iDisplay == int(3):
sDisplay = sDelta
print(sDisplay)
if iDisplay == int(4):
sDisplay = sMin
print(sDisplay)
# Speech engine
engine = ''
if bSpeech == True:
import locale
from mtranslate import translate
# Detect local language
sTmpLang = str(locale.getdefaultlocale())[:4]
sLanguageDetect = sTmpLang[-2:]
sDisplay = translate(sDisplay, sLanguageDetect, 'auto')
if bWait == True and iMin == 0:
# Linux
if platform == str("linux") or platform == str("linux2"):
from google_speech import Speech
engine = Speech(sDisplay.replace('.', ','), sLanguageDetect)
engine.play()
# Windows
if platform == str("win32") or platform == str("win64"):
import pyttsx3
engine = pyttsx3.init()
engine.say(sDisplay.replace('.', ','))
engine.runAndWait()
#if platform == str("darwin"): => MACOS
if bWait == False:
# Linux
if platform == str("linux") or platform == str("linux2"):
from google_speech import Speech
engine = Speech(sDisplay.replace('.', ','), sLanguageDetect)
engine.play()
# Windows
if platform == str("win32") or platform == str("win64"):
import pyttsx3
engine = pyttsx3.init()
engine.say(sDisplay.replace('.', ','))
engine.runAndWait()
#if platform == str("darwin"): => MACOS
# Alerts
if bAlert == True:
from playsound import playsound
if bWait == False:
if iSgv <= iBgLow or iSgv >= iBgHigh or iSgv <= iBgTargetBottom or iSgv >= iBgTargetTop:
playsound(sUrl + '/audio/alarm.mp3')
if bWait == True and iMin == 0:
if iSgv <= iBgLow or iSgv >= iBgHigh or iSgv <= iBgTargetBottom or iSgv >= iBgTargetTop:
playsound(sUrl + '/audio/alarm.mp3')
if bWait == True:
# Check of glucose after every interval for new updates
if str(iMin).endswith(str(iMinInterval)[-1:]) or str(iMin).endswith(str(iMinSecondInterval)[-1:]):
for i in range(10):
time.sleep(1)
if iMin == 0:
time.sleep(fSeconds)
break
else:
time.sleep(fSeconds)
# Close url
urlEntries.close()
urlStatus.close()