Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wip/mqtt/init - tracking mqtt experiments #190

Merged
merged 37 commits into from
Mar 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4f195dd
introduce mqtt
bewest Aug 2, 2014
cc0cd36
lint records first
bewest Aug 2, 2014
8759cb9
Merge remote-tracking branch 'origin/develop' into wip/mqtt/init
bewest Aug 6, 2014
e16328c
Merge branch 'dev' into wip/mqtt/init
bewest Oct 1, 2014
b9e8a5f
make mqtt experiments suitable for hacking
bewest Oct 1, 2014
c8be1de
get mqtt basically working, very rough
bewest Nov 23, 2014
756e9e8
forgot long
bewest Nov 23, 2014
9ad7bf1
test coverage
bewest Nov 25, 2014
33e1a36
Quick hack to get things working
ktind Dec 26, 2014
1f1bc4d
blarg, attempt to debug mqtt
bewest Dec 27, 2014
f70a031
store each record according to own timestamp
bewest Dec 30, 2014
bf2f623
tweak times and protobuf model with @ktind
bewest Dec 31, 2014
70877f7
Adding experimental time calculation algorithm
ktind Dec 31, 2014
de51d22
Merge branch 'wip/mqtt/init' of github.com:ktind/cgm-remote-monitor i…
bewest Dec 31, 2014
650bcd1
First pass at adding MGB, Sensor, Calibration, and Device Status reco…
ktind Dec 31, 2014
b37069e
allow multiple instances of mqtt to multiplex
bewest Dec 31, 2014
e59adfc
Merge remote-tracking branch 'origin/wip/mqtt/init' into wip/mqtt/ini…
ktind Dec 31, 2014
76b9a15
Store the download object in the entries for debugging purposes
ktind Jan 1, 2015
11a7d9d
Updating model and removing dead code
ktind Jan 1, 2015
e4d6675
Merge remote-tracking branch 'origin/dev' into wip/mqtt/init
ktind Jan 9, 2015
6b4041a
fix mbg undefined
bewest Jan 9, 2015
ddc9bf2
only need one stream factory
bewest Jan 9, 2015
de7cb78
force strict zero check
bewest Jan 9, 2015
1b5fc82
Merge branch 'release/0.6.3'
jasoncalabrese Feb 14, 2015
48bdd80
Merge pull request #413 from nightscout/release/0.6.4
jasoncalabrese Feb 14, 2015
d529801
merged master into mqtt/init
jasoncalabrese Feb 16, 2015
abbd231
Merge branch 'dev' into wip/mqtt/init
jasoncalabrese Feb 26, 2015
f21c317
updated with dev
jasoncalabrese Mar 8, 2015
98417ae
Merge branch 'dev' into wip/mqtt/init
jasoncalabrese Mar 15, 2015
a291a0e
Merge pull request #496 from nightscout/wip/context-range-fix
jasoncalabrese Mar 18, 2015
469d868
rebasing, it seems to run
bewest Mar 18, 2015
1a38f4e
fix broken test
bewest Nov 2, 2014
6705b62
bring back ensureIndexes
bewest Nov 4, 2014
82032f4
Merge branch 'wip/mqtt/rebased' into wip/mqtt/init
bewest Mar 18, 2015
0cdd3c0
rm spurious tmp file
bewest Mar 18, 2015
7578c1a
make mqtt listener more unique
bewest Mar 18, 2015
d185010
remove spurious, unused code
bewest Mar 18, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

var express = require('express');
var compression = require('compression');
function create (env, ctx) {
///////////////////////////////////////////////////
// api and json object variables
///////////////////////////////////////////////////
var api = require('./lib/api/')(env, ctx.entries, ctx.settings, ctx.treatments, ctx.profiles, ctx.devicestatus);
var pebble = ctx.pebble;

var app = express();
app.entries = ctx.entries;
app.treatments = ctx.treatments;
app.profiles = ctx.profiles;
app.devicestatus = ctx.devicestatus;
var appInfo = env.name + ' ' + env.version;
app.set('title', appInfo);
app.enable('trust proxy'); // Allows req.secure test on heroku https connections.

app.use(compression({filter: shouldCompress}));

function shouldCompress(req, res) {
//TODO: return false here if we find a condition where we don't want to compress
// fallback to standard filter function
return compression.filter(req, res);
}

//if (env.api_secret) {
// console.log("API_SECRET", env.api_secret);
//}
app.use('/api/v1', api);


// pebble data
app.get('/pebble', pebble(ctx.entries, ctx.treatments, ctx.profiles, ctx.devicestatus));

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

// define static server
//TODO: JC - changed cache to 1 hour from 30d ays to bypass cache hell until we have a real solution
var staticFiles = express.static(env.static_files, {maxAge: 60 * 60 * 1000});

// serve the static content
app.use(staticFiles);

var bundle = require('./bundle')();
app.use(bundle);

// Handle errors with express's errorhandler, to display more readable error messages.

// Handle errors with express's errorhandler, to display more readable error messages.
var errorhandler = require('errorhandler');
//if (process.env.NODE_ENV === 'development') {
app.use(errorhandler());
//}
return app;
}
module.exports = create;

