added meeting api

This commit is contained in:
Martin Donnelly 2016-08-23 13:52:52 +01:00
parent 33730b4311
commit 88cb33f29a
4 changed files with 133 additions and 24 deletions

View File

@ -54,41 +54,107 @@
return newArray; return newArray;
}, },
findOccupancy: function(ts, occupancy) {
/*
Get a branch from the date tree and see if the reduced set of records has a matching timestamp..
*/
let count = 0;
let tsDate = new Date(ts);
let tsMS = tsDate.getTime();
let branch = this.dateTree[tsDate.getFullYear().toString()][tsDate.getMonth().toString()][tsDate.getDate().toString()];
if (typeof branch === 'undefined') return count;
_(branch).each(function(item) {
if ((tsMS >= new Date(item.start).getTime()) && (tsMS <= new Date(item.end).getTime())) {
count = item.count;
return count;
}
}, this);
return count;
},
buildSpeedDateTree: function(occupancy) {
/*
Builds a tree to help speed up occupancy searching
*/
var _tree = {};
_(occupancy).each(function(item) {
let day, month,year;
let _date = new Date(item.start);
day = _date.getDate().toString();
month = _date.getMonth().toString();
year = _date.getFullYear().toString();
if (!_tree.hasOwnProperty(year)) {
_tree[year] = {};
}
if (_tree.hasOwnProperty(year)) {
if (!_tree[year].hasOwnProperty(month)) {
_tree[year][month] = {};
}
if (_tree[year].hasOwnProperty(month)) {
if (!_tree[year][month].hasOwnProperty(day)) {
_tree[year][month][day] = [];
}
_tree[year][month][day].push(item);
}
}
}, this);
return _tree;
},
processAdded: function() { processAdded: function() {
console.log('Model:ProcessAdded'); console.log('Model:ProcessAdded');
var self = this; var self = this;
var skipOccupancy = false;
var tempCollection = new Backbone.Collection(); var tempCollection = new Backbone.Collection();
var occupancy = [];
var events; var events;
_.invoke(DeviceCollection.toArray(), 'destroy'); _.invoke(DeviceCollection.toArray(), 'destroy');
this.temporal = {low: 0, high: 0};
events = this.get('events'); events = this.get('events');
this.dateTree = this.buildSpeedDateTree(events.occupancy);
if (Object.keys(this.dateTree).length === 0) {
skipOccupancy = true;
}
_(events.data).each(function(i) { _(events.data).each(function(i) {
let _occupancy;
if (!skipOccupancy) {
_occupancy = this.findOccupancy(i.timestamp, events.occupancy);
}
tempCollection.add({ tempCollection.add({
dt: Date.create(i.timestamp).addHours(1), dt: Date.create(i.timestamp).addHours(1),
lux: i.lux, lux: i.lux,
temp: i.temp, temp: i.temp,
co2: i.co2, co2: i.co2,
humid: i.humidity, humid: i.humidity,
noise: i.sound/*, noise: i.sound,
occupancy: _occupancy*/ occupancy: _occupancy
}); });
}, this); }, this);
console.log('occupancy:', occupancy);
DeviceCollection.temporal = this.temporal;
DeviceCollection.models = tempCollection.models; DeviceCollection.models = tempCollection.models;
notification.notify('success', 'Data loaded');
DeviceCollection.trigger('update'); DeviceCollection.trigger('update');
console.log('temporal:', this.temporal); notification.notify('success', 'Data loaded');
}, decoder: function(data) { }, decoder: function(data) {
var _obj = {}; var _obj = {};
var _data = window.atob(data).split(''); var _data = window.atob(data).split('');
@ -204,6 +270,7 @@
this.collection.url = '/apiv2/mdot/' + this.model.get('device'); this.collection.url = '/apiv2/mdot/' + this.model.get('device');
$('#output').empty(); $('#output').empty();
notification.notify('info', 'Loading...');
this.collection.fetch(fetchObj); this.collection.fetch(fetchObj);
} else { } else {
console.error('Nothing to get!'); console.error('Nothing to get!');
@ -301,7 +368,7 @@
offset: 50, offset: 50,
position: 'right', position: 'right',
gridColor: '#556374' gridColor: '#556374'
}, }/*,
{ {
id: 'noise', id: 'noise',
axisColor: 'rgb(99, 157, 189)', axisColor: 'rgb(99, 157, 189)',
@ -311,9 +378,30 @@
axisAlpha: 1, axisAlpha: 1,
position: 'left', position: 'left',
gridColor: '#556374' gridColor: '#556374'
} }*/
,{
id: 'occupancy',
axisColor: '#aaaaaa',
axisThickness: 2,
gridAlpha: 0,
offset: 100,
axisAlpha: 1,
position: 'right',
gridColor: '#556374'
}
], ],
graphs: [ graphs: [{id:'occ',
valueAxis: 'occupancy',
"type": "column",
"clustered":true,
"columnWidth":1,
lineColor: '#aaaaaa',
title: 'Occupancy',
valueField: 'occupancy',
fillColor: '#888888',
fillAlphas: 0.2,
fillToAxis: 'x'
},
{ {
valueAxis: 'lux', valueAxis: 'lux',
lineColor: '#FFC802', lineColor: '#FFC802',
@ -341,16 +429,29 @@
title: 'Humidity', title: 'Humidity',
valueField: 'humid', valueField: 'humid',
fillAlphas: 0 fillAlphas: 0
}, }/*,
{ {
valueAxis: 'noise', valueAxis: 'noise',
lineColor: 'rgb(99, 157, 189)', lineColor: 'rgb(99, 157, 189)',
title: 'Sound', title: 'Sound',
valueField: 'noise', valueField: 'noise',
fillAlphas: 0 fillAlphas: 0
} }*/
], ],
chartScrollbar: {}, chartScrollbar: {
graph:'occ',"oppositeAxis":false,
"offset":30,
"scrollbarHeight": 80,
"backgroundAlpha": 0,
"selectedBackgroundAlpha": 0.1,
"selectedBackgroundColor": "#888888",
"graphFillAlpha": 0,
"graphLineAlpha": 0.5,
"selectedGraphFillAlpha": 0,
"selectedGraphLineAlpha": 1,
"autoGridCount":true,
"color":"#AAAAAA"
},
chartCursor: { chartCursor: {
cursorPosition: 'mouse', cursorPosition: 'mouse',
cursorColor: '#14bfff', cursorColor: '#14bfff',
@ -358,7 +459,7 @@
}, },
categoryField: 'date', categoryField: 'date',
categoryAxis: { categoryAxis: {
minPeriod: 'mm', minPeriod: '15mm',
parseDates: true, parseDates: true,
axisColor: 'rgba(255,255,255,0.8)', axisColor: 'rgba(255,255,255,0.8)',
minorGridEnabled: true, minorGridEnabled: true,
@ -382,14 +483,20 @@
humid: i.get('humid'), humid: i.get('humid'),
lux: i.get('lux'), lux: i.get('lux'),
noise: i.get('noise'), noise: i.get('noise'),
temp: i.get('temp') temp: i.get('temp'),
occupancy: i.get('occupancy')
}); });
}); });
// Console.log(chartData);
self.doChartV2(chartData); self.doChartV2(chartData);
} }
}); });
notification.configProfile('global', {
stacking: false
});
var DeviceCollection = new Backbone.Collection; var DeviceCollection = new Backbone.Collection;
var OccupancyCollection = new Backbone.Collection; var OccupancyCollection = new Backbone.Collection;

