From dd233e830d3c4660228852055a78dd44d97e390f Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Sat, 7 Jan 2017 22:33:43 +0000 Subject: [PATCH] graphing --- app/index.html | 19 ++++++++++++ app/js/app.js | 18 ++++++----- app/js/modules/graph.js | 66 +++++++++++++++++++++++++++++++++++++++++ app/js/tempSocket.js | 28 ++++++++++------- gulpfile.js | 2 +- server.js | 9 ++++-- 6 files changed, 121 insertions(+), 21 deletions(-) create mode 100644 app/js/modules/graph.js diff --git a/app/index.html b/app/index.html index c3fa12f..baea4c6 100644 --- a/app/index.html +++ b/app/index.html @@ -10,6 +10,9 @@ + + +
local time
@@ -30,6 +33,21 @@
+ +
+ + + -- + + 0 + + +
+
@@ -46,6 +64,7 @@ + diff --git a/app/js/app.js b/app/js/app.js index bc5c001..3074f7f 100644 --- a/app/js/app.js +++ b/app/js/app.js @@ -10,7 +10,7 @@ */ (function($) { - /* let TodayDataModel = Backbone.Model.extend({ +/* Let TodayDataModel = Backbone.Model.extend({ initialize: function () { this.set('url', '/today/data'); @@ -20,15 +20,19 @@ });*/ - const webSocketModel = new SOCKETMANAGER(); - webSocketModel.turnOn(); +const webSocketModel = new SOCKETMANAGER(); +webSocketModel.turnOn(); - let clock = new Clock({model: new ClockModel()}); +let clock = new Clock({model: new ClockModel()}); - const tempModel = new TempModel(); - let temp = new Temp({model: tempModel}); +const tempModel = new TempModel(); +let temp = new Temp({model: tempModel}); - webSocketModel.setTemp(tempModel); +const graphModel = new GraphModel(); +let graph = new Graph({model: graphModel}); + +webSocketModel.setTemp(tempModel); +webSocketModel.setGraph(graphModel); })(jQuery); diff --git a/app/js/modules/graph.js b/app/js/modules/graph.js new file mode 100644 index 0000000..93b57f2 --- /dev/null +++ b/app/js/modules/graph.js @@ -0,0 +1,66 @@ +/** + * Created by martin on 1/7/17. + */ + +let GraphModel = Backbone.Model.extend({}); + +let Graph = Backbone.View.extend({ + tagName: 'div', + initialize: function() { + _.bindAll(this, 'update'); + this.model.bind('change', this.update); + this.text1ID ='graph-txt1'; + this.lineID ='graph-line'; + + console.log('graph init'); + //This.$indoorTemp = $('#indoorTemp'); + //this.$fan = $('#fan'); + + // this.$fan.hide(); + + }, + update: function() { + console.log('Graph:Update'); + const data = this.model.get('data'); + var _data = data ; + var ceilingLimit; + var ceiling; + + + + ceiling = _data.reduce(function(p, v) { + return (Math.abs(p) > Math.abs(v) ? Math.abs(p) : Math.abs(v)); + }); + + console.dir(ceiling); + + var calcArray = []; + ceilingLimit = (Math.ceil((Math.round(ceiling) + 1) / 1.5) * 1.5); + if (ceilingLimit > 1000) { + ceilingLimit = (Math.ceil((Math.round(ceiling) + 1) / 50) * 50); + } + + var scale = 124 / ceilingLimit; + // Var xstep = (280 - 46) / 100; + var xstep = 2.34; + var startX = 46 + (100 - _data.length) * xstep; + + for (var x = 0;x < _data.length;x++) { + + calcArray.push((startX + (x * xstep)).toFixed(2) + ',' + (136 - ((_data[x]) * scale)).toFixed(2)); + + } + + console.log(calcArray); + + var elm = document.getElementById(this.lineID); + + elm.setAttribute('points',calcArray.join(' ')); + + elm = document.getElementById(this.text1ID); + + elm.textContent = ceilingLimit; + + } + +}); diff --git a/app/js/tempSocket.js b/app/js/tempSocket.js index c23eb5b..b3903dd 100644 --- a/app/js/tempSocket.js +++ b/app/js/tempSocket.js @@ -13,17 +13,17 @@ 'use strict'; -let SOCKETMANAGER = (function () { +let SOCKETMANAGER = (function() { const SocketManager = Backbone.Model.extend({ - initialize: function () { + initialize: function() { _.bindAll(this, 'turnOn', 'turnOff'); this.listeningID = null; this.listening = false; this.webSocket = new WEBSOCKET(this); - this.on('message', function (o) { + this.on('message', function(o) { console.log('On message', this.listening); if (this.listening) { this.checkItem(o); @@ -31,17 +31,17 @@ let SOCKETMANAGER = (function () { }); }, - turnOn: function () { + turnOn: function() { console.log('Socket now listening'); this.listening = true; }, - turnOff: function () { + turnOff: function() { this.listening = false; }, - listenFor: function (id) { + listenFor: function(id) { this.listeningID = this.deviceId.indexOf(id); }, - checkItem: function (item) { + checkItem: function(item) { if (item.hasOwnProperty('id')) { console.log('id:', item.id); @@ -60,14 +60,22 @@ let SOCKETMANAGER = (function () { console.log(item); this.temp.set('data', item.data); } + + if (item.id === 'graph') { + console.log(item); + this.graph.set('data', item.data); + } + } } - , setTemp: function (obj) { + , setTemp: function(obj) { this.temp = obj; - }, - getUpdate: function () { + }, setGraph: function(obj) { + this.graph = obj; + }, + getUpdate: function() { this.webSocket.send('update'); } }); diff --git a/gulpfile.js b/gulpfile.js index 7389aa0..5b33b08 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -28,7 +28,7 @@ var filePath = { var fontOptions = { }; gulp.task('appJS', function() { - return gulp.src(['app/js/websocket.js','app/js/tempSocket.js','app/js/modules/clock.js','app/js/modules/temp.js','app/js/app.js']) + return gulp.src(['app/js/websocket.js','app/js/tempSocket.js','app/js/modules/clock.js','app/js/modules/temp.js','app/js/modules/graph.js','app/js/app.js']) .pipe(stripDebug()) .pipe(jshint('.jshintrc')) .pipe(jshint.reporter('default')) diff --git a/server.js b/server.js index b44b844..593d08a 100644 --- a/server.js +++ b/server.js @@ -35,7 +35,7 @@ let wemoClient = null; let wemo = new Wemo(); -wemo.discover(function(deviceInfo) { +/*wemo.discover(function(deviceInfo) { logger.info('Wemo Device Found: %j', deviceInfo); // Get the client for the found device @@ -48,12 +48,15 @@ wemo.discover(function(deviceInfo) { // Turn the switch on wemoClient.setBinaryState(0); -}); +});*/ busEmitter.on('changeState', function(mode) { logger.info('Changing state..'); - wemoClient.setBinaryState(0); + if (wemoClient !== null) { + wemoClient.setBinaryState(0); + } + });