Skip to content

Commit

Permalink
feat: disable pump battery alarms at night option (nightscout#5359)
Browse files Browse the repository at this point in the history
* feat: add feature to disable pump battery alarms at night

* add timezone handling for server side

* Update pump.test.js

* Update pump.test.js

* Update pump.test.js

Co-authored-by: Jeremy Cunningham <jpcunningh@gmail.com>
Co-authored-by: Sulka Haro <sulka@sulka.net>
  • Loading branch information
3 people authored and arnaudlimbourg committed Jul 4, 2021
1 parent 80dd4ef commit ad19641
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 12 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ To learn more about the Nightscout API, visit https://YOUR-SITE.com/api-docs/ or
### Predefined values for your browser settings (optional)

* `TIME_FORMAT` (`12`)- possible values `12` or `24`
* `DAY_START` (`7.0`) - time for start of day (0.0 - 24.0) for features using day time / night time options
* `DAY_END` (`21.0`) - time for end of day (0.0 - 24.0) for features using day time / night time options
* `NIGHT_MODE` (`off`) - possible values `on` or `off`
* `SHOW_RAWBG` (`never`) - possible values `always`, `never` or `noise`
* `CUSTOM_TITLE` (`Nightscout`) - Title for the main view
Expand Down Expand Up @@ -511,6 +513,7 @@ To learn more about the Nightscout API, visit https://YOUR-SITE.com/api-docs/ or
* `PUMP_URGENT_BATT_P` (`20`) - The % of the pump battery remaining, an urgent alarm will be triggered when dropping below this threshold.
* `PUMP_WARN_BATT_V` (`1.35`) - The voltage (if percent isn't available) of the pump battery, a warning will be triggered when dropping below this threshold.
* `PUMP_URGENT_BATT_V` (`1.30`) - The voltage (if percent isn't available) of the pump battery, an urgent alarm will be triggered when dropping below this threshold.
* `PUMP_WARN_BATT_QUIET_NIGHT` (`false`) - Do not generate battery alarms at night.

##### `openaps` (OpenAPS)
Integrated OpenAPS loop monitoring, uses these extended settings:
Expand Down
25 changes: 19 additions & 6 deletions lib/plugins/pump.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ function init (ctx) {
var retroFields = cleanList(sbx.extendedSettings.retroFields);
retroFields = isEmpty(retroFields) ? ['reservoir', 'battery'] : retroFields;

var profile = sbx.data.profile;
var warnBattQuietNight = sbx.extendedSettings.warnBattQuietNight;

if (warnBattQuietNight && (!profile || !profile.hasData() || !profile.getTimezone())) {
console.warn('PUMP_WARN_BATT_QUIET_NIGHT requires a treatment profile with time zone set to obtain user time zone');
warnBattQuietNight = false;
}

return {
fields: fields
, retroFields: retroFields
Expand All @@ -47,6 +55,9 @@ function init (ctx) {
, urgentBattP: sbx.extendedSettings.urgentBattP || 20
, warnOnSuspend: sbx.extendedSettings.warnOnSuspend || false
, enableAlerts: sbx.extendedSettings.enableAlerts || false
, warnBattQuietNight: warnBattQuietNight || false
, dayStart: sbx.settings.dayStart
, dayEnd: sbx.settings.dayEnd
};
};

Expand Down Expand Up @@ -246,17 +257,17 @@ function init (ctx) {
}
}

function updateBattery (type, prefs, result) {
function updateBattery (type, prefs, result, batteryWarn) {
if (result.battery) {
result.battery.label = 'Battery';
result.battery.display = result.battery.value + type;
var urgent = type === 'v' ? prefs.urgentBattV : prefs.urgentBattP;
var warn = type === 'v' ? prefs.warnBattV : prefs.warnBattP;

if (result.battery.value < urgent) {
if (result.battery.value < urgent && batteryWarn) {
result.battery.level = levels.URGENT;
result.battery.message = 'URGENT: Pump Battery Low';
} else if (result.battery.value < warn) {
} else if (result.battery.value < warn && batteryWarn) {
result.battery.level = levels.WARN;
result.battery.message = 'Warning, Pump Battery Low';
} else {
Expand Down Expand Up @@ -300,7 +311,9 @@ function init (ctx) {

function prepareData (prop, prefs, sbx) {
var pump = (prop && prop.pump) || { };

var time = (sbx.data.profile && sbx.data.profile.getTimezone()) ? moment(sbx.time).tz(sbx.data.profile.getTimezone()) : moment(sbx.time);
var now = time.hours() + time.minutes() / 60.0 + time.seconds() / 3600.0;
var batteryWarn = !(prefs.warnBattQuietNight && (now < prefs.dayStart || now > prefs.dayEnd));
var result = {
level: levels.NONE
, clock: pump.clock ? { value: moment(pump.clock) } : null
Expand All @@ -317,10 +330,10 @@ function init (ctx) {

if (pump.battery && pump.battery.percent) {
result.battery = { value: pump.battery.percent, unit: 'percent' };
updateBattery('%', prefs, result);
updateBattery('%', prefs, result, batteryWarn);
} else if (pump.battery && pump.battery.voltage) {
result.battery = { value: pump.battery.voltage, unit: 'volts'};
updateBattery('v', prefs, result);
updateBattery('v', prefs, result, batteryWarn);
}

result.device = { label: translate('Device'), display: prop.device };
Expand Down
2 changes: 2 additions & 0 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ function init () {
var settings = {
units: 'mg/dl'
, timeFormat: 12
, dayStart: 7.0
, dayEnd: 21.0
, nightMode: false
, editMode: true
, showRawbg: 'never'
Expand Down
61 changes: 55 additions & 6 deletions tests/pump.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var top_ctx = {
top_ctx.language.set('en');
var env = require('../env')();
var levels = require('../lib/levels');
var profile = require('../lib/profilefunctions')();
top_ctx.levels = levels;
var pump = require('../lib/plugins/pump')(top_ctx);
var sandbox = require('../lib/sandbox')(top_ctx);
Expand Down Expand Up @@ -51,6 +52,10 @@ var statuses = [{
}
}];

var profileData =
{
'timezone': moment.tz.guess()
};

var statuses2 = [{
created_at: '2015-12-05T17:35:00.000Z'
Expand Down Expand Up @@ -183,7 +188,7 @@ describe('pump', function ( ) {
var sbx = sandbox.clientInit(ctx, now.valueOf(), {
devicestatus: statuses
});
sbx.extendedSettings = { 'enableAlerts': 'TRUE' };
sbx.extendedSettings = { 'enableAlerts': true };
pump.setProperties(sbx);
pump.checkNotifications(sbx);

Expand Down Expand Up @@ -211,7 +216,7 @@ describe('pump', function ( ) {
var sbx = sandbox.clientInit(ctx, now.valueOf(), {
devicestatus: lowResStatuses
});
sbx.extendedSettings = { 'enableAlerts': 'TRUE' };
sbx.extendedSettings = { 'enableAlerts': true };
pump.setProperties(sbx);
pump.checkNotifications(sbx);

Expand Down Expand Up @@ -240,7 +245,7 @@ describe('pump', function ( ) {
var sbx = sandbox.clientInit(ctx, now.valueOf(), {
devicestatus: lowResStatuses
});
sbx.extendedSettings = { 'enableAlerts': 'TRUE' };
sbx.extendedSettings = { 'enableAlerts': true };
pump.setProperties(sbx);
pump.checkNotifications(sbx);

Expand Down Expand Up @@ -270,7 +275,7 @@ describe('pump', function ( ) {
var sbx = sandbox.clientInit(ctx, now.valueOf(), {
devicestatus: lowBattStatuses
});
sbx.extendedSettings = { 'enableAlerts': 'TRUE' };
sbx.extendedSettings = { 'enableAlerts': true };
pump.setProperties(sbx);
pump.checkNotifications(sbx);

Expand Down Expand Up @@ -299,7 +304,7 @@ describe('pump', function ( ) {
var sbx = sandbox.clientInit(ctx, now.valueOf(), {
devicestatus: lowBattStatuses
});
sbx.extendedSettings = { 'enableAlerts': 'TRUE' };
sbx.extendedSettings = { 'enableAlerts': true };
pump.setProperties(sbx);
pump.checkNotifications(sbx);

Expand All @@ -310,6 +315,50 @@ describe('pump', function ( ) {
done();
});

it('not generate a battery alarm during night when PUMP_WARN_BATT_QUIET_NIGHT is true', function (done) {
var ctx = {
settings: {
units: 'mg/dl'
, dayStart: 24 // Set to 24 so it always evaluates true in test
, dayEnd: 21.0
}
, pluginBase: {
updatePillText: function mockedUpdatePillText(plugin, options) {
options.label.should.equal('Pump');
options.value.should.equal('86.4U');
done();
}
}
, notifications: require('../lib/notifications')(env, top_ctx)
, language: require('../lib/language')()
, levels: levels
};

ctx.notifications.initRequests();

var lowBattStatuses = _.cloneDeep(statuses);
lowBattStatuses[1].pump.battery.voltage = 1.00;

var sbx = sandbox.clientInit(ctx, now.valueOf(), {
devicestatus: lowBattStatuses
, profiles: [profileData]
});
profile.loadData(_.cloneDeep([profileData]));
sbx.data.profile = profile;

sbx.extendedSettings = {
enableAlerts: true
, warnBattQuietNight: true
};
pump.setProperties(sbx);
pump.checkNotifications(sbx);

var highest = ctx.notifications.findHighestAlarm('Pump');
should.not.exist(highest);

done();
});

it('not generate an alert for a stale pump data, when there is an offline marker', function (done) {
var ctx = {
settings: {
Expand All @@ -326,7 +375,7 @@ describe('pump', function ( ) {
devicestatus: statuses
, treatments: [{eventType: 'OpenAPS Offline', mills: now.valueOf(), duration: 60}]
});
sbx.extendedSettings = { 'enableAlerts': 'TRUE' };
sbx.extendedSettings = { 'enableAlerts': true };
pump.setProperties(sbx);
pump.checkNotifications(sbx);

Expand Down

0 comments on commit ad19641

Please sign in to comment.