bus system
This commit is contained in:
parent
da91c44814
commit
81f0222ff6
4
index.js
4
index.js
@ -10,7 +10,7 @@ var busEmitter = new EventEmitter();
|
||||
|
||||
|
||||
var mqttClient = require('./lib/mqtt/mqttClient');
|
||||
var mqttConnect = require('./lib/mqtt/mqttConnect');
|
||||
//var mqttConnect = require('./lib/mqtt/mqttConnect');
|
||||
|
||||
var mqtt = new mqttClient.mqttClient();
|
||||
|
||||
@ -23,8 +23,10 @@ mqttConnect.doConnection();
|
||||
mqttConnect.connectWS();
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
setTimeout(function(){
|
||||
'use strict';
|
||||
mqtt.lightsIn();
|
||||
},15000);
|
||||
*/
|
||||
|
@ -5,6 +5,8 @@ var logger = require('log4js').getLogger();
|
||||
|
||||
var EventEmitter = require('events');
|
||||
|
||||
// var db = require('../server/db-connector').dbConnection;
|
||||
|
||||
//var nano = require('nano')('http://martind2000:1V3D4m526i@localhost:5984');
|
||||
//var nano = require('nano')('http://localhost:5984');
|
||||
var busEmitter = new EventEmitter();
|
||||
@ -12,132 +14,136 @@ var busEmitter = new EventEmitter();
|
||||
var db_name = 'mqtt';
|
||||
//var dbCouch = nano.use(db_name);
|
||||
|
||||
var bus = Minibus.create();
|
||||
|
||||
bus.on('send', function (mode) {
|
||||
var url = ['https://maker.ifttt.com/trigger/', mode, '/with/key/cWvECkeiyAPwmiOPBkXL2D'].join('');
|
||||
|
||||
request(url, function (error, response, body) {
|
||||
if (!error && response.statusCode === 200) {
|
||||
console.log(body);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
var mqttClient = function () {
|
||||
|
||||
var orgId = '';
|
||||
var userName = '';
|
||||
var appKey = 'bob';
|
||||
|
||||
this.livingRoom = {
|
||||
temp: 0,
|
||||
last: 0
|
||||
};
|
||||
var mode = 'FanOff';
|
||||
|
||||
|
||||
var d = new Date();
|
||||
this.lastMsg = d.getTime();
|
||||
|
||||
var mqttClient = function() {
|
||||
|
||||
var orgId = '';
|
||||
var userName = '';
|
||||
var appKey = 'bob';
|
||||
this.livingRoom = {
|
||||
temp: 0,
|
||||
last: 0
|
||||
};
|
||||
var mode = 'FanOff';
|
||||
|
||||
var d = new Date();
|
||||
this.lastMsg = d.getTime();
|
||||
|
||||
|
||||
this.fanTimer = function() {
|
||||
'use strict';
|
||||
var now = new Date;
|
||||
var mod = 900000 - (now.getTime() % 900000);
|
||||
this.fanTimer = function () {
|
||||
'use strict';
|
||||
var now = new Date;
|
||||
var mod = 900000 - (now.getTime() % 900000);
|
||||
|
||||
// var
|
||||
|
||||
if (mode === 'FanOff') {
|
||||
console.log('Fans off, temp should be <= 19.5');
|
||||
mode = (parseFloat(this.livingRoom.temp) <= 19.5) ? 'FanOn' : 'FanOff';
|
||||
} else {
|
||||
console.log('Fans on, temp should not be less than 21.5');
|
||||
mode = (parseFloat(this.livingRoom.temp) <= 21.5) ? 'FanOn' : 'FanOff';
|
||||
}
|
||||
if (mode === 'FanOff') {
|
||||
console.log('Fans off, temp should be <= 19.5');
|
||||
mode = (parseFloat(this.livingRoom.temp) <= 19.5) ? 'FanOn' : 'FanOff';
|
||||
} else {
|
||||
console.log('Fans on, temp should not be less than 22.5');
|
||||
mode = (parseFloat(this.livingRoom.temp) <= 22.5) ? 'FanOn' : 'FanOff';
|
||||
}
|
||||
|
||||
var n = new Date();
|
||||
n = n.getTime() - this.lastMsg;
|
||||
console.log('Last msg',n);
|
||||
var n = new Date();
|
||||
n = n.getTime() - this.lastMsg;
|
||||
console.log('Last msg', n);
|
||||
|
||||
if (n >= 600000) {
|
||||
console.error('No message received for over 10 minutes');
|
||||
mode = 'FanOff';
|
||||
}
|
||||
if (n >= 600000) {
|
||||
console.error('No message received for over 10 minutes');
|
||||
mode = 'FanOff';
|
||||
}
|
||||
|
||||
var url = ['https://maker.ifttt.com/trigger/',mode,'/with/key/cWvECkeiyAPwmiOPBkXL2D'].join('');
|
||||
logger.info('LR temp:', this.livingRoom.temp);
|
||||
|
||||
bus.emit('send', mode);
|
||||
|
||||
logger.info('LR temp:', this.livingRoom.temp);
|
||||
|
||||
request(url, function(error, response, body) {
|
||||
if (!error && response.statusCode === 200) {
|
||||
console.log(body);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
setTimeout(this.fanTimer.bind(this),mod + 10);
|
||||
};
|
||||
|
||||
this.lighting = 'lights';
|
||||
|
||||
this.connected = false;
|
||||
|
||||
var options = {
|
||||
keepalive: 3600,
|
||||
clientId: 'a:' + orgId + ':' + Date.now(),
|
||||
username: userName,
|
||||
password: new Buffer(appKey)
|
||||
|
||||
};
|
||||
|
||||
this.client = mqtt.connect('mqtt://' + orgId + 'silvrtree.co.uk', options);
|
||||
|
||||
this.client.on('connect', function() {
|
||||
connected = true;
|
||||
logger.info('Connected to SIlvr Broker');
|
||||
this.fanTimer();
|
||||
}.bind(this));
|
||||
|
||||
this.client.subscribe('livingroomTemp');
|
||||
this.client.on('message', function(topic, message) {
|
||||
|
||||
var json = JSON.parse(message.toString());
|
||||
logger.debug(json);
|
||||
logger.debug(json.temp);
|
||||
|
||||
this.livingRoom.temp = parseFloat(json.temp);
|
||||
var d = new Date();
|
||||
this.lastMsg = d.getTime();
|
||||
|
||||
}.bind(this));
|
||||
|
||||
this.isConnected = function() {
|
||||
return this.connected;
|
||||
};
|
||||
setTimeout(this.fanTimer.bind(this), mod + 10);
|
||||
};
|
||||
|
||||
this.lighting = 'lights';
|
||||
|
||||
this.connected = false;
|
||||
|
||||
var options = {
|
||||
keepalive: 3600,
|
||||
clientId: 'a:' + orgId + ':' + Date.now(),
|
||||
username: userName,
|
||||
password: new Buffer(appKey)
|
||||
|
||||
};
|
||||
|
||||
/*
|
||||
Client = mqtt.connect();
|
||||
this.client = mqtt.connect('mqtt://' + orgId + 'silvrtree.co.uk', options);
|
||||
|
||||
client.subscribe('presence');
|
||||
client.on('message', function(topic, message) {
|
||||
console.log(message);
|
||||
});
|
||||
this.client.on('connect', function () {
|
||||
connected = true;
|
||||
logger.info('Connected to SIlvr Broker');
|
||||
this.fanTimer();
|
||||
}.bind(this));
|
||||
|
||||
this.client.subscribe('livingroomTemp');
|
||||
|
||||
this.client.on('message', function (topic, message) {
|
||||
|
||||
var json = JSON.parse(message.toString());
|
||||
logger.debug(json);
|
||||
logger.debug(json.temp);
|
||||
|
||||
this.livingRoom.temp = parseFloat(json.temp);
|
||||
|
||||
if (this.livingRoom.temp >= 23) {
|
||||
bus.emit('send', 'FanOff');
|
||||
}
|
||||
var d = new Date();
|
||||
this.lastMsg = d.getTime();
|
||||
|
||||
}.bind(this));
|
||||
|
||||
this.isConnected = function () {
|
||||
return this.connected;
|
||||
};
|
||||
|
||||
|
||||
};
|
||||
|
||||
/*
|
||||
Client = mqtt.connect();
|
||||
|
||||
client.subscribe('presence');
|
||||
client.on('message', function(topic, message) {
|
||||
console.log(message);
|
||||
});
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
mqttClient.prototype.lightsIn = function () {
|
||||
|
||||
mqttClient.prototype.lightsIn = function() {
|
||||
var destinationName = 'lightsIn';
|
||||
this.client.publish(destinationName, '0');
|
||||
var destinationName = 'lightsIn';
|
||||
this.client.publish(destinationName, '1');
|
||||
};
|
||||
|
||||
var destinationName = 'lightsIn';
|
||||
this.client.publish(destinationName, '0');
|
||||
var destinationName = 'lightsIn';
|
||||
this.client.publish(destinationName, '1');
|
||||
};
|
||||
mqttClient.prototype.lightsOut = function () {
|
||||
|
||||
mqttClient.prototype.lightsOut = function() {
|
||||
|
||||
var destinationName = 'lightsOut';
|
||||
this.client.publish(destinationName, 'f');
|
||||
destinationName = 'lightsOut';
|
||||
this.client.publish(destinationName, 'g');
|
||||
};
|
||||
var destinationName = 'lightsOut';
|
||||
this.client.publish(destinationName, 'f');
|
||||
destinationName = 'lightsOut';
|
||||
this.client.publish(destinationName, 'g');
|
||||
};
|
||||
|
||||
module.exports.mqttClient = mqttClient;
|
||||
|
24
lib/server/db-connector.js
Normal file
24
lib/server/db-connector.js
Normal file
@ -0,0 +1,24 @@
|
||||
'uses strict';
|
||||
/**
|
||||
*
|
||||
* User: Martin Donnelly
|
||||
* Date: 2016-03-11
|
||||
* Time: 10:22
|
||||
*
|
||||
*/
|
||||
|
||||
var pgp = require('pg-promise')();
|
||||
|
||||
var amazonCN = {
|
||||
dbinstance:'silvrdb',
|
||||
host: 'silvrdb.cipyprijvzsw.eu-west-1.rds.amazonaws.com',
|
||||
port: 5432,
|
||||
database: 'heat',
|
||||
user: 'martind2000',
|
||||
password: '1V3D4m526i'
|
||||
};
|
||||
|
||||
const cn = amazonCN;
|
||||
|
||||
|
||||
exports.dbConnection = pgp(cn);
|
125
lib/server/db-mdot.js
Normal file
125
lib/server/db-mdot.js
Normal file
@ -0,0 +1,125 @@
|
||||
'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 distinct * 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 distinct * 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('end')) {
|
||||
|
||||
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() {
|
||||
// nothing done
|
||||
})
|
||||
.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) {
|
||||
resolve(combined);
|
||||
})
|
||||
.catch(function(e) {
|
||||
logger.error(e);
|
||||
reject(e);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
return module;
|
||||
};
|
||||
|
53
lib/server/db-meeting.js
Normal file
53
lib/server/db-meeting.js
Normal file
@ -0,0 +1,53 @@
|
||||
'use strict';
|
||||
var logger = require('log4js').getLogger();
|
||||
|
||||
module.exports = function(db) {
|
||||
var module = {};
|
||||
|
||||
module.sqlInsertMeeting = function(data) {
|
||||
let _data = data;
|
||||
|
||||
logger.debug('sqlInsertMeeting', _data.locationid, _data.timestamp);
|
||||
return new Promise(function(resolve, reject) {
|
||||
db.none('INSERT into meeting(locationid, logged, count, start, "end") Values( ${locationid}, ${timestamp}, ${count}, ${start}, ${end})', _data)
|
||||
.then(()=> {
|
||||
return resolve('ok');
|
||||
})
|
||||
.catch((err)=> {
|
||||
return reject(err);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
|
||||
module.addNewMeeting = function(data) {
|
||||
logger.debug('addNewMeeting');
|
||||
var self = this;
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
let _data = {};
|
||||
_data.timestamp = new Date();
|
||||
_data.locationid = data.locationid;
|
||||
_data.count = data.count;
|
||||
_data.start = new Date(data.start);
|
||||
_data.end = new Date(data.end);
|
||||
|
||||
console.log('_data', _data);
|
||||
|
||||
self.sqlInsertMeeting(_data)
|
||||
.then((d)=> {
|
||||
logger.debug('Postgres returns', d);
|
||||
return resolve({reply: 'track inserted'});
|
||||
})
|
||||
.catch((err)=> {
|
||||
return reject(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
return module;
|
||||
};
|
||||
|
154
lib/server/db-save.js
Normal file
154
lib/server/db-save.js
Normal file
@ -0,0 +1,154 @@
|
||||
'use strict';
|
||||
|
||||
var atob = require('atob');
|
||||
|
||||
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.doBulkInsert = function(bulkData) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
db.tx(function(t) {
|
||||
// t = this;
|
||||
// creating a sequence of transaction queries:
|
||||
|
||||
// returning a promise that determines a successful transaction:
|
||||
return this.batch(bulkData); // all of the queries are to be resolved;
|
||||
})
|
||||
.then(function(data) {
|
||||
return resolve(data);
|
||||
})
|
||||
.catch(function(error) {
|
||||
return reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
module.sqlInsertRawEvent = function(data) {
|
||||
let _data = data;
|
||||
console.log('sqlInsertRawEvent');
|
||||
return new Promise(function(resolve, reject) {
|
||||
db.func('insert_raw',
|
||||
[_data.timestamp, _data.event])
|
||||
.then(()=> {
|
||||
return resolve('ok');
|
||||
})
|
||||
.catch((err)=> {
|
||||
console.error(err);
|
||||
return reject(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.sqlInsertDecoded = function(data) {
|
||||
let _data = data;
|
||||
return new Promise(function(resolve, reject) {
|
||||
db.func('insert_decoded',
|
||||
[_data.deviceid, _data.timestamp, _data.lux, _data.co2, _data.temp, _data.humidity, _data.sound])
|
||||
.then(()=> {
|
||||
return resolve('ok');
|
||||
})
|
||||
.catch((err)=> {
|
||||
console.error(err);
|
||||
return reject(err);
|
||||
});
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
module.genRawQuery = function(data) {
|
||||
const timestamp = new Date(data.timestamp['$date']);
|
||||
return db.func('insert_raw', [timestamp, data.device_type, data.device_id, data.evt_type, data.evt]);
|
||||
};
|
||||
|
||||
module.addNewEvent = function(data) {
|
||||
console.log('addNewEvent');
|
||||
var self = this;
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
let _data = {};
|
||||
_data.timestamp = new Date();
|
||||
_data.event = data;
|
||||
|
||||
self.sqlInsertRawEvent(_data)
|
||||
.then((d)=> {
|
||||
console.log('Postgres returns', d);
|
||||
return resolve({reply: 'raw event inserted'});
|
||||
})
|
||||
.catch((err)=> {
|
||||
console.error(err);
|
||||
return reject(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.addProcessedEvent = function(data) {
|
||||
// console.log('addProcessedEvent');
|
||||
var self = this;
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
let _data = self.rawBreaker(data);
|
||||
|
||||
self.sqlInsertDecoded(_data)
|
||||
.then((d)=> {
|
||||
// console.log('Postgres returns', d);
|
||||
return resolve({reply: 'Processed event inserted',data:_data});
|
||||
})
|
||||
.catch((err)=> {
|
||||
console.error(err);
|
||||
return reject(err);
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
module.decoder = function(data) {
|
||||
var _obj = {};
|
||||
var _data = atob(data).split('');
|
||||
|
||||
var bytes = _data.map(i => i.charCodeAt());
|
||||
|
||||
_obj.light = parseInt('0x' + ('0' + bytes[0]).substr(-2) + ('0' + bytes[1]).substr(-2));
|
||||
_obj.co2 = parseInt(_data[2] + _data[3] + _data[4] + _data[5] + _data[6], 10);
|
||||
_obj.temp = (parseInt(_data[7] + _data[8] + _data[9] + _data[10] + _data[11], 10) - 1000) / 10;
|
||||
_obj.humid = (parseInt(_data[12] + _data[13] + _data[14] + _data[15] + _data[16], 10) / 10);
|
||||
_obj.noise = parseInt('0x' + ('0' + bytes[17]).substr(-2) + ('0' + bytes[18]).substr(-2));
|
||||
_obj.binData = bytes;
|
||||
// console.log(_obj);
|
||||
return _obj;
|
||||
};
|
||||
|
||||
module.rawBreaker = function(data) {
|
||||
var self = this;
|
||||
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);
|
||||
|
||||
workObj.lux = _data.light;
|
||||
workObj.co2 = _data.co2;
|
||||
workObj.temp = _data.temp;
|
||||
workObj.humidity = _data.humid;
|
||||
workObj.sound = _data.noise;
|
||||
workObj.timestamp = new Date();
|
||||
|
||||
return workObj;
|
||||
} else {
|
||||
console.error('Data does not have base64 data');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
return module;
|
||||
};
|
||||
|
84
lib/server/db-track.js
Normal file
84
lib/server/db-track.js
Normal file
@ -0,0 +1,84 @@
|
||||
'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;
|
||||
};
|
||||
|
51
package.json
51
package.json
@ -7,33 +7,47 @@
|
||||
"test": "test"
|
||||
},
|
||||
"dependencies": {
|
||||
"atob": "^2.0.3",
|
||||
"babel-preset-es2015": "^6.14.0",
|
||||
"basic-authentication": "^1.6.2",
|
||||
"body-parser": "^1.15.1",
|
||||
"btoa": "^1.1.2",
|
||||
"cfenv": "1.0.x",
|
||||
"cookie-parser": "*",
|
||||
"ejs": "*",
|
||||
"ejs": "^2.5.2",
|
||||
"errorhandler": "*",
|
||||
"events": "^1.1.1",
|
||||
"express": "^4.13.4",
|
||||
"express-session": "*",
|
||||
"express-session-lw": "^1.0.9",
|
||||
"gulp-babel": "^6.1.2",
|
||||
"gulp-banner": "^0.1.3",
|
||||
"htmlparser": "^1.7.7",
|
||||
"log4js": "^0.6.36",
|
||||
"method-override": "*",
|
||||
"minibus": "^3.1.0",
|
||||
"morgan": "*",
|
||||
"mqtt": "^1.10.0",
|
||||
"mqtt": "^2.0.0",
|
||||
"mqtt_over_websockets": "0.0.1",
|
||||
"node-localstorage": "^1.1.2",
|
||||
"request": "^2.72.0",
|
||||
"websocket": "^1.0.22"
|
||||
"pg-promise": "^5.2.7",
|
||||
"request": "^2.75.0",
|
||||
"request-promise": "^4.1.1",
|
||||
"sugar-date": "^2.0.0",
|
||||
"uuid-pure": "^1.0.10",
|
||||
"websocket": "^1.0.23",
|
||||
"ws": "^1.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"after": "^0.8.1",
|
||||
"apn": "^1.7.5",
|
||||
"apn": "^2.0.0",
|
||||
"apns": "^0.1.0",
|
||||
"basic-authentication": "^1.6.2",
|
||||
"basic-authentication": "^1.7.0",
|
||||
"chai": "^3.5.0",
|
||||
"cheerio": "^0.20.0",
|
||||
"cheerio": "^0.22.0",
|
||||
"clone": "^1.0.2",
|
||||
"del": "^2.2.0",
|
||||
"elapsed": "0.0.7",
|
||||
"events": "^1.1.1",
|
||||
"gulp": "^3.9.1",
|
||||
"gulp-autoprefixer": "^3.1.0",
|
||||
"gulp-cache": "^0.4.5",
|
||||
@ -41,9 +55,9 @@
|
||||
"gulp-cssmin": "^0.1.7",
|
||||
"gulp-cssnano": "^2.1.2",
|
||||
"gulp-debug": "^2.1.2",
|
||||
"gulp-google-webfonts": "0.0.12",
|
||||
"gulp-google-webfonts": "0.0.14",
|
||||
"gulp-html-replace": "^1.5.5",
|
||||
"gulp-htmlmin": "^2.0.0",
|
||||
"gulp-htmlmin": "^3.0.0",
|
||||
"gulp-inject": "^4.0.0",
|
||||
"gulp-jshint": "^2.0.1",
|
||||
"gulp-jsmin": "^0.1.5",
|
||||
@ -52,21 +66,20 @@
|
||||
"gulp-rename": "^1.2.2",
|
||||
"gulp-size": "^2.1.0",
|
||||
"gulp-strip-debug": "^1.1.0",
|
||||
"gulp-uglify": "^1.5.3",
|
||||
"gulp-uglify": "^2.0.0",
|
||||
"jshint": "^2.9.2",
|
||||
"jsonfile": "^2.3.1",
|
||||
"jsonfile": "^2.4.0",
|
||||
"log4js": "^0.6.36",
|
||||
"mocha": "^2.4.5",
|
||||
"mocha": "^3.0.2",
|
||||
"mqtt-ws": "^0.2.0",
|
||||
"nano": "^6.2.0",
|
||||
"node-cron": "^1.1.1",
|
||||
"require-dir": "^0.3.0",
|
||||
"should": "^8.3.1",
|
||||
"should": "^11.0.0",
|
||||
"string": "^3.3.1",
|
||||
"sugar": "^1.4.1",
|
||||
"sugar-date": "^1.5.1",
|
||||
"superagent": "^1.7.2",
|
||||
"supertest": "^1.1.0"
|
||||
"sugar": "^2.0.1",
|
||||
"sugar-date": "^2.0.0",
|
||||
"superagent": "^2.3.0",
|
||||
"supertest": "^2.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha --recursive --reporter spec --bail --check-leaks --timeout 3000",
|
||||
|
33
server/create_meeting.sql
Normal file
33
server/create_meeting.sql
Normal file
@ -0,0 +1,33 @@
|
||||
-- Sequence: public.meeting_id_seq
|
||||
|
||||
-- DROP SEQUENCE public.meeting_id_seq;
|
||||
|
||||
CREATE SEQUENCE public.meeting_id_seq
|
||||
INCREMENT 1
|
||||
MINVALUE 1
|
||||
MAXVALUE 9223372036854775807
|
||||
START 1
|
||||
CACHE 1;
|
||||
ALTER TABLE public.meeting_id_seq
|
||||
OWNER TO postgres;
|
||||
|
||||
|
||||
|
||||
-- Table: public.meeting
|
||||
|
||||
-- DROP TABLE public.meeting;
|
||||
|
||||
CREATE TABLE public.meeting
|
||||
(
|
||||
id integer NOT NULL DEFAULT nextval('meeting_id_seq'::regclass),
|
||||
locationid integer,
|
||||
logged timestamp with time zone,
|
||||
count smallint,
|
||||
start timestamp with time zone,
|
||||
"end" timestamp with time zone
|
||||
)
|
||||
WITH (
|
||||
OIDS=FALSE
|
||||
);
|
||||
ALTER TABLE public.meeting
|
||||
OWNER TO postgres;
|
55
server/dbconfig.js
Normal file
55
server/dbconfig.js
Normal file
@ -0,0 +1,55 @@
|
||||
/**
|
||||
*
|
||||
* User: Martin Donnelly
|
||||
* Date: 2016-04-04
|
||||
* Time: 14:46
|
||||
*
|
||||
*/
|
||||
var exec = require('child_process').exec;
|
||||
|
||||
|
||||
|
||||
function run_script() {
|
||||
'use strict';
|
||||
exec('psql postgres://amlrxqev:K11cvCplk0--oNafsYj4ISN-rVQmVS3y@jumbo.db.elephantsql.com:5432/amlrxqev -f ../sql/mdot.sql', function(err) {
|
||||
if (err !== null) {
|
||||
console.log('exec error: ', err);
|
||||
return -1;
|
||||
} else {
|
||||
// addUsers();
|
||||
console.log('All done...');
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function prepare_db() {
|
||||
exec('psql -Upostgres -d oBrand -h localhost -f ./obrand.sql', function(err) {
|
||||
if (err !== null) {
|
||||
console.log('exec error: ' + err);
|
||||
return -1;
|
||||
} else {
|
||||
addUsers();
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function createDB() {
|
||||
'use strict';
|
||||
exec('createdb -Upostgres -h localhost heat', function(err) {
|
||||
if (err !== null) {
|
||||
console.log('exec error: ' + err);
|
||||
return -1;
|
||||
} else {
|
||||
prepare_db();
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
createDB();
|
||||
//run_script();
|
53
server/insert_track.sql
Normal file
53
server/insert_track.sql
Normal file
@ -0,0 +1,53 @@
|
||||
|
||||
-- Sequence: public.track_id_seq
|
||||
|
||||
-- DROP SEQUENCE public.track_id_seq;
|
||||
|
||||
CREATE SEQUENCE public.track_id_seq
|
||||
INCREMENT 1
|
||||
MINVALUE 1
|
||||
MAXVALUE 9223372036854775807
|
||||
START 1
|
||||
CACHE 1;
|
||||
ALTER TABLE public.track_id_seq
|
||||
OWNER TO postgres;
|
||||
|
||||
|
||||
|
||||
-- Table: public.track
|
||||
|
||||
-- DROP TABLE public.track;
|
||||
|
||||
CREATE TABLE public.track
|
||||
(
|
||||
id bigint NOT NULL DEFAULT nextval('track_id_seq'::regclass),
|
||||
locationid integer,
|
||||
logged timestamp with time zone,
|
||||
count smallint,
|
||||
total integer
|
||||
)
|
||||
WITH (
|
||||
OIDS=FALSE
|
||||
);
|
||||
ALTER TABLE public.track
|
||||
OWNER TO postgres;
|
||||
|
||||
|
||||
--
|
||||
|
||||
CREATE OR REPLACE FUNCTION public.insert_track(
|
||||
_locationid integer,
|
||||
_logged timestamp with time zone,
|
||||
_count smallint,
|
||||
_total integer)
|
||||
RETURNS void AS
|
||||
$BODY$
|
||||
|
||||
BEGIN
|
||||
INSERT into track(locationid, logged, count, total) Values( _locationid, _logged, _count, _total);
|
||||
END;
|
||||
$BODY$
|
||||
LANGUAGE plpgsql VOLATILE
|
||||
COST 100;
|
||||
ALTER FUNCTION public.insert_track(integer, timestamp with time zone, smallint, integer)
|
||||
OWNER TO postgres;
|
114
server/restore_meeting.sql
Normal file
114
server/restore_meeting.sql
Normal file
@ -0,0 +1,114 @@
|
||||
--
|
||||
-- PostgreSQL database dump
|
||||
--
|
||||
|
||||
-- Dumped from database version 9.5.1
|
||||
-- Dumped by pg_dump version 9.5.1
|
||||
|
||||
-- Started on 2016-08-18 15:46:39 BST
|
||||
|
||||
SET statement_timeout = 0;
|
||||
SET lock_timeout = 0;
|
||||
SET client_encoding = 'UTF8';
|
||||
SET standard_conforming_strings = on;
|
||||
SET check_function_bodies = false;
|
||||
SET client_min_messages = warning;
|
||||
SET row_security = off;
|
||||
|
||||
SET search_path = public, pg_catalog;
|
||||
|
||||
SET default_tablespace = '';
|
||||
|
||||
SET default_with_oids = false;
|
||||
|
||||
--
|
||||
-- TOC entry 188 (class 1259 OID 63387)
|
||||
-- Name: meeting; Type: TABLE; Schema: public; Owner: postgres
|
||||
--
|
||||
|
||||
DROP SEQUENCE public.meeting_id_seq;
|
||||
DROP TABLE public.meeting;
|
||||
|
||||
|
||||
CREATE TABLE meeting (
|
||||
id integer NOT NULL,
|
||||
locationid integer,
|
||||
logged timestamp with time zone,
|
||||
count smallint,
|
||||
start timestamp with time zone,
|
||||
"end" timestamp with time zone
|
||||
);
|
||||
|
||||
|
||||
ALTER TABLE meeting OWNER TO postgres;
|
||||
|
||||
--
|
||||
-- TOC entry 187 (class 1259 OID 63385)
|
||||
-- Name: meeting_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
|
||||
--
|
||||
|
||||
CREATE SEQUENCE meeting_id_seq
|
||||
START WITH 1
|
||||
INCREMENT BY 1
|
||||
NO MINVALUE
|
||||
NO MAXVALUE
|
||||
CACHE 1;
|
||||
|
||||
|
||||
ALTER TABLE meeting_id_seq OWNER TO postgres;
|
||||
|
||||
--
|
||||
-- TOC entry 2387 (class 0 OID 0)
|
||||
-- Dependencies: 187
|
||||
-- Name: meeting_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
|
||||
--
|
||||
|
||||
ALTER SEQUENCE meeting_id_seq OWNED BY meeting.id;
|
||||
|
||||
|
||||
--
|
||||
-- TOC entry 2266 (class 2604 OID 63390)
|
||||
-- Name: id; Type: DEFAULT; Schema: public; Owner: postgres
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY meeting ALTER COLUMN id SET DEFAULT nextval('meeting_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- TOC entry 2382 (class 0 OID 63387)
|
||||
-- Dependencies: 188
|
||||
-- Data for Name: meeting; Type: TABLE DATA; Schema: public; Owner: postgres
|
||||
--
|
||||
|
||||
COPY meeting (id, locationid, logged, count, start, "end") FROM stdin;
|
||||
3 1 2016-08-18 15:39:26+01 4 2016-08-04 13:00:00+01 2016-08-04 13:45:00+01
|
||||
4 1 2016-08-18 15:39:51+01 11 2014-08-04 14:00:00+01 2014-08-04 14:45:00+01
|
||||
5 1 2016-08-18 15:40:29+01 2 2016-08-04 14:45:00+01 2016-08-04 14:55:00+01
|
||||
6 1 2016-08-18 15:40:55+01 3 2016-08-04 15:04:00+01 2016-08-04 16:06:00+01
|
||||
7 1 2016-08-18 15:41:25+01 2 2016-08-05 11:05:00+01 2016-08-05 12:15:00+01
|
||||
8 1 2016-08-18 15:41:47+01 1 2016-08-05 14:10:00+01 2016-08-05 14:25:00+01
|
||||
9 1 2016-08-18 15:42:08+01 4 2016-08-05 15:00:00+01 2016-08-05 15:30:00+01
|
||||
10 1 2016-08-18 15:42:37+01 4 2016-08-05 15:30:00+01 2016-08-05 16:23:00+01
|
||||
11 1 2016-08-18 15:43:02+01 8 2016-08-08 11:00:00+01 2016-08-08 12:12:00+01
|
||||
12 1 2016-08-18 15:43:29+01 7 2016-08-08 15:37:00+01 2016-08-08 16:50:00+01
|
||||
13 1 2016-08-18 15:44:10+01 5 2016-08-09 10:02:00+01 2016-08-09 10:15:00+01
|
||||
14 1 2016-08-18 15:44:34+01 6 2016-08-09 10:15:00+01 2016-08-09 12:05:00+01
|
||||
15 1 2016-08-18 15:44:52+01 1 2016-08-09 13:05:00+01 2016-08-09 13:10:00+01
|
||||
\.
|
||||
|
||||
|
||||
--
|
||||
-- TOC entry 2388 (class 0 OID 0)
|
||||
-- Dependencies: 187
|
||||
-- Name: meeting_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
|
||||
--
|
||||
|
||||
SELECT pg_catalog.setval('meeting_id_seq', 15, true);
|
||||
|
||||
|
||||
-- Completed on 2016-08-18 15:46:39 BST
|
||||
|
||||
--
|
||||
-- PostgreSQL database dump complete
|
||||
--
|
||||
|
Loading…
Reference in New Issue
Block a user