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;
},
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() {
console.log('Model:ProcessAdded');
var self = this;
var skipOccupancy = false;
var tempCollection = new Backbone.Collection();
var occupancy = [];
var events;
_.invoke(DeviceCollection.toArray(), 'destroy');
this.temporal = {low: 0, high: 0};
events = this.get('events');
this.dateTree = this.buildSpeedDateTree(events.occupancy);
if (Object.keys(this.dateTree).length === 0) {
skipOccupancy = true;
}
_(events.data).each(function(i) {
let _occupancy;
if (!skipOccupancy) {
_occupancy = this.findOccupancy(i.timestamp, events.occupancy);
}
tempCollection.add({
dt: Date.create(i.timestamp).addHours(1),
lux: i.lux,
temp: i.temp,
co2: i.co2,
humid: i.humidity,
noise: i.sound/*,
occupancy: _occupancy*/
noise: i.sound,
occupancy: _occupancy
});
}, this);
console.log('occupancy:', occupancy);
DeviceCollection.temporal = this.temporal;
DeviceCollection.models = tempCollection.models;
notification.notify('success', 'Data loaded');
DeviceCollection.trigger('update');
console.log('temporal:', this.temporal);
notification.notify('success', 'Data loaded');
}, decoder: function(data) {
var _obj = {};
var _data = window.atob(data).split('');
@ -204,6 +270,7 @@
this.collection.url = '/apiv2/mdot/' + this.model.get('device');
$('#output').empty();
notification.notify('info', 'Loading...');
this.collection.fetch(fetchObj);
} else {
console.error('Nothing to get!');
@ -301,7 +368,7 @@
offset: 50,
position: 'right',
gridColor: '#556374'
},
}/*,
{
id: 'noise',
axisColor: 'rgb(99, 157, 189)',
@ -311,9 +378,30 @@
axisAlpha: 1,
position: 'left',
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',
lineColor: '#FFC802',
@ -341,16 +429,29 @@
title: 'Humidity',
valueField: 'humid',
fillAlphas: 0
},
}/*,
{
valueAxis: 'noise',
lineColor: 'rgb(99, 157, 189)',
title: 'Sound',
valueField: 'noise',
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: {
cursorPosition: 'mouse',
cursorColor: '#14bfff',
@ -358,7 +459,7 @@
},
categoryField: 'date',
categoryAxis: {
minPeriod: 'mm',
minPeriod: '15mm',
parseDates: true,
axisColor: 'rgba(255,255,255,0.8)',
minorGridEnabled: true,
@ -382,14 +483,20 @@
humid: i.get('humid'),
lux: i.get('lux'),
noise: i.get('noise'),
temp: i.get('temp')
temp: i.get('temp'),
occupancy: i.get('occupancy')
});
});
// Console.log(chartData);
self.doChartV2(chartData);
}
});
notification.configProfile('global', {
stacking: false
});
var DeviceCollection = new Backbone.Collection;
var OccupancyCollection = new Backbone.Collection;

View File

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

View File

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

View File

@ -11,12 +11,13 @@ var exec = require('child_process').exec;
function run_script() {
'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) {
console.log('exec error: ' + err);
return -1;
} else {
addUsers();
// addUsers();
console.log('All done...');
}
});