Skip to content

Commit

Permalink
Merge pull request nightscout#5 from aug0211/dev-openaps-upload-hack
Browse files Browse the repository at this point in the history
Dev openaps upload hack + pill formatting
  • Loading branch information
aug0211 authored Jun 9, 2024
2 parents 78a4958 + 9292548 commit e751619
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 18 deletions.
7 changes: 7 additions & 0 deletions lib/data/ddata.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,16 @@ function init () {
.filter(function isSameDeviceType (status) {
return status.device === deviceAndType.device && _.has(status, deviceAndType.type)
})
/*
.filter(function notInTheFuture (status) {
return status.mills <= time;
})
*/
//Daniel: Hack to included up to 1 minute delayed entries in filter
.filter(function includeDelayed(status) {
var adjustedTime = time + (1 * 60 * 1000);
return status.mills <= adjustedTime;
})
.sortBy('mills')
.takeRight(10)
.value();
Expand Down
85 changes: 67 additions & 18 deletions lib/plugins/openaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,38 @@ function init (ctx) {

var events = [];

//Daniel: Test line breaks instead of comma separated values for reason wall of text
function addSuggestion() {
if (prop.lastSuggested) {
var bg = prop.lastSuggested.bg;
var units = sbx.data.profile.getUnits();

if (units === 'mmol') {
bg = Math.round(bg / consts.MMOL_TO_MGDL * 10) / 10;
}

// Replace commas with line breaks in prop.lastSuggested.reason
var reasonWithLineBreaks = prop.lastSuggested.reason.replace(/,/g, '<br>');

var valueParts = [
valueString('<br>BG: ', bg),
valueString('<br>IOB: ', prop.lastIOB.iob, 'U'),
valueString('<br>', reasonWithLineBreaks)
// Daniel: commented out due to duplicate info in reason , prop.lastSuggested.sensitivityRatio ? ' • <b>Sensitivity Ratio:</b> ' + (parseFloat(prop.lastSuggested.sensitivityRatio) * 100).toFixed(0) + '%' : ''
];

//if (_.includes(selectedFields, 'iob')) {
//valueParts = concatIOB(valueParts);
//}

events.push({
time: prop.lastSuggested.moment,
value: valueParts.join('')
});
}
}

/*
function addSuggestion () {
if (prop.lastSuggested) {
var bg = prop.lastSuggested.bg;
Expand All @@ -399,29 +431,32 @@ function init (ctx) {
}
var valueParts = [
valueString('BG: ', bg)
, valueString(', ', prop.lastSuggested.reason)
, prop.lastSuggested.sensitivityRatio ? ', <b>Sensitivity Ratio:</b> ' + prop.lastSuggested.sensitivityRatio : ''
valueString('• <b>BG:</b> ', bg)
, valueString(' ', prop.lastSuggested.reason)
, prop.lastSuggested.sensitivityRatio ? ' <b>Sensitivity Ratio:</b> ' + (parseFloat(prop.lastSuggested.sensitivityRatio) * 100).toFixed(0) + '%' : ''
];
if (_.includes(selectedFields, 'iob')) {
//if (_.includes(selectedFields, 'iob')) { //Daniel: Commented out to always add IOB info to suggested devicestatus
valueParts = concatIOB(valueParts);
}
//}
events.push({
time: prop.lastSuggested.moment
, value: valueParts.join('')
});
}
}
*/

function concatIOB (valueParts) {
if (prop.lastIOB) {
valueParts = valueParts.concat([
', IOB: '
'<br>IOB: '
, sbx.roundInsulinForDisplayFormat(prop.lastIOB.iob) + 'U'
, prop.lastIOB.basaliob ? ', Basal IOB ' + sbx.roundInsulinForDisplayFormat(prop.lastIOB.basaliob) + 'U' : ''
, prop.lastIOB.bolusiob ? ', Bolus IOB ' + sbx.roundInsulinForDisplayFormat(prop.lastIOB.bolusiob) + 'U' : ''
/*
, prop.lastIOB.basaliob ? ' (basal IOB ' + sbx.roundInsulinForDisplayFormat(prop.lastIOB.basaliob) + 'U)' : ''
, prop.lastIOB.bolusiob ? ' (bolus IOB ' + sbx.roundInsulinForDisplayFormat(prop.lastIOB.bolusiob) + 'U)' : ''
*/
]);
}

Expand Down Expand Up @@ -476,22 +511,36 @@ function init (ctx) {
return points;
}

if ('enacted' === prop.status.code) {
// Daniel: This is a "hack" that makes iAPS pill and prediction chart update nearly 100% in Nightscout chart
// One downside is that the lastenacted valueparts only shows up in iAPS for the latest entry, and dont show up in iAPS pill in retro view, only the headline that iAPS was enacted, and then the valueparts from last.suggested. This is due to the offset, which i havent been able to resolve in this hack
// I've however added IOB, IOB Basal and IOB Bolus info to suggested status, which makes the suggested info practically identical to enacted valuepart
// "suggested" needs to be uploaded from iAPS in conjunction with "enacted" for retro info to show (some versions of iAPS just uploads enacted, please check this in your instnance and add suggested upload to iAPS)
// Sometimes, when uploads from iAPS comes in late, the prediction lines and iAPS pill can temporary disappar in real time view, but it should come back after approx 10 seconds and also populate eventual missing data in the previous 5 min point to make history complete
// In my testing, all enacted/suggested statuses that actually gets uploaded from iAPS to NS->MongoDB is availible in
// What do the hack do? Together with changes in ddata.js filter (to allow 1 minute late entries) It checks if the event is 'enacted' within the last 10 seconds from "moment", effectively widening the window for how old entries that gets visualized and saved in ddata in Nightscout (which makes them available in the openaps pill retroview)
if ('enacted' === prop.status.code && prop.lastEnacted.moment.isAfter(moment().subtract(10, 'seconds'))) {
var canceled = prop.lastEnacted.rate === 0 && prop.lastEnacted.duration === 0;
var bg = prop.lastEnacted.bg;
var units = sbx.data.profile.getUnits();

if (units === 'mmol') {
bg = Math.round(bg / consts.MMOL_TO_MGDL * 10) / 10;
}

var valueParts = [
valueString('BG: ', prop.lastEnacted.bg)
, ', <b>Temp Basal' + (canceled ? ' Canceled' : ' Started') + '</b>'
, canceled ? '' : ' ' + prop.lastEnacted.rate.toFixed(2) + ' for ' + prop.lastEnacted.duration + 'm'
valueString('• <b>BG:</b> ', bg)
, ' <b>Temp Basal' + (canceled ? ' Canceled' : ' Started') + '</b>'
, canceled ? '' : ' ' + prop.lastEnacted.rate.toFixed(2) + 'U/h for ' + prop.lastEnacted.duration + 'm'
, valueString(', ', prop.lastEnacted.reason)
, prop.lastEnacted.mealAssist && _.includes(selectedFields, 'meal-assist') ? ' <b>Meal Assist:</b> ' + prop.lastEnacted.mealAssist : ''
, prop.lastEnacted.mealAssist && _.includes(selectedFields, 'meal-assist') ? ' <b>Meal Assist:</b> ' + prop.lastEnacted.mealAssist : ''
];

if (prop.lastSuggested && prop.lastSuggested.moment.isAfter(prop.lastEnacted.moment)) {
addSuggestion();
} else {
valueParts = concatIOB(valueParts);
}
//Always add bot addsuggestion and valueparts in enacted info
if (prop.lastSuggested && prop.lastSuggested.moment.isAfter(prop.lastEnacted.moment)) {
addSuggestion();
}

valueParts = concatIOB(valueParts);

events.push({
time: prop.lastEnacted.moment
Expand Down

0 comments on commit e751619

Please sign in to comment.