var bus = {}; MicroEvent.mixin(bus); var path = ''; var skycons = new Skycons({"color": "#e5f7fd"}); var lights = { off: ['frontLightOff', 'middleLightOff', 'backLightOff'], on: ['frontLightOn', 'middleLightOn', '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; function updateCalendar(d) { 'use strict'; var $calendar = $('#calendar'); if (d.current !== null && d.current.hasOwnProperty('dtstart')) { var calString = `
${d.current.summary}
`; $('#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) { var _id = parseInt(id) - 1; var $lightsOff = ['#', lights.off[_id]].join(''); var $lightsOn = ['#', lights.on[_id]].join(''); $.post(path + 'api/v1/lighting/on', {light: id}, function(res) { $($lightsOff).show(); $($lightsOn).hide(); }); } function turnOffLights(id) { var _id = ['a', 'b', 'c'].indexOf(id); var $lightsOff = ['#', lights.off[_id]].join(''); var $lightsOn = ['#', lights.on[_id]].join(''); $.post(path + 'api/v1/lighting/off', {light: id}, function(res) { $($lightsOff).hide(); $($lightsOn).show(); }); } function turnOnHeating() { var $heatingOn = $('#heatingOn'); var $heatingOff = $('#heatingOff'); $.post(path + 'api/v1/heating/on', {}, function(repsonse) { $($heatingOff).show(); $($heatingOn).hide(); }); } function turnOffHeating() { var $heatingOn = $('#heatingOn'); var $heatingOff = $('#heatingOff'); $.post(path + 'api/v1/heating/off', {}, function(repsonse) { $($heatingOn).show(); $($heatingOff).hide(); }); } function turnOnProjector() { var $projectorOn = $('#projectorOn'); var $projectorOff = $('#projectorOff'); $.post(path + 'api/v1/projector/on', {}, function(repsonse) { $($projectorOff).show(); $($projectorOn).hide(); }); } function turnOffProjector() { var $projectorOn = $('#projectorOn'); var $projectorOff = $('#projectorOff'); $.post(path + 'api/v1/projector/off', {}, function(repsonse) { $($projectorOff).hide(); $($projectorOn).show(); }); } $('#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); }); $('#middleLightOn').on('click', function() { turnOnLights(2); }); $('#backLightOn').on('click', function() { turnOnLights(3); }); $('#frontLightOff').on('click', function() { turnOffLights('a'); }); $('#middleLightOff').on('click', function() { turnOffLights('b'); }); $('#backLightOff').on('click', function() { turnOffLights('c'); }); 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 = 'ws://localhost:8080'; var url = 'ws://ec2-52-50-147-81.eu-west-1.compute.amazonaws.com/:8080'; 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.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); $('#lightW').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); $('#projW').css('background-color', w); }; this.handleData = function(d) { 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; 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); $('#weatherText').html(parseInt(data.currently.temperature) + '°c '); skycons.add("icon1", data.currently.icon); }); 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() { var iOS = ['iPad', 'iPhone', 'iPod'].indexOf(navigator.platform) >= 0; if (iOS) { $('#iosTaskbar').show(); path = 'http://localhost:3000/'; } clock(); get_weather(); var soWebSocket = new SOWEBSOCKET(); })();