mdot_server/lib/server/db-mdot.js
Martin Donnelly e32d4dcd54 new graph
2016-08-15 12:16:11 +01:00

89 lines
2.4 KiB
JavaScript

'use strict';
var logger = require('log4js').getLogger();
var Sugar = require('sugar-date');
module.exports = function(db) {
var module = {};
module.deviceIds = ['CENSIS-LoRa-1','CENSIS-LoRa-2','CENSIS-LoRa-3','CENSIS-LoRa-4','HIE-mobile-1','HIE-demo','HIE-mobile-2','HIE-smart-campus-1','HIE-smart-campus-2','HIE-smart-campus-3','HIE-smart-campus-4','HIE-smart-campus-5','HIE-smart-campus-6','HIE-smart-campus-7','HIE-mDot-1'];
module.sqlAllGetDecoded = function(deviceId) {
logger.debug('mdot:sqlAllGetDecoded');
return new Promise(function(resolve, reject) {
db.any('select * from decoded where deviceid=$1 order by timestamp asc;', [deviceId])
.then(function(d) {
return resolve(d);
})
.catch((err)=> {
logger.error(err);
return reject(err);
});
});
};
module.sqlRangedGetDecoded = function(params) {
logger.debug('mdot:sqlRangedGetDecoded');
return new Promise(function(resolve, reject) {
db.any('select * from decoded where deviceid=$1 and timestamp between $2 and $3 order by timestamp asc;', [params.deviceID, params.startTS, params.endTS])
.then(function(d) {
return resolve(d);
})
.catch((err)=> {
logger.error(err);
return reject(err);
});
});
};
module.doGet = function(params) {
var self = this;
var _obj = {};
return new Promise(function(resolve, reject) {
logger.debug('mdot.doGet', params);
_obj.deviceID = module.deviceIds.indexOf(params.id);
if (params.hasOwnProperty('start') && params.hasOwnProperty('start')) {
logger.debug('params.start',params.start);
logger.debug('params.start',Sugar.Date.create(params.start ));
_obj.startTS = Sugar.Date.create(params.start);
_obj.endTS = Sugar.Date.create(params.end);
logger.info(_obj);
self.sqlRangedGetDecoded(_obj)
.then(function(d) {
resolve(d);
})
.catch(function(e) {
logger.error(e);
reject(e);
});
} else {
self.sqlAllGetDecoded(_obj.deviceID)
.then(function(d) {
resolve(d);
})
.catch(function(e) {
logger.error(e);
reject(e);
});
}
});
};
return module;
};