/** * * User: Martin Donnelly * Date: 2016-04-25 * Time: 12:03 * */ 'use strict'; var SOWEBSOCKET = function(newController) { var controller = newController; this.socket = null; this.retry = 0; this.timer = 0; this.previousTemp = 0; function rgb(cv) { return ['rgb(', cv.r, ',', cv.g, ',', cv.b, ')'].join(''); } this.startWebSocket = function() { console.log('Starting socket?'); var url = controller.webSocketURL(); 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('!!WebSocket Error'); console.error(e); }; }; this.updateHeating = function(obj) { var $curTemp = $('#curTemp'); if (this.previousTemp !== obj.temp) { $curTemp.text(obj.temp); $curTemp.css('color', rgb(tempColours[parseInt(obj.temp)])); this.previousTemp = obj.temp; } }; this.toggleLighting = function(id) { var _id; var _off = ['f', 'g']; var _on = ['o', 'n']; var $show; var $hide; _id = _off.indexOf(id); // console.log(id,_id); if (_id > -1) { // Lights are being turnd off $hide = ['#', controller.lights.off[_id]].join(''); $show = ['#', controller.lights.on[_id]].join(''); } else { _id = _on.indexOf(id); $show = ['#', controller.lights.off[_id]].join(''); $hide = ['#', controller.lights.on[_id]].join(''); } $($show).show(); $($hide).hide(); }; this.toggleHeating = function(status) { var $show, $hide; if (status) { $hide = $('#heatingOn'); $show = $('#heatingOff'); } else { $show = $('#heatingOn'); $hide = $('#heatingOff'); } $($show).show(); $($hide).hide(); }; this.toggleProjector = function(status) { var $show, $hide; if (status) { $hide = $('#projectorOn'); $show = $('#projectorOff'); } else { $show = $('#projectorOn'); $hide = $('#projectorOff'); } $($show).show(); $($hide).hide(); }; this.updateLighting = function(obj) { //Console.log(obj); $('#lightR').text(obj.Red); $('#lightG').text(obj.Green); $('#lightB').text(obj.Blue); var r = parseInt(obj.Red); var g = parseInt(obj.Green); var b = parseInt(obj.Blue); var newR = Math.floor(((r / 65535.0) * 100) * 255); var newG = Math.floor(((g / 65535.0) * 100) * 255); var newB = Math.floor(((b / 65535.0) * 100) * 255); var w = '#' + ('0' + parseInt(newR).toString(16)).substr(-2, 2) + ('0' + parseInt(newG).toString(16)).substr(-2, 2) + ('0' + parseInt(newB).toString(16)).substr(-2, 2); $('#lightW').text(w).css('background-color', w); }; this.updateProj = function(obj) { //Console.log(obj); var y; var r = parseInt(obj.Red); var g = parseInt(obj.Green); var b = parseInt(obj.Blue); var newR = Math.floor(((r / 65535.0) * 100) * 255); var newG = Math.floor(((g / 65535.0) * 100) * 255); var newB = Math.floor(((b / 65535.0) * 100) * 255); $('#projR').text(newR); $('#projG').text(newG); $('#projB').text(newB); y = Math.floor((0.2126 * newR + 0.7152 * newG + 0.0722 * newB)); var w = '#' + ('0' + parseInt(y).toString(16)).substr(-2, 2) + ('0' + parseInt(y).toString(16)).substr(-2, 2) + ('0' + parseInt( y).toString(16)).substr(-2, 2); $('#projW').text(w).css('background-color', w); }; this.handleData = function(d) { console.log(d.id); switch (d.id) { case 'LightingDataReceived': // This.updateLighting(d.sensorData.d); break; case 'ProjectorDataReceived': // This.updateProj(d.sensorData.d); break; case 'HeatingDataReceived': this.updateHeating(d.sensorData.d); break; case 'calendar': controller.updateCalendar(d); break; case 'Lighting': this.toggleLighting(d.device); break; case 'Heating': this.toggleHeating(d.status); break; case 'Projector': this.toggleProjector(d.status); break; case 'announce': controller.notify(d); break; default: console.log(d); break; } }; this.handleWebsocketOnOpen = function() { 'use strict'; this.retry = 0; $('#longWait').hide(); $('#noSocket').slideUp(); }; this.handleWebsocketMessage = function(message) { var command; try { command = JSON.parse(message.data); this.updateDateTime(); } catch (e) { /* Do nothing */ } if (command) { this.handleData.call(this,command); } }; this.handleWebsocketClose = function() { console.error('WebSocket Connection Closed.', this.retry); var self = this, delay; if (this.retry > 0) { $('#noSocket').slideDown(); } if (this.retry > 12) { this.retry = 1; } if (this.retry === 3) { $('#longWait').fadeIn(); } if (this.retry > 0) { delay = 5000 * this.retry; } else { delay = 1500; } console.log('Waiting ', delay); this.timer = setTimeout(function() {self.startWebSocket();},delay); this.retry++; }; this.startWebSocket(); };