”2016-09-21”

This commit is contained in:
Martin Donnelly 2016-09-21 16:45:02 +01:00
parent 4d7ee60494
commit 40b338f836
7 changed files with 128 additions and 49 deletions

View File

@ -4,7 +4,8 @@
var express = require('express'); var express = require('express');
var path = require('path'); var path = require('path');
var http = require('http'); var server = require('http').createServer();
var url = require('url');
var ejs = require('ejs'); var ejs = require('ejs');
var morgan = require('morgan'); var morgan = require('morgan');
var cookieparser = require('cookie-parser'); var cookieparser = require('cookie-parser');
@ -19,8 +20,31 @@ var authentication = require('basic-authentication');
var Events = require('events'); var Events = require('events');
var busEmitter = new Events.EventEmitter(); var busEmitter = new Events.EventEmitter();
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ server: server });
//var SocketHandler = require('./lib/sockethandler'); //var SocketHandler = require('./lib/sockethandler');
var SocketHandler = require('./lib/wshandler'); var SocketHandler = require('./lib/wshandlerv2');
console.log('Pre New SocketHandler');
var webSocket = new SocketHandler(busEmitter, wss);
/*
wss.on('connection', function connection(ws) {
var location = url.parse(ws.upgradeReq.url, true);
// you might use location.query.access_token to authenticate or share sessions
// or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312)
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
*/
console.log('Post New SocketHandler');
// Var apn = require('apn'); // Var apn = require('apn');
@ -151,7 +175,7 @@ meetingApi(app);
messageApi(app, busEmitter); messageApi(app, busEmitter);
heartBeat(); //heartBeat();
var port = (process.env.VCAP_APP_PORT || 3011); var port = (process.env.VCAP_APP_PORT || 3011);
var host = (process.env.VCAP_APP_HOST || 'localhost'); var host = (process.env.VCAP_APP_HOST || 'localhost');
@ -167,9 +191,8 @@ var server = app.listen(port, function() {
}); });
*/ */
var server = http.createServer(app).listen(port, function() { server.on('request', app);
console.log('New server listening on port ' + port); server.listen(port, function () { console.log('New server listening on ' + server.address().port) });
});
var webSocket = new SocketHandler(busEmitter, server, host);

View File

