mqtt_server/lib/server/db-track.js
Martin Donnelly 81f0222ff6 bus system
2016-11-13 22:21:18 +00:00

85 lines
2.3 KiB
JavaScript

'use strict';
var logger = require('log4js').getLogger();
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.sqlInsertTrack = function(data) {
let _data = data;
logger.debug('sqlInsertTrack', _data.locationid, _data.timestamp);
return new Promise(function(resolve, reject) {
db.func('insert_track',
[_data.locationid, _data.timestamp, _data.count, _data.total])
.then(()=> {
return resolve('ok');
})
.catch((err)=> {
return reject(err);
});
});
};
module.sqlGetTracksByID = function(locationId) {
logger.debug('track:sqlGetTracksByID', locationId);
return new Promise(function(resolve, reject) {
db.any('select * from track where locationid=$1;', [locationId])
.then(function(d) {
return resolve(d);
})
.catch((err)=> {
logger.error(err);
return reject(err);
});
});
};
module.addNewTrack = function(data) {
logger.debug('addNewTrack');
var self = this;
return new Promise((resolve, reject) => {
let _data = {};
_data.timestamp = new Date();
_data.locationid = data.locationid;
_data.count = data.count;
_data.total = data.total;
self.sqlInsertTrack(_data)
.then((d)=> {
logger.debug('Postgres returns', d);
return resolve({reply: 'track inserted'});
})
.catch((err)=> {
return reject(err);
});
});
};
module.doGet = function(params) {
var self = this;
return new Promise(function(resolve, reject) {
console.log('track.doGet', params);
self.sqlGetTracksByID(params.locationid)
.then(function(d) {
resolve(d);
})
.catch(function(e) {
logger.error(e);
reject(e);
});
});
};
return module;
};