Alert only every 10 minutes (this should be a setting at some point)

This commit is contained in:
Timothy Allen 2024-05-16 16:33:45 +02:00
parent a9e35e3d93
commit 9c24251caa
2 changed files with 81 additions and 35 deletions

View File

@ -3,14 +3,14 @@ This extension integrates Nightscout into GNOME Shell.
## Features ## Features
* Retrieve glucose values from Nightscout. * Retrieve glucose values from Nightscout.
* Show a notification when your glucose readings are missing. * Show a notification when glucose readings are missing.
* High, low and missing readings notifications on your GNOME message tray. * High, low and missing readings notifications on your GNOME message tray.
* Glucose is falling down and raising up notifications on your GNOME message tray. * Notifications if glucose is rapidly falling/rising on your GNOME message tray.
## Missing features ## Missing features
* Localization support * Localization support
* Configurable levels for alerts (now 80 mg/dL for low and 180 mg/dL for high) * Configurable levels for alerts (now 80 mg/dL for low and 180 mg/dL for high)
* Configurable delta for raising and falling alerts (now -10 mg/dL for falling and +10 mg/dL for raising) * Configurable delta for raising and falling alerts (now -20 mg/dL for falling and +20 mg/dL for raising)
* Full graph of recent levels when clicking on the indicator * Full graph of recent levels when clicking on the indicator
### Main repository ### Main repository

View File

