mirror of
https://gitlab.silvrtree.co.uk/martind2000/censis-archive.git
synced 2025-02-04 09:00:14 +00:00
”2016-09-09”
This commit is contained in:
parent
146d769405
commit
b1212ea9bd
@ -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({
|
||||
|
||||
@ -239,7 +229,7 @@
|
||||
}
|
||||
|
||||
}, initialize: function() {
|
||||
_.bindAll(this, 'render', 'changeDevice', 'updateDevice');
|
||||
_.bindAll(this, 'render', 'changeDevice', 'updateDevice','setSocket');
|
||||
this.model.on('change', this.updateDevice);
|
||||
this.render();
|
||||
}, render: function() {
|
||||
@ -248,9 +238,21 @@
|
||||
}, changeDevice: function() {
|
||||
var newDevice;
|
||||
newDevice = this.$el.find('#device')[0].value;
|
||||
console.log('NewDevice', newDevice);
|
||||
|
||||
this.model.set('device', newDevice);
|
||||
|
||||
if (this.socketHandler) {
|
||||
this.socketHandler.listenFor(newDevice);
|
||||
}
|
||||
}, updateDevice: function() {
|
||||
var fetchObj = {beforeSend: sendAuthentication};
|
||||
console.log('this:',this);
|
||||
|
||||
if (this.socketHandler) {
|
||||
this.socketHandler.turnOff();
|
||||
}
|
||||
|
||||
notification.clearAll();
|
||||
if (this.model.has('device')) {
|
||||
this.collection.url = '/apiv2/mdot/' + this.model.get('device');
|
||||
@ -261,6 +263,9 @@
|
||||
console.error('Nothing to get!');
|
||||
}
|
||||
|
||||
},
|
||||
setSocket: function(newSocket) {
|
||||
this.socketHandler = newSocket;
|
||||
}
|
||||
|
||||
});
|
||||
@ -283,6 +288,11 @@
|
||||
this.collection.on('update', function() {
|
||||
// Trigger the redraw
|
||||
this.updateGraphV2();
|
||||
console.log(this);
|
||||
if (typeof this.socketHandler === 'object') {
|
||||
this.socketHandler.turnOn();
|
||||
}
|
||||
|
||||
}, this);
|
||||
|
||||
}, events: {
|
||||
@ -457,14 +467,18 @@
|
||||
|
||||
self.doChartV2(chartData);
|
||||
console.timeEnd('updateGraphV2');
|
||||
}
|
||||
},
|
||||
setSocket: function(newSocket) {
|
||||
this.socketHandler = newSocket;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
notification.configProfile('global', {
|
||||
stacking: false
|
||||
});
|
||||
var webSocketModel = new SocketManager();
|
||||
//Var webSocketModel = new SocketManager();
|
||||
var webSocketModel = new SOCKETMANAGER();
|
||||
|
||||
var DeviceCollection = new Backbone.Collection;
|
||||
var mdotCollection = new mDotCollection();
|
||||
@ -475,8 +489,12 @@
|
||||
collection: mdotCollection, model: mainSettings
|
||||
});
|
||||
|
||||
|
||||
|
||||
views.mdot = new MDOT({collection: mdotCollection});
|
||||
|
||||
views.grapher = new GraphView({collection: DeviceCollection});
|
||||
|
||||
views.mainview.setSocket(webSocketModel);
|
||||
views.grapher.setSocket(webSocketModel);
|
||||
})(jQuery);
|
||||
|
@ -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;
|
||||
}());
|
||||
|
||||
|
@ -3,77 +3,70 @@ var WEBSOCKET = function(model) {
|
||||
var wsUrl = 'ws://localhost:3001';
|
||||
|
||||
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.timer = 0;
|
||||
|
||||
this.startWebSocket = function() {
|
||||
'use strict';
|
||||
console.log('Starting socket?');
|
||||
console.log('**** Websocket Connected ****');
|
||||
};
|
||||
|
||||
var url = wsUrl;
|
||||
this.handleWebsocketMessage = function(message) {
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
};
|
||||
console.log('handleWebsocketMessage:',message);
|
||||
var command;
|
||||
try {
|
||||
command = JSON.parse(message.data);
|
||||
}
|
||||
catch (e) { /* Do nothing */
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
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();
|
||||
this.startWebSocket();
|
||||
|
||||
};
|
||||
|
@ -228,6 +228,7 @@
|
||||
<!-- endbuild -->
|
||||
<!-- build:js -->
|
||||
<script src="js/websocket.js"></script>
|
||||
<script src="js/socketmanager.js"></script>
|
||||
<script src="js/mdot.js"></script>
|
||||
<!-- endbuild -->
|
||||
</body>
|
||||
|
Loading…
Reference in New Issue
Block a user