Skip to content

Commit

Permalink
Manual cancelling of OR code learning now works correctly. Improved I…
Browse files Browse the repository at this point in the history
…R/RF learn logging
  • Loading branch information
lprhodes committed Mar 25, 2018
1 parent 94b2345 commit 96cf50f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
8 changes: 4 additions & 4 deletions helpers/getDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ const delayForDuration = require('./delayForDuration')

const pingFrequency = 5000;

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

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

device.state = 'inactive';
} else if (active && device.state !== 'active') {
Expand Down Expand Up @@ -52,7 +52,7 @@ const discoverDevices = (automatic = true, log, debug, deviceDiscoveryTimeout =
log(`\x1b[35m[INFO]\x1b[0m Discovered Broadlink RM device at ${device.host.address} (${device.host.macAddress})`)
addDevice(device)

startPing(device)
startPing(device, log)
})
}

Expand All @@ -74,7 +74,7 @@ const getDevice = ({ host, log, learnOnly }) => {
const device = { host: { address: host } };
manualDevices[host] = device;

startPing(device)
startPing(device, log)
}
} else { // use the first one of no host is provided
const hosts = Object.keys(discoveredDevices);
Expand Down
18 changes: 11 additions & 7 deletions helpers/learnData.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ const stop = (log) => {
closeClient();
closeClient = null;

log(`Learn Code (stopped)`);
log(`\x1b[35m[INFO]\x1b[0m Learn Code (stopped)`);
}

const start = (host, callback, turnOffCallback, log, disableTimeout) => {
stop()

// Get the Broadlink device
const device = getDevice({ host, log, learnOnly: true });
if (!device) return;
if (!device.enterLearning) return log(`Learn Code (IR learning not supported for device at ${host})`);
if (!device) {
return log(`\x1b[31m[ERROR]\x1b[0m Learn Code (Couldn't learn code, device not found)`);
}

if (!device.enterLearning) return log(`\x1b[31m[ERROR]\x1b[0m Learn Code (IR learning not supported for device at ${host})`);

let onRawData;

Expand All @@ -32,14 +35,15 @@ const start = (host, callback, turnOffCallback, log, disableTimeout) => {
getDataTimeout = null;

device.removeListener('rawData', onRawData);
device.cancelLearn();
};

onRawData = (message) => {
if (!closeClient) return;

const hex = message.toString('hex');
log(`Learn Code (learned hex code: ${hex})`);
log(`Learn Code (complete)`);
log(`\x1b[35m[RESULT]\x1b[0m Learn Code (learned hex code: ${hex})`);
log(`\x1b[35m[INFO]\x1b[0m Learn Code (complete)`);

closeClient();

Expand All @@ -61,8 +65,8 @@ const start = (host, callback, turnOffCallback, log, disableTimeout) => {

// Timeout the client after 10 seconds
timeout = setTimeout(() => {
log('Learn Code (stopped - 10s timeout)');
if (device.cancelRFSweep) device.cancelRFSweep();
log('\x1b[35m[INFO]\x1b[0m Learn Code (stopped - 10s timeout)');
device.cancelLearn();

closeClient();

Expand Down
35 changes: 19 additions & 16 deletions helpers/learnRFData.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ const stop = (log, device) => {

isClosingClient = true;

if (currentDevice) currentDevice.cancelRFSweep();
if (currentDevice) currentDevice.cancelLearn();

setTimeout(() => {
closeClient();
closeClient = null;
isClosingClient = false;

if (log) log(`Scan RF (stopped)`);
if (log) log(`\x1b[35m[INFO]\x1b[0m Scan RF (stopped)`);
}, 500)
}

Expand All @@ -31,9 +31,12 @@ const start = (host, callback, turnOffCallback, log, disableTimeout) => {

// Get the Broadlink device
const device = getDevice({ host, log, learnOnly: true })
if (!device) return;
if (!device.enterLearning) return log(`Learn Code (IR learning not supported for device at ${host})`);
if (!device.enterRFSweep) return log(`Scan RF (RF learning not supported for device at ${host})`);
if (!device) {
return log(`\x1b[35m[INFO]\x1b[0m Learn Code (Couldn't learn code, device not found)`);
}

if (!device.enterLearning) return log(`\x1b[31m[ERROR]\x1b[0m Learn Code (IR/RF learning not supported for device at ${host})`);
if (!device.enterRFSweep) return log(`\x1b[31m[ERROR]\x1b[0m Scan RF (RF learning not supported for device (${device.type}) at ${host})`);

currentDevice = device

Expand Down Expand Up @@ -66,8 +69,8 @@ const start = (host, callback, turnOffCallback, log, disableTimeout) => {
if (getDataTimeout) clearTimeout(getDataTimeout);
getDataTimeout = null;

log(`Scan RF (found frequency - 1 of 2)`);
log(`[Keep holding that button!]`)
log(`\x1b[35m[INFO]\x1b[0m Scan RF (found frequency - 1 of 2)`);
log(`\x1b[35m[ACTION]\x1b[0m Keep holding that button!`)

getDataTimeout2 = setTimeout(() => {
getData2(device);
Expand All @@ -80,8 +83,8 @@ const start = (host, callback, turnOffCallback, log, disableTimeout) => {
if (getDataTimeout2) clearTimeout(getDataTimeout2);
getDataTimeout = null;

log(`Scan RF (found frequency - 2 of 2)`)
log(`[Press the RF button multiple times with a pause between them]`);
log(`\x1b[35m[INFO]\x1b[0m Scan RF (found frequency - 2 of 2)`)
log(`\x1b[35m[ACTION]\x1b[0m Press the RF button multiple times with a pause between them.`);

getDataTimeout3 = setTimeout(() => {
getData3(device);
Expand All @@ -92,10 +95,10 @@ const start = (host, callback, turnOffCallback, log, disableTimeout) => {
if (!closeClient) return;

const hex = message.toString('hex');
log(`Scan RF (complete)`);
log(`[Hex Code: ${hex}]`);
log(`\x1b[35m[INFO]\x1b[0m Scan RF (complete)`);
log(`\x1b[35m[RESULT]\x1b[0m Hex Code: ${hex}`);

device.cancelRFSweep();
device.cancelLearn();

closeClient();

Expand All @@ -107,8 +110,8 @@ const start = (host, callback, turnOffCallback, log, disableTimeout) => {
device.on('rawData', onRawData3);

device.enterRFSweep();
log(`Scan RF (scanning)`);
log(`[Hold down the button that sends the RF frequency]`);
log(`\x1b[35m[INFO]\x1b[0m Scan RF (scanning)`);
log(`\x1b[35m[ACTION]\x1b[0m Hold down the button that sends the RF frequency.`);

if (callback) callback();

Expand All @@ -120,10 +123,10 @@ const start = (host, callback, turnOffCallback, log, disableTimeout) => {

// Timeout the client after 20 seconds
timeout = setTimeout(() => {
device.cancelRFSweep()
device.cancelLearn()

setTimeout(() => {
log('Scan RF (stopped - 20s timeout)');
log('\x1b[35m[INFO]\x1b[0m Scan RF (stopped - 20s timeout)');
closeClient();

turnOffCallback();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"url": "git@github.com:lprhodes/homebridge-broadlink-rm.git"
},
"dependencies": {
"broadlinkjs-rm": "^0.5.5",
"broadlinkjs-rm": "^0.5.6",
"chai": "^4.1.2",
"github-version-checker": "^1.2.0",
"homebridge-platform-helper": "1.0.8",
Expand Down

0 comments on commit 96cf50f

Please sign in to comment.