Skip to content

Commit

Permalink
Merge pull request #386 from nightscout/wip/enable-raw
Browse files Browse the repository at this point in the history
Option(s) to Enable Raw Data
  • Loading branch information
hackingtype1 committed Feb 1, 2015
2 parents 0eef982 + 3331250 commit c0be495
Show file tree
Hide file tree
Showing 11 changed files with 430 additions and 109 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Use the [autoconfigure tool][autoconfigure] to sync an uploader to your config.

#### Features/Labs

* `ENABLE` - Used to enable optional features, currently supports: `careportal`
* `ENABLE` - Used to enable optional features, currently supports: `careportal`, `rawbg` (also `rawbg-on` to auto show raw)
* `API_SECRET` - A secret passphrase that must be at least 12 characters long, required to enable `POST` and `PUT`; also required for the Care Portal
* `BG_HIGH` (`260`) - must be set using mg/dl units; the high BG outside the target range that is considered urgent
* `BG_TARGET_TOP` (`180`) - must be set using mg/dl units; the top of the target range, also used to draw the line on the chart
Expand Down
1 change: 1 addition & 0 deletions lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function create (env, entries, settings, treatments, devicestatus) {
}

if (env.enable) {
app.enabledOptions = env.enable;
env.enable.toLowerCase().split(' ').forEach(function (value) {
var enable = value.trim();
console.info("enabling feature:", enable);
Expand Down
1 change: 1 addition & 0 deletions lib/api/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function configure (app, wares) {
var info = { status: 'ok'
, apiEnabled: app.enabled('api')
, careportalEnabled: app.enabled('api') && app.enabled('careportal')
, enabledOptions: app.enabledOptions
, units: app.get('units')
, head: wares.get_head( )
, version: app.get('version')
Expand Down
69 changes: 45 additions & 24 deletions lib/pebble.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ function directionToTrend (direction) {
}

function pebble (req, res) {
var FORTY_MINUTES = 2400000;
var cgmData = [ ];
var ONE_DAY = 24 * 60 * 60 * 1000;
var uploaderBattery;

function requestMetric() {
Expand All @@ -42,32 +41,51 @@ function pebble (req, res) {

function get_latest (err, results) {
var now = Date.now();
var sgvData = [ ];
var calData = [ ];

results.forEach(function(element, index, array) {
var next = null;
if (index + 1 < results.length) {
next = results[index + 1];
}
if (element) {
var obj = {};
if (!element.sgv) return;
obj.sgv = scaleBg(element.sgv).toString( );
obj.bgdelta = (next ? (scaleBg(element.sgv) - scaleBg(next.sgv) ) : 0);
if (useMetricBg) {
obj.bgdelta = obj.bgdelta.toFixed(1);
}
if ('direction' in element) {
obj.trend = directionToTrend(element.direction);
obj.direction = element.direction;
if (element.sgv) {
var next = null;
var sgvs = results.filter(function(d) {
return !!d.sgv;
});
if (index + 1 < sgvs.length) {
next = sgvs[index + 1];
}
obj.sgv = scaleBg(element.sgv).toString();
obj.bgdelta = (next ? (scaleBg(element.sgv) - scaleBg(next.sgv) ) : 0);
if (useMetricBg) {
obj.bgdelta = obj.bgdelta.toFixed(1);
}
if ('direction' in element) {
obj.trend = directionToTrend(element.direction);
obj.direction = element.direction;
}
obj.datetime = element.date;
if (req.rawbg) {
obj.filtered = element.filtered;
obj.unfiltered = element.unfiltered;
obj.noise = element.noise;
obj.rssi = element.rssi;
}
// obj.date = element.date.toString( );
sgvData.push(obj);
} else if (req.rawbg && element.type == 'cal') {
calData.push(element);
}
// obj.y = element.sgv;
// obj.x = element.date;
obj.datetime = element.date;
obj.battery = uploaderBattery ? "" + uploaderBattery : undefined;
// obj.date = element.date.toString( );
cgmData.push(obj);
}
});
var result = { status: [ {now:now}], bgs: cgmData.slice(0, 1) };

var count = parseInt(req.query.count) || 1;

var bgs = sgvData.slice(0, count);
//for compatibility we're keeping battery here, but it would be better somewhere else
bgs[0].battery = uploaderBattery ? "" + uploaderBattery : undefined;

var result = { status: [ {now:now}], bgs: bgs, cals: calData.slice(0, count) };
res.setHeader('content-type', 'application/json');
res.write(JSON.stringify(result));
res.end( );
Expand All @@ -80,13 +98,16 @@ function pebble (req, res) {
console.error("req.devicestatus.tail", err);
}

req.entries.list({count: 2, find: { "sgv": { $exists: true }}}, get_latest);
var earliest_data = Date.now() - ONE_DAY;
var q = { find: {"date": {"$gte": earliest_data}} };
req.entries.list(q, get_latest);
});
}
function configure (entries, devicestatus) {
function configure (entries, devicestatus, env) {
function middle (req, res, next) {
req.entries = entries;
req.devicestatus = devicestatus;
req.rawbg = env.enable.indexOf('rawbg') > -1;
next( );
}
return [middle, pebble];
Expand Down
36 changes: 25 additions & 11 deletions lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var dir2Char = {
var cgmData = [],
treatmentData = [],
mbgData = [],
calData = [],
patientData = [];

function start ( ) {
Expand Down Expand Up @@ -164,7 +165,18 @@ function update() {
obj.d = element.dateString;
obj.device = element.device;
obj.direction = directionToChar(element.direction);
obj.filtered = element.filtered;
obj.unfiltered = element.unfiltered;
obj.rssi = element.rssi;
cgmData.push(obj);
} else if (element.slope) {
var obj = {};
obj.x = element.date;
obj.d = element.dateString;
obj.scale = element.scale;
obj.intercept = element.intercept;
obj.slope = element.slope;
calData.push(obj);
}
}
});
Expand All @@ -191,6 +203,7 @@ function loadData() {
actualCurrent,
treatment = [],
mbg = [],
cal = [],
errorCode;

if (cgmData) {
Expand All @@ -200,12 +213,6 @@ function loadData() {
});

actualCurrent = actual.length > 0 ? actual[actual.length - 1].y : null;

// sgv less than or equal to 10 means error code
// or warm up period code, so ignore
actual = actual.filter(function (a) {
return a.y > 10;
})
}

if (treatmentData) {
Expand All @@ -222,6 +229,13 @@ function loadData() {
});
}

if (calData) {
cal = calData.slice(calData.length-200, calData.length);
cal.sort(function(a, b) {
return a.x - b.x;
});
}

if (actualCurrent && actualCurrent < 39) errorCode = actualCurrent;

var actualLength = actual.length - 1;
Expand Down Expand Up @@ -255,8 +269,8 @@ function loadData() {
//TODO: need to consider when data being sent has less than the 2 day minimum

// consolidate and send the data to the client
var shouldEmit = is_different(actual, predicted, mbg, treatment, errorCode);
patientData = [actual, predicted, mbg, treatment, errorCode];
var shouldEmit = is_different(actual, predicted, mbg, treatment, cal);
patientData = [actual, predicted, mbg, treatment, cal];
console.log('patientData', patientData.length);
if (shouldEmit) {
emitData( );
Expand Down Expand Up @@ -311,7 +325,7 @@ function loadData() {
}
}

function is_different (actual, predicted, mbg, treatment, errorCode) {
function is_different (actual, predicted, mbg, treatment, cal) {
if (patientData && patientData.length < 3) {
return true;
}
Expand All @@ -320,14 +334,14 @@ function loadData() {
, predicted: patientData[1].slice(-1).pop( )
, mbg: patientData[2].slice(-1).pop( )
, treatment: patientData[3].slice(-1).pop( )
, errorCode: patientData.length >= 5 ? patientData[4] : 0
, cal: patientData[4].slice(-1).pop( )
};
var last = {
actual: actual.slice(-1).pop( )
, predicted: predicted.slice(-1).pop( )
, mbg: mbg.slice(-1).pop( )
, treatment: treatment.slice(-1).pop( )
, errorCode: errorCode
, cal: cal.slice(-1).pop( )
};

// textual diff of objects
Expand Down
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ app.enable('trust proxy'); // Allows req.secure test on heroku https connections
app.use('/api/v1', api);

// pebble data
app.get('/pebble', pebble(entriesStorage, devicestatusStorage));
app.get('/pebble', pebble(entriesStorage, devicestatusStorage, env));

//app.get('/package.json', software);

Expand Down
4 changes: 4 additions & 0 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ <h1 class="customTitle">Nightscout</h1>
<dt>Night Mode <a class="tip" original-title="When enabled the page will be dimmed from 10pm - 6am."><i class="icon-help-circled"></i></a></dt>
<dd><input type="checkbox" name="nightmode-browser" id="nightmode-browser" /><label for="nightmode-browser">Enable</label></dd>
</dl>
<dl id="show-rawbg-option" class="toggle">
<dt>Show Raw BG Data <a class="tip" original-title="When enabled small white dots will be disaplyed for raw BG data"><i class="icon-help-circled"></i></a></dt>
<dd><input type="checkbox" name="show-rawbg" id="show-rawbg" /><label for="show-rawbg">Enable</label></dd>
</dl>
<dl>
<dt>Custom Title</dt>
<dd><input type="text" id="customTitle" value="Nightscout" /></dd>
Expand Down
Loading

0 comments on commit c0be495

Please sign in to comment.