diff options
| author | James Barnett <noreply@jamesbarnett.xyz> | 2018-10-07 16:09:42 +0100 |
|---|---|---|
| committer | James Barnett <noreply@jamesbarnett.xyz> | 2018-10-07 16:09:42 +0100 |
| commit | 8cecaff1ae7240902249676b7d24fadca03fc5a9 (patch) | |
| tree | a381cd45ffa4577dd1818eb192c5ebaf53ae0b7d /services | |
| parent | ffadea4389bcf777b049d75a0f3bae5155b7d584 (diff) | |
| download | tplink-energy-monitor-8cecaff1ae7240902249676b7d24fadca03fc5a9.tar.xz tplink-energy-monitor-8cecaff1ae7240902249676b7d24fadca03fc5a9.zip | |
Basic logging functionality. Logs every 60s.
Needs log rotation/management to avoid huge logfiles.
Diffstat (limited to 'services')
| -rw-r--r-- | services/data-broadcaster.js | 5 | ||||
| -rw-r--r-- | services/data-logger.js | 59 | ||||
| -rw-r--r-- | services/device-manager.js | 3 |
3 files changed, 67 insertions, 0 deletions
diff --git a/services/data-broadcaster.js b/services/data-broadcaster.js index 60c2368..4905eda 100644 --- a/services/data-broadcaster.js +++ b/services/data-broadcaster.js @@ -16,6 +16,10 @@ function broadcastPowerStateUpdate(deviceId, data) { broadcast(generatePayload('powerState', deviceId, data)); } +function broadcastNewLogEntry(deviceId, data) { + broadcast(generatePayload('newLogEntry', deviceId, data)); +} + function broadcast(payload) { app.getWsClients().forEach(client => { client.send(payload); @@ -39,5 +43,6 @@ module.exports = { broadcastDailyUsageUpdate: broadcastDailyUsageUpdate, broadcastMonthlyUsageUpdate: broadcastMonthlyUsageUpdate, broadcastPowerStateUpdate: broadcastPowerStateUpdate, + broadcastNewLogEntry: broadcastNewLogEntry, generatePayload: generatePayload } diff --git a/services/data-logger.js b/services/data-logger.js new file mode 100644 index 0000000..0948221 --- /dev/null +++ b/services/data-logger.js @@ -0,0 +1,59 @@ +const fs = require('fs'); +const dataBroadcaster = require('./data-broadcaster'); + +function startLogging(device) { + setInterval(() => { log(device); }, 60000); + console.log('Logging started for ' + device.alias + ' [' + device.deviceId + ']'); +} + +function log(device) { + + device.emeter.getRealtime().then(response => { + + let logEntry = { + timestamp: Date.now(), + power: (('power_mw' in response) ? (response.power_mw / 1000) : response.power) + } + + // TODO only log up to a max number of entries + + fs.writeFile(device.deviceId + '-log.json', JSON.stringify(logEntry) + '\n', { flag: 'a' }, (err) => { + if(err) { + console.warn('Error writing log entry for ' + device.alias + ' [' + device.deviceId + ']', err); + } + else { + dataBroadcaster.broadcastNewLogEntry(device.deviceId, logEntry); + } + + }); + + }); + +} + +function getAllEntries(deviceId, callback) { + fs.readFile(deviceId + '-log.json', 'utf8', (err, data) => { + if(err) { + console.warn('Error reading usage log ' + deviceId + '-log.json', err); + return; + } + else { + let logLines = data.split(/\r?\n/); + let logEntries = []; + + logLines.forEach(line => { + if(line.length > 0) { + logEntries.push(JSON.parse(line)) + } + }); + + callback(logEntries); + } + }); +} + +module.exports = { + startLogging: startLogging, + log: log, + getAllEntries: getAllEntries +}
\ No newline at end of file diff --git a/services/device-manager.js b/services/device-manager.js index e4d18f3..1e17f75 100644 --- a/services/device-manager.js +++ b/services/device-manager.js @@ -1,4 +1,5 @@ const { Client } = require('tplink-smarthome-api'); +const dataLogger = require('./data-logger'); const client = new Client(); var devices = []; @@ -9,6 +10,8 @@ client.startDiscovery({ }).on('plug-new', plug => { console.log('Found device: ' + plug.alias + ' [' + plug.deviceId + ']'); devices.push(plug); + + dataLogger.startLogging(plug); }); module.exports.getDevice = function(deviceId) { |