var bus = {}; MicroEvent.mixin(bus); var wsUrl = 'ws://ec2-52-50-147-81.eu-west-1.compute.amazonaws.com:8080'; var path = 'http://ec2-52-50-147-81.eu-west-1.compute.amazonaws.com/'; var skycons = new Skycons({color: '#e5f7fd'}); /* var lights = { off: ['frontLightOff', 'middleLightOff', 'backLightOff'], on: ['frontLightOn', 'middleLightOn', 'backLightOn'] }; */ var lights = { off: ['frontLightOff', 'backLightOff'], on: ['frontLightOn', 'backLightOn'] }; //Var tempColours = chroma.scale(['blue','yellow','red']).colors(36); //console.log(tempColours); var tempColours = [ { t: 0, r: 80, g: 181, b: 221 }, { t: 1, r: 80, g: 181, b: 221 }, { t: 2, r: 80, g: 181, b: 221 }, { t: 3, r: 80, g: 181, b: 221 }, { t: 4, r: 80, g: 181, b: 221 }, { t: 5, r: 80, g: 181, b: 221 }, { t: 6, r: 78, g: 178, b: 206 }, { t: 7, r: 76, g: 176, b: 190 }, { t: 8, r: 73, g: 173, b: 175 }, { t: 9, r: 72, g: 171, b: 159 }, { t: 10, r: 70, g: 168, b: 142 }, { t: 11, r: 68, g: 166, b: 125 }, { t: 12, r: 66, g: 164, b: 108 }, { t: 13, r: 102, g: 173, b: 94 }, { t: 14, r: 135, g: 190, b: 64 }, { t: 15, r: 179, g: 204, b: 26 }, { t: 16, r: 214, g: 213, b: 28 }, { t: 17, r: 249, g: 202, b: 3 }, { t: 18, r: 246, g: 181, b: 3 }, { t: 19, r: 244, g: 150, b: 26 }, { t: 20, r: 236, g: 110, b: 5 }, { t: 21, r: 234, g: 90, b: 36 }, { t: 22, r: 228, g: 87, b: 43 }, { t: 23, r: 225, g: 74, b: 41 }, { t: 24, r: 224, g: 65, b: 39 }, { t: 25, r: 217, g: 55, b: 43 }, { t: 26, r: 214, g: 49, b: 41 }, { t: 27, r: 209, g: 43, b: 43 }, { t: 28, r: 205, g: 40, b: 47 }, { t: 29, r: 200, g: 36, b: 50 }, { t: 30, r: 195, g: 35, b: 52 }, { t: 31, r: 190, g: 33, b: 56 }, { t: 32, r: 185, g: 32, b: 59 } ]; var prevDate, prevTime, weatherTimer = 0; function updateCalendar(d) { 'use strict'; var $calendar = $('#calendar'); if (d.current !== null && d.current.hasOwnProperty('dtstart')) { var calString = ['
',d.current.summary,'
'].join(''); $('#caltext').html(calString); $calendar.slideDown(); } else { $calendar.slideUp(); } } function updateDateTime() { var now = Date.create(new Date()); var $date = $('#date'); var $time = $('#time'); var curTime = now.format('{24hr}{mm}'); var curDate = now.format('{yyyy}-{MM}-{dd}'); if (prevTime !== curTime) { $time.html(curTime); // UpdateCalendar(); prevTime = curTime; } if (prevDate !== curDate) { $date.html(now.format('{Weekday}
{Month} {dd}
{yyyy}')); prevDate = curDate; } } function turnOnLights(id) { console.log(id); $.post(path + 'api/v1/lighting/on', {light: id}, function() {}); } function turnOffLights(id) { $.post(path + 'api/v1/lighting/off', {light: id}, function() {}); } function turnOnHeating() { $.post(path + 'api/v1/heating/on', {}, function() {}); } function turnOffHeating() { $.post(path + 'api/v1/heating/off', {}, function() {}); } function turnOnProjector() { $.post(path + 'api/v1/projector/on', {}, function() {}); } function turnOffProjector() { $.post(path + 'api/v1/projector/off', {}, function() {}); } $('#projectorOn').on('click', function() { turnOnProjector(); }); $('#projectorOff').on('click', function() { turnOffProjector(); }); $('#heatingOn').on('click', function() { turnOnHeating(); }); $('#heatingOff').on('click', function() { turnOffHeating(); }); $('#frontLightOn').on('click', function() { //turnOnLights(1); turnOnLights('o'); }); $('#middleLightOn').on('click', function() { turnOnLights(2); }); $('#backLightOn').on('click', function() { //turnOnLights(3); turnOnLights('n'); }); $('#frontLightOff').on('click', function() { //turnOffLights('a'); turnOffLights('f'); }); $('#middleLightOff').on('click', function() { turnOffLights('b'); }); $('#backLightOff').on('click', function() { turnOffLights('g'); }); function rgb(cv) { return ['rgb(', cv.r, ',', cv.g, ',', cv.b, ')'].join(''); } function clock() { 'use strict'; updateDateTime(); var now = new Date; var mod = 60000 - (now.getTime() % 60000); setTimeout(function() {clock();},mod + 10); } var SOWEBSOCKET = function() { this.socket = null; this.retry = 0; this.timer = 0; this.previousTemp = 0; this.startWebSocket = function() { 'use strict'; console.log('Starting socket?'); var url = wsUrl; 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.log('whoops'); console.error(e); }; }; this.updateHeating = function(obj) { var $curTemp = $('#curTemp'); if (this.previousTemp !== obj.temp) { $curTemp.text(obj.temp); // Console.log(tempColours[parseInt(obj.temp)]); $curTemp.css('color', rgb(tempColours[parseInt(obj.temp)])); this.previousTemp = obj.temp; } }; this.toggleLighting = function(id) { 'use strict'; var _id, _off = ['f', 'g'], _on = ['o', 'n'], $show, $hide; _id = _off.indexOf(id); console.log(id,_id); if (_id > -1) { // Lights are being turnd off $hide = ['#', lights.off[_id]].join(''); $show = ['#', lights.on[_id]].join(''); } else { _id = _on.indexOf(id); console.log('>', id,_id); $show = ['#', lights.off[_id]].join(''); $hide = ['#', lights.on[_id]].join(''); } $($show).show(); $($hide).hide(); }; this.toggleHeating = function(status) { 'use strict'; var $show, $hide; if (status) { $hide = $('#heatingOn'); $show = $('#heatingOff'); } else { $show = $('#heatingOn'); $hide = $('#heatingOff'); } $($show).show(); $($hide).hide(); }; this.toggleProjector = function(status) { 'use strict'; var $show, $hide; console.log('toggle projector', status); 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 shiftR = (r & 0xff00) >> 8; 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 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); var 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': updateCalendar(d); break; case 'Lighting': console.log('+++ Lighting'); this.toggleLighting(d.device); break; case 'Heating': this.toggleHeating(d.status); break; case 'Projector': this.toggleProjector(d.status); 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.dispatchCommand(command); this.handleData.call(this,command); } }; this.handleWebsocketClose = function() { console.error('WebSocket Connection Closed.'); $('#noSocket').slideDown(); var self = this; this.retry++; if (this.retry > 12) { this.retry = 1; } if (this.retry === 3) { $('#longWait').fadeIn(); } console.log('Waiting ', 5000 * this.retry); this.timer = setTimeout(function() {self.startWebSocket();},5000 * this.retry); }; this.startWebSocket(); }; var get_weather = function() { navigator.geolocation.getCurrentPosition(show_weather,function(e) {alert(e.code + ' / ' + e.message);}); }; bus.bind('displayWeather', function(data) { console.log(data.currently); $('#weatherText').html(parseInt(data.currently.temperature) + '°c '); skycons.add('icon1', data.currently.icon); }); var weatherClock = function() { 'use strict'; get_weather(); var now = new Date; var mod = 1800000 - (now.getTime() % 1800000); weatherTimer = setTimeout(function() {weatherClock();},mod + 10); } var show_weather = function(position) { var latitude = position.coords.latitude; var longitude = position.coords.longitude; // Let's show a map or do something interesting! $.ajax({ type: 'GET', url: 'https://api.forecast.io/forecast/0657dc0d81c037cbc89ca88e383b6bbf/' + latitude.toString() + ',' + longitude.toString() + '?units=uk2', data: '', dataType: 'jsonp', timeout: 10000, context: $('body'), contentType: ('application/json'), headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type' }, success: function(data) { bus.trigger('displayWeather', data); }, error: function(xhr, type) { console.error('ajax error'); console.error(xhr); console.error(type); } }); }; /* (function() { path = 'http://localhost:3000/'; wsUrl = 'ws://localhost:3001'; var iOS = ['iPad', 'iPhone', 'iPod'].indexOf(navigator.platform) >= 0; if (iOS) { $('#iosTaskbar').show(); path = 'http://localhost:3000/'; } clock(); get_weather(); var soWebSocket = new SOWEBSOCKET(); })(); */ document.addEventListener('deviceready', onDeviceReady, false); function onDeviceReady() { console.log('+++ device ready'); var iOS = ['iPad', 'iPhone', 'iPod'].indexOf(navigator.platform) >= 0; if (iOS) { $('#iosTaskbar').show(); // path = 'http://localhost:3000/'; } clock(); weatherClock(); var soWebSocket = new SOWEBSOCKET(); }