diff options
| author | James Barnett <noreply@jamesbarnett.xyz> | 2018-11-18 13:12:38 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-11-18 13:12:38 +0000 |
| commit | 5877729bdda9ca20f164d9dff634f6bf3aa54204 (patch) | |
| tree | 6401b44495f96411cb14b3b33e5c7ae269929159 /services | |
| parent | fa6a6669d8add3b76ea17dec49bd1d0ae1a7c341 (diff) | |
| parent | c47447f9fca976b9272197085ef35d10e281de6e (diff) | |
| download | tplink-energy-monitor-5877729bdda9ca20f164d9dff634f6bf3aa54204.tar.xz tplink-energy-monitor-5877729bdda9ca20f164d9dff634f6bf3aa54204.zip | |
Merge pull request #12 from jamesbarnett91/bugfixesv0.7
Fix voltage reading and exclude unsupported devices from search
Diffstat (limited to 'services')
| -rw-r--r-- | services/data-fetcher.js | 22 | ||||
| -rw-r--r-- | services/device-manager.js | 17 |
2 files changed, 29 insertions, 10 deletions
diff --git a/services/data-fetcher.js b/services/data-fetcher.js index 7ef3594..b552952 100644 --- a/services/data-fetcher.js +++ b/services/data-fetcher.js @@ -26,11 +26,7 @@ function fetchRealtimeUsage() { let deviceId = device.deviceId; device.emeter.getRealtime().then(response => { - // 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); - + response.voltage = normaliseVoltage(response, device); updateCache(cachedRealtimeUsageData, deviceId, response); dataBroadcaster.broadcastRealtimeUsageUpdate(deviceId, response); @@ -266,6 +262,22 @@ function updateCache(cache, deviceId, data) { } } +/* +* On older firmware versions (not sure exactly which since its not documented anywhere) +* voltage seems to be reported as its peak to peak value, not RMS. +* So we show the RMS value since thats what would you expect to see. +* i.e. 220v not 310v (in the U.K). +* This is applied for all 1.0.x firmware versions. +*/ +function normaliseVoltage(response, device) { + if (device.softwareVersion.startsWith("1.0")) { + return response.voltage / Math.sqrt(2); + } + else { + return response.voltage; + } +} + module.exports.getCachedData = function(deviceId) { return { diff --git a/services/device-manager.js b/services/device-manager.js index 1e17f75..451734e 100644 --- a/services/device-manager.js +++ b/services/device-manager.js @@ -7,12 +7,19 @@ var devices = []; client.startDiscovery({ deviceTypes: ['plug'], discoveryTimeout: 20000 - }).on('plug-new', plug => { - console.log('Found device: ' + plug.alias + ' [' + plug.deviceId + ']'); - devices.push(plug); + }).on('plug-new', registerPlug); - dataLogger.startLogging(plug); -}); +function registerPlug(plug) { + + if (plug.supportsEmeter) { + console.log('Found device with energy monitor support: ' + plug.alias + ' [' + plug.deviceId + ']'); + devices.push(plug); + dataLogger.startLogging(plug); + } else { + console.log('Skipping device: ' + plug.alias + ' [' + plug.deviceId + ']. Energy monitoring not supported.'); + } + +} module.exports.getDevice = function(deviceId) { |