forked from arteck/ioBroker.zigbee2mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
357 lines (323 loc) · 13.9 KB
/
main.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
'use strict';
/*
* Created with @iobroker/create-adapter v2.2.1
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const core = require('@iobroker/adapter-core');
const mqtt = require('mqtt');
const utils = require('./lib/utils');
const schedule = require('node-schedule');
const checkConfig = require('./lib/check').checkConfig;
const adapterInfo = require('./lib/messages').adapterInfo;
const zigbee2mqttInfo = require('./lib/messages').zigbee2mqttInfo;
const Z2mController = require('./lib/z2mController').Z2mController;
const DeviceController = require('./lib/deviceController').DeviceController;
const StatesController = require('./lib/statesController').StatesController;
const WebsocketController = require('./lib/websocketController').WebsocketController;
const MqttServerController = require('./lib/mqttServerController').MqttServerController;
let mqttClient;
// eslint-disable-next-line prefer-const
let deviceCache = [];
// eslint-disable-next-line prefer-const
let groupCache = [];
const createCache = {};
const logCustomizations = { debugDevices: '', logfilter: [] };
let showInfo = true;
let statesController;
let deviceController;
let z2mController;
let websocketController;
let mqttServerController;
class Zigbee2mqtt extends core.Adapter {
constructor(options) {
super({
...options,
name: 'zigbee2mqtt',
});
this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('unload', this.onUnload.bind(this));
}
async onReady() {
statesController = new StatesController(this, deviceCache, groupCache, logCustomizations, createCache);
deviceController = new DeviceController(this, deviceCache, groupCache, this.config, logCustomizations, createCache);
z2mController = new Z2mController(this, deviceCache, groupCache, logCustomizations);
// Initialize your adapter here
adapterInfo(this.config, this.log);
this.setState('info.connection', false, true);
const debugDevicesState = await this.getStateAsync('info.debugmessages');
if (debugDevicesState && debugDevicesState.val) {
logCustomizations.debugDevices = String(debugDevicesState.val);
}
const logfilterState = await this.getStateAsync('info.logfilter');
if (logfilterState && logfilterState.val) {
// @ts-ignore
logCustomizations.logfilter = String(logfilterState.val).split(';').filter(x => x); // filter removes empty strings here
}
if (this.config.coordinatorCheck == true) {
try {
schedule.scheduleJob('coordinatorCheck', this.config.coordinatorCheckCron, () => this.onStateChange('manual_trigger._.info.coordinator_check', { ack: false }));
} catch (e) {
this.log.error(e);
}
}
// MQTT
if (['exmqtt', 'intmqtt'].includes(this.config.connectionType)) {
// External MQTT-Server
if (this.config.connectionType == 'exmqtt') {
if (this.config.externalMqttServerIP == '') {
this.log.warn('Please configure the External MQTT-Server connection!');
return;
}
// MQTT connection settings
const mqttClientOptions = { clientId: `ioBroker.zigbee2mqtt_${Math.random().toString(16).slice(2, 8)}`, clean: true, reconnectPeriod: 500 };
// Set external mqtt credentials
if (this.config.externalMqttServerCredentials == true) {
mqttClientOptions.username = this.config.externalMqttServerUsername;
mqttClientOptions.password = this.config.externalMqttServerPassword;
}
// Init connection
mqttClient = mqtt.connect(`mqtt://${this.config.externalMqttServerIP}:${this.config.externalMqttServerPort}`, mqttClientOptions);
}
// Internal MQTT-Server
else {
mqttServerController = new MqttServerController(this);
await mqttServerController.createMQTTServer();
await this.delay(1500);
mqttClient = mqtt.connect(`mqtt://${this.config.mqttServerIPBind}:${this.config.mqttServerPort}`, { clientId: `ioBroker.zigbee2mqtt_${Math.random().toString(16).slice(2, 8)}`, clean: true, reconnectPeriod: 500 });
}
// MQTT Client
mqttClient.on('connect', () => {
this.log.info(`Connect to Zigbee2MQTT over ${this.config.connectionType == 'exmqtt' ? 'external mqtt' : 'internal mqtt'} connection.`);
});
mqttClient.subscribe('zigbee2mqtt/#');
mqttClient.on('message', (topic, payload) => {
const newMessage = `{"payload":${payload.toString() == '' ? '"null"' : payload.toString()},"topic":"${topic.slice(topic.search('/') + 1)}"}`;
this.messageParse(newMessage);
});
}
// Websocket
else if (this.config.connectionType == 'ws') {
if (this.config.wsServerIP == '') {
this.log.warn('Please configure the Websoket connection!');
return;
}
// Dummy MQTT-Server
if (this.config.dummyMqtt == true) {
mqttServerController = new MqttServerController(this);
await mqttServerController.createDummyMQTTServer();
await this.delay(1500);
}
this.startWebsocket();
}
}
startWebsocket() {
websocketController = new WebsocketController(this);
const wsClient = websocketController.initWsClient();
if (wsClient) {
wsClient.on('open', () => {
this.log.info('Connect to Zigbee2MQTT over websocket connection.');
});
wsClient.on('message', (message) => {
this.messageParse(message);
});
wsClient.on('close', async () => {
this.setStateChanged('info.connection', false, true);
await statesController.setAllAvailableToFalse();
deviceCache = [];
groupCache = [];
});
}
}
async messageParse(message) {
// If the MQTT output type is set to attribute_and_json, the non-valid JSON must be checked here.
if (utils.isJson(message) == false) {
return;
}
const messageObj = JSON.parse(message);
switch (messageObj.topic) {
case 'bridge/config':
break;
case 'bridge/info':
if (showInfo) {
zigbee2mqttInfo(messageObj.payload, this.log);
checkConfig(messageObj.payload.config, this.log);
showInfo = false;
}
break;
case 'bridge/state':
if (messageObj.payload.state != 'online') {
statesController.setAllAvailableToFalse();
}
this.setStateChanged('info.connection', messageObj.payload.state == 'online', true);
break;
case 'bridge/devices':
await deviceController.createDeviceDefinitions(messageObj.payload);
await deviceController.createOrUpdateDevices();
await deviceController.checkAndProgressDeviceRemove();
await statesController.subscribeWritableStates();
statesController.processQueue();
break;
case 'bridge/groups':
await deviceController.createGroupDefinitions(messageObj.payload);
await deviceController.createOrUpdateDevices();
await statesController.subscribeWritableStates();
statesController.processQueue();
break;
case 'bridge/event':
break;
case 'bridge/response/coordinator_check':
deviceController.processCoordinatorCheck(messageObj.payload);
break;
case 'bridge/response/device/remove':
break;
case 'bridge/response/device/options':
break;
case 'bridge/response/permit_join':
break;
case 'bridge/extensions':
break;
case 'bridge/logging':
if (this.config.proxyZ2MLogs == true) {
z2mController.proxyZ2MLogs(messageObj);
}
break;
case 'bridge/response/device/configure':
break;
case 'bridge/response/device/rename':
await deviceController.renameDeviceInCache(messageObj);
await deviceController.createOrUpdateDevices();
statesController.processQueue();
break;
case 'bridge/response/networkmap':
break;
case 'bridge/response/touchlink/scan':
break;
case 'bridge/response/touchlink/identify':
break;
case 'bridge/response/touchlink/factory_reset':
break;
default:
{
// is the payload an availability status?
if (messageObj.topic.endsWith('/availability')) {
// If an availability message for an old device ID comes with a payload of NULL, this is the indicator that a device has been unnamed.
if (messageObj.payload == 'null') {
return;
}
// is it a viable payload?
if (messageObj.payload && messageObj.payload.state) {
// {"payload":{"state":"online"},"topic":"FL.Licht.Links/availability"} ----> {"payload":{"available":true},"topic":"FL.Licht.Links"}
const newMessage = {
payload: { available: messageObj.payload.state == 'online' },
topic: messageObj.topic.replace('/availability', '')
};
statesController.processDeviceMessage(newMessage);
}
// States
} else {
// With the MQTT output type attribute_and_json, primitive payloads arrive here that must be discarded.
if (utils.isObject(messageObj.payload) == false) {
return;
}
// If MQTT is used, I have to filter the self-sent 'set' commands.
if (messageObj.topic.endsWith('/set')) {
return;
}
statesController.processDeviceMessage(messageObj);
}
}
break;
}
}
async onUnload(callback) {
// Close MQTT connections
if (['exmqtt', 'intmqtt'].includes(this.config.connectionType)) {
if (mqttClient && !mqttClient.closed) {
try {
if (mqttClient) {
mqttClient.end();
}
} catch (e) {
this.log.error(e);
}
}
}
// Internal or Dummy MQTT-Server
if (this.config.connectionType == 'intmqtt' || this.config.dummyMqtt == true) {
try {
if (mqttServerController) {
mqttServerController.closeServer();
}
} catch (e) {
this.log.error(e);
}
}
// Websocket
else if (this.config.connectionType == 'ws') {
try {
if (websocketController) {
websocketController.closeConnection();
}
} catch (e) {
this.log.error(e);
}
}
// Set all device available states of false
try {
if (statesController) {
await statesController.setAllAvailableToFalse();
}
} catch (e) {
this.log.error(e);
}
// Clear all websocket timers
try {
if (websocketController) {
await websocketController.allTimerClear();
}
} catch (e) {
this.log.error(e);
}
// Clear all state timers
try {
if (statesController) {
await statesController.allTimerClear();
}
} catch (e) {
this.log.error(e);
}
callback();
}
async onStateChange(id, state) {
if (state && state.ack == false) {
if (id.endsWith('info.debugmessages')) {
logCustomizations.debugDevices = state.val;
this.setState(id, state.val, true);
return;
}
if (id.endsWith('info.logfilter')) {
logCustomizations.logfilter = state.val.split(';').filter(x => x); // filter removes empty strings here
this.setState(id, state.val, true);
return;
}
const message = await z2mController.createZ2MMessage(id, state) || { topic: '', payload: '' };
if (['exmqtt', 'intmqtt'].includes(this.config.connectionType)) {
mqttClient.publish(`zigbee2mqtt/${message.topic}`, JSON.stringify(message.payload));
} else if (this.config.connectionType == 'ws') {
websocketController.send(JSON.stringify(message));
}
}
}
}
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<core.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Zigbee2mqtt(options);
} else {
// otherwise start the instance directly
new Zigbee2mqtt();
}