View File

@ -35,6 +35,7 @@ module.exports = function(app) {
dbMdot.doGet(data) dbMdot.doGet(data)
.then((d) => { .then((d) => {
logger.debug('returning:',d.occupancy);
res.json({events:d}); res.json({events:d});
}) })
.catch((e) => { .catch((e) => {

View File

@ -13,8 +13,9 @@ module.exports = function(db) {
module.sqlGetMeetingByRoom = function(roomId,returnObj) { module.sqlGetMeetingByRoom = function(roomId,returnObj) {
logger.debug('mdot:sqlGetMeetingByRoom', roomId); logger.debug('mdot:sqlGetMeetingByRoom', roomId);
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
db.any('select * from meeting where locationid=$1 order by start asc;', [roomId + 1]) db.any('select distinct * from meeting where locationid=$1 order by start asc;', [roomId + 1])
.then(function(d) { .then(function(d) {
returnObj.occupancy = d; returnObj.occupancy = d;
return resolve(returnObj); return resolve(returnObj);
@ -30,7 +31,7 @@ module.exports = function(db) {
module.sqlAllGetDecoded = function(deviceId) { module.sqlAllGetDecoded = function(deviceId) {
logger.debug('mdot:sqlAllGetDecoded'); logger.debug('mdot:sqlAllGetDecoded');
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
db.any('select * from decoded where deviceid=$1 order by timestamp asc;', [deviceId]) db.any('select distinct * from decoded where deviceid=$1 order by timestamp asc;', [deviceId])
.then(function(d) { .then(function(d) {
return resolve(d); return resolve(d);
}) })
@ -107,7 +108,6 @@ module.exports = function(db) {
return self.sqlGetMeetingByRoom(room,obj); return self.sqlGetMeetingByRoom(room,obj);
}) })
.then(function(combined) { .then(function(combined) {
logger.debug('combined',combined);
resolve(combined); resolve(combined);
}) })
.catch(function(e) { .catch(function(e) {

View File

@ -11,12 +11,13 @@ var exec = require('child_process').exec;
function run_script() { function run_script() {
'use strict'; 'use strict';
exec('psql -Upostgres -d mdot -h localhost -f ./new.sql', function(err) { exec('psql -Upostgres -d mdot -h localhost -f ../mdot.sql', function(err) {
if (err !== null) { if (err !== null) {
console.log('exec error: ' + err); console.log('exec error: ' + err);
return -1; return -1;
} else { } else {
addUsers(); // addUsers();
console.log('All done...');
} }
}); });