@@ -35,10 +35,11 @@ module.exports = function (RED) {
35
35
const getDevices = ( ) => {
36
36
this . trace ( 'getDevices' ) ;
37
37
const devices = this . herdsman . getDevices ( ) ;
38
- devices . forEach ( device => {
38
+ for ( const device of devices ) {
39
39
this . ieeeAddresses [ device . ieeeAddr ] = device ;
40
40
this . names [ device . meta . name ] = device ;
41
- } ) ;
41
+ }
42
+
42
43
this . groups = this . herdsman . getGroups ( ) ;
43
44
this . gotDevices = true ;
44
45
} ;
@@ -136,16 +137,17 @@ module.exports = function (RED) {
136
137
}
137
138
138
139
// TODO understand postfix
139
- let endpoint = this . getEndPointFromDevice ( model , device , isSet ) ;
140
- let payload = this . getPayloadFromMsg ( msg , attribute ) ;
140
+ const endpoint = this . getEndPointFromDevice ( model , device , isSet ) ;
141
+ const payload = this . getPayloadFromMsg ( msg , attribute ) ;
141
142
142
143
// For each key in the JSON message find the matching converter.
143
- Object . keys ( payload ) . sort ( a => ( [ 'state' , 'brightness' ] . includes ( a ) ? - 1 : 1 ) ) . forEach ( key => {
144
+ for ( const key of Object . keys ( payload ) . sort ( a => ( [ 'state' , 'brightness' ] . includes ( a ) ? - 1 : 1 ) ) ) {
144
145
const converter = converters . find ( c => c . key . includes ( key ) ) ;
145
146
if ( ! converter ) {
146
147
this . error ( `No converter available for '${ key } ' (${ payload [ key ] } ) on modelID '${ device . modelID } '` ) ;
147
- return ;
148
+ continue ;
148
149
}
150
+
149
151
const meta = {
150
152
options : { } ,
151
153
message : payload ,
@@ -160,16 +162,15 @@ module.exports = function (RED) {
160
162
}
161
163
} ;
162
164
if ( isSet ) {
163
- if ( isGroup ) {
165
+ if ( isGroup ) {
164
166
this . setToGroup ( converter , group , key , payload , meta , done ) ;
165
- }
166
- else {
167
+ } else {
167
168
this . setToDevice ( converter , endpoint , key , payload , meta , device , done ) ;
168
169
}
169
170
} else { //get
170
171
this . getFromDevice ( converter , device , payload , endpoint , key , meta , done ) ;
171
172
}
172
- } ) ;
173
+ }
173
174
} ) ;
174
175
175
176
const messageHandler = data => {
@@ -189,7 +190,7 @@ module.exports = function (RED) {
189
190
return ;
190
191
}
191
192
192
- let model = this . getModelFromDevice ( device ) ;
193
+ const model = this . getModelFromDevice ( device ) ;
193
194
if ( ! model ) {
194
195
this . warn ( `Received message from unsupported device with Zigbee model '${ data . device . modelID } '` ) ;
195
196
this . warn ( 'Please see: https://www.zigbee2mqtt.io/how_tos/how_to_support_new_devices.html.' ) ;
@@ -213,6 +214,7 @@ module.exports = function (RED) {
213
214
) ;
214
215
this . warn ( 'Please see: https://www.zigbee2mqtt.io/how_tos/how_to_support_new_devices.html.' ) ;
215
216
}
217
+
216
218
return ;
217
219
}
218
220
@@ -235,27 +237,28 @@ module.exports = function (RED) {
235
237
out . topic = this . topicReplace ( config . topic , out ) ;
236
238
const publish = convertedPayload => {
237
239
if ( config . payload === 'plain' ) {
238
- Object . keys ( convertedPayload ) . forEach ( key => {
240
+ for ( const key of Object . keys ( convertedPayload ) ) {
239
241
if ( config . attribute === '' || config . attribute === key ) {
240
242
const msg = { ...out , topic : out . topic + '/' + key ,
241
243
payload : convertedPayload [ key ] ,
242
244
retain : ! [ 'click' , 'action' , 'angle' ] . includes ( key ) } ;
243
245
this . send ( msg ) ;
244
246
}
245
- } ) ;
247
+ }
246
248
} else {
247
249
// indicate out.payload has been "extended"
248
250
// with converted data
249
251
wait -= 1 ;
250
252
Object . assign ( out . payload , convertedPayload ) ;
251
253
}
252
254
} ;
253
- converters . forEach ( converter => {
255
+
256
+ for ( const converter of converters ) {
254
257
const convertedPayload = converter . convert ( model , data , publish , { } , device . meta ) ;
255
258
if ( convertedPayload && Object . keys ( convertedPayload ) . length > 0 ) {
256
259
publish ( convertedPayload ) ;
257
260
}
258
- } ) ;
261
+ }
259
262
260
263
// if at least one converter produced out.payload data
261
264
// send the combined result
@@ -295,18 +298,18 @@ module.exports = function (RED) {
295
298
getModelFromDevice ( device ) {
296
299
if ( this . models . has ( device . modelID ) ) {
297
300
return this . models . get ( device . modelID ) ;
298
- } else {
299
- let model = herdsmanConverters . findByDevice ( device ) ;
300
- this . models . set ( device . modelID , model ) ;
301
- return model ;
302
301
}
302
+
303
+ const model = herdsmanConverters . findByDevice ( device ) ;
304
+ this . models . set ( device . modelID , model ) ;
305
+ return model ;
303
306
}
304
307
305
308
getPayloadFromMsg ( msg , attribute ) {
306
309
if ( typeof msg . payload === 'string' && msg . payload . startsWith ( '{' ) ) {
307
310
try {
308
311
msg . payload = JSON . parse ( msg . payload ) ;
309
- } catch { }
312
+ } catch { }
310
313
}
311
314
312
315
let payload ;
@@ -317,22 +320,25 @@ module.exports = function (RED) {
317
320
payload = msg . payload ;
318
321
} else if ( typeof msg . payload === 'string' ) {
319
322
// No attribute supplied, payload not an object - assume state.
320
- payload = { state : msg . payload } ;
323
+ payload = { state : msg . payload } ;
321
324
} else {
322
- payload = { state : msg . payload ? 'ON' : 'OFF' } ;
325
+ payload = { state : msg . payload ? 'ON' : 'OFF' } ;
323
326
}
327
+
324
328
return payload ;
325
329
}
326
330
327
331
getEndPointFromDevice ( model , device , isSet ) {
328
332
if ( typeof model . endpoint === 'undefined' ) {
329
333
return device . endpoints [ 0 ] ;
330
334
}
335
+
331
336
const endpoints = model . endpoint ( device ) ;
332
- let endpoint = endpoints [ isSet ? 'set' : 'get' ] ;
333
- if ( ( endpoint === null || typeof endpoint === 'undefined' ) && eps . default !== 'undefined' ) {
337
+ const endpoint = endpoints [ isSet ? 'set' : 'get' ] ;
338
+ if ( ( endpoint === null || typeof endpoint === 'undefined' ) && eps . default !== 'undefined' ) {
334
339
return eps . default ;
335
340
}
341
+
336
342
return device . endpoints [ 0 ] ;
337
343
}
338
344
@@ -341,6 +347,7 @@ module.exports = function (RED) {
341
347
this . error ( `Converter can not read '${ key } ' (${ payload [ key ] } ) on modelID '${ device . modelID } '` ) ;
342
348
return ;
343
349
}
350
+
344
351
converter . convertGet ( endpoint , key , meta ) . then ( result => {
345
352
this . handleResult ( device , result ) ;
346
353
done ( ) ;
@@ -356,6 +363,7 @@ module.exports = function (RED) {
356
363
if ( result && typeof result . readAfterWriteTime === 'undefined' && ! device . meta . reporting ) {
357
364
result . readAfterWriteTime = 0 ;
358
365
}
366
+
359
367
if ( result && typeof result . readAfterWriteTime !== 'undefined' && ! device . meta . reporting ) {
360
368
setTimeout ( ( ) => {
361
369
this . debug ( `readAfterWrite ${ device . ieeeAddr } ${ device . meta . name } ` ) ;
@@ -395,23 +403,23 @@ module.exports = function (RED) {
395
403
}
396
404
397
405
const msgLower = { } ;
398
- Object . keys ( msg ) . forEach ( k => {
406
+ for ( const k of Object . keys ( msg ) ) {
399
407
msgLower [ k . toLowerCase ( ) ] = msg [ k ] ;
400
- } ) ;
408
+ }
401
409
402
410
const match = topic . match ( / \$ { [ ^ } ] + } / g) ;
403
411
if ( match ) {
404
- match . forEach ( v => {
412
+ for ( const v of match ) {
405
413
const key = v . substr ( 2 , v . length - 3 ) ;
406
414
const rx = new RegExp ( '\\${' + key + '}' , 'g' ) ;
407
415
const rkey = key . toLowerCase ( ) ;
408
416
topic = topic . replace ( rx , msgLower [ rkey ] || '' ) ;
409
- } ) ;
417
+ }
410
418
}
411
419
412
420
return topic ;
413
421
}
414
422
}
415
423
416
424
RED . nodes . registerType ( 'zigbee-converter' , ZigbeeConverter ) ;
417
- } ;
425
+ } ;
0 commit comments