diff --git a/accessories/learnCode.js b/accessories/learnCode.js index 732a12ee..f558193e 100644 --- a/accessories/learnCode.js +++ b/accessories/learnCode.js @@ -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); @@ -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); diff --git a/helpers/learnRFData.js b/helpers/learnRFData.js index 5710b830..747bdb81 100644 --- a/helpers/learnRFData.js +++ b/helpers/learnRFData.js @@ -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 @@ -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); @@ -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(); @@ -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) => { @@ -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 } diff --git a/package.json b/package.json index 9c8ad263..48b05fff 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -21,7 +21,7 @@ "url": "git@github.com: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" }