Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import change from #30 #36

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ var CARELINK_TREND_TO_NIGHTSCOUT_TREND = {
}
};

function parsePumpTime(pumpTimeString, offset, medicalDeviceFamily) {
function parsePumpTime(pumpTimeString, offset, offsetMilliseconds, medicalDeviceFamily) {
if (process.env['MMCONNECT_SERVER'] === 'EU' || medicalDeviceFamily === 'GUARDIAN') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The offset issues were affecting me (with MCONNECT_SERVER: US) so I added "or US" to this to get the same fix. I'm troubleshooting intermittent other issues but I'm guessing those are related to deploying on kubernetes, since it seems fine running in dev.

mattster98@21e07ca

A more elegant solution (assuming appending the tz offset is still needed in some situations) might be to see if the first method works, and if so, go with it. If it doesn't, use the second method. I'm guessing medtronic changed how they're sending the device time out, but not sure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think US and EU are doing the same thing, and the separation is a historical artifact we can ignore.

I've been meaning to more carefully to emulate what's happening in XDripCarelinkFollower: https://github.com/benceszasz/xDripCareLinkFollower/tree/master/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note, Sulka disabled some additional datetime munging just recently here: nightscout/cgm-remote-monitor@c1de8a5

return Date.parse(pumpTimeString);
return Date.parse(pumpTimeString) - offsetMilliseconds ; // FIX BY sirKitKat
} else {
return Date.parse(pumpTimeString + ' ' + offset);
}
Expand All @@ -51,7 +51,7 @@ function timestampAsString(timestamp) {
}

function deviceName(data) {
return 'connect://' + data['medicalDeviceFamily'].toLowerCase();
return 'connect-' + data['medicalDeviceFamily'].toLowerCase();
}

var guessPumpOffset = (function () {
Expand All @@ -61,7 +61,7 @@ var guessPumpOffset = (function () {
// 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 pumpTimeAsIfUTC = Date.parse(data['sMedicalDeviceTime']);
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';
Expand All @@ -73,7 +73,18 @@ var guessPumpOffset = (function () {
};
})();

function deviceStatusEntry(data, offset) {
var guessPumpOffsetMilliseconds = (function () {
return function (data) {
var pumpTimeAsIfUTC = Date.parse(data['sMedicalDeviceTime']);
var serverTimeUTC = data['currentServerTime'];
var offsetMilliseconds = pumpTimeAsIfUTC - serverTimeUTC;
var offsetMilliseconds = Math.round(offsetMilliseconds / (60 * 60 * 1000))*(60 * 60 * 1000)
return offsetMilliseconds ;
};
})();


function deviceStatusEntry(data, offset, offsetMilliseconds) {
if (data['medicalDeviceFamily'] === 'GUARDIAN') {
return {
'created_at': timestampAsString(data['lastMedicalDeviceDataUpdateServerTime']),
Expand Down Expand Up @@ -104,12 +115,12 @@ function deviceStatusEntry(data, offset) {
'battery': {
'percent': data['medicalDeviceBatteryLevelPercent'],
},
'reservoir': data['reservoirAmount'],
'reservoir': data['reservoirRemainingUnits'],
'iob': {
'timestamp': timestampAsString(data['lastMedicalDeviceDataUpdateServerTime']),
'bolusiob': _.get(data, 'activeInsulin.amount') >= 0 ? _.get(data, 'activeInsulin.amount') : undefined,
},
'clock': timestampAsString(parsePumpTime(data['sMedicalDeviceTime'], offset, data['medicalDeviceFamily'])),
'clock': timestampAsString(parsePumpTime(data['sMedicalDeviceTime'], offset, offsetMilliseconds, 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
},
Expand All @@ -128,15 +139,15 @@ function deviceStatusEntry(data, offset) {
}
}

function sgvEntries(data, offset) {
function sgvEntries(data, offset, offsetMilliseconds) {
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']);
var timestamp = parsePumpTime(sgv['datetime'], offset, offsetMilliseconds, data['medicalDeviceFamily']);
return {
'type': SENSOR_GLUCOSE_ENTRY_TYPE,
'sgv': sgv['sg'],
Expand Down Expand Up @@ -167,12 +178,13 @@ module.exports = function (data, sgvLimit) {
}

var offset = guessPumpOffset(data);
var offsetMilliseconds = guessPumpOffsetMilliseconds(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),
devicestatus: [deviceStatusEntry(data, offset, offsetMilliseconds)],
entries: _.takeRight(sgvEntries(data, offset, offsetMilliseconds), sgvLimit),
};
};