forked from bureus/MMM-Nightscout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
237 lines (226 loc) · 6.8 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
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;
this.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 options = {
method: "GET",
if (self.config.securitytoken) {
uri: this.config.baseUrl + "/api/v1/status" + "?token=" + this.config.token,
} else {
uri: this.config.baseUrl + "/api/v1/status",
}
headers: {
Accept: "application/json"
}
};
request(options)
.then(function(body) {
let config = JSON.parse(body);
debug(
"getServerConfig: data retrived, units is" +
config.settings.units +
", status is: " +
config.status
);
resolve(config);
})
.catch(function(error) {
log(
"getServerConfig: failed when trying to retrive 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) {
let options = {
method: "GET",
if (self.config.securitytoken) {
uri: this.config.baseUrl + "/api/v1/entries.json?count=" +
this.config.chartHours * 12 + "?token=" + this.config.token,
} else {
uri:
this.config.baseUrl +
"/api/v1/entries.json?count=" +
this.config.chartHours * 12
}
};
request(options)
.then(function(body) {
let glucoseData = JSON.parse(body);
debug("getGlucoseData: data retrived");
resolve(glucoseData);
})
.catch(function(error) {
log("getGlucoseData: failed when trying to retrive data: " + error);
resolve();
});
} else {
log("Missing configed base url");
self.sendSocketNotification(
"SERVICE_FAILURE",
"Missing configed base url"
);
resolve();
}
});
},
// --------------------------------------- Init
update: async function() {
let self = this;
if (self.config.baseUrl && self.config.server.settings.units) {
clearInterval(this.updatetimer); // Clear the timer so that we can set it again
let glucoseData = await self.getGlucoseData();
if (glucoseData) {
self.glucoseData = glucoseData;
let dto = generateDto(
self.glucoseData,
self.config.server.settings.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");
this.config = payload;
this.debugMe = this.config.debug;
this.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),
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 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);
}