@ -117,35 +117,35 @@
console.time('processAdd'); console.time('processAdd');
var tempCollection = new Backbone.Collection(); var tempCollection = new Backbone.Collection();
var minmaxes = { var minmaxes = {
'1':{low:578,high:656, mp:617}, 1: {low: 578,high: 656, mp: 617},
'2':{low:657,high:709, mp:695}, 2: {low: 657,high: 709, mp: 695},
'3':{low:710,high:737, mp:723}, 3: {low: 710,high: 737, mp: 723},
'4':{low:738,high:778, mp:752}, 4: {low: 738,high: 778, mp: 752},
'5':{low:779,high:824, mp:804}, 5: {low: 779,high: 824, mp: 804},
'6':{low:825,high:860, mp:844}, 6: {low: 825,high: 860, mp: 844},
'7':{low:861,high:892, mp:876}, 7: {low: 861,high: 892, mp: 876},
'8':{low:0,high:0, mp:0}, 8: {low: 0,high: 0, mp: 0},
'9':{low:0,high:0, mp:0}, 9: {low: 0,high: 0, mp: 0},
'10':{low:0,high:0, mp:0}, 10: {low: 0,high: 0, mp: 0},
'11':{low:0,high:0, mp:0}, 11: {low: 0,high: 0, mp: 0},
'12':{low:0,high:0, mp:0}, 12: {low: 0,high: 0, mp: 0},
'13':{low:0,high:0, mp:0}, 13: {low: 0,high: 0, mp: 0},
'14':{low:0,high:0, mp:0}, 14: {low: 0,high: 0, mp: 0},
'15':{low:0,high:0, mp:0}, 15: {low: 0,high: 0, mp: 0},
'16':{low:0,high:0, mp:0}, 16: {low: 0,high: 0, mp: 0},
'17':{low:0,high:0, mp:0}, 17: {low: 0,high: 0, mp: 0},
'18':{low:0,high:0, mp:0}, 18: {low: 0,high: 0, mp: 0},
'19':{low:0,high:0, mp:0}, 19: {low: 0,high: 0, mp: 0},
}; };
var minmaxesA = [ var minmaxesA = [
{low:640,high:656, mp:617, count:1}, {low: 640,high: 656, mp: 617, count: 1},
{low:657,high:709, mp:695, count:2}, {low: 657,high: 709, mp: 695, count: 2},
{low:710,high:737, mp:723, count:3}, {low: 710,high: 737, mp: 723, count: 3},
{low:738,high:778, mp:752, count:4}, {low: 738,high: 778, mp: 752, count: 4},
{low:779,high:824, mp:804, count:5}, {low: 779,high: 824, mp: 804, count: 5},
{low:825,high:860, mp:844, count:6}, {low: 825,high: 860, mp: 844, count: 6},
{low:861,high:892, mp:876, count:7} {low: 861,high: 892, mp: 876, count: 7}
]; ];
var events; var events;
@ -168,8 +168,7 @@
if (!skipOccupancy) { if (!skipOccupancy) {
_occupancy = this.findOccupancy(i.timestamp, events.occupancy); _occupancy = this.findOccupancy(i.timestamp, events.occupancy);
} else { } else {
for(let _of=0;_of < 7;_of++) for (let _of = 0;_of < 7;_of++) {
{
if ((i.co2 >= minmaxesA[_of].low) && (i.co2 <= minmaxesA[_of].high)) { if ((i.co2 >= minmaxesA[_of].low) && (i.co2 <= minmaxesA[_of].high)) {
_occupancy = minmaxesA[_of].count; _occupancy = minmaxesA[_of].count;
} }

View File

@ -41,7 +41,6 @@ var SOCKETMANAGER = (function() {
if (item.hasOwnProperty('tick')) { if (item.hasOwnProperty('tick')) {
// Ticks are ignored // Ticks are ignored
} else { } else {
console.log('deviceid', item.data.deviceid);
if (item.data.deviceid === this.listeningID) { if (item.data.deviceid === this.listeningID) {
console.log('Updated', item.data); console.log('Updated', item.data);
this.set({data: item.data}); this.set({data: item.data});

View File

@ -3,16 +3,17 @@ var WEBSOCKET = function(model) {
var wsUrl = 'ws://localhost'; var wsUrl = 'ws://localhost';
var wsPort = 3011; var wsPort = 3011;
if ('https:' === document.location.protocol) { if ('https:' === document.location.protocol) {
wsUrl = 'wss://mdotserver.bluemix.net'; wsUrl = 'wss://mdotserver.mybluemix.net/';
wsPort = '443'; wsPort = '';
} else { } else {
//wsUrl = 'ws://localhost:3001'; //wsUrl = 'ws://localhost:3001';
wsUrl = 'ws://mdotserver.bluemix.net'; wsUrl = 'ws://mdotserver.mybluemix.net/';
wsPort = ''; wsPort = '';
} }
this.socket = null; this.socket = null;
this.timer = 0; this.timer = 0;
this.clock = null;
this.startWebSocket = function() { this.startWebSocket = function() {
'use strict'; 'use strict';
@ -42,6 +43,7 @@ var WEBSOCKET = function(model) {
this.retry = 0; this.retry = 0;
console.log('**** Websocket Connected ****'); console.log('**** Websocket Connected ****');
this.clock = new Date();
}; };
this.handleWebsocketMessage = function(message) { this.handleWebsocketMessage = function(message) {
@ -52,7 +54,6 @@ var WEBSOCKET = function(model) {
} }
catch (e) { /* Do nothing */ catch (e) { /* Do nothing */
} }
console.log(command);
if (command) { if (command) {
this.handleData.call(this,command); this.handleData.call(this,command);
} }
@ -60,6 +61,10 @@ var WEBSOCKET = function(model) {
this.handleWebsocketClose = function() { this.handleWebsocketClose = function() {
console.error('WebSocket Connection Closed.'); console.error('WebSocket Connection Closed.');
var now = new Date();
var uptime = now.getTime() - this.clock.getTime();
console.log('Socket alive for', uptime / 1000);
var self = this; var self = this;
console.log('Waiting ', 5000); console.log('Waiting ', 5000);
this.timer = setTimeout(function() {self.startWebSocket();},5000); this.timer = setTimeout(function() {self.startWebSocket();},5000);

View File

@ -7,6 +7,7 @@
*/ */
var WebSocketServer = require('ws').Server; var WebSocketServer = require('ws').Server;
var url = require('url');
var logger = require('log4js').getLogger(); var logger = require('log4js').getLogger();
var Sugar = require('sugar-date'); var Sugar = require('sugar-date');
@ -19,15 +20,18 @@ module.exports = function(events, server, host) {
var port = 3011; var port = 3011;
logger.debug('Creating Socket Server');
//wsServer = new WebSocketServer({server:server, port:4443}); //wsServer = new WebSocketServer({server:server, port:4443});
wsServer = new WebSocketServer({server:server, url: host}); wsServer = new WebSocketServer({server:server});
logger.debug(wsServer);
function originIsAllowed(origin) { function originIsAllowed(origin) {
// Put logic here to detect whether the specified origin is allowed. // Put logic here to detect whether the specified origin is allowed.
return true; return true;
} }
wsServer.on('connection', function connection(ws) { wsServer.on('connection', function connection(ws) {
var location = url.parse(ws.upgradeReq.url, true);
logger.info('Creating event for this connection'); logger.info('Creating event for this connection');
@ -47,13 +51,7 @@ module.exports = function(events, server, host) {
events.on('sendSocket', sendSocketHandler); events.on('sendSocket', sendSocketHandler);
ws.on('message', function(message) { ws.on('message', function(message) {
if (message.type === 'utf8') { console.log('received:', message);
logger.info('Received Message: ' + message.utf8Data);
ws.send(message.utf8Data);
} else if (message.type === 'binary') {
logger.info('Received Binary Message of ' + message.binaryData.length + ' bytes');
ws.send(message.binaryData);
}
}); });
ws.on('close', function(reasonCode, description) { ws.on('close', function(reasonCode, description) {

View File

@ -0,0 +1,52 @@
/**
*
* User: Martin Donnelly
* Date: 2016-09-07
* Time: 15:33
*
*/
var url = require('url');
var logger = require('log4js').getLogger();
module.exports = function(events, wsServer) {
'use strict';
logger.debug(wsServer);
wsServer.on('connection', function connection(ws) {
var location = url.parse(ws.upgradeReq.url, true);
logger.info('Creating event for this connection');
logger.info((new Date()) + ' Connection accepted.');
var sendSocketHandler = (obj) => {
logger.debug('sendSocketHandler', obj);
try {
ws.send(JSON.stringify(obj));
}
catch (err) {
logger.error(err);
logger.warn('Offending object: ', obj);
}
};
events.on('sendSocket', sendSocketHandler);
ws.on('message', function(message) {
console.log('received:', message);
});
ws.on('close', function(reasonCode, description) {
logger.info((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
events.removeListener('sendSocket', sendSocketHandler);
});
});
return module;
};

View File

@ -88,5 +88,8 @@
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+http://gitlab.silvrtree.co.uk/martind2000/mdot_server.git" "url": "git+http://gitlab.silvrtree.co.uk/martind2000/mdot_server.git"
} },
"engines": {
"node": "6.x"
}
} }