forked from mddub/nightscout-osx-menubar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nightscout_osx_menubar.py
271 lines (232 loc) · 8.83 KB
/
nightscout_osx_menubar.py
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# -*- coding: utf-8 -*-
import os
import sys
import traceback
import webbrowser
from ConfigParser import ConfigParser
from datetime import datetime
import requests
import rumps
import simplejson
VERSION = '0.3.3'
APP_NAME = 'Nightscout Menubar'
PROJECT_HOMEPAGE = 'https://github.com/mddub/nightscout-osx-menubar'
SGVS_PATH = '/api/v1/entries/sgv.json?count={count}&token={token}'
UPDATE_FREQUENCY_SECONDS = 20
MAX_SECONDS_TO_SHOW_DELTA = 600
HISTORY_LENGTH = 5
MAX_BAD_REQUEST_ATTEMPTS = 3
REQUEST_TIMEOUT_SECONDS = 2
################################################################################
# Display options
MENUBAR_TEXT = u"{sgv} {direction} {delta} [{time_ago}]"
MENU_ITEM_TEXT = u"{sgv} {direction} {delta} [{time_ago}]"
def time_ago(seconds):
if seconds >= 3600:
return "%sh" % (seconds / 3600)
elif seconds >= 60:
return "%sm" % (seconds / 60)
else:
return "%ss" % seconds
################################################################################
class NightscoutException(Exception): pass
class NightscoutConfig(object):
FILENAME = 'config'
SECTION = 'NightscoutMenubar'
HOST = 'nightscout_host'
USE_MMOL = 'use_mmol'
TOKEN = 'token'
def __init__(self, app_name):
self.config_path = os.path.join(rumps.application_support(app_name), self.FILENAME)
self.config = ConfigParser()
self.config.read([self.config_path])
if not self.config.has_section(self.SECTION):
self.config.add_section(self.SECTION)
if not self.config.has_option(self.SECTION, self.HOST):
self.set_host('')
if not self.config.has_option(self.SECTION, self.USE_MMOL):
self.set_use_mmol(False)
if not self.config.has_option(self.SECTION, self.TOKEN):
self.set_token('')
def get_host(self):
return self.config.get(self.SECTION, self.HOST)
def set_host(self, host):
self.config.set(self.SECTION, self.HOST, host)
with open(self.config_path, 'w') as f:
self.config.write(f)
def get_token(self):
return self.config.get(self.SECTION, self.TOKEN)
def set_token(self, token):
self.config.set(self.SECTION, self.TOKEN, token)
with open(self.config_path, 'w') as f:
self.config.write(f)
def get_use_mmol(self):
return bool(self.config.get(self.SECTION, self.USE_MMOL))
def set_use_mmol(self, mmol):
self.config.set(self.SECTION, self.USE_MMOL, 'true' if mmol else '')
with open(self.config_path, 'w') as f:
self.config.write(f)
config = NightscoutConfig(APP_NAME)
def maybe_convert_units(mgdl):
return round(mgdl / 18.0, 1) if config.get_use_mmol() else mgdl
def update_menu(title, items):
app.title = title
app.menu.clear()
app.menu.update(items + last_updated_menu_items() + post_history_menu_options() + [app.quit_button])
def last_updated_menu_items():
return [
None,
"Updated %s" % datetime.now().strftime("%a %-I:%M %p"),
]
def post_history_menu_options():
mgdl = rumps.MenuItem('mg/dL', callback=choose_units_mgdl)
mgdl.state = not config.get_use_mmol()
mmol = rumps.MenuItem('mmol/L', callback=choose_units_mmol)
mmol.state = config.get_use_mmol()
open_ns_url = rumps.MenuItem('Open Nightscout site...', callback=None)
if config.get_host():
open_ns_url.set_callback(open_nightscout_url)
items = [
None,
[
'Settings',
[
mgdl,
mmol,
None,
rumps.MenuItem('Set Nightscout URL...', callback=configuration_window),
rumps.MenuItem('Help...', callback=open_project_homepage),
None,
"Version {}".format(VERSION)
],
],
None,
open_ns_url,
None,
]
return items
def get_entries(retries=0, last_exception=None):
if retries >= MAX_BAD_REQUEST_ATTEMPTS:
print "Retried too many times: %s" % last_exception
raise NightscoutException(last_exception)
try:
resp = requests.get(
config.get_host() + SGVS_PATH.format(count=(HISTORY_LENGTH + 1), token=config.get_token()),
# For the sake of keeping this portable without adding a lot of complexity, don't verify SSL certificates.
# https://github.com/kennethreitz/requests/issues/557
verify=False,
# Don't let bad connectivity cause the app to freeze
timeout=REQUEST_TIMEOUT_SECONDS,
)
except requests.exceptions.Timeout, e:
# Don't retry timeouts, since the app is unresponsive while a request is in progress,
# and a new request will be made in UPDATE_FREQUENCY_SECONDS seconds anyway.
print "Timed out: %s" % repr(e)
raise NightscoutException(repr(e))
except requests.exceptions.RequestException, e:
return get_entries(retries + 1, repr(e))
if resp.status_code != 200:
return get_entries(retries + 1, "Nightscout returned status %s" % resp.status_code)
try:
arr = resp.json()
if type(arr) == list and (len(arr) == 0 or type(arr[0]) == dict):
return arr
else:
return get_entries(retries + 1, "Nightscout returned bad data")
except simplejson.scanner.JSONDecodeError:
return get_entries(retries + 1, "Nightscout returned bad JSON")
def filter_bgs(entries):
bgs = [e.copy() for e in entries if 'sgv' in e]
for bg in bgs:
bg['sgv'] = int(bg['sgv'])
return bgs
def seconds_ago(timestamp):
return int(datetime.now().strftime('%s')) - timestamp / 1000
def get_direction(entry):
return {
'DoubleUp': u'⇈',
'SingleUp': u'↑',
'FortyFiveUp': u'↗',
'Flat': u'→',
'FortyFiveDown': u'↘',
'SingleDown': u'↓',
'DoubleDown': u'⇊',
}.get(entry.get('direction'), '-')
def get_delta(last, second_to_last):
return ('+' if last['sgv'] >= second_to_last['sgv'] else u'−') + str(abs(maybe_convert_units(last['sgv'] - second_to_last['sgv'])))
def get_menubar_text(entries):
bgs = filter_bgs(entries)
last, second_to_last = bgs[0:2]
if (last['date'] - second_to_last['date']) / 1000 <= MAX_SECONDS_TO_SHOW_DELTA:
delta = get_delta(last, second_to_last)
else:
delta = '?'
return MENUBAR_TEXT.format(
sgv=maybe_convert_units(last['sgv']),
delta=delta,
direction=get_direction(last),
time_ago=time_ago(seconds_ago(last['date'])),
)
def get_history_menu_items(entries):
bgs = filter_bgs(entries)
return [
MENU_ITEM_TEXT.format(
sgv=maybe_convert_units(e['sgv']),
delta=get_delta(e, bgs[i + 1]) if i + 1 < len(bgs) else '?',
direction=get_direction(e),
time_ago=time_ago(seconds_ago(e['date'])),
)
for i, e in enumerate(bgs)
][1:HISTORY_LENGTH + 1]
@rumps.timer(UPDATE_FREQUENCY_SECONDS)
def update_data(sender):
entries = None
try:
try:
entries = get_entries()
except NightscoutException, e:
if config.get_host():
update_menu("<?>", [e.message[:100]])
else:
update_menu("<Need settings>", [])
else:
update_menu(get_menubar_text(entries), get_history_menu_items(entries))
except Exception, e:
print "Nightscout data: " + simplejson.dumps(entries)
print repr(e)
_, _, tb = sys.exc_info()
traceback.print_tb(tb)
update_menu("<!>", [repr(e)[:100]])
def configuration_window(sender):
window = rumps.Window(
title='Nightscout Menubar Configuration',
message='Enter your nightscout URL below.\n\nIt probably looks like:\nhttps://SOMETHING.azurewebsites.net\nURL may or may not end with ?token=xx.',
dimensions=(320, 22),
)
window.default_text = config.get_host() + "?token=" + config.get_token()
window.add_buttons('Cancel')
response = window.run()
if response.clicked == 1:
if response.text.strip().find("token") == -1:
config.set_host(response.text.strip())
else:
nshost,nstoken = response.text.strip().split("?token=")
config.set_host(nshost)
config.set_token(nstoken)
update_data(None)
def open_project_homepage(sender):
webbrowser.open_new(PROJECT_HOMEPAGE)
def open_nightscout_url(sender):
webbrowser.open_new(config.get_host())
def choose_units_mgdl(sender):
config.set_use_mmol(False)
update_data(None)
def choose_units_mmol(sender):
config.set_use_mmol(True)
update_data(None)
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == '--debug':
rumps.debug_mode(True)
app = rumps.App(APP_NAME, title='<Connecting to Nightscout...>')
app.menu = ['connecting...'] + post_history_menu_options()
app.run()