Skip to content

Commit

Permalink
Improvements to how ping’s work to improve performance and device re-…
Browse files Browse the repository at this point in the history
…discovery
  • Loading branch information
Luke Rhodes committed Jan 1, 2020
1 parent 7ce4537 commit b9e68a4
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 33 deletions.
10 changes: 6 additions & 4 deletions accessories/outlet.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const ping = require('ping');
const ServiceManagerTypes = require('../helpers/serviceManagerTypes');

const delayForDuration = require('../helpers/delayForDuration')
Expand All @@ -8,16 +7,19 @@ class OutletAccessory extends SwitchAccessory {

pingCallback (active) {
const { config, state, serviceManager } = this;
const newState = active ? true : false;

// Only update Homkit if the switch state haven changed.
if (previousState === newState) return

if (config.pingIPAddressStateOnly) {
state.outletInUse = active ? true : false;
state.outletInUse = newState;
serviceManager.refreshCharacteristicUI(Characteristic.OutletInUse)

return
}

const value = active ? true : false;
serviceManager.setCharacteristic(Characteristic.OutletInUse, value);
serviceManager.setCharacteristic(Characteristic.OutletInUse, newState);
}

setOutletInUse (value, callback) {
Expand Down
25 changes: 20 additions & 5 deletions accessories/switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class SwitchAccessory extends BroadlinkRMAccessory {

setDefaults () {
const { config } = this;
config.pingFrequency = config.pingFrequency || 1;
config.pingFrequency = config.pingFrequency || 2;
config.pingFrequency = Math.max(config.pingFrequency, 2);

config.offDuration = config.offDuration || 60;
config.onDuration = config.onDuration || 60;
Expand Down Expand Up @@ -69,17 +70,31 @@ class SwitchAccessory extends BroadlinkRMAccessory {
}

pingCallback (active) {
const { config, state, serviceManager } = this;
let { debug, config, log, name, state, serviceManager } = this;
debug = true

const previousState = state.switchState
const newState = active ? true : false;

// Only update Homkit if the switch state haven changed.
const hasStateChanged = (previousState === newState)
if (debug) log(`${name} pingCallback: state ${hasStateChanged ? 'not changed, ignoring' : 'changed'} (device ${newState ? 'active' : 'inactive'})`);

if (hasStateChanged) return

if (config.pingIPAddressStateOnly) {
state.switchState = active ? true : false;
if (debug) log(`${name} pingCallback: UI updated only`);

state.switchState = newState

serviceManager.refreshCharacteristicUI(Characteristic.On);

return;
}

const value = active ? true : false;
serviceManager.setCharacteristic(Characteristic.On, value);
if (debug) log(`${name} pingCallback: UI updated and command sent`);

serviceManager.setCharacteristic(Characteristic.On, newState);
}

async setSwitchState (hexData) {
Expand Down
28 changes: 15 additions & 13 deletions helpers/getDevice.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
const ping = require('ping');
const broadlink = require('./broadlink')
const delayForDuration = require('./delayForDuration')

const pingFrequency = 5000;
const ping = require('net-ping').createSession({
retries: 3,
timeout: 1000
});

const pingFrequency = 5000; // 5s

const startPing = (device, log) => {
device.state = 'unknown';

setInterval(() => {
try {
ping.sys.probe(device.host.address, (active) => {
if (!active && device.state === 'active') {
log(`Broadlink RM device at ${device.host.address} (${device.host.macAddress || ''}) is no longer reachable.`);
ping.pingHost(device.host.address, (error, target) => {
if (error && device.state === 'active') {
log(`Broadlink RM device at ${device.host.address} (${device.host.macAddress || ''}) is no longer reachable.`);

device.state = 'inactive';
} else if (active && device.state !== 'active') {
if (device.state === 'inactive') log(`Broadlink RM device at ${device.host.address} (${device.host.macAddress || ''}) has been re-discovered.`);
device.state = 'inactive';
} else if (!error && device.state !== 'active') {
if (device.state === 'inactive') log(`Broadlink RM device at ${device.host.address} (${device.host.macAddress || ''}) has been re-discovered.`);

device.state = 'active';
}
})
} catch (err) {}
device.state = 'active';
}
})
}, pingFrequency);
}

Expand Down
15 changes: 7 additions & 8 deletions helpers/ping.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const ping = require('ping');
const ping = require('net-ping').createSession({
retries: 0,
timeout: 1000
});

const pingIPAddress = (ipAddress, interval, callback) => {
setInterval(() => {
try {
ping.sys.probe(ipAddress, (isActive) => {
callback(isActive)
})
} catch (err) {
callback(false)
}
ping.pingHost(ipAddress, (error) => {
callback(!error)
})
}, interval * 1000);
}

Expand Down
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
"url": "git@github.com:lprhodes/homebridge-broadlink-rm.git"
},
"dependencies": {
"chai": "^4.2.0",
"broadlinkjs-rm": "^0.7.5",
"chai": "^4.2.0",
"find-key": "^2.0.1",
"github-version-checker": "^1.2.0",
"homebridge-platform-helper": "1.2.0",
"net-ping": "^1.2.3",
"ping": "^0.2.2",
"uuid": "^3.3.3"
},
Expand Down
2 changes: 0 additions & 2 deletions test/helpers/fakePing.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const ping = require('ping');

const pingIPAddress = function (ipAddress, interval, callback) {
performPing(this.isActive, callback)

Expand Down

0 comments on commit b9e68a4

Please sign in to comment.