”2016-09-09”

This commit is contained in:
Martin Donnelly 2016-09-09 16:45:02 +01:00
parent 146d769405
commit b1212ea9bd
4 changed files with 143 additions and 77 deletions

View File

@ -17,16 +17,6 @@
|_| |_|\___/|___/|___|____|___/ |_| |_|\___/|___/|___|____|___/
*/ */
var SocketManager = Backbone.Model.extend({
initialize: function() {
// _.bindAll(this, 'processAdded');
this.webSocket = new WEBSOCKET(this);
this.on('message', function(o) {
console.log('Message', o);
});
}
});
var MainModel = Backbone.Model.extend({ var MainModel = Backbone.Model.extend({
@ -239,7 +229,7 @@
} }
}, initialize: function() { }, initialize: function() {
_.bindAll(this, 'render', 'changeDevice', 'updateDevice'); _.bindAll(this, 'render', 'changeDevice', 'updateDevice','setSocket');
this.model.on('change', this.updateDevice); this.model.on('change', this.updateDevice);
this.render(); this.render();
}, render: function() { }, render: function() {
@ -248,9 +238,21 @@
}, changeDevice: function() { }, changeDevice: function() {
var newDevice; var newDevice;
newDevice = this.$el.find('#device')[0].value; newDevice = this.$el.find('#device')[0].value;
console.log('NewDevice', newDevice);
this.model.set('device', newDevice); this.model.set('device', newDevice);
if (this.socketHandler) {
this.socketHandler.listenFor(newDevice);
}
}, updateDevice: function() { }, updateDevice: function() {
var fetchObj = {beforeSend: sendAuthentication}; var fetchObj = {beforeSend: sendAuthentication};
console.log('this:',this);
if (this.socketHandler) {
this.socketHandler.turnOff();
}
notification.clearAll(); notification.clearAll();
if (this.model.has('device')) { if (this.model.has('device')) {
this.collection.url = '/apiv2/mdot/' + this.model.get('device'); this.collection.url = '/apiv2/mdot/' + this.model.get('device');
@ -261,6 +263,9 @@
console.error('Nothing to get!'); console.error('Nothing to get!');
} }
},
setSocket: function(newSocket) {
this.socketHandler = newSocket;
} }
}); });
@ -283,6 +288,11 @@
this.collection.on('update', function() { this.collection.on('update', function() {
// Trigger the redraw // Trigger the redraw
this.updateGraphV2(); this.updateGraphV2();
console.log(this);
if (typeof this.socketHandler === 'object') {
this.socketHandler.turnOn();
}
}, this); }, this);
}, events: { }, events: {
@ -457,14 +467,18 @@
self.doChartV2(chartData); self.doChartV2(chartData);
console.timeEnd('updateGraphV2'); console.timeEnd('updateGraphV2');
} },
setSocket: function(newSocket) {
this.socketHandler = newSocket;
}
}); });
notification.configProfile('global', { notification.configProfile('global', {
stacking: false stacking: false
}); });
var webSocketModel = new SocketManager(); //Var webSocketModel = new SocketManager();
var webSocketModel = new SOCKETMANAGER();
var DeviceCollection = new Backbone.Collection; var DeviceCollection = new Backbone.Collection;
var mdotCollection = new mDotCollection(); var mdotCollection = new mDotCollection();
@ -475,8 +489,12 @@
collection: mdotCollection, model: mainSettings collection: mdotCollection, model: mainSettings
}); });
views.mdot = new MDOT({collection: mdotCollection}); views.mdot = new MDOT({collection: mdotCollection});
views.grapher = new GraphView({collection: DeviceCollection}); views.grapher = new GraphView({collection: DeviceCollection});
views.mainview.setSocket(webSocketModel);
views.grapher.setSocket(webSocketModel);
})(jQuery); })(jQuery);

View File

