Skip to content

Commit

Permalink
Scan RF fixes. Fix for “disableAutomaticOff” option on the “learn-cod…
Browse files Browse the repository at this point in the history
…e” accessory
  • Loading branch information
lprhodes committed Apr 27, 2017
1 parent 9e627de commit 05cdd86
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 24 deletions.
18 changes: 4 additions & 14 deletions accessories/learnCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,12 @@ class LearnIRAccessory extends BroadlinkRMAccessory {
const { disableAutomaticOff, scanRF } = config;

const turnOffCallback = () => {

if (disableAutomaticOff) {
learnData.start(this.host, null, turnOffCallback, this.log, true);
} else {
this.learnService.setCharacteristic(Characteristic.On, false);
}
}

const scanRFCallback = (success) => {
if (!success) return turnOffCallback();

learnData.start(this.host, null, turnOffCallback, this.log);
this.learnService.setCharacteristic(Characteristic.On, false);
}

if (scanRF) {
if (on) {
learnRFData.start(this.host, callback, scanRFCallback, this.log);
learnRFData.start(this.host, callback, turnOffCallback, this.log, disableAutomaticOff);
} else {
learnRFData.stop(this.log);

Expand All @@ -44,7 +33,8 @@ class LearnIRAccessory extends BroadlinkRMAccessory {
}

if (on) {
learnData.start(this.host, callback, turnOffCallback, this.log);

learnData.start(this.host, callback, turnOffCallback, this.log, disableAutomaticOff);
} else {
learnData.stop(this.log);

Expand Down
84 changes: 76 additions & 8 deletions helpers/learnRFData.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const getDevice = require('./getDevice');
let closeClient = null;
let timeout = null;
let getDataTimeout = null;
let getDataTimeout2 = null;
let getDataTimeout3 = null;

let currentDevice

Expand All @@ -29,11 +31,13 @@ const start = (host, callback, turnOffCallback, log, disableTimeout) => {
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(`Learn RF (RF learning not supported for device at ${host})`);
if (!device.enterRFSweep) return log(`Scan RF (RF learning not supported for device at ${host})`);

currentDevice = device

let onRawData;
let onRawData2;
let onRawData3;

closeClient = (err) => {
if (timeout) clearTimeout(timeout);
Expand All @@ -42,26 +46,68 @@ const start = (host, callback, turnOffCallback, log, disableTimeout) => {
if (getDataTimeout) clearTimeout(getDataTimeout);
getDataTimeout = null;

device.removeListener('rawData', onRawData);
if (getDataTimeout2) clearTimeout(getDataTimeout2);
getDataTimeout2 = null;

if (getDataTimeout3) clearTimeout(getDataTimeout3);
getDataTimeout2 = null;


device.removeListener('rawRFData', onRawData);
device.removeListener('rawRFData2', onRawData2);
device.removeListener('rawData', onRawData3);
};

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

if (getDataTimeout) clearTimeout(getDataTimeout);
getDataTimeout = null;

log(`Scan RF (found frequency - 1 of 2)`);
log(`[Keep holding that button!]`)

getDataTimeout2 = setTimeout(() => {
getData2(device);
}, 1000);
};

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

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]`);

getDataTimeout3 = setTimeout(() => {
getData3(device);
}, 1000);
};

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

const hex = message.toString('hex');
log(`Scan RF (found frequency - now press the RF button multiple times with a pause between them)`);
log(`Scan RF (complete)`);
log(`[Hex Code: ${hex}]`);

device.cancelRFSweep();

closeClient();
closeClient = null;

turnOffCallback(true);
turnOffCallback();
};

device.on('rawRFData', onRawData);
device.on('rawRFData2', onRawData2);
device.on('rawData', onRawData3);

device.enterRFSweep();
log(`Scan RF (scanning - keep holding the button that sends the RF frequency)`);
log(`Scan RF (scanning)`);
log(`[Hold down the button that sends the RF frequency]`);

if (callback) callback();

Expand All @@ -71,18 +117,18 @@ const start = (host, callback, turnOffCallback, log, disableTimeout) => {

if (disableTimeout) return;

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

setTimeout(() => {
log('Scan RF (stopped - 15s timeout)');
log('Scan RF (stopped - 20s timeout)');
closeClient();
closeClient = null;

turnOffCallback();
}, 1000);
}, 15 * 1000); // 15s
}, 20 * 1000); // 20s
}

const getData = (device) => {
Expand All @@ -96,4 +142,26 @@ const getData = (device) => {
}, 1000);
}

const getData2 = (device) => {
if (getDataTimeout2) clearTimeout(getDataTimeout2);
if (!closeClient) return;

device.checkRFData2();

getDataTimeout2 = setTimeout(() => {
getData2(device);
}, 1000);
}

const getData3 = (device) => {
if (getDataTimeout3) clearTimeout(getDataTimeout3);
if (!closeClient) return;

device.checkData()

getDataTimeout3 = setTimeout(() => {
getData3(device);
}, 1000);
}

module.exports = { start, stop }
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homebridge-broadlink-rm",
"version": "2.1.0",
"version": "2.1.1",
"description": "Broadlink RM plugin (including the mini and pro) for homebridge: https://github.com/nfarina/homebridge",
"license": "ISC",
"keywords": [
Expand All @@ -21,7 +21,7 @@
"url": "[email protected]:lprhodes/homebridge-broadlink-rm.git"
},
"dependencies": {
"broadlinkjs": "git+https://github.com/lprhodes/broadlinkjs.git#4a90e87",
"broadlinkjs": "git+https://github.com/lprhodes/broadlinkjs.git#a9180ad",
"node-persist": "^2.0.10",
"semver": "^5.3.0"
}
Expand Down

0 comments on commit 05cdd86

Please sign in to comment.