mirror of
https://gitlab.silvrtree.co.uk/martind2000/mdot_server.git
synced 2025-01-30 18:10:13 +00:00
de8af028e5
Before convertion of graph object to use AMCharts..
89 lines
2.4 KiB
JavaScript
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;', [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;', [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;
|
|
};
|
|
|