”2016-08-23”

This commit is contained in:
Martin Donnelly 2016-08-23 16:45:03 +01:00
parent ff8e7a9912
commit a5ff1dff52
8 changed files with 117001 additions and 52 deletions

View File

@ -1,5 +1,5 @@
'use strict';
/* global Backbone, _, $, AmCharts */
/* global Backbone, _, $, AmCharts, notification */
/* global mainview */
/* jshint browser: true , devel: true*/
@ -38,57 +38,112 @@
});
},
splitOccupancy: function(item) {
findOccupancy: function(ts) {
/*
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()];
var start = new Date(item.start);
var msStart = ~~(start.getTime() / 300000);
var end = new Date(item.end);
var msEnd = ~~(end.getTime() / 300000);
var newArray = [];
for (var t = 0; t < (msEnd - msStart);t++) {
newArray.push({date: new Date((msStart * (300000)) + (t * 300000)), count: item.count});
}
return newArray;
if (typeof branch === 'undefined') {return count;}
_(branch).each(function(item) {
if ((tsMS >= item.startMS) && (tsMS <= item.endMS)) {
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 newItem = item;
let day, month,year;
let _date = new Date(item.start);
newItem.startMS = new Date(item.start).getTime();
newItem.endMS = new Date(item.end).getTime();
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(newItem);
}
}
}, this);
return _tree;
},
processAdded: function() {
console.log('Model:ProcessAdded');
var self = this;
console.time('processAdd');
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 = 0;
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');
console.timeEnd('processAdd');
DeviceCollection.trigger('update');
console.log('temporal:', this.temporal);
notification.notify('success', 'Data loaded');
}, decoder: function(data) {
var _obj = {};
var _data = window.atob(data).split('');
@ -164,8 +219,6 @@
});
}, render: function() {
var that = this;
console.log('MDOT:render');
$('#output').empty();
@ -204,6 +257,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!');
@ -249,13 +303,10 @@
doChartV2: function(chartData) {
var self = this;
console.time('doChartV2');
self.chart = AmCharts.makeChart('chartdiv', {
type: 'serial',
theme: 'light',
dataSets: [
],
legend: {
useGraphSettings: true,
@ -301,7 +352,7 @@
offset: 50,
position: 'right',
gridColor: '#556374'
},
}/*,
{
id: 'noise',
axisColor: 'rgb(99, 157, 189)',
@ -311,9 +362,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: false,
columnWidth: 1,
lineColor: '#aaaaaa',
title: 'Occupancy',
valueField: 'occupancy',
fillColor: '#888888',
fillAlphas: 0.2,
fillToAxis: 'x'
},
{
valueAxis: 'lux',
lineColor: '#FFC802',
@ -341,16 +413,30 @@
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',
updateOnReleaseOnly: true
},
chartCursor: {
cursorPosition: 'mouse',
cursorColor: '#14bfff',
@ -358,7 +444,7 @@
},
categoryField: 'date',
categoryAxis: {
minPeriod: 'mm',
minPeriod: '15mm',
parseDates: true,
axisColor: 'rgba(255,255,255,0.8)',
minorGridEnabled: true,
@ -372,9 +458,12 @@
$('#chartdiv').empty();
self.chart.write('chartdiv');
console.timeEnd('doChartV2');
}, updateGraphV2: function() {
console.time('updateGraphV2');
var self = this;
var chartData = [];
console.time('chartData');
_(this.collection.models).each(function(i) {
chartData.push({
date: i.get('dt'),
@ -382,14 +471,23 @@
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.timeEnd('chartData');
console.log('Record count:',chartData.length);
// Console.log(chartData);
self.doChartV2(chartData);
console.timeEnd('updateGraphV2');
}
});
notification.configProfile('global', {
stacking: false
});
var DeviceCollection = new Backbone.Collection;
var OccupancyCollection = new Backbone.Collection;

View File

@ -3,6 +3,8 @@
<head>
<meta charset="UTF-8">
<title>Graph</title>
<meta name="viewport"
content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link href="css/mui.css" rel="stylesheet" type="text/css"/>
<link href="css/test.css" rel="stylesheet" type="text/css"/>
<link href="css/style.css" rel="stylesheet" type="text/css"/>

View File

@ -44,14 +44,14 @@ var mdot = require('./lib/mdot/mdot.js');
};
var device = 3;
var device = 13;
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());
// logger.debug(startTs, endTs);
// logger.debug(startTs.getTime(), endTs.getTime());
data.start = startTs.getTime();
data.end = endTs.getTime();
@ -64,7 +64,7 @@ var mdot = require('./lib/mdot/mdot.js');
for (var item in d.events) {
var newItem = d.events[item].evt;
newItem.topic=['','','','',ids[device]].join('/');
logger.debug(newItem);
// logger.debug(newItem);
var gwTime = new Date(newItem.gateway_info['0'].gw_time);
if ((gwTime > startTs) && (gwTime < endTs)) {
doInsertEntry(newItem);

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) {

File diff suppressed because it is too large Load Diff

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...');
}
});

View File

@ -177,9 +177,7 @@ You can overwrite profile options with your own options object by calling:
``` javascript
// Configure the 'social' profile
notification.configProfile( 'social', {
// ...
} );
stack
```
#### Remove a profile