-
Notifications
You must be signed in to change notification settings - Fork 0
/
findDevices.js
38 lines (35 loc) · 1.01 KB
/
findDevices.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
const debug = require('debug')('gate-addon-http');
module.exports = function findDevices(callback) {
const timeoutId = setTimeout(() => {
debug('Internal new device timeout');
this.emit('internalNewDeviceTimeout');
}, 30000);
this.client.on('message', (topic, message) => {
if (topic !== 'http2mqtt/bridge/log') {
return;
}
debug('On message');
debug(message.toString());
const logMessage = JSON.parse(message.toString());
const messageType = logMessage.type;
if (messageType !== 'device_connected') {
return;
}
const ieeeAddr = logMessage.message.friendly_name;
const newDevice = {
parameters: this.options,
ieeeAddr,
protocol: 'http',
};
clearTimeout(timeoutId);
this.emit('internalNewDevice', newDevice);
});
const topic = 'http2mqtt/configure/set';
const payload = {
...this.options,
model: this.deviceType.model,
id: this.id,
};
this.client.publish(topic, JSON.stringify(payload));
return callback();
};