forked from mddub/minimed-connect-to-nightscout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.js
178 lines (164 loc) · 5.76 KB
/
transform.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
/* jshint node: true */
"use strict";
var _ = require('lodash');
var logger = require('./logger');
var STALE_DATA_THRESHOLD_MINUTES = 20;
var SENSOR_GLUCOSE_ENTRY_TYPE = 'sgv';
var CARELINK_TREND_TO_NIGHTSCOUT_TREND = {
'NONE': {
'trend': 0,
'direction': 'NONE'
},
'UP_TRIPLE': {
'trend': 1,
'direction': 'TripleUp'
},
'UP_DOUBLE': {
'trend': 1,
'direction': 'DoubleUp'
},
'UP': {
'trend': 2,
'direction': 'SingleUp'
},
'DOWN': {
'trend': 6,
'direction': 'SingleDown'
},
'DOWN_DOUBLE': {
'trend': 7,
'direction': 'DoubleDown'
},
'DOWN_TRIPLE': {
'trend': 7,
'direction': 'TripleDown'
}
};
function parsePumpTime(pumpTimeString, offset, medicalDeviceFamily) {
if (process.env['MMCONNECT_SERVER'] === 'EU' || medicalDeviceFamily === 'GUARDIAN') {
return Date.parse(pumpTimeString);
} else {
return Date.parse(pumpTimeString + ' ' + offset);
}
}
function timestampAsString(timestamp) {
return new Date(timestamp).toISOString();
}
function deviceName(data) {
return 'connect://' + data['medicalDeviceFamily'].toLowerCase();
}
var guessPumpOffset = (function () {
var lastGuess;
// From my observations, sMedicalDeviceTime is advanced by the server even when the app is
// not reporting data or the pump is not connected, so its difference from server time is
// always close to a whole number of hours, and can be used to guess the pump's timezone:
// https://gist.github.com/mddub/f673570e6427c93784bf
return function (data) {
var pumpTimeAsIfUTC = Date.parse(data['sMedicalDeviceTime'] + ' +0');
var serverTimeUTC = data['currentServerTime'];
var hours = Math.round((pumpTimeAsIfUTC - serverTimeUTC) / (60 * 60 * 1000));
var offset = (hours >= 0 ? '+' : '-') + (Math.abs(hours) < 10 ? '0' : '') + Math.abs(hours) + '00';
if (offset !== lastGuess) {
logger.log('Guessed pump timezone ' + offset + ' (pump time: "' + data['sMedicalDeviceTime'] + '"; server time: ' + new Date(data['currentServerTime']) + ')');
}
lastGuess = offset;
return offset;
};
})();
function deviceStatusEntry(data, offset) {
if (data['medicalDeviceFamily'] === 'GUARDIAN') {
return {
'created_at': timestampAsString(data['lastMedicalDeviceDataUpdateServerTime']),
'device': deviceName(data),
'uploader': {
'battery': data['medicalDeviceBatteryLevelPercent'],
},
'connect': {
'sensorState': data['sensorState'],
'calibStatus': data['calibStatus'],
'sensorDurationHours': data['sensorDurationHours'],
'timeToNextCalibHours': data['timeToNextCalibHours'],
'conduitInRange': data['conduitInRange'],
'conduitMedicalDeviceInRange': data['conduitMedicalDeviceInRange'],
'conduitSensorInRange': data['conduitSensorInRange'],
'medicalDeviceBatteryLevelPercent': data['medicalDeviceBatteryLevelPercent'],
'medicalDeviceFamily': data['medicalDeviceFamily']
}
};
} else {
return {
'created_at': timestampAsString(data['lastMedicalDeviceDataUpdateServerTime']),
'device': deviceName(data),
'uploader': {
'battery': data['conduitBatteryLevel'],
},
'pump': {
'battery': {
'percent': data['medicalDeviceBatteryLevelPercent'],
},
'reservoir': data['reservoirAmount'],
'iob': {
'timestamp': timestampAsString(data['lastMedicalDeviceDataUpdateServerTime']),
'bolusiob': _.get(data, 'activeInsulin.amount') >= 0 ? _.get(data, 'activeInsulin.amount') : undefined,
},
'clock': timestampAsString(parsePumpTime(data['sMedicalDeviceTime'], offset, data['medicalDeviceFamily'])),
// TODO: add last alarm from data['lastAlarm']['code'] and data['lastAlarm']['datetime']
// https://gist.github.com/mddub/a95dc120d9d1414a433d#file-minimed-connect-codes-js-L79
},
'connect': {
// For the values these can take, see:
// https://gist.github.com/mddub/5e4a585508c93249eb51
'sensorState': data['sensorState'],
'calibStatus': data['calibStatus'],
'sensorDurationHours': data['sensorDurationHours'],
'timeToNextCalibHours': data['timeToNextCalibHours'],
'conduitInRange': data['conduitInRange'],
'conduitMedicalDeviceInRange': data['conduitMedicalDeviceInRange'],
'conduitSensorInRange': data['conduitSensorInRange'],
}
};
}
}
function sgvEntries(data, offset) {
if (!data['sgs'] || !data['sgs'].length) {
return [];
}
var sgvs = data['sgs'].filter(function (entry) {
return entry['kind'] === 'SG' && entry['sg'] !== 0;
}).map(function (sgv) {
var timestamp = parsePumpTime(sgv['datetime'], offset, data['medicalDeviceFamily']);
return {
'type': SENSOR_GLUCOSE_ENTRY_TYPE,
'sgv': sgv['sg'],
'date': timestamp,
'dateString': timestampAsString(timestamp),
'device': deviceName(data),
};
});
if (data['sgs'][data['sgs'].length - 1]['sg'] !== 0) {
sgvs[sgvs.length - 1] = _.merge(
sgvs[sgvs.length - 1],
CARELINK_TREND_TO_NIGHTSCOUT_TREND[data['lastSGTrend']]
);
}
return sgvs;
}
module.exports = function (data, sgvLimit) {
var recency = (data['currentServerTime'] - data['lastMedicalDeviceDataUpdateServerTime']) / (60 * 1000);
if (recency > STALE_DATA_THRESHOLD_MINUTES) {
logger.log('Stale CareLink data: ' + recency.toFixed(2) + ' minutes old');
return {
devicestatus: [],
entries: [],
};
}
var offset = guessPumpOffset(data);
if (sgvLimit === undefined) {
sgvLimit = Infinity;
}
return {
// XXX: lower-case and singular for consistency with cgm-remote-monitor collection name
devicestatus: [deviceStatusEntry(data, offset)],
entries: _.takeRight(sgvEntries(data, offset), sgvLimit),
};
};