Skip to content

Commit

Permalink
Merge pull request #1 from nightscout/wip/azurepush
Browse files Browse the repository at this point in the history
Wip/azurepush
  • Loading branch information
srmoss authored Dec 16, 2016
2 parents 550e218 + 218b00a commit 977d1dd
Show file tree
Hide file tree
Showing 56 changed files with 2,444 additions and 251 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ Community maintained fork of the
- [`pump` (Pump Monitoring)](#pump-pump-monitoring)
- [`openaps` (OpenAPS)](#openaps-openaps)
- [`loop` (Loop)](#loop-loop)
- [`alexa` (Amazon Alexa)](#alexa-amazon-alexa)
- [`cors` (CORS)](#cors-cors)
- [Extended Settings](#extended-settings)
- [Pushover](#pushover)
- [IFTTT Maker](#ifttt-maker)
Expand Down Expand Up @@ -151,6 +153,7 @@ You can get many more results, by using the `count`, `date`, `dateString`, and `
(replace `http://localhost:1337` with your base url, YOUR-SITE)

* 100's: `http://localhost:1337/api/v1/entries.json?find[sgv]=100`
* Count of 100's in a month: `http://localhost:1337/api/v1/count/entries/where?find[dateString][$gte]=2016-09&find[dateString][$lte]=2016-10&find[sgv]=100`
* BGs between 2 days: `http://localhost:1337/api/v1/entries/sgv.json?find[dateString][$gte]=2015-08-28&find[dateString][$lte]=2015-08-30`
* Juice Box corrections in a year: `http://localhost:1337/api/v1/treatments.json?count=1000&find[carbs]=15&find[eventType]=Carb+Correction&find[created_at][$gte]=2015`
* Boluses over 2U: `http://localhost:1337/api/v1/treatments.json?find[insulin][$gte]=2`
Expand All @@ -177,6 +180,7 @@ To learn more about the Nightscout API, visit https://YOUR-SITE.com/api-docs.htm
* `AUTH_DEFAULT_ROLES` (`readable`) - possible values `readable`, `denied`, or any valid role
name. When `readable`, anyone can view Nightscout without a token.
Setting it to `denied` will require a token from every visit, using `status-only` will enable api-secret based login.
* `IMPORT_CONFIG` - Used to import settings and extended settings from a url such as a gist. Structure of file should be something like: `{"settings": {"theme": "colors"}, "extendedSettings": {"upbat": {"enableAlerts": true}}}`
* `TREATMENTS_AUTH` (`on`) - possible values `on` or `off`. Deprecated, if set to `off` the `careportal` role will be added to `AUTH_DEFAULT_ROLES`


Expand Down Expand Up @@ -400,6 +404,12 @@ To learn more about the Nightscout API, visit https://YOUR-SITE.com/api-docs.htm
* `LOOP_URGENT` (`60`) - The number of minutes since the last loop that needs to be exceeded before an urgent alarm is triggered
* Add `loop` to `SHOW_FORECAST` to show forecasted BG.

##### `alexa` (Amazon Alexa)
Integration with Amazon Alexa, [detailed setup instructions](lib/plugins/alexa-plugin.md)

##### `cors` (CORS)
Enabled [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) so other websites can make request to your Nightscout site, uses these extended settings:
* `CORS_ALLOW_ORIGIN` (`*`) - The list of sites that are allow to make requests

#### Extended Settings
Some plugins support additional configuration using extra environment variables. These are prefixed with the name of the plugin and a `_`. For example setting `MYPLUGIN_EXAMPLE_VALUE=1234` would make `extendedSettings.exampleValue` available to the `MYPLUGIN` plugin.
Expand Down
20 changes: 20 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var _ = require('lodash');
var express = require('express');
var compression = require('compression');
var bodyParser = require('body-parser');
Expand All @@ -15,10 +16,28 @@ function create (env, ctx) {
return app;
}

if (env.settings.isEnabled('cors')) {
var allowOrigin = _.get(env, 'extendedSettings.cors.allowOrigin') || '*';
console.info('Enabled CORS, allow-origin:', allowOrigin);
app.use(function allowCrossDomain (req, res, next) {
res.header('Access-Control-Allow-Origin', allowOrigin);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');

// intercept OPTIONS method
if ('OPTIONS' === req.method) {
res.send(200);
} else {
next();
}
});
}

///////////////////////////////////////////////////
// api and json object variables
///////////////////////////////////////////////////
var api = require('./lib/api/')(env, ctx);
var ddata = require('./lib/data/endpoints')(env, ctx);

app.use(compression({filter: function shouldCompress(req, res) {
//TODO: return false here if we find a condition where we don't want to compress
Expand All @@ -34,6 +53,7 @@ function create (env, ctx) {

app.use('/api/v2/properties', ctx.properties);
app.use('/api/v2/authorization', ctx.authorization.endpoints);
app.use('/api/v2/ddata', ddata);

// pebble data
app.get('/pebble', ctx.pebble);
Expand Down
10 changes: 9 additions & 1 deletion azuredeploy.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
"mongoConnection": {
"type": "string"
},
"mongoCollection": {
"type": "string",
"defaultValue": "entries"
},
"displayUnits": {
"type": "string",
"allowedValues": [
Expand Down Expand Up @@ -236,7 +240,11 @@
"appSettings": [{
"name": "MONGO_CONNECTION",
"value": "[parameters('mongoConnection')]"
}, {
},
{
"name": "MONGO_COLLECTION",
"value": "[parameters('mongoCollection')]"
}, {
"name": "DISPLAY_UNITS",
"value": "[parameters('displayUnits')]"
}, {
Expand Down
5 changes: 3 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "nightscout",
"version": "0.9.1-dev-20161023",
"version": "0.9.3-dev-20161212b",
"dependencies": {
"colorbrewer": "~1.0.0",
"jQuery-Storage-API": "~1.7.2",
"jquery": "2.1.0",
"jquery-flot": "0.8.3",
"jquery-ui": "~1.11.3",
"swagger-ui": "~2.1.2",
"tipsy-jmalonzo": "~1.0.1"
"tipsy-jmalonzo": "~1.0.1",
"tone": "*"
},
"resolutions": {
"jquery": "2.1.0"
Expand Down
1 change: 1 addition & 0 deletions env.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function config ( ) {
env.DISPLAY_UNITS = readENV('DISPLAY_UNITS', 'mg/dl');
env.PORT = readENV('PORT', 1337);
env.HOSTNAME = readENV('HOSTNAME', null);
env.IMPORT_CONFIG = readENV('IMPORT_CONFIG', null);
env.static_files = readENV('NIGHTSCOUT_STATIC_FILES', __dirname + '/static/');

if (env.err) {
Expand Down
32 changes: 32 additions & 0 deletions lib/aggregate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var find_options = require('./query');

function create (conf, api) {

var template = function ( ) {
return [
{
$group: {
_id: null
, count: { $sum: 1 }
}
}
];
};

// var collection = api( );
function aggregate (opts, done) {
var query = find_options(opts);

var pipeline = (conf.pipeline || [ ]).concat(opts.pipeline || [ ]);
var groupBy = [ {$match: query } ].concat(pipeline).concat(template( ));
console.log('$match query', query);
console.log('AGGREGATE', groupBy);
api( ).aggregate(groupBy, done);
}

return aggregate;

}

module.exports = create;

160 changes: 160 additions & 0 deletions lib/api/alexa/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
'use strict';

var moment = require('moment');
var _ = require('lodash');


function configure (app, wares, ctx, env) {
var entries = ctx.entries;
var express = require('express')
, api = express.Router( )
;

// invoke common middleware
api.use(wares.sendJSONStatus);
// text body types get handled as raw buffer stream
api.use(wares.bodyParser.raw());
// json body types get handled as parsed json
api.use(wares.bodyParser.json());

ctx.plugins.eachEnabledPlugin(function each(plugin){
if (plugin.alexa) {
if (plugin.alexa.intentHandlers) {
console.log(plugin.name + ' is Alexa enabled');
_.each(plugin.alexa.intentHandlers, function (route) {
if (route) {
ctx.alexa.configureIntentHandler(route.intent, route.intentHandler, route.routableSlot, route.slots);
}
});
}
if (plugin.alexa.rollupHandlers) {
console.log(plugin.name + ' is Alexa rollup enabled');
_.each(plugin.alexa.rollupHandlers, function (route) {
console.log('Route');
console.log(route);
if (route) {
ctx.alexa.addToRollup(route.rollupGroup, route.rollupHandler, route.rollupName);
}
});
}
} else {
console.log('Plugin ' + plugin.name + ' is not Alexa enabled');
}
});

api.post('/alexa', ctx.authorization.isPermitted('api:*:read'), function (req, res, next) {
console.log('Incoming request from Alexa');
switch (req.body.request.type) {
case 'IntentRequest':
onIntent(req.body.request.intent, function (title, response) {
res.json(ctx.alexa.buildSpeechletResponse(title, response, '', 'true'));
next( );
});
break;
case 'LaunchRequest':
onLaunch(req.body.request.intent, function (title, response) {
res.json(ctx.alexa.buildSpeechletResponse(title, response, '', 'true'));
next( );
});
break;
case 'SessionEndedRequest':
onSessionEnded(req.body.request.intent, function (alexaResponse) {
res.json(alexaResponse);
next( );
});
break;
}
});

ctx.alexa.addToRollup('Status', function bgRollupHandler(slots, sbx, callback) {
entries.list({count: 1}, function(err, records) {
var direction = '';
if (records[0].direction === 'FortyFiveDown') {
direction = ' and slightly dropping';
} else if (records[0].direction === 'FortyFiveUp') {
direction = ' and slightly rising';
} else if (records[0].direction === 'Flat') {
direction = ' and holding';
} else if (records[0].direction === 'SingleUp') {
direction = ' and rising';
} else if (records[0].direction === 'SingleDown') {
direction = ' and dropping';
} else if (records[0].direction === 'DoubleDown') {
direction = ' and rapidly dropping';
} else if (records[0].direction === 'DoubleUp') {
direction = ' and rapidly rising';
} else {
direction = records[0].direction;
}
var status = sbx.scaleMgdl(records[0].sgv) + direction + ' as of ' + moment(records[0].date).from(moment(sbx.time)) + '.';
callback(null, {results: status, priority: -1});
});
// console.log('BG results called');
// callback(null, 'BG results');
}, 'BG Status');

ctx.alexa.configureIntentHandler('MetricNow', function (callback, slots, sbx) {
entries.list({count: 1}, function(err, records) {
var direction = '';
if (records[0].direction === 'FortyFiveDown') {
direction = ' and slightly dropping';
} else if (records[0].direction === 'FortyFiveUp') {
direction = ' and slightly rising';
} else if (records[0].direction === 'Flat') {
direction = ' and holding';
} else if (records[0].direction === 'SingleUp') {
direction = ' and rising';
} else if (records[0].direction === 'SingleDown') {
direction = ' and dropping';
} else if (records[0].direction === 'DoubleDown') {
direction = ' and rapidly dropping';
} else if (records[0].direction === 'DoubleUp') {
direction = ' and rapidly rising';
}
var status = sbx.scaleMgdl(records[0].sgv) + direction + ' as of ' + moment(records[0].date).from(moment(sbx.time));
callback('Current blood glucose', status);
});
}, 'metric', ['bg', 'blood glucose', 'number']);

ctx.alexa.configureIntentHandler('NSStatus', function(callback, slots, sbx) {
ctx.alexa.getRollup('Status', sbx, slots, function (status) {
callback('Full status', status);
});
});


function onLaunch() {
console.log('Session launched');
}

function onIntent(intent, next) {
console.log('Received intent request');
console.log(JSON.stringify(intent));
handleIntent(intent.name, intent.slots, next);
}

function onSessionEnded() {
console.log('Session ended');
}

function handleIntent(intentName, slots, next) {
var handler = ctx.alexa.getIntentHandler(intentName, slots);
if (handler){
var sbx = initializeSandbox();
handler(next, slots, sbx);
} else {
next('Unknown Intent', 'I\'m sorry I don\'t know what you\'re asking for');
}
}

function initializeSandbox() {
var sbx = require('../../sandbox')();
sbx.serverInit(env, ctx);
ctx.plugins.setProperties(sbx);
return sbx;
}

return api;
}

module.exports = configure;
21 changes: 20 additions & 1 deletion lib/api/entries/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var _ = require('lodash');
var consts = require('../../constants');
var es = require('event-stream');
var sgvdata = require('sgvdata');
Expand Down Expand Up @@ -341,6 +342,22 @@ function configure (app, wares, ctx) {
});
}

function count_records (req, res, next) {
var query = req.query;
var storage = req.storage || ctx.entries;
storage.aggregate(query, function payload (err, entries) {
// assign payload
res.entries = entries;
res.entries_err = err;
return next(err);
});
}

function format_results (req, res, next) {
res.json(res.entries);
next( );
}

/**
* @function delete_records
* Delete entries. The query logic works the same way as find/list. This
Expand Down Expand Up @@ -523,7 +540,7 @@ curl -s -g 'http://localhost:1337/api/v1/times/20{14..15}/T{13..18}:{00..15}'.js
* by default.
*/
function prep_storage (req, res, next) {
if (req.params.storage) {
if (req.params.storage && _.includes(['entries', 'treatments', 'devicestatus'], req.params.storage)) {
req.storage = ctx[req.params.storage];
} else {
req.storage = ctx.entries;
Expand Down Expand Up @@ -555,6 +572,8 @@ curl -s -g 'http://localhost:1337/api/v1/times/20{14..15}/T{13..18}:{00..15}'.js
*/
api.get('/times/:prefix?/:regex?', prep_storage, prep_pattern_field, prep_patterns, prep_patterns, query_models, format_entries);

api.get('/count/:storage/where', prep_storage, count_records, format_results);

/**
* @module get#/slice/:storage/:field/:type/:prefix/:regex
* @routed
Expand Down
5 changes: 5 additions & 0 deletions lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function create (env, ctx) {
app.all('/echo/*', entriesRouter);
app.all('/times/*', entriesRouter);
app.all('/slice/*', entriesRouter);
app.all('/count/*', entriesRouter);

app.all('/treatments*', require('./treatments/')(app, wares, ctx));
app.all('/profile*', require('./profile/')(app, wares, ctx));
Expand All @@ -62,6 +63,10 @@ function create (env, ctx) {
// Status first
app.all('/status*', require('./status')(app, wares, env, ctx));

if (ctx.alexa) {
app.all('/alexa*', require('./alexa/')(app, wares, ctx, env));
}

return app;
}

Expand Down
Loading

0 comments on commit 977d1dd

Please sign in to comment.