Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
alexa ar2 translatable
  • Loading branch information
tanja3981 committed Nov 1, 2018
commit c95c71ee1f94e96d0a31d41bb7aa5054d30b4fc7
8 changes: 8 additions & 0 deletions lib/language.js
Original file line number Diff line number Diff line change
Expand Up @@ -12312,6 +12312,14 @@ function init() {
, pl: 'Glikemia RAW wynosi %1'
, ru: 'ваши необработанные данные RAW $1'
},
'alexaAr2NoHandler': {
en: 'AR2 plugin does not seem to be enabled',
de: 'AR2 Plugin ist nicht aktiviert'
},
'alexaAr2Handler': {
en: 'You are expected to be between %1 and %2 over the %3',
de: 'Der %3 erwartete Blutzucker liegt zwischen %1 und %2'
},
'alexaOpenAPSForecast': {
en: 'The OpenAPS Eventual BG is %1'
, cs: 'OpenAPS Eventual BG je %1'
Expand Down
71 changes: 43 additions & 28 deletions lib/plugins/ar2.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ var AR = [-0.723, 1.716];
//TODO: move this to css
var AR2_COLOR = 'cyan';

function init (ctx) {

function init(ctx) {
var translate = ctx.language.translate;
var ar2 = {
name: 'ar2'
, label: 'AR2'
, pluginType: 'forecast'
};

function buildTitle(prop, sbx) {
var rangeLabel = prop.eventName ? sbx.translate(prop.eventName, { ci: true }).toUpperCase() : sbx.translate('Check BG');
var rangeLabel = prop.eventName ? sbx.translate(prop.eventName, {ci: true}).toUpperCase() : sbx.translate('Check BG');
var title = sbx.levels.toDisplay(prop.level) + ', ' + rangeLabel;

var sgv = sbx.lastScaledSGV();
Expand All @@ -34,8 +34,8 @@ function init (ctx) {
return title;
}

ar2.setProperties = function setProperties (sbx) {
sbx.offerProperty('ar2', function setAR2 ( ) {
ar2.setProperties = function setProperties(sbx) {
sbx.offerProperty('ar2', function setAR2() {

var prop = {
forecast: ar2.forecast(sbx)
Expand All @@ -49,7 +49,9 @@ function init (ctx) {
}

var predicted = prop.forecast && prop.forecast.predicted;
var scaled = predicted && _.map(predicted, function(p) { return sbx.scaleEntry(p) } );
var scaled = predicted && _.map(predicted, function (p) {
return sbx.scaleEntry(p)
});

if (scaled && scaled.length >= 3) {
prop.displayLine = 'BG 15m: ' + scaled[2] + ' ' + sbx.unitsLabel;
Expand All @@ -59,7 +61,7 @@ function init (ctx) {
});
};

ar2.checkNotifications = function checkNotifications (sbx) {
ar2.checkNotifications = function checkNotifications(sbx) {
if (sbx.time - sbx.lastSGVMills() > times.mins(10).msecs) {
return;
}
Expand All @@ -79,7 +81,7 @@ function init (ctx) {
}
};

ar2.forecast = function forecast (sbx) {
ar2.forecast = function forecast(sbx) {

var result = {
predicted: []
Expand Down Expand Up @@ -110,7 +112,7 @@ function init (ctx) {
sbx.pluginBase.addForecastPoints(ar2.forecastCone(sbx), {type: 'ar2', label: 'AR2 Forecast'});
};

ar2.forecastCone = function forecastCone (sbx) {
ar2.forecastCone = function forecastCone(sbx) {

if (!okToForecast(sbx)) {
return [];
Expand All @@ -124,12 +126,12 @@ function init (ctx) {
//offset from points so they are at a unique time
if (coneFactor > 0) {
next.points.push(ar2Point(next
, { offset: 2000, coneFactor: -coneFactor, step: step }
, {offset: 2000, coneFactor: -coneFactor, step: step}
));
}

next.points.push(ar2Point(next
, { offset: 4000, coneFactor: coneFactor, step: step }
, {offset: 4000, coneFactor: coneFactor, step: step}
));

return next;
Expand All @@ -145,7 +147,7 @@ function init (ctx) {
return result.points;
};

function alexaAr2Handler (next, slots, sbx) {
function alexaAr2Handler(next, slots, sbx) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The ar2.js should not contain any alexa specific code in it.

if (sbx.properties.ar2.forecast.predicted) {
var forecast = sbx.properties.ar2.forecast.predicted;
var max = forecast[0].mgdl;
Expand All @@ -162,18 +164,25 @@ function init (ctx) {
maxForecastMills = forecast[i].mills;
}
}
var response = 'You are expected to be between ' + min + ' and ' + max + ' over the ' + moment(maxForecastMills).from(moment(sbx.time));
var response = translate('alexaAr2Handler', {
params: [
min,//min
max,//max
moment(maxForecastMills).from(moment(sbx.time))//time
]
}
);
next('AR2 Forecast', response);
} else {
next('AR2 Forecast', 'AR2 plugin does not seem to be enabled');
next('AR2 Forecast', translate('alexaAr2NoHandler') );
}
}

ar2.alexa = {
intentHandlers: [{
intent: 'MetricNow'
, routableSlot:'metric'
, slots:['ar2 forecast', 'forecast']
, routableSlot: 'metric'
, slots: ['ar2 forecast', 'forecast']
, intentHandler: alexaAr2Handler
}]
};
Expand All @@ -185,9 +194,9 @@ function checkForecast(forecast, sbx) {
var result = undefined;

if (forecast && forecast.avgLoss > URGENT_THRESHOLD) {
result = { level: sbx.levels.URGENT };
result = {level: sbx.levels.URGENT};
} else if (forecast && forecast.avgLoss > WARN_THRESHOLD) {
result = { level: sbx.levels.WARN };
result = {level: sbx.levels.WARN};
}

if (result) {
Expand All @@ -198,8 +207,10 @@ function checkForecast(forecast, sbx) {
return result;
}

function selectEventType (prop, sbx) {
var predicted = prop.forecast && _.map(prop.forecast.predicted, function(p) { return sbx.scaleEntry(p) } );
function selectEventType(prop, sbx) {
var predicted = prop.forecast && _.map(prop.forecast.predicted, function (p) {
return sbx.scaleEntry(p)
});

var in20mins = predicted && predicted.length >= 4 ? predicted[3] : undefined;

Expand All @@ -217,7 +228,7 @@ function selectEventType (prop, sbx) {
return eventName;
}

function pushoverSound (prop, levels) {
function pushoverSound(prop, levels) {
var sound;

if (prop.level === levels.URGENT) {
Expand All @@ -231,7 +242,7 @@ function pushoverSound (prop, levels) {
return sound;
}

function getConeFactor (sbx) {
function getConeFactor(sbx) {
var value = Number(sbx.extendedSettings.coneFactor);
if (isNaN(value) || value < 0) {
value = 2;
Expand All @@ -251,7 +262,7 @@ function okToForecast(sbx) {
return bgnow.mean >= BG_MIN && delta.mean5MinsAgo && _.isNumber(delta.mean5MinsAgo);
}

function initAR2 (sbx) {
function initAR2(sbx) {
var bgnow = sbx.properties.bgnow;
var delta = sbx.properties.delta;
var mean5MinsAgo = delta.mean5MinsAgo;
Expand All @@ -264,7 +275,7 @@ function initAR2 (sbx) {
};
}

function incrementAR2 (result) {
function incrementAR2(result) {
return {
forecastTime: result.forecastTime + times.mins(5).msecs
, points: result.points || []
Expand All @@ -278,7 +289,7 @@ function pushPoint(result) {

next.points.push(ar2Point(
next
, { offset: 2000 }
, {offset: 2000}
));

return next;
Expand All @@ -302,15 +313,19 @@ function ar2Point(next, options) {
}


function buildDebug (prop, sbx) {
function buildDebug(prop, sbx) {
return prop.forecast && {
forecast: {
avgLoss: prop.forecast.avgLoss
, predicted: _.map(prop.forecast.predicted, function(p) { return sbx.scaleEntry(p) }).join(', ')
, predicted: _.map(prop.forecast.predicted, function (p) {
return sbx.scaleEntry(p)
}).join(', ')
}
};
}

function log10(val) { return Math.log(val) / Math.LN10; }
function log10(val) {
return Math.log(val) / Math.LN10;
}

module.exports = init;