Check devicestatus for COB from OpenAPS and Loop along with calculating from treatments

This commit is contained in:
Jason Calabrese
2016-12-01 01:07:38 -08:00
parent 2ee48322ec
commit ce12f6cbcb
+120 -16
View File
@@ -1,7 +1,8 @@
'use strict';
var _ = require('lodash')
, moment = require('moment');
, moment = require('moment')
, times = require('../times');
function init(ctx) {
var translate = ctx.language.translate;
@@ -13,6 +14,8 @@ function init(ctx) {
, pluginType: 'pill-minor'
};
cob.RECENCY_THRESHOLD = times.mins(30).msecs;
cob.setProperties = function setProperties(sbx) {
sbx.offerProperty('cob', function setCOB ( ) {
return cob.cobTotal(sbx.data.treatments, sbx.data.devicestatus, sbx.data.profile, sbx.time);
@@ -31,19 +34,115 @@ function init(ctx) {
return {};
}
if (typeof time === 'undefined') {
time = Date.now();
} else if (time && time.getTime) {
time = time.getTime();
}
var devicestatusCOB = cob.lastCOBDeviceStatus(devicestatus, time);
var treatmentCOB = (treatments !== undefined && treatments.length) ? cob.fromTreatments(treatments, devicestatus, profile, time, spec_profile) : {};
if (devicestatusCOB) {
console.info('>>>>devicestatusCOB', devicestatusCOB);
}
var result = devicestatusCOB;
if (_.isEmpty(result)) {
result = treatmentCOB;
} else if (treatmentCOB.cob) {
result.treatmentCOB = treatmentCOB;
}
return addDisplay(result);
};
function addDisplay(cob) {
if (_.isEmpty(cob) || cob.cob === undefined) {
return {};
}
var display = Math.round(cob.cob * 10) / 10;
return _.merge(cob, {
display: display
, displayLine: 'COB: ' + display + 'g'
});
}
cob.lastCOBDeviceStatus = function lastCOBDeviceStatus (devicestatus, time) {
var futureMills = time + times.mins(5).msecs; //allow for clocks to be a little off
var recentMills = time - cob.RECENCY_THRESHOLD;
return _.chain(devicestatus)
.map(cob.fromDeviceStatus)
.reject(_.isEmpty)
.filter(function (cobStatus) {
return cobStatus.mills <= futureMills && cobStatus.mills >= recentMills;
})
.sortBy('mills')
.last()
.value();
};
cob.fromDeviceStatus = function fromDeviceStatus(devicestatusEntry) {
var cobObj;
if (_.get(devicestatusEntry, 'openaps') !== undefined) {
var suggested = devicestatusEntry.openaps.suggested;
var enacted = devicestatusEntry.openaps.enacted;
var lastCOB = 0;
var lastMoment = null;
if (suggested && enacted) {
var suggestedMoment = moment(suggested.timestamp);
var enactedMoment = moment(enacted.timestamp);
if (enactedMoment.isAfter(suggestedMoment)) {
lastCOB = enacted.COB;
lastMoment = enactedMoment;
} else {
lastCOB = suggested.COB;
lastMoment = suggestedMoment;
}
} else if (enacted) {
lastCOB = enacted.COB;
lastMoment = moment(enacted.timestamp);
} else if (suggested) {
lastCOB = suggested.COB;
lastMoment = moment(suggested.timestamp);
}
if (lastCOB === 0 || !lastMoment) {
return {};
}
return {
cob: lastCOB
, source: 'OpenAPS'
, device: devicestatusEntry.device
, mills: lastMoment.valueOf( )
};
} else if (_.get(devicestatusEntry, 'loop.cob') !== undefined) {
cobObj = devicestatusEntry.loop.cob;
return {
cob: cobObj.cob
, source: 'Loop'
, device: devicestatusEntry.device
, mills: moment(cobObj.timestamp).valueOf( )
};
} else {
return {};
}
};
cob.fromTreatments = function fromTreatments (treatments, devicestatus, profile, time, spec_profile) {
// TODO: figure out the liverSensRatio that gives the most accurate purple line predictions
var liverSensRatio = 8;
var totalCOB = 0;
var lastCarbs = null;
if (!treatments) {
return {};
}
if (typeof time === 'undefined') {
time = Date.now();
}
var isDecaying = 0;
var lastDecayedBy = 0;
@@ -83,15 +182,13 @@ function init(ctx) {
});
var rawCarbImpact = isDecaying * profile.getSensitivity(time, spec_profile) / profile.getCarbRatio(time, spec_profile) * profile.getCarbAbsorptionRate(time, spec_profile) / 60;
var display = Math.round(totalCOB * 10) / 10;
return {
decayedBy: lastDecayedBy
, isDecaying: isDecaying
, carbs_hr: profile.getCarbAbsorptionRate(time, spec_profile)
, rawCarbImpact: rawCarbImpact
, cob: totalCOB
, display: display
, displayLine: 'COB: ' + display + 'g'
, lastCarbs: lastCarbs
};
};
@@ -154,15 +251,22 @@ function init(ctx) {
var prop = sbx.properties.cob;
console.info('>>>prop', prop);
if (prop === undefined || prop.cob === undefined) { return; }
var displayCob = Math.round(prop.cob * 10) / 10;
var info = null;
if (prop.lastCarbs) {
var when = new Date(prop.lastCarbs.mills).toLocaleString();
var amount = prop.lastCarbs.carbs + 'g';
info = [{label: translate('Last Carbs'), value: amount + ' @ ' + when }];
if (prop.treatmentCOB !== undefined) {
info = [ ];
info.push({label: translate('Careportal COB'), value: Math.round(prop.treatmentCOB.cob * 10) / 10});
if (prop.treatmentCOB.lastCarbs) {
var when = new Date(prop.treatmentCOB.lastCarbs.mills).toLocaleString();
var amount = prop.treatmentCOB.lastCarbs.carbs + 'g';
info.push({label: translate('Last Carbs'), value: amount + ' @ ' + when});
}
}
sbx.pluginBase.updatePillText(sbx, {