aboutsummaryrefslogtreecommitdiff
path: root/routes/energy-usage.js
diff options
context:
space:
mode:
authorJames Barnett <noreply@jamesbarnett.xyz>2018-03-31 19:56:55 +0100
committerJames Barnett <noreply@jamesbarnett.xyz>2018-03-31 19:56:55 +0100
commitf53f1d433b5d458ff0b8a5893e8127a8be3630f7 (patch)
treea7145c7947a9ab6a77e49077c1aeb777fd81288e /routes/energy-usage.js
parentf5e0f6bd1b12e347387ff4cefcfc54d9d7dcc348 (diff)
downloadtplink-energy-monitor-f53f1d433b5d458ff0b8a5893e8127a8be3630f7.tar.xz
tplink-energy-monitor-f53f1d433b5d458ff0b8a5893e8127a8be3630f7.zip
Add daily kWH usage total for last 30 days
Diffstat (limited to 'routes/energy-usage.js')
-rw-r--r--routes/energy-usage.js71
1 files changed, 69 insertions, 2 deletions
diff --git a/routes/energy-usage.js b/routes/energy-usage.js
index 847248d..5706c75 100644
--- a/routes/energy-usage.js
+++ b/routes/energy-usage.js
@@ -2,16 +2,17 @@ const express = require('express');
const router = express.Router();
const deviceManager = require('../services/device-manager');
+const moment = require('moment');
router.get('/:deviceId/realtime', function(req, res, next) {
-
+
let deviceId = req.params.deviceId;
let realtimeUsage = {};
// TODO - cache results with a short TTL so we don't hammer the plug if multiple clients are requesting data
deviceManager.getDevice(deviceId).emeter.getRealtime().then(response => {
- // Voltage seems to be reported as its peak to peak voltage, not RMS.
+ // Voltage seems to be reported as its peak to peak value, not RMS.
// Show the RMS value since thats what would you expect to see.
// i.e. 220v not 310v (in the U.K)
response.voltage = response.voltage / Math.sqrt(2);
@@ -21,4 +22,70 @@ router.get('/:deviceId/realtime', function(req, res, next) {
});
+router.get('/:deviceId/day-stats', function(req, res, next) {
+
+ let deviceId = req.params.deviceId;
+
+ // Get last x days
+ let totalDaysRequired = 30; // TODO currently only works for up to 2 months spans
+ let currentMoment = moment();
+ let previousMoment = moment().subtract(totalDaysRequired, 'days');
+
+ // Month + 1 as the API months are index 1 based.
+ deviceManager.getDevice(deviceId).emeter.getDayStats(currentMoment.year(), currentMoment.month() +1).then(currentPeriodStats => {
+
+ // Check if we also need the previous month to meet the required total number of samples
+ if(currentMoment.month() !== previousMoment.month()) {
+
+ // Get previous month. This currently wont work if the previousMoment is more than 1 month before the currentMoment (see above)
+ deviceManager.getDevice(deviceId).emeter.getDayStats(previousMoment.year(), previousMoment.month() +1).then(previousPeriodStats => {
+
+ let currentMonthStats = fillMissingDays(currentPeriodStats, currentMoment);
+ let previousMonthStats = fillMissingDays(previousPeriodStats, previousMoment);
+ let combinedStats = previousMonthStats.concat(currentMonthStats);
+
+ res.json(trimDayStatResults(combinedStats, totalDaysRequired));
+
+ });
+ }
+ else {
+ let dayStats = fillMissingDays(currentPeriodStats, currentMoment);
+
+ res.json(trimDayStatResults(dayStats, totalDaysRequired));
+ }
+
+ });
+
+});
+
+function fillMissingDays(sparseDayStats, statsMoment) {
+ let denseDayStats = [];
+
+ // Fill in any missing days
+ let totalDays = statsMoment.date();
+ Array.from({length: totalDays}, (x,i) => i + 1).forEach(d => {
+
+ let stat = sparseDayStats.day_list.find(i => i.day === d);
+
+ if(stat === undefined) {
+ denseDayStats.push({
+ year: statsMoment.year(),
+ month: statsMoment.month() +1,
+ day: d,
+ energy: 0
+ })
+ }
+ else {
+ denseDayStats.push(stat);
+ }
+
+ });
+
+ return denseDayStats;
+}
+
+function trimDayStatResults(stats, maxSamples) {
+ return stats.splice(stats.length - maxSamples, stats.length);
+}
+
module.exports = router; \ No newline at end of file