/
broadlink-adapter.js
254 lines (203 loc) · 6.39 KB
/
broadlink-adapter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
*
* BroadlinkAdapter - Adapter which manages ir devices with Broadlink.
*
*/
'use strict';
const EventEmitter = require('events').EventEmitter;
const Queue = require('promise-queue');
const BroadlinkJS = require('broadlinkjs-rm');
const IRDevice = require('./ir-device');
var DEBUG = false;
const {Adapter, Database} = require('gateway-addon');
class BroadlinkAdapter extends Adapter {
constructor(addonManager, manifest, broadlink) {
super(addonManager, 'broadlink-' + broadlink.host.macAddress, manifest.name);
this.mac = broadlink.host.macAddress;
this._broadlink = broadlink;
this._queue = new Queue(1, Infinity);
this.manager.addAdapter(this);
const config = manifest.moziot.config;
this.event = new EventEmitter();
this._loadConfig(config);
}
_loadConfig(config) {
if (config && config.devices && Array.isArray(config.devices)) {
const devices = config.devices;
devices.forEach(function(device) {
this._createDeviceFromConfig(device);
}.bind(this));
}
}
learnCode() {
return new Promise((resolve, reject) =>{
this.event.emit('stopLearning', new Error('learnCode called while learning'));
const onRawData = (message) => {
const hex = message.toString('hex');
this.event.emit('stopLearning');
resolve(hex);
};
const ping = setInterval(() => {
this._broadlink.checkData();
}, 1000);
const timeout = setTimeout(() => {
this.event.emit('stopLearning', new Error('learnCode timeout'));
}, 60000);
const cleanup = (err) => {
this._broadlink.removeListener('rawData', onRawData);
clearInterval(ping);
clearTimeout(timeout);
if (err) {
this._broadlink.cancelLearn();
reject(err);
}
};
this.event.once('stopLearning', (err)=>{
cleanup(err);
});
this._broadlink.on('rawData', onRawData);
this._broadlink.enterLearning();
});
}
sendData(hexData) {
if (!hexData) {
console.log('Missing params, sendData failed');
return;
}
const hexDataBuffer = new Buffer(hexData, 'hex');
this._queue.add(() => {
this._broadlink.sendData(hexDataBuffer);
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 200);
});
});
}
waitSendSequenceResolve(device, property) {
this._queue.add(() => {
return new Promise((resolve) => {
var deferredSet = property.deferredSet;
if (deferredSet) {
property.deferredSet = null;
deferredSet.resolve(property.value);
}
device.notifyPropertyChanged(property);
resolve();
});
});
}
sendSequence(device, property, sequence) {
for (const i in sequence) {
const data = sequence[i];
this.sendData(data);
}
this.waitSendSequenceResolve(device, property);
}
_createDeviceFromConfig(config) {
// ToDo check config
if (!config || config.mac != this.mac) return;
this.handleDeviceAdded(new IRDevice(this, config));
}
removeThing(device) {
if (DEBUG) console.log('removeThing(' + device.id + ')');
this.handleDeviceRemoved(device);
}
cancelRemoveThing(_node) {
// Nothing to do.
}
}
class BroadlinkManager extends Adapter {
constructor(addonManager, manifest) {
super(addonManager, 'broadlink-adapter', manifest.name);
this._destaddr = manifest.address || '255.255.255.255';
this._port1 = manifest.port1 || 0;
this._port2 = manifest.port2 || 0;
this._isPairing = false;
this._broadlinkFinder = new BroadlinkJS();
this.event = new EventEmitter();
this._discoveredDevices = {};
this._broadlinkFinder.on('deviceReady', this._onDeviceReady.bind(this));
this.startPairing(10);
this.manager.addAdapter(this);
}
_onDeviceReady(device) {
const macAddressParts = device.mac.toString('hex').match(/[\s\S]{1,2}/g) || [];
const macAddress = macAddressParts.join(':');
device.host.macAddress = macAddress;
if (this._discoveredDevices[device.host.address] ||
this._discoveredDevices[device.host.macAddress]) return;
if (DEBUG) console.log(
`Discovered Broadlink RM device at ${device.host.address} (${device.host.macAddress})`);
this._discoveredDevices[device.host.address] = device;
this._discoveredDevices[device.host.macAddress] = device;
this.event.emit('deviceReady', device);
}
_discoverDevices() {
if (this._isPairing == false) {
return;
}
if (this._timeoutSeconds <= 0) {
this._isPairing = false;
return;
}
this._broadlinkFinder.discover(this._port1, this._port2, this._destaddr);
setTimeout(() => {
this._timeoutSeconds -= 5;
this._discoverDevices();
}, 5 * 1000);
}
startPairing(timeoutSeconds) {
this._timeoutSeconds = timeoutSeconds;
if (this._isPairing == true) {
return;
}
this._isPairing = true;
if (DEBUG) console.log('Pairing mode started, timeout =', timeoutSeconds);
this._discoverDevices();
}
cancelPairing() {
if (DEBUG) console.log('Cancelling pairing mode');
this.isPairing = false;
}
getDevice(host, learnOnly) {
let device;
const hosts = Object.keys(this._discoveredDevices);
if (host) {
device = this._discoveredDevices[host];
} else if (learnOnly) {
for (let i = 0; i < hosts.length; i++) {
const currentDevice = this._discoveredDevices[hosts[i]];
if (currentDevice.enterLearning) {
device = currentDevice;
break;
}
}
} else {
device = this._discoveredDevices[hosts[0]];
}
if (!device) {
if (DEBUG) console.log(`Attempting to discover RM devices for 5s`);
this._discoverDevices(5);
}
return device;
}
}
function loadBroadlinkAdapters(addonManager, manifest, _errorCallback) {
const db = new Database(manifest.name);
const broadlinkManager = new BroadlinkManager(addonManager, manifest);
const broadlinks = [];
broadlinkManager.event.on('deviceReady', async (device) => {
try {
await db.open();
const config = await db.loadConfig();
broadlinks.push(device.host.macAddress);
config.broadlinks = broadlinks;
await db.saveConfig(config);
} catch (err) {
console.error(err);
}
new BroadlinkAdapter(addonManager, manifest, device);
});
}
module.exports = loadBroadlinkAdapters;