/** * * User: Martin Donnelly * Date: 2016-09-09 * Time: 11:38 * */ /* global Backbone, _, WEBSOCKET */ /* jshint browser: true , devel: true*/ 'use strict'; const _ = require('underscore'); const Backbone = require('backbone'); const WEBSOCKET = require('./websocket'); const 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 === 'btc' && this.btc !== undefined) this.btc.btcFromSocket(item.data); if (item.id === 'balance' && this.btc !== undefined) this.btc.balanceFromSocket(item.data); } }, 'setBTC': function (obj) { this.btc = obj; }, 'setTrain': function (obj) { this.train = obj; }, 'getUpdate': function () { this.webSocket.send('update'); } }); return SocketManager; }()); module.exports = SOCKETMANAGER;