Skip to content

Commit

Permalink
Merge pull request nightscout#204 from nightscout/wip/mbg-push
Browse files Browse the repository at this point in the history
Send Pushover notification when MBG is received via the api
  • Loading branch information
bewest committed Oct 23, 2014
2 parents 9b5ef3c + d2e76fe commit c0e6056
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
8 changes: 8 additions & 0 deletions bin/post-mbg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
# "date": "1413782506964"

curl -H "Content-Type: application/json" -H "api-secret: $API_SECRET" -XPOST 'http://localhost:1337/api/v1/entries/' -d '{
"mbg": 100,
"type": "mbg",
"date": "1413779270000"
}'
32 changes: 31 additions & 1 deletion lib/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
var es = require('event-stream');
var sgvdata = require('sgvdata');

var TEN_MINS = 10 * 60 * 1000;

/**********\
* Entries
* Encapsulate persistent storage of sgv entries.
\**********/

function entries (name, storage) {
function entries (name, storage, pushover) {

// TODO: Code is a little redundant.

Expand Down Expand Up @@ -106,11 +108,39 @@ function entries (name, storage) {
firstErr = firstErr || err;
totalCreated += created;
});
sendPushover(doc);
});
fn(firstErr, totalCreated, docs);
});
}

//currently the Android upload will send the last MBG over and over, make sure we get a single notification
var lastMBG = 0;

function sendPushover(doc) {
if (doc.type && doc.mbg && doc.type == 'mbg' && doc.date && doc.date != lastMBG && pushover) {
var offset = new Date().getTime() - doc.date;
if (offset > TEN_MINS) {
console.info('No MBG Pushover, offset: ' + offset + ' too big, doc.date: ' + doc.date + ', now: ' + new Date().getTime());
} else {
var msg = {
expire: 14400, // 4 hours
message: '\nMeter BG: ' + doc.mbg,
title: 'Calibration',
sound: 'magic',
timestamp: new Date(doc.date),
priority: 0,
retry: 30
};

pushover.send(msg, function (err, result) {
console.log(result);
});
}
lastMBG = doc.date;
}
}

function getEntry(fn, id) {
console.info("trying to find entry for id: " + id);
with_collection(function(err, collection) {
Expand Down
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var pushover = require('./lib/pushover')(env);
///////////////////////////////////////////////////
// api and json object variables
///////////////////////////////////////////////////
var entries = require('./lib/entries')(env.mongo_collection, store);
var entries = require('./lib/entries')(env.mongo_collection, store, pushover);
var settings = require('./lib/settings')(env.settings_collection, store);
var treatments = require('./lib/treatments')(env.treatments_collection, store, pushover);
var devicestatus = require('./lib/devicestatus')(env.devicestatus_collection, store);
Expand Down

0 comments on commit c0e6056

Please sign in to comment.