'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.roomIds = ['CENSIS-LoRa-1','CENSIS-LoRa-2','CENSIS-LoRa-3','CENSIS-LoRa-4']; module.sqlGetMeetingByRoom = function(roomId,returnObj) { logger.debug('mdot:sqlGetMeetingByRoom', roomId); return new Promise(function(resolve, reject) { db.any('select * from meeting where locationid=$1 order by start asc;', [roomId + 1]) .then(function(d) { returnObj.occupancy = d; return resolve(returnObj); }) .catch((err)=> { logger.error(err); return reject(err); }); }); }; 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 = {}; var room; return new Promise(function(resolve, reject) { logger.debug('mdot.doGet', params); _obj.deviceID = module.deviceIds.indexOf(params.id); room = _obj.deviceID; 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) { self.sqlGetMeetingByRoom(room) .then(function(mrd) { console.log('d:',d); console.log('mrd:',mrd); }) .catch(function(e) { logger.error(e); reject(e); }); resolve(d); }) .catch(function(e) { logger.error(e); reject(e); }); } else { self.sqlAllGetDecoded(_obj.deviceID) .then(function(d) { var obj = {data: d}; return self.sqlGetMeetingByRoom(room,obj); }) .then(function(combined) { logger.debug('combined',combined); resolve(combined); }) .catch(function(e) { logger.error(e); reject(e); }); } }); }; return module; };