Skip to content

Commit

Permalink
Fix stale data alarms on latest iOS (nightscout#4542)
Browse files Browse the repository at this point in the history
* Suspend TimeAgo reports for 15 seconds if the app has been sleeping. Add a BACK link to reports (due to iOS now not resetting springboard web apps, so it's impossible to go back to the main view)

* Move detection to another plugin call
  • Loading branch information
sulkaharo authored and tanja3981 committed May 21, 2019
1 parent d7b2aed commit 15eff26
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 62 deletions.
135 changes: 76 additions & 59 deletions lib/plugins/timeago.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

var levels = require('../levels');
var times = require('../times');
var lastChecked = new Date();
var lastSuspendTime = new Date("1900-01-01");

function init (ctx) {
function init(ctx) {
var translate = ctx.language.translate;

var timeago = {
name: 'timeago'
, label: 'Timeago'
, pluginType: 'pill-status'
, pillFlip: true
name: 'timeago',
label: 'Timeago',
pluginType: 'pill-status',
pillFlip: true
};

timeago.checkNotifications = function checkNotifications (sbx) {
timeago.checkNotifications = function checkNotifications(sbx) {

console.log('timeago.checkNotifications');

if (!sbx.extendedSettings.enableAlerts) {
return;
Expand All @@ -31,44 +35,59 @@ function init (ctx) {
return lines.join('\n');
}

function sendAlarm (opts) {
function sendAlarm(opts) {
var agoDisplay = timeago.calcDisplay(lastSGVEntry, sbx.time);

sbx.notifications.requestNotify({
level: opts.level
, title: translate('Stale data, check rig?')
, message: buildMessage(agoDisplay)
, eventName: timeago.name
, plugin: timeago
, group: 'Time Ago'
, pushoverSound: opts.pushoverSound
, debug: agoDisplay
level: opts.level,
title: translate('Stale data, check rig?'),
message: buildMessage(agoDisplay),
eventName: timeago.name,
plugin: timeago,
group: 'Time Ago',
pushoverSound: opts.pushoverSound,
debug: agoDisplay
});
}

var status = timeago.checkStatus(sbx);
if (status === 'urgent') {
sendAlarm({
level: levels.URGENT
, pushoverSound: 'echo'
level: levels.URGENT,
pushoverSound: 'echo'
});
} else if (status === 'warn') {
sendAlarm({
level: levels.WARN
, pushoverSound: 'echo'
level: levels.WARN,
pushoverSound: 'echo'
});
}

};

timeago.checkStatus = function checkStatus (sbx) {
timeago.checkStatus = function checkStatus(sbx) {

// Check if the app has been suspended; if yes, snooze data missing alarmn for 15 seconds
var now = new Date();
var delta = now.getTime() - lastChecked.getTime();
lastChecked = now;

if (delta > 15 * 1000) { // Looks like we've been hibernating
lastSuspendTime = now;
}

var timeSinceLastSuspended = now.getTime() - lastSuspendTime.getTime();

var lastSGVEntry = sbx.lastSGVEntry()
, warn = sbx.settings.alarmTimeagoWarn
, warnMins = sbx.settings.alarmTimeagoWarnMins || 15
, urgent = sbx.settings.alarmTimeagoUrgent
, urgentMins = sbx.settings.alarmTimeagoUrgentMins || 30
;
if (timeSinceLastSuspended < (15 * 10000)) {
console.log('Hibernation detected, suspending timeago alarm');
return 'current';
}

var lastSGVEntry = sbx.lastSGVEntry(),
warn = sbx.settings.alarmTimeagoWarn,
warnMins = sbx.settings.alarmTimeagoWarnMins || 15,
urgent = sbx.settings.alarmTimeagoUrgent,
urgentMins = sbx.settings.alarmTimeagoUrgentMins || 30;

function isStale(mins) {
return sbx.time - lastSGVEntry.mills > times.mins(mins).msecs;
Expand All @@ -88,63 +107,60 @@ function init (ctx) {

};

timeago.isMissing = function isMissing (opts) {
timeago.isMissing = function isMissing(opts) {
if (!opts || !opts.entry || isNaN(opts.entry.mills) || isNaN(opts.time) || isNaN(opts.timeSince)) {
return {
label: translate('time ago')
, shortLabel: translate('ago')
label: translate('time ago'),
shortLabel: translate('ago')
};
}
};

timeago.inTheFuture = function inTheFuture (opts) {
timeago.inTheFuture = function inTheFuture(opts) {
if (opts.entry.mills - times.mins(5).msecs > opts.time) {
return {
label: translate('in the future')
, shortLabel: translate('future')
label: translate('in the future'),
shortLabel: translate('future')
};
}
};

timeago.almostInTheFuture = function almostInTheFuture (opts) {
timeago.almostInTheFuture = function almostInTheFuture(opts) {
if (opts.entry.mills > opts.time) {
return {
value: 1
, label: translate('min ago')
, shortLabel: 'm'
value: 1,
label: translate('min ago'),
shortLabel: 'm'
};
}
};

timeago.isLessThan = function isLessThan (limit, divisor, label, shortLabel) {
return function checkIsLessThan (opts) {
timeago.isLessThan = function isLessThan(limit, divisor, label, shortLabel) {
return function checkIsLessThan(opts) {
if (opts.timeSince < limit) {
return {
value: Math.max(1, Math.round(opts.timeSince / divisor))
, label: label
, shortLabel: shortLabel
value: Math.max(1, Math.round(opts.timeSince / divisor)),
label: label,
shortLabel: shortLabel
};
}
};
};

timeago.resolvers = [
timeago.isMissing
, timeago.inTheFuture
, timeago.almostInTheFuture
, timeago.isLessThan(times.mins(2).msecs, times.min().msecs, 'min ago', 'm')
, timeago.isLessThan(times.hour().msecs, times.min().msecs, 'mins ago', 'm')
, timeago.isLessThan(times.hours(2).msecs, times.hour().msecs, 'hour ago', 'h')
, timeago.isLessThan(times.day().msecs, times.hour().msecs, 'hours ago', 'h')
, timeago.isLessThan(times.days(2).msecs, times.day().msecs, 'day ago', 'd')
, timeago.isLessThan(times.week().msecs, times.day().msecs, 'days ago', 'd')
, function ( ) { return { label: 'long ago', shortLabel: 'ago' } }
timeago.isMissing, timeago.inTheFuture, timeago.almostInTheFuture, timeago.isLessThan(times.mins(2).msecs, times.min().msecs, 'min ago', 'm'), timeago.isLessThan(times.hour().msecs, times.min().msecs, 'mins ago', 'm'), timeago.isLessThan(times.hours(2).msecs, times.hour().msecs, 'hour ago', 'h'), timeago.isLessThan(times.day().msecs, times.hour().msecs, 'hours ago', 'h'), timeago.isLessThan(times.days(2).msecs, times.day().msecs, 'day ago', 'd'), timeago.isLessThan(times.week().msecs, times.day().msecs, 'days ago', 'd'),
function () {
return {
label: 'long ago',
shortLabel: 'ago'
}
}
];

timeago.calcDisplay = function calcDisplay (entry, time) {
timeago.calcDisplay = function calcDisplay(entry, time) {
var opts = {
time: time
, entry: entry
time: time,
entry: entry
};

if (time && entry && entry.mills) {
Expand All @@ -159,15 +175,16 @@ function init (ctx) {
}
};

timeago.updateVisualisation = function updateVisualisation (sbx) {
timeago.updateVisualisation = function updateVisualisation(sbx) {
var agoDisplay = timeago.calcDisplay(sbx.lastSGVEntry(), sbx.time);
var inRetroMode = sbx.data.inRetroMode;

sbx.pluginBase.updatePillText(timeago, {
value: inRetroMode ? null : agoDisplay.value
, label: inRetroMode ? translate('RETRO') : translate(agoDisplay.label)
//no warning/urgent class when in retro mode
, pillClass: inRetroMode ? 'current' : timeago.checkStatus(sbx)
value: inRetroMode ? null : agoDisplay.value,
label: inRetroMode ? translate('RETRO') : translate(agoDisplay.label)
//no warning/urgent class when in retro mode
,
pillClass: inRetroMode ? 'current' : timeago.checkStatus(sbx)
});
};

Expand Down
4 changes: 2 additions & 2 deletions static/css/report.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ body {
#tabnav {
text-align: left;
margin: 1em 0 1em 0;
font: bold 11px verdana, arial, sans-serif;
font: bold 11pt verdana, arial, sans-serif;
border-bottom: 1px solid #6c6;
list-style-type: none;
padding: 3px 10px 3px 10px;
Expand Down Expand Up @@ -59,7 +59,7 @@ body {
text-align: left;
padding: 4px;
font-size: 14px;
line-height: 15px;
line-height: 15pt;
background: white;
border: 2px solid black;
border-radius: 8px;
Expand Down
2 changes: 1 addition & 1 deletion views/reportindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

</head>
<body>
<h1><img src="/images/logo1.png"><span class="translate">Nightscout reporting</span></h1>
<p><img src="/images/logo1.png"><b style="font-size: 14pt;"><span class="translate">Nightscout reporting</span></b> <a style="font-size: 11pt;" href="/">Back main view</a></p>
<ul id="tabnav">
</ul>
<table>
Expand Down

0 comments on commit 15eff26

Please sign in to comment.