-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMMM-Nightscout.js
317 lines (303 loc) · 8.91 KB
/
MMM-Nightscout.js
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
Module.register("MMM-Nightscout", {
// Default module config.
defaults: {
baseUrl: null,
debug: false,
colorEnabled: false,
chartWidth: 350,
chartHours: 4,
renderChart: true,
token: false,
showTIR: false,
units: false,
extendedHeader: true
},
getScripts: function() {
return [
"modules/" +
this.name +
"/node_modules/chart.js/dist/Chart.bundle.min.js",
"modules/" +
this.name +
"/node_modules/chartjs-plugin-annotation/chartjs-plugin-annotation.min.js"
];
},
start: function() {
Log.info("Starting module: " + this.name);
this.origLineElement = Chart.elements.Line;
//Send config to node_helper
Log.info("Send configs to node_helper..");
this.sendSocketNotification("CONFIG", this.config);
this.updateDom();
},
getDom: function() {
Log.info("getDom triggered");
let wrapper = document.createElement("div");
if (!this.loaded && !this.failure) {
wrapper.innerHTML =
"<img style='width: 100px' src='https://avatars3.githubusercontent.com/u/7661012?s=280&v=4'></img>";
return wrapper;
}
if (this.failure) {
wrapper.innerHTML = "Connection to Nightscout failed. Please review logs";
wrapper.className = "dimmed light small";
return wrapper;
}
if (this.glucoseData) {
let head = document.createElement("div");
head.appendChild(this.getBS());
if (this.config.extendedHeader && this.glucoseData.settings.customTitle) {
head.appendChild(this.getCustomTitle(this.glucoseData.settings.customTitle));
} else if(this.config.showTIR) {
head.appendChild(this.getCustomTitle(this.glucoseData.TIR));
}
wrapper.appendChild(head);
if (this.config.renderChart) {
wrapper.appendChild(this.getChart());
}
return wrapper;
}
},
getCustomTitle: function(title) {
let updatedTime = new Date(this.glucoseData.date);
let customTitle = document.createElement("div");
customTitle.style = "float: left;clear: none; padding-left: 10px;";
customTitle.innerHTML =
"<span class='normal medium dimmed' style='display:block;'>" +
title +
"</span><span class='light small dimmed' style='display:block;'>" +
updatedTime.toLocaleString() +
"</span>";
return customTitle;
},
getBS: function() {
let div = document.createElement("div");
div.style = "float: left;clear: none;";
let bs = document.createElement("div");
bs.style = "display: table;";
let bsStyle = this.config.colorEnabled
? "display: table-cell;vertical-align:middle; color:" +
this.glucoseData.fontColor +
";"
: "display: table-cell;vertical-align:middle;";
bs.innerHTML =
'<span class="bright large light" style="' +
bsStyle +
'">' +
this.glucoseData.bs +
'</span><span class="bright medium light" style="display: table-cell;vertical-align:middle;">' +
this.glucoseData.direction +
"</span>";
div.appendChild(bs);
let delta = document.createElement("div");
delta.className = "light small dimmed";
let units = this.glucoseData.unit;
if (this.config.units) {
units = this.config.units;
}
delta.innerHTML =
this.glucoseData.delta +
" " +
(units == "mmol" ? "mmol/L" : "mg/dL");
div.appendChild(delta);
return div;
},
getChart: function() {
let chartWrapper = document.createElement("div");
let units = this.glucoseData.unit;
if (this.config.units) {
units = this.config.units;
}
// Get high, low and target thresholds from server
let bgHigh = this.glucoseData.thresholds.bgHigh;
let bgLow = this.glucoseData.thresholds.bgLow;
let targetTop = this.glucoseData.thresholds.targetTop;
let targetBottom = this.glucoseData.thresholds.targetBottom;
chartWrapper.style =
"position: relative; display: inline-block; width:" +
this.config.chartWidth +
"px;";
let chartElement = document.createElement("canvas");
chartElement.style = "width:" + this.config.chartWidth + "px;";
let chartConfig = {
type: "line",
data: {
datasets: [
{
//backgroundColor: this.glucoseData.data.colorSet,
pointBackgroundColor: this.glucoseData.data.colorSet,
pointRadius: 2,
borderColor: chartColors.white,
data: this.glucoseData.data.dataSet,
type: "line",
fill: true,
showLine: false
}
]
},
options: {
title: {
display: false
},
annotation: {
annotations: [
{
// BG low
borderDash: [3],
type: "line",
mode: "horizontal",
scaleID: "y-axis-0",
value: bgLow,
borderColor: "rgba(242, 241, 239, 0.5)",
borderWidth: 1,
label: {
enabled: false
}
},
{
// BG high
borderDash: [3],
type: "line",
mode: "horizontal",
scaleID: "y-axis-0",
value: bgHigh,
borderColor: "rgba(242, 241, 239, 0.5)",
borderWidth: 1,
label: {
enabled: false
}
},
{
// Target range low
borderDash: [10],
type: "line",
mode: "horizontal",
scaleID: "y-axis-0",
value: targetBottom,
borderColor: "rgba(242, 241, 239, 0.5)",
borderWidth: 1,
label: {
enabled: false
}
},
{
// Target range high
borderDash: [10],
type: "line",
mode: "horizontal",
scaleID: "y-axis-0",
value: targetTop,
borderColor: "rgba(242, 241, 239, 0.5)",
borderWidth: 1,
label: {
enabled: false
}
}
]
},
legend: {
display: false
},
scales: {
xAxes: [
{
type: "time",
time: {
parser: "HH:mm",
unit: "minute",
unitStepSize: 30,
displayFormats: {
minute:
this.glucoseData.settings.timeFormat == 24
? "HH:mm"
: "hh:mm A"
}
},
scaleLabel: {
display: false
},
distribution: "series",
ticks: {
source: "data",
autoSkip: true
},
data: this.glucoseData.data,
gridLines: {
offsetGridLines: true
}
}
],
yAxes: [
{
typ: "logarithmic",
ticks: {
beginAtZero: true,
max: units == "mmol" ? 18 : 400,
min: units == "mmol" ? 2 : 30
},
display: true,
scaleLabel: {
display: true,
labelString:
units == "mmol" ? "mmol/L" : "mg/dL"
}
}
]
}
}
};
this.chart = new Chart(chartElement.getContext("2d"), chartConfig);
chartWrapper.appendChild(chartElement);
return chartWrapper;
},
// --------------------------------------- Handle socketnotifications
socketNotificationReceived: function(notification, payload) {
Log.info(
"socketNotificationReceived: " + notification + ", payload: " + payload
);
if (notification === "GLUCOSE") {
this.loaded = true;
this.failure = undefined;
this.glucoseData = payload;
this.updateDom();
} else if (notification == "SERVICE_FAILURE") {
this.loaded = true;
this.failure = payload;
if (payload) {
Log.info(
"Service failure: " +
this.failure.resp.StatusCode +
":" +
this.failure.resp.Message
);
this.updateDom();
}
}
},
notificationReceived: function(notification, payload, sender) {
if (notification == "DOM_OBJECTS_CREATED") {
this.domObjectCreated = true;
}
}
});
//Utilities
var chartColors = {
red: "rgb(255, 99, 132)",
yellow: "rgb(255, 205, 86)",
green: "rgb(75, 192, 192)",
white: "rgba(255,255,255, 0.9)"
};
var roundNumber = function(num, scale) {
var number = Math.round(num * Math.pow(10, scale)) / Math.pow(10, scale);
if (num - number > 0) {
return (
number +
Math.floor(
(2 * Math.round((num - number) * Math.pow(10, scale + 1))) / 10
) /
Math.pow(10, scale)
);
} else {
return number;
}
};