/** * 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); } if (item.id === 'graph') { console.log(item); this.graph.set('data', item.data); } } } , setTemp: function(obj) { this.temp = obj; }, setGraph: function(obj) { this.graph = obj; }, getUpdate: function() { this.webSocket.send('update'); } }); return SocketManager; }());