From efb4aa96e7f682a660ecc3ff2bc0ce8e1809aad0 Mon Sep 17 00:00:00 2001 From: Jason Calabrese Date: Sun, 19 Oct 2014 22:33:15 -0700 Subject: [PATCH 1/3] when a recent MBG is received via the api and pushover is configured send a notification --- lib/entries.js | 28 +++++++++++++++++++++++++++- server.js | 2 +- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/entries.js b/lib/entries.js index 1746bcefa5e..5b4b578e5db 100644 --- a/lib/entries.js +++ b/lib/entries.js @@ -3,12 +3,14 @@ var es = require('event-stream'); var sgvdata = require('sgvdata'); +var TWENTY_MINS = 20 * 60 * 1000; + /**********\ * Entries * Encapsulate persistent storage of sgv entries. \**********/ -function entries (name, storage) { +function entries (name, storage, pushover) { // TODO: Code is a little redundant. @@ -106,11 +108,35 @@ function entries (name, storage) { firstErr = firstErr || err; totalCreated += created; }); + sendPushover(doc); }); fn(firstErr, totalCreated, docs); }); } + function sendPushover(doc) { + if (doc.type && doc.mbg && doc.type == 'mbg' && doc.date && pushover) { + var offset = Math.abs(doc.date - new Date().getTime()); + if (offset > TWENTY_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); + }); + } + } + } + function getEntry(fn, id) { console.info("trying to find entry for id: " + id); with_collection(function(err, collection) { diff --git a/server.js b/server.js index ce2e52e448a..eac35ca3fca 100644 --- a/server.js +++ b/server.js @@ -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); From 9d1fd8a4d5ce175980f1e4cbdc0f6fb4f7129ab6 Mon Sep 17 00:00:00 2001 From: Jason Calabrese Date: Sun, 19 Oct 2014 22:42:44 -0700 Subject: [PATCH 2/3] a simple script to post an MBG --- bin/post-mbg.sh | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100755 bin/post-mbg.sh diff --git a/bin/post-mbg.sh b/bin/post-mbg.sh new file mode 100755 index 00000000000..46906c25592 --- /dev/null +++ b/bin/post-mbg.sh @@ -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" +}' From d4f597995a937cc11f2995044b5aed41f52a50a7 Mon Sep 17 00:00:00 2001 From: Jason Calabrese Date: Mon, 20 Oct 2014 09:13:44 -0700 Subject: [PATCH 3/3] changed offset to 10 minues; store/check lastMBG time to prevent sending dupes --- lib/entries.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/entries.js b/lib/entries.js index 5b4b578e5db..c813c73aa88 100644 --- a/lib/entries.js +++ b/lib/entries.js @@ -3,7 +3,7 @@ var es = require('event-stream'); var sgvdata = require('sgvdata'); -var TWENTY_MINS = 20 * 60 * 1000; +var TEN_MINS = 10 * 60 * 1000; /**********\ * Entries @@ -114,10 +114,13 @@ function entries (name, storage, pushover) { }); } + //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 && pushover) { - var offset = Math.abs(doc.date - new Date().getTime()); - if (offset > TWENTY_MINS) { + 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 = { @@ -134,6 +137,7 @@ function entries (name, storage, pushover) { console.log(result); }); } + lastMBG = doc.date; } }