@ -48,6 +48,13 @@ export default class NightscoutExtension extends Extension {
// () => this.openPreferences()); // () => this.openPreferences());
Main.panel.addToStatusArea(this.uuid, this._indicator); Main.panel.addToStatusArea(this.uuid, this._indicator);
this._dismissUp = -1;
this._dismissDown = -1;
this._dismissHigh = -1;
this._dismissLow = -1;
this._dismissMissing = -1;
this._update(); this._update();
this._timeout = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 60, () => { this._timeout = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 60, () => {
this._update(); this._update();
@ -94,8 +101,18 @@ export default class NightscoutExtension extends Extension {
return; return;
} }
let now = Date.now();
let units = this._settings.get_string('units'); let units = this._settings.get_string('units');
// These should probably be settings
let deltaUp = 20;
let deltaDown = -20;
let minHigh = 180;
let maxLow = 80;
let maxElapsedSecs = 600;
let warnEverySecs = 10 * 60;
let glucoseValue = entry.sgv; let glucoseValue = entry.sgv;
let directionValue = entry.direction; let directionValue = entry.direction;
let delta = entry.delta; let delta = entry.delta;
@ -110,48 +127,72 @@ export default class NightscoutExtension extends Extension {
displayDelta = displayDelta.toFixed(2); displayDelta = displayDelta.toFixed(2);
} }
let elapsedSec = Math.floor((Date.now() - date) / 1000); let elapsedSecs = Math.floor((now - date) / 1000);
let elapsedMin = Math.floor(elapsedSec / 60); let elapsedMins = Math.floor(elapsedSecs / 60);
let arrow = this._fromNameToArrowCharacter(directionValue); let arrow = this._fromNameToArrowCharacter(directionValue);
let text = `${displayGlucoseValue} ${arrow}`; let text = `${displayGlucoseValue} ${arrow}`;
if (elapsedSec >= 600) { if (elapsedSecs >= maxElapsedSecs) {
this._label.style_class = 'expired-data'; this._label.style_class = 'expired-data';
// only warn every warnEverySecs
if (this._dismissMissing < 0 || Math.floor((now - this._dismissMissing) / 1000) > warnEverySecs ) {
this._notify({ this._notify({
title: _('Missing readings'), title: _('Missing readings'),
body: _('There have been no new readings since %d minutes ago'.format(elapsedMin)), body: _('There have been no new readings since %d minutes ago'.format(elapsedMins)),
}); });
this._dismissMissing = now;
}
} else { } else {
this._label.style_class = 'fresh-data'; this._label.style_class = 'fresh-data';
} }
if (glucoseValue < 80) { if (glucoseValue < maxLow) {
this._label.style_class = 'low-glucose'; this._label.style_class = 'low-glucose';
// only warn every warnEverySecs
if (this._dismissLow < 0 || Math.floor((now - this._dismissLow) / 1000) > warnEverySecs ) {
this._notify({ this._notify({
title: _('Blood glucose is low!'), title: _('Blood glucose is low!'),
body: _('Your glucose is now %d %s'.format(displayGlucoseValue, units)), body: _('Your glucose is now %f %s'.format(displayGlucoseValue, units)),
}); });
} else if (glucoseValue > 180) { this._dismissLow = now;
}
} else if (glucoseValue > minHigh) {
this._label.style_class = 'high-glucose'; this._label.style_class = 'high-glucose';
// only warn every warnEverySecs
if (this._dismissHigh < 0 || Math.floor((now - this._dismissHigh) / 1000) > warnEverySecs ) {
this._notify({ this._notify({
title: _('Blood glucose is high!'), title: _('Blood glucose is high!'),
body: _('Your glucose is now %d %s'.format(displayGlucoseValue, units)), body: _('Your glucose is now %f %s'.format(displayGlucoseValue, units)),
}); });
this._dismissHigh = now;
}
} else { } else {
this._label.style_class = 'fresh-data'; this._label.style_class = 'fresh-data';
} }
if (delta >= 10) { if (delta >= deltaUp) {
console.log("delta: ", delta);
// only warn every warnEverySecs
if (this._dismissUp < 0 || Math.floor((now - this._dismissUp) / 1000) > warnEverySecs ) {
this._notify({ this._notify({
title: _('Blood glucose rising quickly'), title: _('Blood glucose rising quickly'),
body: _('Your glucose has risen %d %s since the last reading'.format(displayDelta, units)), body: _('Your glucose has risen %f %s since the last reading'.format(displayDelta, units)),
}); });
} else if (delta <= -10) { this._dismissUp = now;
}
} else if (delta <= deltaDown) {
console.log("delta: ", delta);
console.log("displayDelta: ", displayDelta);
console.log("deltaDown: ", deltaDown);
// only warn every warnEverySecs
if (this._dismissDown < 0 || Math.floor((now - this._dismissDown) / 1000) > warnEverySecs ) {
this._notify({ this._notify({
title: _('Blood glucose falling quickly'), title: _('Blood glucose falling quickly'),
body: _('Your glucose has fallen %d %s since the last reading'.format(displayDelta, units)), body: _('Your glucose has fallen %f %s since the last reading'.format(displayDelta, units)),
}); });
this._dismissDown = now;
}
} }
this._label.set_text(text); this._label.set_text(text);
@ -202,7 +243,7 @@ export default class NightscoutExtension extends Extension {
this._notify({ this._notify({
title: _('Nightscout Extension'), title: _('Nightscout Extension'),
body: _('Unable to retrieve data: authorization failed'), body: _('Unable to retrieve data: authorization failed'),
callback: () => { this.openPreferences() }, activate_callback: () => { this.openPreferences() },
}); });
} else { } else {
Main.notify('Nightscout Extension', _('Unable to retrieve data: please check your internet connection')); Main.notify('Nightscout Extension', _('Unable to retrieve data: please check your internet connection'));
@ -210,7 +251,7 @@ export default class NightscoutExtension extends Extension {
}); });
}; };
_notify({title, body, callback}) { _notify({title, body, activated_callback, destroy_callback}) {
const notification = new MessageTray.Notification({ const notification = new MessageTray.Notification({
source: this._systemSource, source: this._systemSource,
title: title, title: title,
@ -220,9 +261,14 @@ export default class NightscoutExtension extends Extension {
gicon: this._getIcon("nightscout-icon"), gicon: this._getIcon("nightscout-icon"),
urgency: MessageTray.Urgency.NORMAL, urgency: MessageTray.Urgency.NORMAL,
}); });
if (typeof(callback) === 'function') { if (typeof(activate_callback) === 'function') {
notification.connect('activated', _notification => { notification.connect('activated', (_notification, reason) => {
callback(); activate_callback(_notification, reason);
});
}
if (typeof(destroy_callback) === 'function') {
notification.connect('destroy', (_notification, reason) => {
destroy_callback(_notification, reason);
}); });
} }
this._systemSource.addNotification(notification); this._systemSource.addNotification(notification);