SODashServer/app/js/sowebsocket.js
2016-05-17 15:58:16 +01:00

316 lines
7.7 KiB
JavaScript

/**
*
* 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.updateTimer = 0;
this.previousTemp = 0;
this.checks = {
lights: false,
rearLight: false,
lightsCount: 0,
rearLightCount: 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;
var $aux;
_id = _off.indexOf(id);
if (_id > -1) {
// Lights are being turnd off
$hide = ['#', controller.lights.off[_id]].join('');
$show = ['#', controller.lights.on[_id]].join('');
$aux = ['#', controller.lights.aux[_id]].join('');
$($aux).fadeOut();
} else {
_id = _on.indexOf(id);
$show = ['#', controller.lights.off[_id]].join('');
$hide = ['#', controller.lights.on[_id]].join('');
$aux = ['#', controller.lights.aux[_id]].join('');
$($aux).fadeIn(500);
}
$($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;
var $aux;
$aux = $('#auxProjector');
if (status) {
$hide = $('#projectorOn');
$show = $('#projectorOff');
$aux.fadeIn();
} else {
$show = $('#projectorOn');
$hide = $('#projectorOff');
$aux.fadeOut();
}
$($show).show();
$($hide).hide();
};
this.updateLighting = function(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) {
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) {
switch (d.id) {
case 'LightingDataReceived':
console.log('Light data received..');
this.checks.lights = true;
// 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':
case 'ProjectorISP15':
this.toggleProjector(d.status);
break;
case 'deviceNotConnecting':
this.checks.lights = false;
break;
case 'announce':
controller.notify(d);
break;
default:
console.log(d);
break;
}
};
this.handleWebsocketOnOpen = function() {
'use strict';
var self = this;
this.retry = 0;
clearTimeout(this.updateTimer);
this.updateTimer = 0;
$('#longWait').hide();
$('#noSocket').slideUp();
$('#front-light').removeClass('lostConnection');
$('#back-light').removeClass('lostConnection');
$('#frontLightOn').attr('disabled', false);
$('#frontLightOff').attr('disabled', false);
$('#frontUp').attr('disabled', false);
$('#frontDown').attr('disabled', false);
$('#backLightOn').attr('disabled', false);
$('#backLightOff').attr('disabled', false);
$('#backUp').attr('disabled', false);
$('#backDown').attr('disabled', false);
var now = new Date;
var mod = 60000 - (now.getTime() % 60000);
this.updateTimer = setTimeout(function() {this.connectedCheck();}.bind(this), mod);
};
this.connectedCheck = function() {
console.log('connectedCheck');
var now = new Date;
var mod = 60000 - (now.getTime() % 60000);
if (this.checks.lights) {
this.checks.lightsCount = 0;
} else {
this.checks.lightsCount++;
}
if (this.checks.lightsCount >= 5) {
console.error('The lights have possibly been disconnected');
$('#noDevice:hidden').slideDown();
} else {
$('#noDevice:visible').slideUp();
}
this.checks.lights = false;
this.updateTimer = setTimeout(function() {this.connectedCheck();}.bind(this), mod);
};
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;
var delay;
if (this.retry > 0) {
$('#noSocket:hidden').slideDown();
}
if (this.retry > 12) {
this.retry = 1;
}
if (this.retry === 3) {
$('#longWait:hidden').fadeIn();
$('#front-light').addClass('lostConnection');
$('#back-light').addClass('lostConnection');
$('#frontLightOn').attr('disabled', true);
$('#frontLightOff').attr('disabled', true);
$('#frontUp').attr('disabled', true);
$('#frontDown').attr('disabled', true);
$('#backLightOn').attr('disabled', true);
$('#backLightOff').attr('disabled', true);
$('#backUp').attr('disabled', true);
$('#backDown').attr('disabled', true);
//LostConnection
}
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();
// But this call into an emit
};