Wip/mqtt/init - tracking mqtt experiments #190

Merged
merged 37 commits into from Mar 19, 2015
Commits
+389 −141
Split
View
59 app.js
@@ -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;
+
View
5 env.js
@@ -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');
View
@@ -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;
View
@@ -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;
View
@@ -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;
+
Oops, something went wrong.