5 changes: 4 additions & 1 deletion env.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ function config ( ) {
}
env.version = software.version;
env.name = software.name;

env.MQTT_MONITOR = process.env.MQTT_MONITOR || null;
env.DISPLAY_UNITS = readENV('DISPLAY_UNITS', 'mg/dl');
env.PORT = readENV('PORT', 1337);
env.mongo = readENV('MONGO_CONNECTION') || readENV('MONGO') || readENV('MONGOLAB_URI');
env.mongo_collection = readENV('MONGO_COLLECTION', 'entries');
if (env.MQTT_MONITOR) {
env.mqtt_client_id = [env.mongo.split('/').pop( ), env.mongo_collection].join('.');
}
env.settings_collection = readENV('MONGO_SETTINGS_COLLECTION', 'settings');
env.treatments_collection = readENV('MONGO_TREATMENTS_COLLECTION', 'treatments');
env.profile_collection = readENV('MONGO_PROFILE_COLLECTION', 'profile');
Expand Down
39 changes: 39 additions & 0 deletions lib/bootevent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

var bootevent = require('bootevent');

function boot (env) {
var store = require('./storage')(env);
var proc = bootevent( )
.acquire(function db (ctx, next) {
// initialize db connections
store( function ready ( ) {
console.log('storage system ready');
ctx.store = store;
next( );
});
})
.acquire(function data (ctx, next) {
///////////////////////////////////////////////////
// api and json object variables
///////////////////////////////////////////////////
ctx.pushover = require('./pushover')(env);
ctx.entries = require('./entries')(env.mongo_collection, ctx.store, ctx.pushover);
ctx.settings = require('./settings')(env.settings_collection, ctx.store);
ctx.treatments = require('./treatments')(env.treatments_collection, ctx.store, ctx.pushover);
ctx.devicestatus = require('./devicestatus')(env.devicestatus_collection, ctx.store);
ctx.profiles = require('./profile')(env.profile_collection, ctx.store);
ctx.pebble = require('./pebble');
console.info("Ensuring indexes");

console.log(ctx.entries, ctx.entries.indexedFields);
store.ensureIndexes(ctx.entries( ), ctx.entries.indexedFields);
store.ensureIndexes(ctx.treatments( ), ctx.treatments.indexedFields);
store.ensureIndexes(ctx.devicestatus( ), ctx.devicestatus.indexedFields);

next( );
})
;
return proc;

}
module.exports = boot;
74 changes: 37 additions & 37 deletions lib/devicestatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,47 @@

function storage (collection, storage) {

function create (obj, fn) {
obj.created_at = (new Date( )).toISOString( );
api( ).insert(obj, function (err, doc) {
fn(null, doc);
});
}

function last(fn) {
return api( ).find({ }).sort({created_at: -1}).limit(1).toArray(function(err, entries) {
if (entries && entries.length > 0)
fn(err, entries[0]);
else
fn(err, null);
});
}

function list (fn) {
return api( ).find({ }).sort({created_at: -1}).toArray(fn);
}

function api ( ) {
return storage.pool.db.collection(collection);
}
function create(obj, fn) {
if (! obj.hasOwnProperty("created_at")){
obj.created_at = (new Date()).toISOString();
}
api().insert(obj, function (err, doc) {
fn(null, doc);
});
}

function create_date_included(obj, fn) {
api().insert(obj, function (err, doc) {
fn(null, doc);
});

}

function last(fn) {
return api().find({}).sort({created_at: -1}).limit(1).toArray(function (err, entries) {
if (entries && entries.length > 0)
fn(err, entries[0]);
else
fn(err, null);
});
}

function list(fn) {
return api().find({}).sort({created_at: -1}).toArray(fn);
}

function api() {
return storage.pool.db.collection(collection);
}


api.list = list;
api.create = create;
api.last = last;
api.indexedFields = indexedFields;
return api;
}
var indexedFields = ['created_at'];
storage.indexedFields = indexedFields;

function ensureIndexes(name, storage) {
storage.with_collection(name)(function (err, collection) {
if (err) {
console.error("ensureIndexes, unable to get collection for: " + name + " - " + err);
} else {
storage.ensureIndexes(collection, ['created_at']);
}
});
}

module.exports = {
storage: storage,
ensureIndexes: ensureIndexes
};
module.exports = storage;
19 changes: 6 additions & 13 deletions lib/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,14 @@ function storage(name, storage, pushover) {
api.persist = persist;
api.getEntries = getEntries;
api.getEntry = getEntry;
api.indexedFields = indexedFields;
return api;
}

function ensureIndexes(name, storage) {
storage.with_collection(name)(function(err, collection) {
if (err) {
console.error("ensureIndexes, unable to get collection for: " + name + " - " + err);
} else {
storage.ensureIndexes(collection, ['date', 'type', 'sgv']);
}
});
}
var indexedFields = [ 'date', 'type', 'sgv' ];
storage.indexedFields = indexedFields;

// expose module
module.exports = {
storage: storage,
ensureIndexes: ensureIndexes
};
storage.storage = storage;
module.exports = storage;

Loading