@ -0,0 +1,54 @@
/**
*
* User: Martin Donnelly
* Date: 2016-09-09
* Time: 11:38
*
*/
/* global Backbone, _, $, AmCharts, notification, WEBSOCKET */
/* global mainview */
/* jshint browser: true , devel: true*/
'use strict';
var SOCKETMANAGER = (function() {
var SocketManager = Backbone.Model.extend({
deviceId : ['CENSIS-LoRa-1','CENSIS-LoRa-2','CENSIS-LoRa-3','CENSIS-LoRa-4','HIE-mobile-1----','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'],
initialize: function() {
_.bindAll(this, 'turnOn', 'turnOff');
this.listeningID = null;
this.listening = false;
this.webSocket = new WEBSOCKET(this);
this.on('message', function(o) {
if (this.listening)
{
console.log('Message', o);
}
});
},
turnOn: function() {
console.log('SocketManager:turnOn');
this.listening = true;
},
turnOff: function() {
console.log('SocketManager:turnOff');
this.listening = false;
},
listenFor: function(id) {
console.log('index of', this.deviceId.indexOf(id));
this.listeningID = id;
}
});
return SocketManager;
}());

View File

@ -3,77 +3,70 @@ var WEBSOCKET = function(model) {
var wsUrl = 'ws://localhost:3001'; var wsUrl = 'ws://localhost:3001';
this.socket = null; this.socket = null;
this.retry = 0;
this.timer = 0;
this.startWebSocket = function() {
'use strict';
console.log('Starting socket?');
var url = wsUrl;
var wsCtor = window['MozWebSocket'] ? MozWebSocket : WebSocket;
this.socket = new wsCtor(url, 'stream');
this.socket.onopen = this.handleWebsocketOnOpen.bind(this);
this.socket.onmessage = this.handleWebsocketMessage.bind(this);
this.socket.onclose = this.handleWebsocketClose.bind(this);
this.socket.onerror = function(e) {
console.error(e);
};
};
this.handleData = function(d) {
model.trigger('message',d);
switch (d.id) {
default:
console.log('Default:',d);
break;
}
};
this.handleWebsocketOnOpen = function() {
'use strict';
this.retry = 0; this.retry = 0;
this.timer = 0;
this.startWebSocket = function() { console.log('**** Websocket Connected ****');
'use strict'; };
console.log('Starting socket?');
var url = wsUrl; this.handleWebsocketMessage = function(message) {
var wsCtor = window['MozWebSocket'] ? MozWebSocket : WebSocket; console.log('handleWebsocketMessage:',message);
this.socket = new wsCtor(url, 'stream'); var command;
try {
this.socket.onopen = this.handleWebsocketOnOpen.bind(this); command = JSON.parse(message.data);
this.socket.onmessage = this.handleWebsocketMessage.bind(this); }
this.socket.onclose = this.handleWebsocketClose.bind(this); catch (e) { /* Do nothing */
this.socket.onerror = function(e) { }
console.error(e);
};
};
this.handleData = function(d) { if (command) {
//This.dispatchCommand(command);
this.handleData.call(this,command);
}
};
this.handleWebsocketClose = function() {
console.error('WebSocket Connection Closed.');
var self = this;
console.log('Waiting ', 5000);
this.timer = setTimeout(function() {self.startWebSocket();},5000);
};
model.trigger('message',d); this.startWebSocket();
switch (d.id) {
/*
case 'LightingDataReceived':
// This.updateLighting(d.sensorData.d);
break;
*/
default:
console.log('Default:',d);
break;
}
};
this.handleWebsocketOnOpen = function() {
'use strict';
this.retry = 0;
console.log('**** Websocket Connected ****');
};
this.handleWebsocketMessage = function(message) {
console.log('handleWebsocketMessage:',message);
var command;
try {
command = JSON.parse(message.data);
}
catch (e) { /* Do nothing */
}
if (command) {
//This.dispatchCommand(command);
this.handleData.call(this,command);
}
};
this.handleWebsocketClose = function() {
console.error('WebSocket Connection Closed.');
var self = this;
console.log('Waiting ', 5000);
this.timer = setTimeout(function() {self.startWebSocket();},5000);
};
this.startWebSocket();
}; };

View File

@ -228,6 +228,7 @@
<!-- endbuild --> <!-- endbuild -->
<!-- build:js --> <!-- build:js -->
<script src="js/websocket.js"></script> <script src="js/websocket.js"></script>
<script src="js/socketmanager.js"></script>
<script src="js/mdot.js"></script> <script src="js/mdot.js"></script>
<!-- endbuild --> <!-- endbuild -->
</body> </body>