-
Notifications
You must be signed in to change notification settings - Fork 7
/
node_helper.js
285 lines (265 loc) · 7.94 KB
/
node_helper.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
const NodeHelper = require("node_helper");
const request = require("request-promise");
var debugMe = false;
module.exports = NodeHelper.create({
start: function() {
log("Starting helper: " + this.name);
this.started = false;
},
// --------------------------------------- Schedule a stands update
scheduleUpdate: function() {
let self = this;
self.updatetimer = setInterval(function() {
// This timer is saved in uitimer so that we can cancel it
self.update();
}, 60000);
},
// --------------------------------------- Get Nightscout server configs
getServerConfig: async function() {
let self = this;
return new Promise(resolve => {
if (self.config.baseUrl) {
let statusUrl = "/api/v1/status";
if (self.config.token) {
statusUrl = statusUrl + "?token=" + self.config.token;
}
let options = {
method: "GET",
uri: self.config.baseUrl + statusUrl,
headers: {
Accept: "application/json"
}
};
request(options)
.then(function(body) {
let config = JSON.parse(body);
debug(
"getServerConfig: data retrieved, units is " +
config.settings.units +
", status is: " +
config.status
);
resolve(config);
})
.catch(function(error) {
log(
"getServerConfig: failed when trying to retrieve data: " + error
);
reject();
});
} else {
log("getServerConfig: Missing configed base url");
self.sendSocketNotification(
"SERVICE_FAILURE",
"getServerConfig: Missing configed base url"
);
reject();
}
});
},
// --------------------------------------- Get glucose data from Nightscout
getGlucoseData: async function() {
let self = this;
return new Promise(resolve => {
if (self.config.baseUrl) {
// *12 since data is updated every 5 minutes, meaning we get 12 values every hour.
let entriesUrl = "/api/v1/entries.json?count=" + self.config.chartHours*12;
if (self.config.token) {
entriesUrl = entriesUrl + "&token=" + self.config.token;
}
let options = {
method: "GET",
uri:
self.config.baseUrl +
entriesUrl
};
request(options)
.then(function(body) {
let glucoseData = JSON.parse(body);
debug("getGlucoseData: data retrieved");
resolve(glucoseData);
})
.catch(function(error) {
log("getGlucoseData: failed when trying to retrieve data: " + error);
resolve();
});
} else {
log("Missing base url in configuration");
self.sendSocketNotification(
"SERVICE_FAILURE",
"Missing base url in configuration"
);
resolve();
}
});
},
// --------------------------------------- Init
update: async function() {
let self = this;
if (self.config.baseUrl && self.config.server.settings.units) {
clearInterval(self.updatetimer); // Clear the timer so that we can set it again
let glucoseData = await self.getGlucoseData();
if (glucoseData) {
self.glucoseData = glucoseData;
let units = self.config.server.settings.units;
if (self.config.units) {
// If unit is set in configuration, overwrite server setting.
units = self.config.units;
}
let dto = generateDto(
self.glucoseData,
units,
self.config.server.settings.thresholds,
self.config.server.settings
);
debug(JSON.stringify(dto));
debug("bs value is: " + dto.bs + " " + dto.unit);
self.sendSocketNotification("GLUCOSE", dto); // Send glucose data to presentation layer
}
self.scheduleUpdate();
} else {
debug("update: missing needed configs");
}
},
// --------------------------------------- Init
init: async function() {
let self = this;
if (self.started && self.config.baseUrl) {
self.config.server = await self.getServerConfig();
await self.update();
}
},
// --------------------------------------- Handle notifications
socketNotificationReceived: async function(notification, payload) {
const self = this;
log("socketNotificationReceived");
if (notification === "CONFIG") {
log("CONFIG event received");
self.config = payload;
self.debugMe = self.config.debug;
self.started = true;
self.init();
}
}
});
//Utils
function convertSvgToMmol(sgv) {
debug("Converting " + sgv + " mg/dL to mmol/L");
return (Math.round((sgv / 18) * 10) / 10).toFixed(1);
}
function directionToUnicode(direction) {
switch (direction) {
case "NONE":
return "⇼";
case "DoubleUp":
return "⇈";
case "SingleUp":
return "↑";
case "FortyFiveUp":
return "↗";
case "Flat":
return "→";
case "FortyFiveDown":
return "↘";
case "SingleDown":
return "↓";
case "DoubleDown":
return "⇊";
case "RATE OUT OF RANGE":
return "⇕";
default:
return "-";
}
}
function generateDto(data, unit, thresholds, settings) {
debug(JSON.stringify(data));
return {
bs: unit == "mmol" ? convertSvgToMmol(data[0].sgv) : data[0].sgv,
delta:
unit == "mmol"
? convertSvgToMmol(data[0].sgv - data[1].sgv)
: data[0].sgv - data[1].sgv,
unit: unit,
date: data[0].date,
trend: data[0].trend,
direction: directionToUnicode(data[0].direction),
fontColor: getFontColor(data[0].sgv, thresholds),
TIR: "TIR: " + getTIR(data, thresholds) + "%",
thresholds: getThresholds(unit, thresholds),
data: getCharDataSet(data, unit == "mmol", thresholds),
settings: settings
};
}
//
function getFontColor(sgv, thresholds, isChart) {
if (sgv >= thresholds.bgHigh || sgv <= thresholds.bgLow) {
return isChart ? "rgb(255, 0, 0)" : "#FF3333";
}
if (sgv <= thresholds.bgTargetTop && sgv >= thresholds.bgTargetBottom) {
return isChart ? "rgb(76, 255, 0)" : "#33FF33";
}
return isChart ? "rgb(255, 255, 0)" : "#FFFF33";
}
function getTIR(data, thresholds) {
log("Getting Time-In-Range");
let inRange = 0;
for (let i = 0; i < data.length; i++) {
if (data[i].sgv <= thresholds.bgTargetTop &&
data[i].sgv >= thresholds.bgTargetBottom) {
inRange++;
}
}
if (data.length != 0) {
return Math.floor(1000*inRange/data.length)/10;
}
else {
return 0;
}
}
function getThresholds(units, thresholds) {
let bgHigh = thresholds.bgHigh;
let bgLow = thresholds.bgLow;
let targetTop = thresholds.bgTargetTop;
let targetBottom = thresholds.bgTargetBottom;
if (units == "mmol") {
bgHigh = convertSvgToMmol(bgHigh);
bgLow = convertSvgToMmol(bgLow);
targetTop = convertSvgToMmol(targetTop);
targetBottom = convertSvgToMmol(targetBottom);
}
return {
bgHigh : bgHigh,
bgLow : bgLow,
targetTop : targetTop,
targetBottom : targetBottom };
}
function getCharDataSet(data, convert, thresholds) {
debug(
"getCharDataSet: data set length: " +
data.length +
", convertSvgToMmol:" +
convert
);
let colorSet = [];
let dataSet = [];
for (let i = 0; i < data.length; i++) {
dataSet.push({
t: data[i].date,
y: convert ? convertSvgToMmol(data[i].sgv) : data[i].sgv
});
colorSet.push(getFontColor(data[i].sgv, thresholds, true));
}
return { dataSet: dataSet, colorSet: colorSet };
}
// --------------------------------------- At beginning of log entries
function logStart() {
return new Date(Date.now()).toLocaleTimeString() + " MMM-Nightscout: ";
}
// --------------------------------------- Logging
function log(msg) {
console.log(logStart() + msg);
}
// --------------------------------------- Debugging
function debug(msg) {
if (debugMe) log(msg);
}