forked from ioBroker/ioBroker.zigbee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
985 lines (902 loc) · 41.1 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
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
/**
*
* Zigbee devices adapter
*
*/
'use strict';
let debug;
try {
debug = require('zigbee-herdsman/node_modules/debug');
} catch (e) {
debug = require('debug');
}
const originalLogMethod = debug.log;
const safeJsonStringify = require('./lib/json');
const fs = require('fs');
const path = require('path');
const utils = require('@iobroker/adapter-core'); // Get common adapter utils
const SerialListPlugin = require('./lib/seriallist');
const CommandsPlugin = require('./lib/commands');
const GroupsPlugin = require('./lib/groups');
const NetworkMapPlugin = require('./lib/networkmap');
const DeveloperPlugin = require('./lib/developer');
const BindingPlugin = require('./lib/binding');
const OtaPlugin = require('./lib/ota');
const BackupPlugin = require('./lib/backup');
const ZigbeeController = require('./lib/zigbeecontroller');
const StatesController = require('./lib/statescontroller');
const ExcludePlugin = require('./lib/exclude');
const zigbeeHerdsmanConverters = require('zigbee-herdsman-converters');
const vm = require('vm');
const util = require('util');
const createByteArray = function (hexString) {
const bytes = [];
for (let c = 0; c < hexString.length; c += 2) {
bytes.push(parseInt(hexString.substr(c, 2), 16));
}
return bytes;
};
const E_INFO = 1;
const E_DEBUG = 2;
const E_WARN = 3;
const E_ERROR = 4;
let _pairingMode = false;
const errorCodes = {
9999: {severity: E_INFO, message: 'No response'},
233: {severity: E_DEBUG, message: 'MAC NO ACK'},
205: {severity: E_WARN, message: 'No network route'},
134: {severity: E_WARN, message: 'Unsupported Attribute'},
};
class Zigbee extends utils.Adapter {
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
constructor(options) {
super(Object.assign(options || {}, {
dirname: __dirname.indexOf('node_modules') !== -1 ? undefined : __dirname,
name: 'zigbee',
systemConfig: true,
}));
this.on('ready', () => this.onReady());
this.on('unload', callback => this.onUnload(callback));
this.on('message', obj => this.onMessage(obj));
this.query_device_block = [];
this.stController = new StatesController(this);
this.stController.on('log', this.onLog.bind(this));
this.stController.on('changed', this.publishFromState.bind(this));
this.plugins = [
new SerialListPlugin(this),
new CommandsPlugin(this),
new GroupsPlugin(this),
new NetworkMapPlugin(this),
new DeveloperPlugin(this),
new BindingPlugin(this),
new ExcludePlugin(this),
new OtaPlugin(this),
new BackupPlugin(this),
];
}
async onMessage(obj) {
if (typeof obj === 'object' && obj.command) {
switch (obj.command) {
case 'SendToDevice': {
let rv = {
success: false,
loc: -1,
};
try {
rv = await this.sendPayload(obj.message);
} catch (e) {
rv.error = e;
}
this.sendTo(obj.from, obj.command, rv, obj.callback);
break;
}
}
}
}
sendError(error, message) {
if (this.supportsFeature && this.supportsFeature('PLUGINS')) {
const sentryInstance = this.getPluginInstance('sentry');
if (sentryInstance) {
const Sentry = sentryInstance.getSentryObject();
if (Sentry) {
if (message) {
Sentry.configureScope(scope =>
scope.addBreadcrumb({
type: 'error', // predefined types
category: 'error message',
level: Sentry.Severity.Error,
message
}));
}
if (typeof error == 'string') {
Sentry.captureException(new Error(error));
} else {
Sentry.captureException(error);
}
}
}
}
}
filterError(errormessage, message, error) {
if (error.code === undefined) {
let em = error.stack.match(/failed \((.+?)\) at/);
em = em || error.stack.match(/failed \((.+?)\)/);
this.log.error(`${message} no error code (${(em ? em[1] : 'undefined')})`);
this.sendError(error, `${message} no error code`);
this.log.debug(`Stack trace for ${em}: ${error.stack}`);
return;
}
const ecode = errorCodes[error.code];
if (ecode === undefined) {
this.log.error(errormessage);
this.sendError(error, errormessage);
return;
}
switch (ecode.severity) {
case E_INFO:
this.log.info(`${message}: Code ${error.code} (${ecode.message})`);
break;
case E_DEBUG:
this.log.debug(`${message}: Code ${error.code} (${ecode.message})`);
break;
case E_WARN:
this.log.warn(`${message}: Code ${error.code} (${ecode.message})`);
break;
case E_ERROR:
this.log.error(`${message}: Code ${error.code} (${ecode.message})`);
this.sendError(error, `${message}: Code ${error.code} (${ecode.message})`);
break;
default:
this.log.error(`${message}: Code ${error.code} (malformed error)`);
this.sendError(error, `${message}: Code ${error.code} (malformed error)`);
break;
}
}
debugLog(data, ...args) {
const message = (args) ? util.format(data, ...args) : data;
this.log.debug(message.slice(message.indexOf('zigbee-herdsman')));
}
async onReady() {
if (this.config.debugHerdsman) {
debug.log = this.debugLog.bind(this);
debug.enable('zigbee-herdsman*');
}
// external converters
this.applyExternalConverters();
// get exclude list from object
this.getState('exclude.all', (err, state) =>
this.stController.getExcludeExposes(state));
this.subscribeStates('*');
// set connection false before connect to zigbee
this.setState('info.connection', false, true);
const zigbeeOptions = this.getZigbeeOptions();
this.zbController = new ZigbeeController(this);
this.zbController.on('log', this.onLog.bind(this));
this.zbController.on('ready', this.onZigbeeAdapterReady.bind(this));
this.zbController.on('disconnect', this.onZigbeeAdapterDisconnected.bind(this));
this.zbController.on('new', this.newDevice.bind(this));
this.zbController.on('leave', this.leaveDevice.bind(this));
this.zbController.on('pairing', this.onPairing.bind(this));
this.zbController.on('event', this.onZigbeeEvent.bind(this));
this.zbController.on('msg', this.onZigbeeEvent.bind(this));
this.zbController.on('publish', this.publishToState.bind(this));
this.zbController.configure(zigbeeOptions);
await this.callPluginMethod('configure', [zigbeeOptions]);
this.reconnectCounter = 1;
this.doConnect();
}
* getExternalDefinition() {
if (this.config.external === undefined) {
return;
}
const extfiles = this.config.external.split(';');
for (const moduleName of extfiles) {
if (!moduleName) continue;
const sandbox = {
require,
module: {},
};
const mN = (fs.existsSync(moduleName) ? moduleName : this.expandFileName(moduleName).replace('.', '_'));
if (!fs.existsSync(mN)) {
this.log.warn(`External converter not loaded - neither ${moduleName} nor ${mN} exist.`)
}
else {
const converterCode = fs.readFileSync(mN, {encoding: 'utf8'}).toString();
let converterLoaded = true;
if (converterCode.match(/..\/lib\/legacy/gm)) {
this.log.warn(`External converter ${mN} contains an unsupported reference to '/lib/legacy' - external converter not loaded.`)
converterLoaded = false;
}
else
{
// remove the require statements and attempt to place them in the sandbox
const requiredLibraries = converterCode.matchAll(/(\w+) += +require\(['"](\S+)['"]\);/gm);
for (const line of requiredLibraries) {
const movedLine = line[2].replace('..', '../zigbee-herdsman-converters')
try {
sandbox[line[1]] = require(movedLine);
}
catch (e) {
this.log.warn(`error adding ${line[1]} (${movedLine}) to the sandbox: ${e}`);
converterLoaded = false;
}
}
}
if (converterLoaded) {
this.log.info(`Apply converter from module: ${mN}`);
//this.log.warn(converterCode.replace(/const (\w+) += +require\(['"](\S+)['"]\);/gm, ''));
try {
vm.runInNewContext(converterCode.replace(/const (\w+) += +require\(['"](\S+)['"]\);/gm, ''), sandbox);
const converter = sandbox.module.exports;
if (Array.isArray(converter)) for (const item of converter) yield item;
else yield converter;
}
catch (e) {
this.log.error(`Unable to apply converter from module: ${mN} - the code does not run: ${e}`)
}
}
else
this.log.info(`Ignoring converter from module: ${mN} - see warn messages for reason`);
}
}
/* if (this.config.external === undefined) {
return;
}
const extfiles = this.config.external.split(';');
for (const moduleName of extfiles) {
if (!moduleName) continue;
this.log.info(`Apply converter from module: ${moduleName}`);
const sandbox = {
require,
module: {},
};
const converterCode = fs.readFileSync(moduleName, {encoding: 'utf8'});
vm.runInNewContext(converterCode, sandbox);
const converter = sandbox.module.exports;
if (Array.isArray(converter)) {
for (const item of converter) {
yield item;
}
} else {
yield converter;
}
}
*/
}
applyExternalConverters() {
try {
for (const definition of this.getExternalDefinition()) {
const toAdd = {...definition};
delete toAdd['homeassistant'];
try {
zigbeeHerdsmanConverters.addDeviceDefinition(toAdd);
}
catch { this.log.error(`unable to apply external converter ${JSON.stringfy(toAdd)}`) }
}
}
catch(error) {
this.log.error('error applying external converters');
}
}
async doConnect() {
let debugversion = '';
try {
const DebugIdentify = require('./debugidentify');
debugversion = DebugIdentify.ReportIdentifier();
} catch {
debugversion = ' npm ...';
}
// installed version
let gitVers = '';
try {
this.log.info(`Starting Zigbee ${debugversion}`);
await this.getForeignObject(`system.adapter.${this.namespace}`, (err, obj) => {
if (!err && obj && obj.common.installedFrom && obj.common.installedFrom.includes('://')) {
const instFrom = obj.common.installedFrom;
gitVers = gitVers + instFrom.replace('tarball', 'commit');
} else {
gitVers = obj.common.installedFrom;
}
this.log.info(`Installed Version: ${gitVers}`);
});
await this.zbController.start();
} catch (error) {
this.setState('info.connection', false, true);
this.log.error(`Failed to start Zigbee`);
if (error.stack) {
this.log.error(error.stack);
} else {
this.log.error(error);
}
this.sendError(error, `Failed to start Zigbee`);
if (this.reconnectCounter > 0) {
this.tryToReconnect();
}
}
}
async onZigbeeAdapterDisconnected() {
this.reconnectCounter = 5;
this.log.error('Adapter disconnected, stopping');
this.sendError('Adapter disconnected, stopping');
this.setState('info.connection', false, true);
await this.callPluginMethod('stop');
this.tryToReconnect();
}
tryToReconnect() {
this.reconnectTimer = setTimeout(() => {
if (this.config.port.includes('tcp://')) {
// Controller connect though Wi-Fi.
// Unlikely USB dongle, connection broken may only cause user unplugged the dongle,
// Wi-Fi connected gateway is possible that device connection is broken caused by
// AP issue or Zigbee gateway power is turned off unexpectedly.
// So try to reconnect gateway every 10 seconds all the time.
this.log.info(`Try to reconnect.`);
} else {
this.log.info(`Try to reconnect. ${this.reconnectCounter} attempts left`);
this.reconnectCounter -= 1;
}
this.doConnect();
}, 10 * 1000); // every 10 seconds
}
async onZigbeeAdapterReady() {
this.reconnectTimer && clearTimeout(this.reconnectTimer);
this.log.info(`Zigbee started`);
// https://github.com/ioBroker/ioBroker.zigbee/issues/668
const extPanIdFix = this.config.extPanIdFix ? this.config.extPanIdFix : false;
if (!extPanIdFix) {
const configExtPanId = this.config.extPanID ? `0x${this.config.extPanID.toLowerCase()}` : '0xdddddddddddddddd';
let networkExtPanId = (await this.zbController.herdsman.getNetworkParameters()).extendedPanID;
let needChange = false;
this.log.debug(`Config value ${configExtPanId} : Network value ${networkExtPanId}`);
const adapterType = this.config.adapterType || 'zstack';
if (adapterType === 'zstack') {
if (configExtPanId !== networkExtPanId) {
try {
// try to read from nvram
const result = await this.zbController.herdsman.adapter.znp.request(
1, // Subsystem.SYS
'osalNvRead',
{
id: 45, // EXTENDED_PAN_ID
len: 0x08,
offset: 0x00,
},
null, [
0, // ZnpCommandStatus.SUCCESS
2, // ZnpCommandStatus.INVALID_PARAM
]
);
const nwExtPanId = `0x${result.payload.value.reverse().toString('hex')}`;
this.log.debug(`Config value ${configExtPanId} : nw value ${nwExtPanId}`);
if (configExtPanId !== nwExtPanId) {
networkExtPanId = nwExtPanId;
needChange = true;
}
} catch (e) {
this.log.error(`Unable to apply ExtPanID changes: ${e}`);
this.sendError(e, `Unable to apply ExtPanID changes`);
needChange = false;
}
} else {
needChange = true;
}
}
if (needChange) {
// need change config value and mark that fix is applied
this.log.debug(`Fix extPanId value to ${networkExtPanId}. And restart adapter.`);
this.updateConfig({extPanID: networkExtPanId.substr(2), extPanIdFix: true});
} else {
// only mark that fix is applied
this.log.debug(`Fix without changes. And restart adapter.`);
this.updateConfig({extPanIdFix: true});
}
}
this.setState('info.connection', true, true);
const devicesFromDB = await this.zbController.getClients(false);
for (const device of devicesFromDB) {
const entity = await this.zbController.resolveEntity(device);
if (entity) {
const model = entity.mapped ? entity.mapped.model : entity.device.modelID;
this.stController.updateDev(device.ieeeAddr.substr(2), model, model, () =>
this.stController.syncDevStates(device, model));
}
}
await this.callPluginMethod('start', [this.zbController, this.stController]);
}
async checkIfModelUpdate(entity) {
const model = entity.mapped ? entity.mapped.model : entity.device.modelID,
device = entity.device,
devId = device.ieeeAddr.substr(2);
return new Promise((resolve) => {
this.getObject(devId, (err, obj) => {
if (obj && obj.common.type !== model) {
// let's change model
this.getStatesOf(devId, (err, states) => {
if (!err && states) {
const chain = [];
states.forEach((state) =>
chain.push(this.deleteStateAsync(devId, null, state._id)));
Promise.all(chain)
.then(() =>
this.stController.deleteDeviceStates(devId, () =>
this.stController.updateDev(devId, model, model, async () => {
await this.stController.syncDevStates(device, model);
resolve();
})));
} else {
resolve();
}
});
} else {
resolve();
}
});
});
}
async onZigbeeEvent(type, entity, message) {
this.log.debug(`Type ${type} device ${safeJsonStringify(entity)} incoming event: ${safeJsonStringify(message)}`);
const device = entity.device;
const mappedModel = entity.mapped;
const model = entity.mapped ? entity.mapped.model : entity.device.modelID;
const cluster = message.cluster;
const devId = device.ieeeAddr.substr(2);
const meta = {device};
// this assigment give possibility to use iobroker logger in code of the converters, via meta.logger
meta.logger = this.log;
await this.checkIfModelUpdate(entity);
// always publish link_quality
if (message.linkquality) {
this.publishToState(devId, model, {linkquality: message.linkquality});
}
// publish raw event to "from_zigbee"
// some cleanup
const msgForState = Object.assign({}, message);
delete msgForState['device'];
delete msgForState['endpoint'];
msgForState['endpoint_id'] = message.endpoint.ID;
this.publishToState(devId, model, {msg_from_zigbee: safeJsonStringify(msgForState)});
if (!entity.mapped) {
return;
}
let converters = mappedModel.fromZigbee.filter(c => c && c.cluster === cluster && (
(c.type instanceof Array) ? c.type.includes(type) : c.type === type));
if (!converters.length && type === 'readResponse') {
converters = mappedModel.fromZigbee.filter(c => c.cluster === cluster && (
(c.type instanceof Array) ? c.type.includes('attributeReport') : c.type === 'attributeReport'));
}
if (!converters.length) {
if (type !== 'readResponse') {
this.log.debug(
`No converter available for '${mappedModel.model}' with cluster '${cluster}' and type '${type}'`
);
}
return;
}
converters.forEach((converter) => {
const publish = (payload) => {
this.log.debug(`Publish ${safeJsonStringify(payload)} to ${safeJsonStringify(devId)}`);
if (payload) {
this.publishToState(devId, model, payload);
}
};
this.stController.collectOptions(devId, model, (options) => {
const payload = converter.convert(mappedModel, message, publish, options, meta);
if (payload) {
// Add device linkquality.
publish(payload);
}
});
});
}
publishToState(devId, model, payload) {
this.stController.publishToState(devId, model, payload);
}
acknowledgeState(deviceId, model, stateDesc, value) {
if (model === 'group') {
const stateId = `${this.namespace}.group_${deviceId}.${stateDesc.id}`;
this.setState(stateId, value, true);
} else {
const stateId = `${this.namespace}.${deviceId.replace('0x', '')}.${stateDesc.id}`;
this.setState(stateId, value, true);
}
}
processSyncStatesList(deviceId, model, syncStateList) {
syncStateList.forEach((syncState) => {
this.acknowledgeState(deviceId, model, syncState.stateDesc, syncState.value);
});
}
async publishFromState(deviceId, model, stateModel, stateList, options) {
let isGroup = false;
if (model === 'group') {
isGroup = true;
deviceId = parseInt(deviceId);
}
const entity = await this.zbController.resolveEntity(deviceId);
this.log.debug(`entity: ${safeJsonStringify(entity)}`);
const mappedModel = entity.mapped;
if (!mappedModel) {
this.log.debug(`No mapped model for ${model}`);
return;
}
this.log.debug(`Mapped Model: ${JSON.stringify(mappedModel)}`);
stateList.forEach(async changedState => {
const stateDesc = changedState.stateDesc;
const value = changedState.value;
if (stateDesc.id === 'send_payload') {
try {
const json_value = JSON.parse(value);
const payload = {device: deviceId.replace('0x', ''), payload: json_value};
const result = await this.sendPayload(payload);
if (result.hasOwnProperty('success') && result.success) {
this.acknowledgeState(deviceId, model, stateDesc, value);
}
} catch (error) {
this.log.warn(`send_payload: ${value} does not parse as JSON Object : ${error.message}`);
return;
}
return;
}
if (stateDesc.isOption) {
// acknowledge state with given value
this.acknowledgeState(deviceId, model, stateDesc, value);
// process sync state list
//this.processSyncStatesList(deviceId, modelId, syncStateList);
// if this is the device query state => trigger the device query
// on activation of the 'device_query' state trigger hardware query where possible
if (stateDesc.id === 'device_query') {
if (this.query_device_block.indexOf(deviceId) > -1) {
this.log.warn(`Device query for '${entity.device.ieeeAddr}' blocked`);
return;
}
if (mappedModel) {
this.query_device_block.push(deviceId);
this.log.debug(`Device query for '${entity.device.ieeeAddr}' started`);
for (const converter of mappedModel.toZigbee) {
if (converter.hasOwnProperty('convertGet')) {
for (const ckey of converter.key) {
try {
await converter.convertGet(entity.device.endpoints[0], ckey, {});
} catch (error) {
this.log.warn(`Failed to read state '${JSON.stringify(ckey)}'of '${entity.device.ieeeAddr}' after query with '${JSON.stringify(error)}'`);
}
}
}
}
this.log.debug(`Device query for '${entity.device.ieeeAddr}' done`);
const idToRemove = deviceId;
setTimeout(() => {
const idx = this.query_device_block.indexOf(idToRemove);
if (idx > -1) {
this.query_device_block.splice(idx);
}
}, 10000);
}
return;
}
return;
}
const converter = mappedModel.toZigbee.find(c => c && (c.key.includes(stateDesc.prop) || c.key.includes(stateDesc.setattr) || c.key.includes(stateDesc.id)));
if (!converter) {
this.log.error(`No converter available for '${model}' with key '${stateDesc.id}' `);
this.sendError(`No converter available for '${model}' with key '${stateDesc.id}' `);
return;
}
const preparedValue = (stateDesc.setter) ? stateDesc.setter(value, options) : value;
const preparedOptions = (stateDesc.setterOpt) ? stateDesc.setterOpt(value, options) : {};
let syncStateList = [];
if (stateModel && stateModel.syncStates) {
stateModel.syncStates.forEach(syncFunct => {
const res = syncFunct(stateDesc, value, options);
if (res) {
syncStateList = syncStateList.concat(res);
}
});
}
const epName = stateDesc.epname !== undefined ? stateDesc.epname : (stateDesc.prop || stateDesc.id);
const key = stateDesc.setattr || stateDesc.prop || stateDesc.id;
this.log.debug(`convert ${key}, ${safeJsonStringify(preparedValue)}, ${safeJsonStringify(preparedOptions)}`);
let target;
if (model === 'group') {
target = entity.mapped;
} else {
target = await this.zbController.resolveEntity(deviceId, epName);
target = target.endpoint;
}
this.log.debug(`target: ${safeJsonStringify(target)}`);
const meta = {
endpoint_name: epName,
options: preparedOptions,
device: entity.device,
mapped: model === 'group' ? [] : mappedModel,
message: {[key]: preparedValue},
logger: this.log,
state: {},
};
if (preparedOptions.hasOwnProperty('state')) {
meta.state = preparedOptions.state;
}
try {
const result = await converter.convertSet(target, key, preparedValue, meta);
this.log.debug(`convert result ${safeJsonStringify(result)}`);
if (result !== undefined) {
if (stateModel && !isGroup) {
this.acknowledgeState(deviceId, model, stateDesc, value);
}
// process sync state list
this.processSyncStatesList(deviceId, model, syncStateList);
if (isGroup) {
await this.callPluginMethod('queryGroupMemberState', [deviceId, stateDesc]);
this.acknowledgeState(deviceId, model, stateDesc, value);
}
}
} catch (error) {
this.filterError(`Error ${error.code} on send command to ${deviceId}.` +
` Error: ${error.stack}`, `Send command to ${deviceId} failed with`, error);
}
});
}
// This function is introduced to explicitly allow user level scripts to send Commands
// directly to the zigbee device. It utilizes the zigbee-herdsman-converters to generate
// the exact zigbee message to be sent and can be used to set device options which are
// not exposed as states. It serves as a wrapper function for "publishFromState" with
// extended parameter checking
//
// The payload can either be a JSON object or the string representation of a JSON object
// The following keys are supported in the object:
// device: name of the device. For a device zigbee.0.0011223344556677 this would be 0011223344556677
// payload: The data to send to the device as JSON object (key/Value pairs)
// endpoint: optional: the endpoint to send the data to, if supported.
//
async sendPayload(payload) {
this.log.debug(`publishToDevice called with ${safeJsonStringify(payload)}`);
let payloadObj = {};
if (typeof payload === 'string') {
try {
payloadObj = JSON.parse(payload);
} catch (e) {
this.log.error(`Unable to parse ${safeJsonStringify(payload)}: ${safeJsonStringify(e)}`);
this.sendError(e, `Unable to parse ${safeJsonStringify(payload)}: ${safeJsonStringify(e)}`);
return {
success: false,
error: `Unable to parse ${safeJsonStringify(payload)}: ${safeJsonStringify(e)}`
};
}
} else if (typeof payload === 'object') {
payloadObj = payload;
}
if (payloadObj.hasOwnProperty('device') && payloadObj.hasOwnProperty('payload')) {
try {
const isDevice = !payload.device.includes('group_');
const stateList = [];
const devID = isDevice ? `0x${payload.device}` : parseInt(payload.device.replace('group_', ''));
const entity = await this.zbController.resolveEntity(devID);
if (!entity) {
this.log.error(`Device ${safeJsonStringify(payloadObj.device)} not found`);
this.sendError(`Device ${safeJsonStringify(payloadObj.device)} not found`);
return {success: false, error: `Device ${safeJsonStringify(payloadObj.device)} not found`};
}
const mappedModel = entity.mapped;
if (!mappedModel) {
this.log.error(`No Model for Device ${safeJsonStringify(payloadObj.device)}`);
this.sendError(`No Model for Device ${safeJsonStringify(payloadObj.device)}`);
return {success: false, error: `No Model for Device ${safeJsonStringify(payloadObj.device)}`};
}
if (typeof payloadObj.payload !== 'object') {
this.log.error(`Illegal payload type for ${safeJsonStringify(payloadObj.device)}`);
this.sendError(`Illegal payload type for ${safeJsonStringify(payloadObj.device)}`);
return {success: false, error: `Illegal payload type for ${safeJsonStringify(payloadObj.device)}`};
}
for (const key in payloadObj.payload) {
if (payloadObj.payload[key] != undefined) {
const datatype = typeof payloadObj.payload[key];
stateList.push({
stateDesc: {
id: key,
prop: key,
role: 'state',
type: datatype,
epname: payloadObj.endpoint,
},
value: payloadObj.payload[key],
index: 0,
timeout: 0,
});
}
}
try {
this.log.debug(`Calling publish to state for ${safeJsonStringify(payloadObj.device)} with ${safeJsonStringify(stateList)}`);
await this.publishFromState(`0x${payload.device}`, '', undefined, stateList, payload.options);
return {success: true};
} catch (error) {
this.filterError(`Error ${error.code} on send command to ${payload.device}.` +
` Error: ${error.stack}`, `Send command to ${payload.device} failed with`, error);
return {success: false, error};
}
} catch (e) {
return {success: false, error: e};
}
}
return {success: false, error: `missing parameter device or payload in message ${JSON.stringify(payload)}`};
}
newDevice(entity) {
this.log.debug(`New device event: ${safeJsonStringify(entity)}`);
const dev = entity.device;
if (dev) {
this.getObject(dev.ieeeAddr.substr(2), (err, obj) => {
if (!obj) {
const model = (entity.mapped) ? entity.mapped.model : entity.device.modelID;
this.log.debug(`new device ${dev.ieeeAddr} ${dev.networkAddress} ${model} `);
this.logToPairing(`New device joined '${dev.ieeeAddr}' model ${model}`, true);
this.stController.updateDev(dev.ieeeAddr.substr(2), model, model, () =>
this.stController.syncDevStates(dev, model));
}
// else this.log.warn(`Device ${safeJsonStringify(entity)} rejoined, no new device`);
});
}
}
leaveDevice(ieeeAddr) {
this.log.debug(`Leave device event: ${ieeeAddr}`);
if (ieeeAddr) {
const devId = ieeeAddr.substr(2);
this.log.debug(`Delete device ${devId} from iobroker.`);
this.stController.deleteDeviceStates(devId);
}
}
async callPluginMethod(method, parameters) {
for (const plugin of this.plugins) {
if (plugin[method]) {
try {
if (parameters !== undefined) {
await plugin[method](...parameters);
} else {
await plugin[method]();
}
} catch (error) {
if (error && !error.hasOwnProperty('code')) {
this.log.error(`Failed to call '${plugin.constructor.name}' '${method}' (${error.stack})`);
this.sendError(error, `Failed to call '${plugin.constructor.name}' '${method}'`);
}
throw error;
}
}
}
}
/**
* @param {() => void} callback
*/
async onUnload(callback) {
try {
if (this.config.debugHerdsman) {
debug.disable();
debug.log = originalLogMethod;
}
this.log.info('cleaned everything up...');
if (this.reconnectTimer) clearTimeout(this.reconnectTimer);
await this.callPluginMethod('stop');
if (this.zbController) {
await this.zbController.stop();
}
callback();
} catch (error) {
if (error) {
this.log.error(`Unload error (${error.stack})`);
}
this.sendError(error, `Unload error`);
callback();
}
}
getZigbeeOptions() {
// file path for db
let dbDir = path.join(utils.getAbsoluteInstanceDataDir(this), '');
dbDir = dbDir.replace('.', '_');
if (this.systemConfig && !fs.existsSync(dbDir)) {
try {
fs.mkdirSync(dbDir);
} catch (e) {
this.log.error(`Cannot create directory ${dbDir}: ${e}`);
this.sendError(`Cannot create directory ${dbDir}: ${e}`);
}
}
const port = this.config.port;
if (!port) {
this.log.error('Serial port not selected! Go to settings page.');
this.sendError('Serial port not selected! Go to settings page.');
}
const panID = parseInt(this.config.panID ? this.config.panID : 0x1a62);
const channel = parseInt(this.config.channel ? this.config.channel : 11);
const precfgkey = createByteArray(this.config.precfgkey ? this.config.precfgkey : '01030507090B0D0F00020406080A0C0D');
const extPanId = createByteArray(this.config.extPanID ? this.config.extPanID : 'DDDDDDDDDDDDDDDD').reverse();
const adapterType = this.config.adapterType || 'zstack';
// https://github.com/ioBroker/ioBroker.zigbee/issues/668
const extPanIdFix = this.config.extPanIdFix ? this.config.extPanIdFix : false;
return {
net: {
panId: panID,
extPanId: extPanId,
channelList: [channel],
precfgkey: precfgkey
},
sp: {
port: port,
baudRate: 115200,
rtscts: false,
adapter: adapterType,
},
dbDir: dbDir,
dbPath: 'shepherd.db',
backupPath: 'nvbackup.json',
disableLed: this.config.disableLed,
disablePing: this.config.disablePing,
transmitPower: this.config.transmitPower,
disableBackup: this.config.disableBackup,
extPanIdFix: extPanIdFix,
startWithInconsistent: this.config.startWithInconsistent || false,
};
}
onPairing(message, data) {
if (Number.isInteger(data)) {
_pairingMode = true;
this.setState('info.pairingCountdown', data, true);
_pairingMode = true;
}
if (data === 0) {
// set pairing mode off
this.setState('info.pairingMode', false, true);
_pairingMode = false;
}
if (data) {
this.logToPairing(`${message}: ${data.toString()}`);
} else {
this.logToPairing(`${message}`);
}
}
logToPairing(message) {
this.setState('info.pairingMessage', message, true);
}
expandFileName(fn) {
return path.join(utils.getAbsoluteInstanceDataDir(this), fn);
}
onLog(level, msg, data) {
if (msg) {
let logger = this.log.info;
switch (level) {
case 'error':
logger = this.log.error;
if (data)
data = data.toString();
this.logToPairing(`Error: ${msg}. ${data}`, true);
this.sendError(`Error: ${msg}. ${data}`);
break;
case 'debug':
logger = this.log.debug;
break;
case 'info':
logger = this.log.info;
break;
case 'warn':
logger = this.log.warn;
break;
}
if (data) {
if (typeof data === 'string') {
logger(`${msg}. ${data}`);
} else {
logger(`${msg}. ${safeJsonStringify(data)}`);
}
} else {
logger(msg);
}
}
}
}
if (module && module.parent) {
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Zigbee(options);
} else {
// or start the instance directly
new Zigbee();
}