From c1ea6a65576b940f0b1cb5ecdd4cf3fb5fd330bd Mon Sep 17 00:00:00 2001 From: Wild Stray Date: Thu, 2 Aug 2018 00:06:02 +0000 Subject: [PATCH 1/2] Updated dash.js with bug fix for "millis" Values could be returned byt the plug in mV/mA/mW instead of V/A/W. --- public/javascripts/dash.js | 56 ++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/public/javascripts/dash.js b/public/javascripts/dash.js index e52992f..9335229 100644 --- a/public/javascripts/dash.js +++ b/public/javascripts/dash.js @@ -5,23 +5,23 @@ var dash = { realtimeTrendChart: null, realtimeTrendLastSample: 0, - dailyUsageChart: null, - monthlyUsageChart: null, - - init: function(deviceId) { - this.deviceId = deviceId; - - if (this.deviceId) { - $('.' + deviceId).addClass('active'); - } - - this.initRealtimeGauge(); - this.initRealtimeTrendChart(); - this.initDailyUsageChart(); - this.initMonthlyUsageChart(); - - this.initWsConnection(); - }, + dailyUsageChart: null, + monthlyUsageChart: null, + + init: function(deviceId) { + this.deviceId = deviceId; + + if (this.deviceId) { + $('.' + deviceId).addClass('active'); + } + + this.initRealtimeGauge(); + this.initRealtimeTrendChart(); + this.initDailyUsageChart(); + this.initMonthlyUsageChart(); + + this.initWsConnection(); + }, initWsConnection: function() { var wsUri = 'ws://' + window.location.host + '/ws' @@ -212,9 +212,9 @@ var dash = { refreshRealtimeDisplay: function(realtime) { - var power = Math.round(realtime.power); - var current = realtime.current.toFixed(2); - var voltage = Math.round(realtime.voltage); + var power = Math.round((realtime.power_mw) ? (realtime.power_mw/1000) : realtime.power); + var current = ((realtime.current_ma) ? (realtime.current_ma/1000) : realtime.current).toFixed(2); + var voltage = Math.round((realtime.voltage_mv) ? (realtime.voltage_mv/1000) : realtime.voltage); this.realtimeGauge.set(power); // might switch to Vue.js if this gets tedious @@ -240,7 +240,7 @@ var dash = { dash.dailyUsageChart.data.labels.push(day.format('MMM D')); dash.dailyUsageChart.data.datasets.forEach(function(dataset) { - dataset.data.push(entry.energy); + dataset.data.push((entry.energy_wh) ? (entry.energy_wh/1000) : entry.energy); }); }); @@ -254,10 +254,12 @@ var dash = { return d.day === moment().date() && d.month === (moment().month()+1) && d.year === moment().year() }); - $("#total-day").text(dailyTotal.energy.toFixed(2)); + var energy = (dailyTotal.energy_wh) ? (dailyTotal.energy_wh/1000) : dailyTotal.energy + $("#total-day").text(energy.toFixed(2)); - var total = usageData.reduce(function(t, d) {return t + d.energy}, 0); + var total = usageData.reduce(function(t, d) {return t + ((d.energy_wh) ? (d.energy_wh/1000) : d.energy)}, 0); var avg = total/usageData.length; + console $("#avg-day").text(avg.toFixed(2)); @@ -277,7 +279,7 @@ var dash = { dash.monthlyUsageChart.data.labels.push(month.format('MMM')); dash.monthlyUsageChart.data.datasets.forEach(function(dataset) { - dataset.data.push(entry.energy); + dataset.data.push((entry.energy_wh) ? (entry.energy_wh/1000) : entry.energy); }); }); @@ -290,10 +292,10 @@ var dash = { var monthlyTotal = usageData.find(function(m) { return m.month === (moment().month()+1) && m.year === moment().year() }); + var energy = (monthlyTotal.energy_wh) ? (monthlyTotal.energy_wh/1000) : monthlyTotal.energy + $("#total-month").text(energy.toFixed(2)); - $("#total-month").text(monthlyTotal.energy.toFixed(2)); - - var total = usageData.reduce(function(t, m) {return t + m.energy}, 0); + var total = usageData.reduce(function(t, m) {return t + ((m.energy_wh) ? (m.energy_wh/1000) : m.energy)}, 0); var avg = total/usageData.length; $("#avg-month").text(avg.toFixed(2)); From 4e9950e4926a32cedbf7324a2b4d14c8af53c0c9 Mon Sep 17 00:00:00 2001 From: Wild Stray Date: Thu, 2 Aug 2018 00:21:02 +0000 Subject: [PATCH 2/2] Update dash.js --- public/javascripts/dash.js | 52 +++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/public/javascripts/dash.js b/public/javascripts/dash.js index 9335229..489cd21 100644 --- a/public/javascripts/dash.js +++ b/public/javascripts/dash.js @@ -5,23 +5,23 @@ var dash = { realtimeTrendChart: null, realtimeTrendLastSample: 0, - dailyUsageChart: null, - monthlyUsageChart: null, - - init: function(deviceId) { - this.deviceId = deviceId; - - if (this.deviceId) { - $('.' + deviceId).addClass('active'); - } - - this.initRealtimeGauge(); - this.initRealtimeTrendChart(); - this.initDailyUsageChart(); - this.initMonthlyUsageChart(); - - this.initWsConnection(); - }, + dailyUsageChart: null, + monthlyUsageChart: null, + + init: function(deviceId) { + this.deviceId = deviceId; + + if (this.deviceId) { + $('.' + deviceId).addClass('active'); + } + + this.initRealtimeGauge(); + this.initRealtimeTrendChart(); + this.initDailyUsageChart(); + this.initMonthlyUsageChart(); + + this.initWsConnection(); + }, initWsConnection: function() { var wsUri = 'ws://' + window.location.host + '/ws' @@ -212,9 +212,9 @@ var dash = { refreshRealtimeDisplay: function(realtime) { - var power = Math.round((realtime.power_mw) ? (realtime.power_mw/1000) : realtime.power); - var current = ((realtime.current_ma) ? (realtime.current_ma/1000) : realtime.current).toFixed(2); - var voltage = Math.round((realtime.voltage_mv) ? (realtime.voltage_mv/1000) : realtime.voltage); + var power = Math.round(('power_mw' in realtime) ? (realtime.power_mw/1000) : realtime.power); + var current = (('current_ma' in realtime) ? (realtime.current_ma/1000) : realtime.current).toFixed(2); + var voltage = Math.round(('voltage_mv' in realtime) ? (realtime.voltage_mv/1000) : realtime.voltage); this.realtimeGauge.set(power); // might switch to Vue.js if this gets tedious @@ -240,7 +240,7 @@ var dash = { dash.dailyUsageChart.data.labels.push(day.format('MMM D')); dash.dailyUsageChart.data.datasets.forEach(function(dataset) { - dataset.data.push((entry.energy_wh) ? (entry.energy_wh/1000) : entry.energy); + dataset.data.push(('energy_wh' in entry) ? (entry.energy_wh/1000) : entry.energy); }); }); @@ -254,10 +254,10 @@ var dash = { return d.day === moment().date() && d.month === (moment().month()+1) && d.year === moment().year() }); - var energy = (dailyTotal.energy_wh) ? (dailyTotal.energy_wh/1000) : dailyTotal.energy + var energy = ('energy_wh' in dailyTotal) ? (dailyTotal.energy_wh/1000) : dailyTotal.energy $("#total-day").text(energy.toFixed(2)); - var total = usageData.reduce(function(t, d) {return t + ((d.energy_wh) ? (d.energy_wh/1000) : d.energy)}, 0); + var total = usageData.reduce(function(t, d) {return t + (('energy_wh' in d) ? (d.energy_wh/1000) : d.energy)}, 0); var avg = total/usageData.length; console @@ -279,7 +279,7 @@ var dash = { dash.monthlyUsageChart.data.labels.push(month.format('MMM')); dash.monthlyUsageChart.data.datasets.forEach(function(dataset) { - dataset.data.push((entry.energy_wh) ? (entry.energy_wh/1000) : entry.energy); + dataset.data.push(('energy_wh' in entry) ? (entry.energy_wh/1000) : entry.energy); }); }); @@ -292,10 +292,10 @@ var dash = { var monthlyTotal = usageData.find(function(m) { return m.month === (moment().month()+1) && m.year === moment().year() }); - var energy = (monthlyTotal.energy_wh) ? (monthlyTotal.energy_wh/1000) : monthlyTotal.energy + var energy = ('energy_wh' in monthlyTotal) ? (monthlyTotal.energy_wh/1000) : monthlyTotal.energy $("#total-month").text(energy.toFixed(2)); - var total = usageData.reduce(function(t, m) {return t + ((m.energy_wh) ? (m.energy_wh/1000) : m.energy)}, 0); + var total = usageData.reduce(function(t, m) {return t + (('energy_wh' in m) ? (m.energy_wh/1000) : m.energy)}, 0); var avg = total/usageData.length; $("#avg-month").text(avg.toFixed(2));