From 612bbf30e66782f5e95f13166e7b28405d76d202 Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Mon, 22 Aug 2016 14:56:42 +0100 Subject: [PATCH] added meeting api --- filler.js | 83 +++++++++++++++++++++++++++++++++++++++++++ lib/server/db-save.js | 8 +++-- package.json | 1 + 3 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 filler.js diff --git a/filler.js b/filler.js new file mode 100644 index 0000000..e5d4ca6 --- /dev/null +++ b/filler.js @@ -0,0 +1,83 @@ +var log4js = require('log4js'); +var logger = log4js.getLogger(); + + +var db = require('./lib/server/db-connector').dbConnection; +var dbSave = require('./lib/server/db-save')(db); + +var Sugar = require('sugar-date'); + +var mdot = require('./lib/mdot/mdot.js'); + + + +( + function() { + + var doInsertEntry = (obj) => { + // Logger.info('sendSocket: ' + JSON.stringify(obj)); + + // insertEntry(obj); +/* + + dbSave.addNewEvent(obj) + .then(function(d) { + 'use strict'; + logger.info('Finished - Raw',d); + }) + .catch(function(e) { + 'use strict'; + logger.error(e); + }); +*/ + + + dbSave.addProcessedEvent(obj) + .then(function(d) { + 'use strict'; + logger.info('Finished - Processed',d); + }) + .catch(function(e) { + 'use strict'; + logger.error(e); + }); + }; + + + var device = 3; + var ids = ['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']; + var data = {id: ids[device]}; + + var startTs = Sugar.Date.create('2016-08-19 14:46:48'); + var endTs = Sugar.Date.create('2016-08-21 10:03:39'); + logger.debug(startTs, endTs); + logger.debug(startTs.getTime(), endTs.getTime()); + + data.start = startTs.getTime(); + data.end = endTs.getTime(); + + + mdot.doGet(data) + .then(function(d) { + 'use strict'; + //Logger.debug(d); + for (var item in d.events) { + var newItem = d.events[item].evt; + newItem.topic=['','','','',ids[device]].join('/'); + logger.debug(newItem); + var gwTime = new Date(newItem.gateway_info['0'].gw_time); + if ((gwTime > startTs) && (gwTime < endTs)) { + doInsertEntry(newItem); + } else { + logger.warn('Out of range...'); + } + + } + + }) + .catch(function(err) { + 'use strict'; + logger.error(err); + }); + } +)(); diff --git a/lib/server/db-save.js b/lib/server/db-save.js index c128bc2..5b800f7 100644 --- a/lib/server/db-save.js +++ b/lib/server/db-save.js @@ -26,6 +26,8 @@ module.exports = function(db) { let _data = data; console.log('sqlInsertDecoded', _data.deviceid, _data.timestamp); + console.log('insert_decoded', + [_data.deviceid, _data.timestamp, _data.lux, _data.co2, _data.temp, _data.humidity, _data.sound]); return new Promise(function(resolve, reject) { db.func('insert_decoded', [_data.deviceid, _data.timestamp, _data.lux, _data.co2, _data.temp, _data.humidity, _data.sound]) @@ -66,6 +68,8 @@ module.exports = function(db) { let _data = self.rawBreaker(data); + console.log(_data); + self.sqlInsertDecoded(_data) .then((d)=> { console.log('Postgres returns', d); @@ -100,9 +104,7 @@ module.exports = function(db) { var workObj = {}; var device_name = data.topic.split('/')[4]; - console.log('Device_name', device_name); workObj.deviceid = self.deviceIds.indexOf(device_name); - if (data.hasOwnProperty('data')) { var _data = self.decoder(data.data); @@ -112,7 +114,7 @@ module.exports = function(db) { workObj.temp = _data.temp; workObj.humidity = _data.humid; workObj.sound = _data.noise; - workObj.timestamp = new Date(); + workObj.timestamp = new Date(data.gateway_info['0'].gw_time); return workObj; } else { diff --git a/package.json b/package.json index 147453b..cd1d31b 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "test": "test" }, "dependencies": { + "atob": "^2.0.3", "body-parser": "^1.15.1", "btoa": "^1.1.2", "cookie-parser": "*",