-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
147 lines (132 loc) Β· 4.18 KB
/
index.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
const _ = require('lodash');
const debug = require('debug')('gate-addon-zigbee');
const EventEmitter = require('events');
const mqtt = require('mqtt');
const async = require('async');
const findDevices = require('./findDevices');
class GateAddOnZigbee extends EventEmitter {
constructor(id, type, allDevices, options = {}) {
super();
this.id = id;
this.data = {};
const zigbeeId = type.protocols[0];
this.knownDevices = allDevices[zigbeeId] || [];
this.deviceType = type;
this.client = mqtt.connect('mqtt://eclipse-mosquitto', {
username: process.env.MQTT_USERNAME,
password: process.env.MQTT_PASSWORD,
});
this.touchlink = options.touchlink;
}
setKnownDevices(knownDevices) {
this.knownDevices = knownDevices;
}
subscribe(callback) {
return this.client.subscribe('zigbee2mqtt/bridge/log', (err) => {
if (err) {
return callback(err);
}
return callback();
});
}
factoryReset(callback) {
if (this.touchlink) {
return this.client.publish('zigbee2mqtt/bridge/config/touchlink/factory_reset', JSON.stringify({}), (err) => {
if (err) {
return callback(err);
}
setTimeout(() => {
return callback();
}, 5000);
});
}
return callback();
}
init() {
this.on('internalNewDeviceTimeout', () => {
const payload = {
status: {
eventType: 'not_paired',
},
deviceId: this.id,
};
this.emit('timeoutDiscovering', payload);
});
this.on('internalNewDevice', (newDevice) => {
this.client.removeAllListeners('message');
this.client.unsubscribe('zigbee2mqtt/bridge/log');
this.emit('newDevice', newDevice);
this.start(newDevice);
})
this.client.on('connect', () => {
async.waterfall([
this.subscribe.bind(this),
findDevices.bind(this),
this.factoryReset.bind(this),
]);
});
}
start(device) {
const { ieeeAddr } = device;
const topic = `zigbee2mqtt/${ieeeAddr}`;
this.client.on('message', (topic, message) => {
const parsed = JSON.parse(message.toString());
this.emit('data', parsed);
});
if (this.deviceType.type === 'sensor') {
this.client.subscribe(topic);
}
}
stop() { }
control(message, action) {
const zigbeeDevice = this.knownDevices.filter((zigbeeDeviceFilter) => {
return zigbeeDeviceFilter.id === this.id;
})[0];
const friendlyName = _.get(zigbeeDevice, 'ieeeAddr');
const topic = `zigbee2mqtt/${friendlyName}/${action}`;
if (action === 'get') {
const responseTopic = `zigbee2mqtt/${friendlyName}`;
this.client.once('message', (topic, responseMessage) => {
const parsed = JSON.parse(responseMessage.toString());
this.client.unsubscribe(responseTopic, (err) => { });
const response = {
payload: parsed,
requestId: message.requestId,
deviceId: message.deviceId,
projectId: process.env.PROJECT_ID
};
this.emit('status', response);
});
this.client.subscribe(responseTopic, (err) => { });
}
const payloadDevice = (action === 'get') ? message.payload : message;
this.client.publish(topic, JSON.stringify(payloadDevice));
}
remove() {
const zigbeeDevice = this.knownDevices.filter((zigbeeDeviceFilter) => {
return zigbeeDeviceFilter.id === this.id;
})[0];
const friendlyName = _.get(zigbeeDevice, 'ieeeAddr');
this.subscribe((err) => {
this.client.on('message', (topic, message) => {
if (topic !== 'zigbee2mqtt/bridge/log') {
return;
}
const logMessage = JSON.parse(message.toString());
const messageType = logMessage.type;
if (messageType !== 'device_force_removed') {
return;
}
const friendlyNameRemoved = logMessage.message;
if (friendlyNameRemoved === friendlyName) {
this.emit('deviceRemoved', this.id);
this.removeAllListeners();
this.client.end();
}
});
});
const topic = 'zigbee2mqtt/bridge/config/force_remove';
this.client.publish(topic, friendlyName);
}
}
module.exports = GateAddOnZigbee;