Skip to content

Commit

Permalink
Merge pull request #12 from jamesbarnett91/bugfixes
Browse files Browse the repository at this point in the history
Fix voltage reading and exclude unsupported devices from search
  • Loading branch information
jamesbarnett91 authored Nov 18, 2018
2 parents 0b07580 + 01f4993 commit 82d0398
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
22 changes: 17 additions & 5 deletions services/data-fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 12 additions & 5 deletions services/device-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down

0 comments on commit 82d0398

Please sign in to comment.