Skip to content

Commit

Permalink
lots of cage refactoring to show warning/urgent status in pill
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoncalabrese committed May 22, 2016
1 parent 17c9ac8 commit 65cd0e4
Showing 1 changed file with 89 additions and 61 deletions.
150 changes: 89 additions & 61 deletions lib/plugins/cannulaage.js
Expand Up @@ -2,6 +2,7 @@

var _ = require('lodash');
var moment = require('moment');
var levels = require('../levels');

var cage = {
name: 'cage'
Expand All @@ -15,52 +16,40 @@ function init() {

module.exports = init;

cage.getPrefs = function getPrefs(sbx) {
cage.getPrefs = function getPrefs (sbx) {
// CAGE_INFO = 44 CAGE_WARN=48 CAGE_URGENT=70
var info = Number(sbx.extendedSettings.info);
var warn = Number(sbx.extendedSettings.warn);
var urgent = Number(sbx.extendedSettings.urgent);

return {
'info' : sbx.extendedSettings.info ? sbx.extendedSettings.info : 44,
'warn' : sbx.extendedSettings.warn ? sbx.extendedSettings.warn : 48,
'urgent' : sbx.extendedSettings.urgent ? sbx.extendedSettings.urgent : 72,
'display' : sbx.extendedSettings.display ? sbx.extendedSettings.display : 'hours',
'enableAlerts' : sbx.extendedSettings.enableAlerts
info: !isNaN(info) ? info : 44,
warn: !isNaN(warn) ? warn : 48,
urgent: !isNaN(urgent) ? urgent : 72,
display: sbx.extendedSettings.display ? sbx.extendedSettings.display : 'hours',
enableAlerts: sbx.extendedSettings.enableAlerts
};
};

cage.checkNotifications = function checkNotifications(sbx) {

cage.checkNotifications = function checkNotifications (sbx) {
var cannulaInfo = cage.findLatestTimeChange(sbx);
var prefs = cage.getPrefs(sbx);

if (!prefs.enableAlerts || !cannulaInfo.checkForAlert) { return; }

var sendNotification = false;
var title = 'Cannula age ' + cannulaInfo.age +' hours';
var sound = 'incoming';
var msg = ', change due soon';
var level = 1;

if (cannulaInfo.age === Number(prefs.info)) { sendNotification = true; }
if (cannulaInfo.age === Number(prefs.warn)) { sendNotification = true; msg = ', time to change'; }
if (cannulaInfo.age === Number(prefs.urgent)) { sendNotification = true; msg = ', change overdue!'; sound = 'persistent'; level = 2;}

if (sendNotification) {
sbx.notifications.requestNotify({
level: level
, title: title
, message: title + msg
, pushoverSound: sound
, plugin: cage
, group: 'CAGE'
, debug: {
cannulaInfo: cannulaInfo
}
});
if (cannulaInfo.notification) {
sbx.notifications.requestNotify(cannulaInfo.notification);
}
};

cage.findLatestTimeChange = function findLatestTimeChange(sbx) {
cage.findLatestTimeChange = function findLatestTimeChange (sbx) {

var prefs = cage.getPrefs(sbx);

var returnValue = {'message':'', 'found': false, 'age': 0, 'treatmentDate': null, 'checkForAlert': false};
var cannulaInfo = {
message:''
, found: false
, age: 0
, treatmentDate: null
, checkForAlert: false
};

var prevDate = 0;

Expand All @@ -69,39 +58,67 @@ cage.findLatestTimeChange = function findLatestTimeChange(sbx) {
if (treatmentDate > prevDate && treatmentDate <= sbx.time) {

prevDate = treatmentDate;
returnValue.treatmentDate = treatmentDate;
cannulaInfo.treatmentDate = treatmentDate;

//allow for 30 minute period after a full hour during which we'll alert the user
var a = moment(sbx.time);
var b = moment(returnValue.treatmentDate);
var b = moment(cannulaInfo.treatmentDate);
var days = a.diff(b,'days');
var hours = a.diff(b,'hours') - days * 24;
var age = a.diff(b,'hours');
var minFractions = a.diff(b,'minutes') - age * 60;

returnValue.checkForAlert = minFractions <= 30;

if (!returnValue.found) {
returnValue.found = true;
returnValue.age = age;
returnValue.days = days;
returnValue.hours = hours;
} else {
if (age >= 0 && age < returnValue.age) {
returnValue.age = age;
returnValue.days = days;
returnValue.hours = hours;
if (treatment.notes) {
returnValue.message = treatment.notes;
} else {
returnValue.message = '';
}
}

cannulaInfo.minFractions = a.diff(b,'minutes') - age * 60;

if (!cannulaInfo.found) {
cannulaInfo.found = true;
cannulaInfo.age = age;
cannulaInfo.days = days;
cannulaInfo.hours = hours;
} else if (age >= 0 && age < cannulaInfo.age) {
cannulaInfo.age = age;
cannulaInfo.days = days;
cannulaInfo.hours = hours;
cannulaInfo.notes = treatment.notes;
}
}
});

return returnValue;
cannulaInfo.level = levels.NONE;

var sound = 'incoming';
var message;
var sendNotification = false;

if (cannulaInfo.age >= prefs.urgent) {
sendNotification = cannulaInfo.age === prefs.urgent;
message = 'Cannula change overdue!';
sound = 'persistent';
cannulaInfo.level = levels.URGENT;
} else if (cannulaInfo.age >= prefs.warn) {
sendNotification = cannulaInfo.age === prefs.warn;
message = 'Time to change cannula';
cannulaInfo.level = levels.WARN;
} else if (cannulaInfo.age > prefs.info) {
sendNotification = cannulaInfo.age === prefs.info;
message = 'Change cannula soon';
cannulaInfo.level = levels.INFO;
}

if (sendNotification && cannulaInfo.minFractions <= 30) {
cannulaInfo.notification = {
title: 'Cannula age ' + cannulaInfo.age + ' hours'
, message: message
, pushoverSound: sound
, level: cannulaInfo.level
, plugin: cage
, group: 'CAGE'
, debug: {
cannulaInfo: cannulaInfo
}
};
}

return cannulaInfo;
};

cage.updateVisualisation = function updateVisualisation (sbx) {
Expand All @@ -110,9 +127,12 @@ cage.updateVisualisation = function updateVisualisation (sbx) {
var prefs = cage.getPrefs(sbx);

var info = [{ label: 'Inserted', value: new Date(cannulaInfo.treatmentDate).toLocaleString() }];
if (cannulaInfo.message !== '') { info.push({label: 'Notes:', value: cannulaInfo.message}); }
var shownAge;
shownAge = '';

if (!_.isEmpty(cannulaInfo.notes)) {
info.push({label: 'Notes:', value: cannulaInfo.notes});
}

var shownAge = '';
if (prefs.display === 'days' && cannulaInfo.found) {
if (cannulaInfo.age >= 24) {
shownAge += cannulaInfo.days + 'd';
Expand All @@ -122,9 +142,17 @@ cage.updateVisualisation = function updateVisualisation (sbx) {
shownAge = cannulaInfo.found ? cannulaInfo.age + 'h' : 'n/a ';
}

var statusClass = null;
if (cannulaInfo.level === levels.URGENT) {
statusClass = 'urgent';
} else if (cannulaInfo.level === levels.WARN) {
statusClass = 'warn';
}

sbx.pluginBase.updatePillText(cage, {
value: shownAge
, label: 'CAGE'
, info: info
, pillClass: statusClass
});
};

0 comments on commit 65cd0e4

Please sign in to comment.