Skip to content

Commit

Permalink
Merge pull request #5 from nightscout/wip/alarm-types-update
Browse files Browse the repository at this point in the history
Wip/alarm types update
  • Loading branch information
jimsiff committed Jan 23, 2015
2 parents 649ecba + dae7388 commit d132ca0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 30 deletions.
4 changes: 2 additions & 2 deletions lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ var dir2Char = {
function emitData ( ) {
console.log('running emitData', now, patientData && patientData.length);
if (patientData.length > 0) {
io.sockets.emit("now", now);
io.sockets.emit("sgv", patientData);
}
}
Expand All @@ -62,7 +61,6 @@ var dir2Char = {

function listeners ( ) {
io.sockets.on('connection', function (socket) {
io.sockets.emit("now", now);
io.sockets.emit("sgv", patientData);
io.sockets.emit("clients", ++watchers);
socket.on('ack', function(alarmType, silenceTime) {
Expand Down Expand Up @@ -312,6 +310,7 @@ function loadData() {
}
}
}

function is_different (actual, predicted, mbg, treatment, errorCode) {
if (patientData && patientData.length < 3) {
return true;
Expand All @@ -333,6 +332,7 @@ function loadData() {

// textual diff of objects
if (JSON.stringify(old) == JSON.stringify(last)) {
console.info("data isn't different, will not send to clients");
return false;
}
return true;
Expand Down
3 changes: 2 additions & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ <h1 class="customTitle">Nightscout</h1>
<dd><input type="radio" name="timeformat-browser" id="24-browser" value="24" /><label for="24-browser">24 hours</label></dd>
</dl>
<dl class="toggle">
<dt>Enable Alarms <a class="tip" original-title="When enabled the an alarm will sound."><i class="icon-help-circled"></i></a></dt>
<dt>Enable Alarms <a class="tip" original-title="When enabled the an alarm may sound."><i class="icon-help-circled"></i></a></dt>
<dd><input type="checkbox" id="alarm-urgenthigh-browser" /><label for="alarm-urgenthigh-browser">Urgent High Alarm</label></dd>
<dd><input type="checkbox" id="alarm-high-browser" /><label for="alarm-high-browser">High Alarm</label></dd>
<dd><input type="checkbox" id="alarm-low-browser" /><label for="alarm-low-browser">Low Alarm</label></dd>
Expand Down Expand Up @@ -213,6 +213,7 @@ <h1 class="customTitle">Nightscout</h1>
<audio src="/audio/alarm.mp3" preload="auto" loop="true" class="alarm mp3" type="audio/mp3"></audio>
<audio src="/audio/alarm2.mp3" preload="auto" loop="true" class="urgent alarm2 mp3" type="audio/mp3"></audio>
</div>

<script src="/socket.io/socket.io.js"></script>
<script src="/api/v1/status.js"></script>
<script src="/bower_components/d3/d3.min.js"></script>
Expand Down
71 changes: 48 additions & 23 deletions static/js/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var app = {}, browserSettings = {}, browserStorage = $.localStorage;
var socket
, isInitialData = false
, latestSGV
, latestUpdateTime
, prevSGV
, errorCode
, treatments
Expand Down Expand Up @@ -192,6 +193,14 @@ var app = {}, browserSettings = {}, browserStorage = $.localStorage;
.style('opacity', function (d) { return highlightBrushPoints(d) });
}

function inRetroMode() {
if (!brush) return false;

var brushExtent = brush.extent();
var elementHidden = document.getElementById('bgButton').hidden == '';
return brushExtent[1].getTime() - THIRTY_MINS_IN_MS < now && elementHidden != true;
}

// function to call when context chart is brushed
function brushed(skipTimer) {

Expand Down Expand Up @@ -230,7 +239,6 @@ var app = {}, browserSettings = {}, browserStorage = $.localStorage;
}
}

var element = document.getElementById('bgButton').hidden == '';
var nowDate = new Date(brushExtent[1] - THIRTY_MINS_IN_MS);

// predict for retrospective data
Expand All @@ -239,7 +247,7 @@ var app = {}, browserSettings = {}, browserStorage = $.localStorage;
// the dexcom switches from unfiltered to filtered at the start of a rapid rise or fall, while preserving
// almost identical predications at other times.
var lookback = 2;
if (brushExtent[1].getTime() - THIRTY_MINS_IN_MS < now && element != true) {
if (inRetroMode()) {
// filter data for -12 and +5 minutes from reference time for retrospective focus data prediction
var lookbackTime = (lookback+2)*FIVE_MINS_IN_MS + 2*ONE_MIN_IN_MS;
var nowDataRaw = data.filter(function(d) {
Expand Down Expand Up @@ -319,9 +327,8 @@ var app = {}, browserSettings = {}, browserStorage = $.localStorage;
focusData = focusData.concat(prediction);
var dateTime = new Date(now);
nowDate = dateTime;
$('#currentTime')
.text(formatTime(dateTime))
.css('text-decoration', '');

updateClockDisplay();

if (errorCode) {
var errorDisplay;
Expand Down Expand Up @@ -355,9 +362,7 @@ var app = {}, browserSettings = {}, browserStorage = $.localStorage;

} else {

var secsSinceLast = (Date.now() - new Date(latestSGV.x).getTime()) / 1000;
$('#lastEntry').text(timeAgo(secsSinceLast)).toggleClass('current', secsSinceLast < 10 * 60);

updateTimeAgo();
//in this case the SGV is unscaled
if (latestSGV.y < 40) {
$('.container .currentBG').text('LOW');
Expand Down Expand Up @@ -1174,6 +1179,37 @@ var app = {}, browserSettings = {}, browserStorage = $.localStorage;
return predicted;
}

function updateClock() {
updateClockDisplay();
var interval = (60 - (new Date()).getSeconds()) * 1000 + 5;
setTimeout(updateClock,interval);

updateTimeAgo();

// Dim the screen by reducing the opacity when at nighttime
if (browserSettings.nightMode) {
if (opacity.current != opacity.NIGHT && (dateTime.getHours() > 21 || dateTime.getHours() < 7)) {
$('body').css({ 'opacity': opacity.NIGHT });
} else {
$('body').css({ 'opacity': opacity.DAY });
}
}
}

function updateClockDisplay() {
if (inRetroMode()) return;
now = Date.now();
var dateTime = new Date(now);
$('#currentTime').text(formatTime(dateTime)).css('text-decoration', '');
}

function updateTimeAgo() {
if (!latestSGV || inRetroMode()) return;

var secsSinceLast = (Date.now() - new Date(latestSGV.x).getTime()) / 1000;
$('#lastEntry').text(timeAgo(secsSinceLast)).toggleClass('current', secsSinceLast < 10 * 60);
}

function init() {

jqWindow = $(window);
Expand Down Expand Up @@ -1244,6 +1280,8 @@ var app = {}, browserSettings = {}, browserStorage = $.localStorage;
}, 100);
};

updateClock();

var silenceDropdown = new Dropdown('.dropdown-menu');

$('#bgButton').click(function (e) {
Expand All @@ -1260,27 +1298,13 @@ var app = {}, browserSettings = {}, browserStorage = $.localStorage;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
socket = io.connect();

socket.on('now', function (d) {
now = d;
var dateTime = new Date(now);
$('#currentTime').text(formatTime(dateTime));

// Dim the screen by reducing the opacity when at nighttime
if (browserSettings.nightMode) {
if (opacity.current != opacity.NIGHT && (dateTime.getHours() > 21 || dateTime.getHours() < 7)) {
$('body').css({ 'opacity': opacity.NIGHT });
} else {
$('body').css({ 'opacity': opacity.DAY });
}
}
});

socket.on('sgv', function (d) {
if (d.length > 1) {
errorCode = d.length >= 5 ? d[4] : undefined;

// change the next line so that it uses the prediction if the signal gets lost (max 1/2 hr)
if (d[0].length) {
latestUpdateTime = Date.now();
latestSGV = d[0][d[0].length - 1];
prevSGV = d[0][d[0].length - 2];
}
Expand Down Expand Up @@ -1389,6 +1413,7 @@ var app = {}, browserSettings = {}, browserStorage = $.localStorage;
, head: xhr.head
, apiEnabled: xhr.apiEnabled
, thresholds: xhr.thresholds
, alarm_types: xhr.alarm_types
, units: xhr.units
, careportalEnabled: xhr.careportalEnabled
};
Expand Down
12 changes: 8 additions & 4 deletions static/js/ui-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ function getBrowserSettings(storage) {
}
}

function appendThresholdValue(threshold) {
return app.alarm_types.indexOf('simple') == -1 ? '' : ' (' + scaleBg(threshold) + ')';
}

try {
var json = {
"units": storage.get("units"),
Expand All @@ -49,10 +53,10 @@ function getBrowserSettings(storage) {
json.alarmHigh = setDefault(json.alarmHigh, defaultSettings.alarmHigh);
json.alarmLow = setDefault(json.alarmLow, defaultSettings.alarmLow);
json.alarmUrgentLow = setDefault(json.alarmUrgentLow, defaultSettings.alarmUrgentLow);
$("#alarm-urgenthigh-browser").prop("checked", json.alarmUrgentHigh).next().text('Urgent High Alarm (' + scaleBg(app.thresholds.bg_high) + ')');
$("#alarm-high-browser").prop("checked", json.alarmHigh).next().text('High Alarm (' + scaleBg(app.thresholds.bg_target_top) + ')');
$("#alarm-low-browser").prop("checked", json.alarmLow).next().text('Low Alarm (' + scaleBg(app.thresholds.bg_target_bottom) + ')');
$("#alarm-urgentlow-browser").prop("checked", json.alarmUrgentLow).next().text('Urgent Low Alarm (' + scaleBg(app.thresholds.bg_low) + ')');
$("#alarm-urgenthigh-browser").prop("checked", json.alarmUrgentHigh).next().text('Urgent High Alarm' + appendThresholdValue(app.thresholds.bg_high));
$("#alarm-high-browser").prop("checked", json.alarmHigh).next().text('High Alarm' + appendThresholdValue(app.thresholds.bg_target_top));
$("#alarm-low-browser").prop("checked", json.alarmLow).next().text('Low Alarm' + appendThresholdValue(app.thresholds.bg_target_bottom));
$("#alarm-urgentlow-browser").prop("checked", json.alarmUrgentLow).next().text('Urgent Low Alarm' + appendThresholdValue(app.thresholds.bg_low));

json.nightMode = setDefault(json.nightMode, defaultSettings.nightMode);
$("#nightmode-browser").prop("checked", json.nightMode);
Expand Down

0 comments on commit d132ca0

Please sign in to comment.