forked from loudnate/openaps-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.py
95 lines (74 loc) · 2.77 KB
/
monitor.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
import os
import urllib2
import sys
from flask import Flask, render_template
from openapscontrib.predict.predict import Schedule
from highchart import glucose_target_range_chart
from highchart import input_history_area_chart
from highchart import line_chart
from openaps_reports import OpenAPS, Settings
app = Flask(__name__)
@app.route('/')
def monitor():
aps = app.config['OPENAPS']
recent_glucose = aps.recent_glucose()
predicted_glucose = aps.predicted_glucose()
targets = Schedule(aps.read_bg_targets()['targets'])
normalized_history = aps.normalized_history()
iob = aps.iob()
basal, bolus, square, carbs = input_history_area_chart(reversed(normalized_history))
actual_glucose = line_chart(reversed(recent_glucose), name='Glucose')
predicted_glucose = line_chart(predicted_glucose, name='Predicted')
iob = line_chart(iob, 'IOB')
target_glucose = glucose_target_range_chart(targets, actual_glucose, predicted_glucose)
return render_template(
'monitor.html',
openaps=aps,
actual_glucose=actual_glucose,
predicted_glucose=predicted_glucose,
target_glucose=target_glucose,
iob=iob,
basal=basal,
bolus=bolus,
square=square,
carbs=carbs,
CSS_ASSETS=CSS_ASSETS,
JS_ASSETS=JS_ASSETS,
display_unit=Settings.DISPLAY_UNIT
)
CSS_ASSETS = (
('static/third_party/bootstrap.css', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css'),
('static/styles.css', None)
)
JS_ASSETS = (
('static/third_party/jquery.js', 'https://code.jquery.com/jquery-2.1.4.min.js'),
('static/third_party/bootstrap.js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js'),
('static/third_party/highcharts.js', 'http://code.highcharts.com/highcharts.js'),
('static/third_party/highcharts-more.js', 'http://code.highcharts.com/highcharts-more.js'),
('static/monitor.js', None),
)
def preload_assets():
for filename, url in (JS_ASSETS + CSS_ASSETS):
if not os.path.exists(filename):
print '{} not found, downloading from {}'.format(filename, url)
try:
contents = urllib2.urlopen(url).read()
except ValueError, urllib2.HTTPError:
pass
else:
try:
os.makedirs(os.path.dirname(filename))
except os.error:
pass
with open(filename, mode='w') as fp:
fp.write(contents)
if __name__ == '__main__':
path = sys.argv[1]
preload_assets()
if os.path.exists(path):
path = os.path.abspath(path)
app.config['OPENAPS'] = OpenAPS(path)
app.debug = True
app.run(host='0.0.0.0')
else:
exit(1)