79 lines
1.5 KiB
JavaScript
79 lines
1.5 KiB
JavaScript
|
/**
|
||
|
* Created by martin on 11/23/16.
|
||
|
*/
|
||
|
/**
|
||
|
*
|
||
|
* User: Martin Donnelly
|
||
|
* Date: 2016-09-09
|
||
|
* Time: 11:38
|
||
|
*
|
||
|
*/
|
||
|
/* global Backbone, _, WEBSOCKET */
|
||
|
/* jshint browser: true , devel: true*/
|
||
|
|
||
|
'use strict';
|
||
|
|
||
|
let SOCKETMANAGER = (function () {
|
||
|
|
||
|
const SocketManager = Backbone.Model.extend({
|
||
|
|
||
|
initialize: function () {
|
||
|
_.bindAll(this, 'turnOn', 'turnOff');
|
||
|
this.listeningID = null;
|
||
|
this.listening = false;
|
||
|
this.webSocket = new WEBSOCKET(this);
|
||
|
|
||
|
this.on('message', function (o) {
|
||
|
console.log('On message', this.listening);
|
||
|
if (this.listening) {
|
||
|
this.checkItem(o);
|
||
|
}
|
||
|
|
||
|
});
|
||
|
},
|
||
|
turnOn: function () {
|
||
|
console.log('Socket now listening');
|
||
|
this.listening = true;
|
||
|
},
|
||
|
turnOff: function () {
|
||
|
this.listening = false;
|
||
|
},
|
||
|
listenFor: function (id) {
|
||
|
this.listeningID = this.deviceId.indexOf(id);
|
||
|
},
|
||
|
checkItem: function (item) {
|
||
|
|
||
|
if (item.hasOwnProperty('id')) {
|
||
|
console.log('id:', item.id);
|
||
|
if (item.id === 'weather') {
|
||
|
console.log('Setting weather');
|
||
|
this.weather.set(item.data);
|
||
|
}
|
||
|
|
||
|
if (item.id === 'trains') {
|
||
|
console.log(item);
|
||
|
this.train.set('data', item.data);
|
||
|
|
||
|
}
|
||
|
|
||
|
if (item.id === 'temperature') {
|
||
|
console.log(item);
|
||
|
this.temp.set('data', item.data);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
, setTemp: function (obj) {
|
||
|
this.temp = obj;
|
||
|
},
|
||
|
getUpdate: function () {
|
||
|
this.webSocket.send('update');
|
||
|
}
|
||
|
});
|
||
|
|
||
|
return SocketManager;
|
||
|
}());
|
||
|
|
||
|
|