-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathcontroller.ts
741 lines (628 loc) · 29.6 KB
/
controller.ts
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
import events from 'events';
import Database from './database';
import {TsType as AdapterTsType, Adapter, Events as AdapterEvents} from '../adapter';
import {Entity, Device} from './model';
import {ZclFrameConverter} from './helpers';
import * as Events from './events';
import {KeyValue, DeviceType, GreenPowerEvents, GreenPowerDeviceJoinedPayload} from './tstype';
import Debug from "debug";
import fs from 'fs';
import {Utils as ZclUtils, FrameControl} from '../zcl';
import Touchlink from './touchlink';
import GreenPower from './greenPower';
import {BackupUtils} from "../utils";
import assert from 'assert';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import mixin from 'mixin-deep';
import Group from './model/group';
import {LoggerStub} from "./logger-stub";
interface Options {
network: AdapterTsType.NetworkOptions;
serialPort: AdapterTsType.SerialPortOptions;
databasePath: string;
databaseBackupPath: string;
backupPath: string;
adapter: AdapterTsType.AdapterOptions;
/**
* This lambda can be used by an application to explictly reject or accept an incoming device.
* When false is returned zigbee-herdsman will not start the interview process and immidiately
* try to remove the device from the network.
*/
acceptJoiningDeviceHandler: (ieeeAddr: string) => Promise<boolean>;
}
async function catcho(func: () => Promise<void>, errorMessage: string): Promise<void> {
try {
await func();
} catch (error) {
debug.error(`${errorMessage}: ${error}`);
}
}
const DefaultOptions: Options = {
network: {
networkKeyDistribute: false,
networkKey: [0x01, 0x03, 0x05, 0x07, 0x09, 0x0B, 0x0D, 0x0F, 0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0D],
panID: 0x1a62,
extendedPanID: [0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD],
channelList: [11],
},
serialPort: {},
databasePath: null,
databaseBackupPath: null,
backupPath: null,
adapter: {disableLED: false},
acceptJoiningDeviceHandler: null,
};
const debug = {
error: Debug('zigbee-herdsman:controller:error'),
log: Debug('zigbee-herdsman:controller:log'),
};
/**
* @noInheritDoc
*/
class Controller extends events.EventEmitter {
private options: Options;
private database: Database;
private adapter: Adapter;
private greenPower: GreenPower;
// eslint-disable-next-line
private permitJoinNetworkClosedTimer: any;
// eslint-disable-next-line
private permitJoinTimeoutTimer: any;
private permitJoinTimeout: number;
// eslint-disable-next-line
private backupTimer: any;
// eslint-disable-next-line
private databaseSaveTimer: any;
private touchlink: Touchlink;
private stopping: boolean;
private networkParametersCached: AdapterTsType.NetworkParameters;
private logger?: LoggerStub;
/**
* Create a controller
*
* To auto detect the port provide `null` for `options.serialPort.path`
*/
public constructor(options: Options, logger?: LoggerStub) {
super();
this.stopping = false;
this.options = mixin(JSON.parse(JSON.stringify(DefaultOptions)), options);
this.logger = logger;
// Validate options
for (const channel of this.options.network.channelList) {
if (channel < 11 || channel > 26) {
throw new Error(`'${channel}' is an invalid channel, use a channel between 11 - 26.`);
}
}
if (!Array.isArray(this.options.network.networkKey) || this.options.network.networkKey.length !== 16) {
throw new Error(`Network key must be 16 digits long, got ${this.options.network.networkKey.length}.`);
}
if (!Array.isArray(this.options.network.extendedPanID) || this.options.network.extendedPanID.length !== 8) {
throw new Error(`ExtendedPanID must be 8 digits long, got ${this.options.network.extendedPanID.length}.`);
}
if (this.options.network.panID >= 0xFFFF || this.options.network.panID <= 0) {
throw new Error(`PanID must have a value of 0x0001 (1) - 0xFFFE (65534), ` +
`got ${this.options.network.panID}.`);
}
}
/**
* Start the Herdsman controller
*/
public async start(): Promise<AdapterTsType.StartResult> {
// Database (create end inject)
this.database = Database.open(this.options.databasePath);
Entity.injectDatabase(this.database);
// Adapter (create and inject)
this.adapter = await Adapter.create(this.options.network,
this.options.serialPort, this.options.backupPath, this.options.adapter, this.logger);
debug.log(`Starting with options '${JSON.stringify(this.options)}'`);
const startResult = await this.adapter.start();
debug.log(`Started with result '${startResult}'`);
Entity.injectAdapter(this.adapter);
// log injection
debug.log(`Injected database: ${this.database != null}, adapter: ${this.adapter != null}`);
this.greenPower = new GreenPower(this.adapter);
this.greenPower.on(GreenPowerEvents.deviceJoined, this.onDeviceJoinedGreenPower.bind(this));
// Register adapter events
this.adapter.on(AdapterEvents.Events.deviceJoined, this.onDeviceJoined.bind(this));
this.adapter.on(AdapterEvents.Events.zclData, (data) => this.onZclOrRawData('zcl', data));
this.adapter.on(AdapterEvents.Events.rawData, (data) => this.onZclOrRawData('raw', data));
this.adapter.on(AdapterEvents.Events.disconnected, this.onAdapterDisconnected.bind(this));
this.adapter.on(AdapterEvents.Events.deviceAnnounce, this.onDeviceAnnounce.bind(this));
this.adapter.on(AdapterEvents.Events.deviceLeave, this.onDeviceLeave.bind(this));
this.adapter.on(AdapterEvents.Events.networkAddress, this.onNetworkAddress.bind(this));
if (startResult === 'reset') {
if (this.options.databaseBackupPath && fs.existsSync(this.options.databasePath)) {
fs.copyFileSync(this.options.databasePath, this.options.databaseBackupPath);
}
debug.log('Clearing database...');
for (const group of Group.all()) {
group.removeFromDatabase();
}
for (const device of Device.all()) {
device.removeFromDatabase();
}
}
if (startResult === 'reset' || (this.options.backupPath && !fs.existsSync(this.options.backupPath))) {
await this.backup();
}
// Add coordinator to the database if it is not there yet.
const coordinator = await this.adapter.getCoordinator();
if (Device.byType('Coordinator').length === 0) {
debug.log('No coordinator in database, querying...');
Device.create(
'Coordinator', coordinator.ieeeAddr, coordinator.networkAddress, coordinator.manufacturerID,
undefined, undefined, undefined, true, coordinator.endpoints
);
}
// Update coordinator ieeeAddr if changed, can happen due to e.g. reflashing
const databaseCoordinator = Device.byType('Coordinator')[0];
if (databaseCoordinator.ieeeAddr !== coordinator.ieeeAddr) {
debug.log(`Coordinator address changed, updating to '${coordinator.ieeeAddr}'`);
databaseCoordinator.changeIeeeAddress(coordinator.ieeeAddr);
}
// Set backup timer to 1 day.
this.backupTimer = setInterval(() => this.backup(), 86400000);
// Set database save timer to 1 hour.
this.databaseSaveTimer = setInterval(() => this.databaseSave(), 3600000);
this.touchlink = new Touchlink(this.adapter);
return startResult;
}
public async touchlinkIdentify(ieeeAddr: string, channel: number): Promise<void> {
await this.touchlink.identify(ieeeAddr, channel);
}
public async touchlinkScan(): Promise<{ieeeAddr: string; channel: number}[]> {
return this.touchlink.scan();
}
public async touchlinkFactoryReset(ieeeAddr: string, channel: number): Promise<boolean> {
return this.touchlink.factoryReset(ieeeAddr, channel);
}
public async touchlinkFactoryResetFirst(): Promise<boolean> {
return this.touchlink.factoryResetFirst();
}
public async addInstallCode(installCode: string): Promise<void> {
const aqaraMatch = installCode.match(/^G\$M:.+\$A:(.+)\$I:(.+)$/);
let ieeeAddr, key;
if (aqaraMatch) {
ieeeAddr = aqaraMatch[1];
key = aqaraMatch[2];
} else {
assert(installCode.length === 95 || installCode.length === 91,
`Unsupported install code, got ${installCode.length} chars, expected 95 or 91`);
const keyStart = installCode.length - (installCode.length === 95 ? 36 : 32);
ieeeAddr = installCode.substring(keyStart - 19, keyStart - 3);
key = installCode.substring(keyStart, installCode.length);
}
ieeeAddr = `0x${ieeeAddr}`;
key = Buffer.from(key.match(/.{1,2}/g).map(d => parseInt(d, 16)));
await this.adapter.addInstallCode(ieeeAddr, key);
}
public async permitJoin(permit: boolean, device?: Device, time?: number): Promise<void> {
await this.permitJoinInternal(permit, 'manual', device, time);
}
public async permitJoinInternal(
permit: boolean, reason: 'manual' | 'timer_expired', device?: Device, time?: number): Promise<void> {
clearInterval(this.permitJoinNetworkClosedTimer);
clearInterval(this.permitJoinTimeoutTimer);
this.permitJoinNetworkClosedTimer = null;
this.permitJoinTimeoutTimer = null;
this.permitJoinTimeout = undefined;
if (permit) {
await this.adapter.permitJoin(254, !device ? null : device.networkAddress);
await this.greenPower.permitJoin(254, !device ? null : device.networkAddress);
// Zigbee 3 networks automatically close after max 255 seconds, keep network open.
this.permitJoinNetworkClosedTimer = setInterval(async (): Promise<void> => {
await this.adapter.permitJoin(254, !device ? null : device.networkAddress);
await this.greenPower.permitJoin(254, !device ? null : device.networkAddress);
}, 200 * 1000);
if (typeof time === 'number') {
this.permitJoinTimeout = time;
this.permitJoinTimeoutTimer = setInterval(async (): Promise<void> => {
this.permitJoinTimeout--;
if (this.permitJoinTimeout <= 0) {
await this.permitJoinInternal(false, 'timer_expired');
} else {
const data: Events.PermitJoinChangedPayload =
{permitted: true, timeout: this.permitJoinTimeout, reason};
this.emit(Events.Events.permitJoinChanged, data);
}
}, 1000);
}
const data: Events.PermitJoinChangedPayload = {permitted: true, reason, timeout: this.permitJoinTimeout};
this.emit(Events.Events.permitJoinChanged, data);
} else {
debug.log('Disable joining');
await this.greenPower.permitJoin(0, null);
await this.adapter.permitJoin(0, null);
const data: Events.PermitJoinChangedPayload = {permitted: false, reason, timeout: this.permitJoinTimeout};
this.emit(Events.Events.permitJoinChanged, data);
}
}
public getPermitJoin(): boolean {
return this.permitJoinNetworkClosedTimer != null;
}
public getPermitJoinTimeout(): number {
return this.permitJoinTimeout;
}
public isStopping(): boolean {
return this.stopping;
}
public async stop(): Promise<void> {
this.stopping = true;
this.databaseSave();
// Unregister adapter events
this.adapter.removeAllListeners(AdapterEvents.Events.deviceJoined);
this.adapter.removeAllListeners(AdapterEvents.Events.zclData);
this.adapter.removeAllListeners(AdapterEvents.Events.rawData);
this.adapter.removeAllListeners(AdapterEvents.Events.disconnected);
this.adapter.removeAllListeners(AdapterEvents.Events.deviceAnnounce);
this.adapter.removeAllListeners(AdapterEvents.Events.deviceLeave);
await catcho(() => this.permitJoinInternal(false, 'manual'), "Failed to disable join on stop");
clearInterval(this.backupTimer);
clearInterval(this.databaseSaveTimer);
await this.backup();
await this.adapter.stop();
}
private databaseSave(): void {
for (const device of Device.all()) {
device.save(false);
}
for (const group of Group.all()) {
group.save(false);
}
this.database.write();
}
public async backup(): Promise<void> {
this.databaseSave();
if (this.options.backupPath && await this.adapter.supportsBackup()) {
debug.log('Creating coordinator backup');
const backup = await this.adapter.backup();
const unifiedBackup = await BackupUtils.toUnifiedBackup(backup);
const tmpBackupPath = this.options.backupPath + '.tmp';
fs.writeFileSync(tmpBackupPath, JSON.stringify(unifiedBackup, null, 2));
fs.renameSync(tmpBackupPath, this.options.backupPath);
debug.log(`Wrote coordinator backup to '${this.options.backupPath}'`);
}
}
public async reset(type: 'soft' | 'hard'): Promise<void> {
await this.adapter.reset(type);
}
public async getCoordinatorVersion(): Promise<AdapterTsType.CoordinatorVersion> {
return this.adapter.getCoordinatorVersion();
}
public async getNetworkParameters(): Promise<AdapterTsType.NetworkParameters> {
// Cache network parameters as they don't change anymore after start.
if (!this.networkParametersCached) {
this.networkParametersCached = await this.adapter.getNetworkParameters();
}
return this.networkParametersCached;
}
/**
* Get all devices
*/
public getDevices(): Device[] {
return Device.all();
}
/**
* Get all devices with a specific type
*/
public getDevicesByType(type: DeviceType): Device[] {
return Device.byType(type);
}
/**
* Get device by ieeeAddr
*/
public getDeviceByIeeeAddr(ieeeAddr: string): Device {
return Device.byIeeeAddr(ieeeAddr);
}
/**
* Get device by networkAddress
*/
public getDeviceByNetworkAddress(networkAddress: number): Device {
return Device.byNetworkAddress(networkAddress);
}
/**
* Get group by ID
*/
public getGroupByID(groupID: number): Group {
return Group.byGroupID(groupID);
}
/**
* Get all groups
*/
public getGroups(): Group[] {
return Group.all();
}
/**
* Create a Group
*/
public createGroup(groupID: number): Group {
return Group.create(groupID);
}
/**
* Set transmit power of the adapter
*/
public async setTransmitPower(value: number): Promise<void> {
return this.adapter.setTransmitPower(value);
}
private onNetworkAddress(payload: AdapterEvents.NetworkAddressPayload): void {
debug.log(`Network address '${payload.ieeeAddr}'`);
const device = Device.byIeeeAddr(payload.ieeeAddr);
if (!device) {
debug.log(`Network address is from unknown device '${payload.ieeeAddr}'`);
return;
}
this.selfAndDeviceEmit(device, Events.Events.lastSeenChanged,
{device, reason: 'networkAddress'} as Events.LastSeenChangedPayload);
if (device.networkAddress !== payload.networkAddress) {
debug.log(`Device '${payload.ieeeAddr}' got new networkAddress '${payload.networkAddress}'`);
device.networkAddress = payload.networkAddress;
device.save();
const data: Events.DeviceNetworkAddressChangedPayload = {device};
this.selfAndDeviceEmit(device, Events.Events.deviceNetworkAddressChanged, data);
}
}
private onDeviceAnnounce(payload: AdapterEvents.DeviceAnnouncePayload): void {
debug.log(`Device announce '${payload.ieeeAddr}'`);
const device = Device.byIeeeAddr(payload.ieeeAddr);
if (!device) {
debug.log(`Device announce is from unknown device '${payload.ieeeAddr}'`);
return;
}
device.updateLastSeen();
this.selfAndDeviceEmit(device, Events.Events.lastSeenChanged,
{device, reason: 'deviceAnnounce'} as Events.LastSeenChangedPayload);
device.implicitCheckin();
if (device.networkAddress !== payload.networkAddress) {
debug.log(`Device '${payload.ieeeAddr}' announced with new networkAddress '${payload.networkAddress}'`);
device.networkAddress = payload.networkAddress;
device.save();
}
const data: Events.DeviceAnnouncePayload = {device};
this.selfAndDeviceEmit(device, Events.Events.deviceAnnounce, data);
}
private onDeviceLeave(payload: AdapterEvents.DeviceLeavePayload): void {
debug.log(`Device leave '${payload.ieeeAddr}'`);
const device = Device.byIeeeAddr(payload.ieeeAddr);
if (device) {
debug.log(`Removing device from database '${payload.ieeeAddr}'`);
device.removeFromDatabase();
}
const data: Events.DeviceLeavePayload = {ieeeAddr: payload.ieeeAddr};
this.selfAndDeviceEmit(device, Events.Events.deviceLeave, data);
}
private async onAdapterDisconnected(): Promise<void> {
debug.log(`Adapter disconnected'`);
await catcho(() => this.adapter.stop(), 'Failed to stop adapter on disconnect');
this.emit(Events.Events.adapterDisconnected);
}
private async onDeviceJoinedGreenPower(payload: GreenPowerDeviceJoinedPayload): Promise<void> {
debug.log(`Green power device '${JSON.stringify(payload)}' joined`);
// Green power devices don't have an ieeeAddr, the sourceID is unique and static so use this.
let ieeeAddr = payload.sourceID.toString(16);
ieeeAddr = `0x${'0'.repeat(16 - ieeeAddr.length)}${ieeeAddr}`;
// Green power devices dont' have a modelID, create a modelID based on the deviceID (=type)
const modelID = `GreenPower_${payload.deviceID}`;
let device = Device.byIeeeAddr(ieeeAddr, true);
if (!device) {
debug.log(`New green power device '${ieeeAddr}' joined`);
debug.log(`Creating device '${ieeeAddr}'`);
device = Device.create(
'GreenPower', ieeeAddr, payload.networkAddress, null,
undefined, undefined, modelID, true, [],
);
device.save();
this.selfAndDeviceEmit(device, Events.Events.deviceJoined, {device} as Events.DeviceJoinedPayload);
const deviceInterviewPayload: Events.DeviceInterviewPayload = {status: 'successful', device};
this.selfAndDeviceEmit(device, Events.Events.deviceInterview, deviceInterviewPayload);
} else if (device.isDeleted) {
debug.log(`Deleted green power device '${ieeeAddr}' joined`);
device.undelete(true);
this.selfAndDeviceEmit(device, Events.Events.deviceJoined, {device} as Events.DeviceJoinedPayload);
const deviceInterviewPayload: Events.DeviceInterviewPayload = {status: 'successful', device};
this.selfAndDeviceEmit(device, Events.Events.deviceInterview, deviceInterviewPayload);
}
}
private selfAndDeviceEmit(device: Device, event: string, data: KeyValue): void {
device?.emit(event, data);
this.emit(event, data);
}
private async onDeviceJoined(payload: AdapterEvents.DeviceJoinedPayload): Promise<void> {
debug.log(`Device '${payload.ieeeAddr}' joined`);
if (this.options.acceptJoiningDeviceHandler) {
if (!(await this.options.acceptJoiningDeviceHandler(payload.ieeeAddr))) {
debug.log(`Device '${payload.ieeeAddr}' rejected by handler, removing it`);
await catcho(() => this.adapter.removeDevice(payload.networkAddress, payload.ieeeAddr),
'Failed to remove rejected device');
return;
} else {
debug.log(`Device '${payload.ieeeAddr}' accepted by handler`);
}
}
let device = Device.byIeeeAddr(payload.ieeeAddr, true);
if (!device) {
debug.log(`New device '${payload.ieeeAddr}' joined`);
debug.log(`Creating device '${payload.ieeeAddr}'`);
device = Device.create(
'Unknown', payload.ieeeAddr, payload.networkAddress, undefined,
undefined, undefined, undefined, false, []
);
this.selfAndDeviceEmit(device, Events.Events.deviceJoined, {device} as Events.DeviceJoinedPayload);
} else if (device.isDeleted) {
debug.log(`Delete device '${payload.ieeeAddr}' joined, undeleting`);
device.undelete();
this.selfAndDeviceEmit(device, Events.Events.deviceJoined, {device} as Events.DeviceJoinedPayload);
}
if (device.networkAddress !== payload.networkAddress) {
debug.log(
`Device '${payload.ieeeAddr}' is already in database with different networkAddress, ` +
`updating networkAddress`
);
device.networkAddress = payload.networkAddress;
device.save();
}
device.updateLastSeen();
this.selfAndDeviceEmit(device, Events.Events.lastSeenChanged,
{device, reason: 'deviceJoined'} as Events.LastSeenChangedPayload);
device.implicitCheckin();
if (!device.interviewCompleted && !device.interviewing) {
const payloadStart: Events.DeviceInterviewPayload = {status: 'started', device};
debug.log(`Interview '${device.ieeeAddr}' start`);
this.selfAndDeviceEmit(device, Events.Events.deviceInterview, payloadStart);
try {
await device.interview();
debug.log(`Succesfully interviewed '${device.ieeeAddr}'`);
const event: Events.DeviceInterviewPayload = {status: 'successful', device};
this.selfAndDeviceEmit(device, Events.Events.deviceInterview, event);
} catch (error) {
debug.error(`Interview failed for '${device.ieeeAddr} with error '${error}'`);
const event: Events.DeviceInterviewPayload = {status: 'failed', device};
this.selfAndDeviceEmit(device, Events.Events.deviceInterview, event);
}
} else {
debug.log(
`Not interviewing '${payload.ieeeAddr}', completed '${device.interviewCompleted}', ` +
`in progress '${device.interviewing}'`
);
}
}
private isZclDataPayload(
dataPayload: AdapterEvents.ZclDataPayload | AdapterEvents.RawDataPayload, type: 'zcl' | 'raw'
): dataPayload is AdapterEvents.ZclDataPayload {
return type === 'zcl';
}
private async onZclOrRawData(
dataType: 'zcl' | 'raw', dataPayload: AdapterEvents.ZclDataPayload | AdapterEvents.RawDataPayload
): Promise<void> {
const logDataPayload = JSON.parse(JSON.stringify(dataPayload));
if (dataType === 'zcl') {
delete logDataPayload.frame.Cluster;
}
debug.log(`Received '${dataType}' data '${JSON.stringify(logDataPayload)}'`);
let gpDevice = null;
if (this.isZclDataPayload(dataPayload, dataType)) {
if (dataPayload.frame.Cluster.name === 'touchlink') {
// This is handled by touchlink
return;
} else if (dataPayload.frame.Cluster.name === 'greenPower') {
await this.greenPower.onZclGreenPowerData(dataPayload);
// lookup encapsulated gpDevice for further processing
gpDevice = Device.byNetworkAddress(dataPayload.frame.Payload.srcID & 0xFFFF);
}
}
let device = gpDevice ? gpDevice : (typeof dataPayload.address === 'string' ?
Device.byIeeeAddr(dataPayload.address) : Device.byNetworkAddress(dataPayload.address));
/**
* Handling of re-transmitted Xiaomi messages.
* https://github.com/Koenkk/zigbee2mqtt/issues/1238
* https://github.com/Koenkk/zigbee2mqtt/issues/3592
*
* Some Xiaomi router devices re-transmit messages from Xiaomi end devices.
* The network address of these message is set to the one of the Xiaomi router.
* Therefore it looks like if the message came from the Xiaomi router, while in
* fact it came from the end device.
* Handling these message would result in false state updates.
* The group ID attribute of these message defines the network address of the end device.
*/
if (device?.manufacturerName === 'LUMI' && device?.type == 'Router' && dataPayload.groupID) {
debug.log(`Handling re-transmitted Xiaomi message ${device.networkAddress} -> ${dataPayload.groupID}`);
device = Device.byNetworkAddress(dataPayload.groupID);
}
if (!device) {
debug.log(
`'${dataType}' data is from unknown device with address '${dataPayload.address}', ` +
`skipping...`
);
return;
}
device.updateLastSeen();
device.implicitCheckin();
device.linkquality = dataPayload.linkquality;
let endpoint = device.getEndpoint(dataPayload.endpoint);
if (!endpoint) {
debug.log(
`'${dataType}' data is from unknown endpoint '${dataPayload.endpoint}' from device with ` +
`network address '${dataPayload.address}', creating it...`
);
endpoint = device.createEndpoint(dataPayload.endpoint);
}
// Parse command for event
let type: Events.MessagePayloadType = undefined;
let data: KeyValue;
let clusterName = undefined;
const meta: {
zclTransactionSequenceNumber?: number;
manufacturerCode?: number;
frameControl?: FrameControl;
} = {};
if (this.isZclDataPayload(dataPayload, dataType)) {
const frame = dataPayload.frame;
const command = frame.getCommand();
clusterName = frame.Cluster.name;
meta.zclTransactionSequenceNumber = frame.Header.transactionSequenceNumber;
meta.manufacturerCode = frame.Header.manufacturerCode;
meta.frameControl = frame.Header.frameControl;
if (frame.isGlobal()) {
if (frame.isCommand('report')) {
type = 'attributeReport';
data = ZclFrameConverter.attributeKeyValue(dataPayload.frame);
} else if (frame.isCommand('read')) {
type = 'read';
data = ZclFrameConverter.attributeList(dataPayload.frame);
} else if (frame.isCommand('write')) {
type = 'write';
data = ZclFrameConverter.attributeKeyValue(dataPayload.frame);
} else {
/* istanbul ignore else */
if (frame.isCommand('readRsp')) {
type = 'readResponse';
data = ZclFrameConverter.attributeKeyValue(dataPayload.frame);
}
}
} else {
/* istanbul ignore else */
if (frame.isSpecific()) {
if (Events.CommandsLookup[command.name]) {
type = Events.CommandsLookup[command.name];
data = dataPayload.frame.Payload;
} else {
debug.log(`Skipping command '${command.name}' because it is missing from the lookup`);
}
}
}
if (type === 'readResponse' || type === 'attributeReport') {
// Some device report, e.g. it's modelID through a readResponse or attributeReport
for (const [key, value] of Object.entries(data)) {
const property = Device.ReportablePropertiesMapping[key];
if (property && !device[property.key]) {
property.set(value, device);
}
}
endpoint.saveClusterAttributeKeyValue(frame.Cluster.ID, data);
}
} else {
type = 'raw';
data = dataPayload.data;
const name = ZclUtils.getCluster(dataPayload.clusterID).name;
clusterName = Number.isNaN(Number(name)) ? name : Number(name);
}
if (type && data) {
const endpoint = device.getEndpoint(dataPayload.endpoint);
const linkquality = dataPayload.linkquality;
const groupID = dataPayload.groupID;
const eventData: Events.MessagePayload = {
type: type, device, endpoint, data, linkquality, groupID, cluster: clusterName, meta
};
this.selfAndDeviceEmit(device, Events.Events.message, eventData);
this.selfAndDeviceEmit(device, Events.Events.lastSeenChanged,
{device, reason: 'messageEmitted'} as Events.LastSeenChangedPayload);
} else {
this.selfAndDeviceEmit(device, Events.Events.lastSeenChanged,
{device, reason: 'messageNonEmitted'} as Events.LastSeenChangedPayload);
}
if (this.isZclDataPayload(dataPayload, dataType)) {
device.onZclData(dataPayload, endpoint);
}
}
}
export default Controller;