@willieee802/zigbee-herdsman 0.18.2 → 0.18.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (327) hide show
  1. package/dist/adapter/adapter.d.ts +61 -61
  2. package/dist/adapter/adapter.d.ts.map +1 -1
  3. package/dist/adapter/adapter.js +146 -146
  4. package/dist/adapter/deconz/adapter/deconzAdapter.d.ts +68 -68
  5. package/dist/adapter/deconz/adapter/deconzAdapter.js +1060 -1060
  6. package/dist/adapter/deconz/adapter/index.d.ts +2 -2
  7. package/dist/adapter/deconz/adapter/index.js +10 -10
  8. package/dist/adapter/deconz/driver/constants.d.ts +104 -104
  9. package/dist/adapter/deconz/driver/constants.js +55 -55
  10. package/dist/adapter/deconz/driver/driver.d.ts +81 -81
  11. package/dist/adapter/deconz/driver/driver.js +732 -732
  12. package/dist/adapter/deconz/driver/frame.d.ts +6 -6
  13. package/dist/adapter/deconz/driver/frame.js +13 -13
  14. package/dist/adapter/deconz/driver/frameParser.d.ts +2 -2
  15. package/dist/adapter/deconz/driver/frameParser.js +443 -443
  16. package/dist/adapter/deconz/driver/parser.d.ts +12 -12
  17. package/dist/adapter/deconz/driver/parser.js +61 -61
  18. package/dist/adapter/deconz/driver/writer.d.ts +8 -8
  19. package/dist/adapter/deconz/driver/writer.js +44 -44
  20. package/dist/adapter/events.d.ts +47 -47
  21. package/dist/adapter/events.js +14 -14
  22. package/dist/adapter/ezsp/adapter/backup.d.ts +9 -9
  23. package/dist/adapter/ezsp/adapter/backup.js +53 -53
  24. package/dist/adapter/ezsp/adapter/ezspAdapter.d.ts +59 -59
  25. package/dist/adapter/ezsp/adapter/ezspAdapter.d.ts.map +1 -1
  26. package/dist/adapter/ezsp/adapter/ezspAdapter.js +603 -585
  27. package/dist/adapter/ezsp/adapter/ezspAdapter.js.map +1 -1
  28. package/dist/adapter/ezsp/adapter/index.d.ts +2 -2
  29. package/dist/adapter/ezsp/adapter/index.js +10 -10
  30. package/dist/adapter/ezsp/driver/commands.d.ts +36 -36
  31. package/dist/adapter/ezsp/driver/commands.js +2359 -2359
  32. package/dist/adapter/ezsp/driver/consts.d.ts +10 -10
  33. package/dist/adapter/ezsp/driver/consts.js +13 -13
  34. package/dist/adapter/ezsp/driver/driver.d.ts +103 -103
  35. package/dist/adapter/ezsp/driver/driver.js +639 -639
  36. package/dist/adapter/ezsp/driver/ezsp.d.ts +96 -96
  37. package/dist/adapter/ezsp/driver/ezsp.js +586 -586
  38. package/dist/adapter/ezsp/driver/index.d.ts +3 -3
  39. package/dist/adapter/ezsp/driver/index.js +8 -8
  40. package/dist/adapter/ezsp/driver/multicast.d.ts +12 -12
  41. package/dist/adapter/ezsp/driver/multicast.js +74 -74
  42. package/dist/adapter/ezsp/driver/parser.d.ts +12 -12
  43. package/dist/adapter/ezsp/driver/parser.js +111 -111
  44. package/dist/adapter/ezsp/driver/types/basic.d.ts +62 -62
  45. package/dist/adapter/ezsp/driver/types/basic.js +208 -208
  46. package/dist/adapter/ezsp/driver/types/basic.js.map +1 -1
  47. package/dist/adapter/ezsp/driver/types/index.d.ts +9 -9
  48. package/dist/adapter/ezsp/driver/types/index.js +133 -133
  49. package/dist/adapter/ezsp/driver/types/named.d.ts +697 -697
  50. package/dist/adapter/ezsp/driver/types/named.js +1726 -1726
  51. package/dist/adapter/ezsp/driver/types/named.js.map +1 -1
  52. package/dist/adapter/ezsp/driver/types/struct.d.ts +251 -251
  53. package/dist/adapter/ezsp/driver/types/struct.js +708 -708
  54. package/dist/adapter/ezsp/driver/types/struct.js.map +1 -1
  55. package/dist/adapter/ezsp/driver/uart.d.ts +44 -44
  56. package/dist/adapter/ezsp/driver/uart.js +368 -368
  57. package/dist/adapter/ezsp/driver/utils/crc16ccitt.d.ts +2 -2
  58. package/dist/adapter/ezsp/driver/utils/crc16ccitt.js +55 -55
  59. package/dist/adapter/ezsp/driver/utils/index.d.ts +18 -18
  60. package/dist/adapter/ezsp/driver/utils/index.js +67 -67
  61. package/dist/adapter/ezsp/driver/writer.d.ts +13 -13
  62. package/dist/adapter/ezsp/driver/writer.js +88 -88
  63. package/dist/adapter/index.d.ts +4 -4
  64. package/dist/adapter/index.js +35 -35
  65. package/dist/adapter/serialPort.d.ts +8 -8
  66. package/dist/adapter/serialPort.js +22 -22
  67. package/dist/adapter/serialPort.js.map +1 -1
  68. package/dist/adapter/serialPortUtils.d.ts +12 -12
  69. package/dist/adapter/serialPortUtils.js +18 -18
  70. package/dist/adapter/socketPortUtils.d.ts +10 -10
  71. package/dist/adapter/socketPortUtils.js +16 -16
  72. package/dist/adapter/tstype.d.ts +85 -85
  73. package/dist/adapter/tstype.js +2 -2
  74. package/dist/adapter/z-stack/adapter/adapter-backup.d.ts +62 -62
  75. package/dist/adapter/z-stack/adapter/adapter-backup.d.ts.map +1 -1
  76. package/dist/adapter/z-stack/adapter/adapter-backup.js +460 -441
  77. package/dist/adapter/z-stack/adapter/adapter-backup.js.map +1 -1
  78. package/dist/adapter/z-stack/adapter/adapter-nv-memory.d.ts +150 -150
  79. package/dist/adapter/z-stack/adapter/adapter-nv-memory.js +258 -258
  80. package/dist/adapter/z-stack/adapter/endpoints.d.ts +11 -11
  81. package/dist/adapter/z-stack/adapter/endpoints.js +73 -73
  82. package/dist/adapter/z-stack/adapter/index.d.ts +2 -2
  83. package/dist/adapter/z-stack/adapter/index.js +8 -8
  84. package/dist/adapter/z-stack/adapter/manager.d.ts +86 -86
  85. package/dist/adapter/z-stack/adapter/manager.js +476 -476
  86. package/dist/adapter/z-stack/adapter/tstype.d.ts +6 -6
  87. package/dist/adapter/z-stack/adapter/tstype.js +10 -10
  88. package/dist/adapter/z-stack/adapter/zStackAdapter.d.ts +81 -81
  89. package/dist/adapter/z-stack/adapter/zStackAdapter.d.ts.map +1 -1
  90. package/dist/adapter/z-stack/adapter/zStackAdapter.js +868 -868
  91. package/dist/adapter/z-stack/adapter/zStackAdapter.js.map +1 -1
  92. package/dist/adapter/z-stack/constants/af.d.ts +23 -23
  93. package/dist/adapter/z-stack/constants/af.js +27 -27
  94. package/dist/adapter/z-stack/constants/common.d.ts +278 -278
  95. package/dist/adapter/z-stack/constants/common.js +289 -289
  96. package/dist/adapter/z-stack/constants/dbg.d.ts +22 -22
  97. package/dist/adapter/z-stack/constants/dbg.js +24 -24
  98. package/dist/adapter/z-stack/constants/index.d.ts +10 -10
  99. package/dist/adapter/z-stack/constants/index.js +47 -47
  100. package/dist/adapter/z-stack/constants/mac.d.ts +127 -127
  101. package/dist/adapter/z-stack/constants/mac.js +129 -129
  102. package/dist/adapter/z-stack/constants/sapi.d.ts +24 -24
  103. package/dist/adapter/z-stack/constants/sapi.js +26 -26
  104. package/dist/adapter/z-stack/constants/sys.d.ts +71 -71
  105. package/dist/adapter/z-stack/constants/sys.js +73 -73
  106. package/dist/adapter/z-stack/constants/util.d.ts +81 -81
  107. package/dist/adapter/z-stack/constants/util.js +83 -83
  108. package/dist/adapter/z-stack/constants/utils.d.ts +4 -4
  109. package/dist/adapter/z-stack/constants/utils.js +14 -14
  110. package/dist/adapter/z-stack/constants/zdo.d.ts +102 -102
  111. package/dist/adapter/z-stack/constants/zdo.js +104 -104
  112. package/dist/adapter/z-stack/models/index.d.ts +1 -1
  113. package/dist/adapter/z-stack/models/index.js +17 -17
  114. package/dist/adapter/z-stack/models/startup-options.d.ts +12 -12
  115. package/dist/adapter/z-stack/models/startup-options.js +2 -2
  116. package/dist/adapter/z-stack/structs/entries/address-manager-entry.d.ts +23 -23
  117. package/dist/adapter/z-stack/structs/entries/address-manager-entry.js +45 -45
  118. package/dist/adapter/z-stack/structs/entries/address-manager-table.d.ts +10 -10
  119. package/dist/adapter/z-stack/structs/entries/address-manager-table.js +22 -22
  120. package/dist/adapter/z-stack/structs/entries/aps-link-key-data-entry.d.ts +10 -10
  121. package/dist/adapter/z-stack/structs/entries/aps-link-key-data-entry.js +21 -21
  122. package/dist/adapter/z-stack/structs/entries/aps-link-key-data-table.d.ts +10 -10
  123. package/dist/adapter/z-stack/structs/entries/aps-link-key-data-table.js +23 -23
  124. package/dist/adapter/z-stack/structs/entries/aps-tc-link-key-entry.d.ts +10 -10
  125. package/dist/adapter/z-stack/structs/entries/aps-tc-link-key-entry.js +24 -24
  126. package/dist/adapter/z-stack/structs/entries/aps-tc-link-key-table.d.ts +10 -10
  127. package/dist/adapter/z-stack/structs/entries/aps-tc-link-key-table.js +23 -23
  128. package/dist/adapter/z-stack/structs/entries/channel-list.d.ts +8 -8
  129. package/dist/adapter/z-stack/structs/entries/channel-list.js +15 -15
  130. package/dist/adapter/z-stack/structs/entries/has-configured.d.ts +8 -8
  131. package/dist/adapter/z-stack/structs/entries/has-configured.js +16 -16
  132. package/dist/adapter/z-stack/structs/entries/index.d.ts +16 -16
  133. package/dist/adapter/z-stack/structs/entries/index.js +32 -32
  134. package/dist/adapter/z-stack/structs/entries/nib.d.ts +10 -10
  135. package/dist/adapter/z-stack/structs/entries/nib.js +68 -68
  136. package/dist/adapter/z-stack/structs/entries/nwk-key-descriptor.d.ts +10 -10
  137. package/dist/adapter/z-stack/structs/entries/nwk-key-descriptor.js +18 -18
  138. package/dist/adapter/z-stack/structs/entries/nwk-key.d.ts +8 -8
  139. package/dist/adapter/z-stack/structs/entries/nwk-key.js +15 -15
  140. package/dist/adapter/z-stack/structs/entries/nwk-pan-id.d.ts +8 -8
  141. package/dist/adapter/z-stack/structs/entries/nwk-pan-id.js +15 -15
  142. package/dist/adapter/z-stack/structs/entries/nwk-sec-material-descriptor-entry.d.ts +13 -13
  143. package/dist/adapter/z-stack/structs/entries/nwk-sec-material-descriptor-entry.js +23 -23
  144. package/dist/adapter/z-stack/structs/entries/nwk-sec-material-descriptor-table.d.ts +10 -10
  145. package/dist/adapter/z-stack/structs/entries/nwk-sec-material-descriptor-table.js +22 -22
  146. package/dist/adapter/z-stack/structs/entries/security-manager-entry.d.ts +20 -20
  147. package/dist/adapter/z-stack/structs/entries/security-manager-entry.js +36 -36
  148. package/dist/adapter/z-stack/structs/entries/security-manager-table.d.ts +10 -10
  149. package/dist/adapter/z-stack/structs/entries/security-manager-table.js +24 -24
  150. package/dist/adapter/z-stack/structs/index.d.ts +4 -4
  151. package/dist/adapter/z-stack/structs/index.js +20 -20
  152. package/dist/adapter/z-stack/structs/serializable-memory-object.d.ts +13 -13
  153. package/dist/adapter/z-stack/structs/serializable-memory-object.js +2 -2
  154. package/dist/adapter/z-stack/structs/struct.d.ts +99 -99
  155. package/dist/adapter/z-stack/structs/struct.js +295 -295
  156. package/dist/adapter/z-stack/structs/table.d.ts +94 -94
  157. package/dist/adapter/z-stack/structs/table.js +161 -161
  158. package/dist/adapter/z-stack/unpi/constants.d.ts +28 -28
  159. package/dist/adapter/z-stack/unpi/constants.js +41 -41
  160. package/dist/adapter/z-stack/unpi/frame.d.ts +16 -16
  161. package/dist/adapter/z-stack/unpi/frame.js +48 -48
  162. package/dist/adapter/z-stack/unpi/index.d.ts +5 -5
  163. package/dist/adapter/z-stack/unpi/index.js +37 -37
  164. package/dist/adapter/z-stack/unpi/parser.d.ts +10 -10
  165. package/dist/adapter/z-stack/unpi/parser.js +74 -74
  166. package/dist/adapter/z-stack/unpi/writer.d.ts +10 -10
  167. package/dist/adapter/z-stack/unpi/writer.js +44 -44
  168. package/dist/adapter/z-stack/utils/channel-list.d.ts +20 -20
  169. package/dist/adapter/z-stack/utils/channel-list.js +40 -40
  170. package/dist/adapter/z-stack/utils/index.d.ts +2 -2
  171. package/dist/adapter/z-stack/utils/index.js +18 -18
  172. package/dist/adapter/z-stack/utils/network-options.d.ts +8 -8
  173. package/dist/adapter/z-stack/utils/network-options.js +22 -22
  174. package/dist/adapter/z-stack/znp/buffaloZnp.d.ts +11 -11
  175. package/dist/adapter/z-stack/znp/buffaloZnp.js +113 -113
  176. package/dist/adapter/z-stack/znp/definition.d.ts +5 -5
  177. package/dist/adapter/z-stack/znp/definition.js +3050 -3050
  178. package/dist/adapter/z-stack/znp/index.d.ts +3 -3
  179. package/dist/adapter/z-stack/znp/index.js +10 -10
  180. package/dist/adapter/z-stack/znp/parameterType.d.ts +22 -22
  181. package/dist/adapter/z-stack/znp/parameterType.js +25 -25
  182. package/dist/adapter/z-stack/znp/tstype.d.ts +21 -21
  183. package/dist/adapter/z-stack/znp/tstype.js +2 -2
  184. package/dist/adapter/z-stack/znp/znp.d.ts +43 -43
  185. package/dist/adapter/z-stack/znp/znp.js +325 -325
  186. package/dist/adapter/z-stack/znp/zpiObject.d.ts +19 -19
  187. package/dist/adapter/z-stack/znp/zpiObject.js +96 -96
  188. package/dist/adapter/zigate/adapter/index.d.ts +2 -2
  189. package/dist/adapter/zigate/adapter/index.js +10 -10
  190. package/dist/adapter/zigate/adapter/zigateAdapter.d.ts +69 -69
  191. package/dist/adapter/zigate/adapter/zigateAdapter.js +678 -678
  192. package/dist/adapter/zigate/debug.d.ts +7 -7
  193. package/dist/adapter/zigate/debug.js +22 -22
  194. package/dist/adapter/zigate/driver/buffaloZiGate.d.ts +18 -18
  195. package/dist/adapter/zigate/driver/buffaloZiGate.js +139 -139
  196. package/dist/adapter/zigate/driver/commandType.d.ts +41 -41
  197. package/dist/adapter/zigate/driver/commandType.js +385 -385
  198. package/dist/adapter/zigate/driver/constants.d.ts +276 -276
  199. package/dist/adapter/zigate/driver/constants.js +371 -371
  200. package/dist/adapter/zigate/driver/frame.d.ts +26 -26
  201. package/dist/adapter/zigate/driver/frame.js +172 -172
  202. package/dist/adapter/zigate/driver/frame.js.map +1 -1
  203. package/dist/adapter/zigate/driver/messageType.d.ts +11 -11
  204. package/dist/adapter/zigate/driver/messageType.js +278 -278
  205. package/dist/adapter/zigate/driver/parameterType.d.ts +20 -20
  206. package/dist/adapter/zigate/driver/parameterType.js +23 -23
  207. package/dist/adapter/zigate/driver/ziGateObject.d.ts +23 -23
  208. package/dist/adapter/zigate/driver/ziGateObject.js +106 -106
  209. package/dist/adapter/zigate/driver/zigate.d.ts +49 -49
  210. package/dist/adapter/zigate/driver/zigate.js +303 -303
  211. package/dist/buffalo/buffalo.d.ts +50 -50
  212. package/dist/buffalo/buffalo.js +322 -322
  213. package/dist/buffalo/index.d.ts +3 -3
  214. package/dist/buffalo/index.js +33 -33
  215. package/dist/buffalo/tstype.d.ts +8 -8
  216. package/dist/buffalo/tstype.js +2 -2
  217. package/dist/controller/controller.d.ts +113 -110
  218. package/dist/controller/controller.d.ts.map +1 -1
  219. package/dist/controller/controller.js +619 -607
  220. package/dist/controller/controller.js.map +1 -1
  221. package/dist/controller/database.d.ts +18 -18
  222. package/dist/controller/database.js +93 -93
  223. package/dist/controller/events.d.ts +58 -55
  224. package/dist/controller/events.d.ts.map +1 -1
  225. package/dist/controller/events.js +101 -101
  226. package/dist/controller/events.js.map +1 -1
  227. package/dist/controller/greenPower.d.ts +12 -12
  228. package/dist/controller/greenPower.js +220 -220
  229. package/dist/controller/helpers/index.d.ts +2 -2
  230. package/dist/controller/helpers/index.js +28 -28
  231. package/dist/controller/helpers/request.d.ts +22 -22
  232. package/dist/controller/helpers/request.js +71 -71
  233. package/dist/controller/helpers/zclFrameConverter.d.ts +7 -7
  234. package/dist/controller/helpers/zclFrameConverter.js +31 -31
  235. package/dist/controller/helpers/zclTransactionSequenceNumber.d.ts +5 -5
  236. package/dist/controller/helpers/zclTransactionSequenceNumber.js +13 -13
  237. package/dist/controller/index.d.ts +5 -5
  238. package/dist/controller/index.js +8 -8
  239. package/dist/controller/logger-stub.d.ts +6 -6
  240. package/dist/controller/logger-stub.js +2 -2
  241. package/dist/controller/model/device.d.ts +133 -132
  242. package/dist/controller/model/device.d.ts.map +1 -1
  243. package/dist/controller/model/device.js +717 -708
  244. package/dist/controller/model/device.js.map +1 -1
  245. package/dist/controller/model/endpoint.d.ts +131 -131
  246. package/dist/controller/model/endpoint.d.ts.map +1 -1
  247. package/dist/controller/model/endpoint.js +816 -817
  248. package/dist/controller/model/endpoint.js.map +1 -1
  249. package/dist/controller/model/entity.d.ts +14 -14
  250. package/dist/controller/model/entity.js +26 -26
  251. package/dist/controller/model/group.d.ts +38 -38
  252. package/dist/controller/model/group.js +221 -221
  253. package/dist/controller/model/index.d.ts +5 -5
  254. package/dist/controller/model/index.js +14 -14
  255. package/dist/controller/touchlink.d.ts +19 -19
  256. package/dist/controller/touchlink.js +157 -157
  257. package/dist/controller/tstype.d.ts +21 -21
  258. package/dist/controller/tstype.js +9 -9
  259. package/dist/index.d.ts +3 -3
  260. package/dist/index.js +33 -33
  261. package/dist/models/backup-storage-legacy.d.ts +26 -26
  262. package/dist/models/backup-storage-legacy.js +2 -2
  263. package/dist/models/backup-storage-unified.d.ts +49 -49
  264. package/dist/models/backup-storage-unified.js +2 -2
  265. package/dist/models/backup.d.ts +37 -37
  266. package/dist/models/backup.js +2 -2
  267. package/dist/models/index.d.ts +4 -4
  268. package/dist/models/index.js +20 -20
  269. package/dist/models/network-options.d.ts +12 -12
  270. package/dist/models/network-options.js +2 -2
  271. package/dist/utils/assertString.d.ts +2 -2
  272. package/dist/utils/assertString.js +8 -8
  273. package/dist/utils/backup.d.ts +20 -20
  274. package/dist/utils/backup.js +187 -187
  275. package/dist/utils/equalsPartial.d.ts +2 -2
  276. package/dist/utils/equalsPartial.js +11 -11
  277. package/dist/utils/index.d.ts +9 -9
  278. package/dist/utils/index.js +45 -45
  279. package/dist/utils/isNumberArray.d.ts +2 -2
  280. package/dist/utils/isNumberArray.js +6 -6
  281. package/dist/utils/queue.d.ts +11 -11
  282. package/dist/utils/queue.js +50 -50
  283. package/dist/utils/realpathSync.d.ts +2 -2
  284. package/dist/utils/realpathSync.js +12 -12
  285. package/dist/utils/wait.d.ts +2 -2
  286. package/dist/utils/wait.js +8 -8
  287. package/dist/utils/waitress.d.ts +21 -21
  288. package/dist/utils/waitress.js +61 -61
  289. package/dist/zcl/buffaloZcl.d.ts +41 -41
  290. package/dist/zcl/buffaloZcl.js +591 -591
  291. package/dist/zcl/definition/buffaloZclDataType.d.ts +17 -17
  292. package/dist/zcl/definition/buffaloZclDataType.js +20 -20
  293. package/dist/zcl/definition/cluster.d.ts +29 -29
  294. package/dist/zcl/definition/cluster.js +5335 -5335
  295. package/dist/zcl/definition/dataType.d.ts +59 -59
  296. package/dist/zcl/definition/dataType.js +64 -64
  297. package/dist/zcl/definition/direction.d.ts +5 -5
  298. package/dist/zcl/definition/direction.js +8 -8
  299. package/dist/zcl/definition/endpointDeviceType.d.ts +4 -4
  300. package/dist/zcl/definition/endpointDeviceType.js +15 -15
  301. package/dist/zcl/definition/foundation.d.ts +11 -11
  302. package/dist/zcl/definition/foundation.js +167 -167
  303. package/dist/zcl/definition/frameControl.d.ts +10 -10
  304. package/dist/zcl/definition/frameControl.js +2 -2
  305. package/dist/zcl/definition/frameType.d.ts +5 -5
  306. package/dist/zcl/definition/frameType.js +8 -8
  307. package/dist/zcl/definition/index.d.ts +13 -13
  308. package/dist/zcl/definition/index.js +51 -51
  309. package/dist/zcl/definition/manufacturerCode.d.ts +1074 -1074
  310. package/dist/zcl/definition/manufacturerCode.js +1079 -1079
  311. package/dist/zcl/definition/powerSource.d.ts +4 -4
  312. package/dist/zcl/definition/powerSource.js +12 -12
  313. package/dist/zcl/definition/status.d.ts +38 -38
  314. package/dist/zcl/definition/status.js +41 -41
  315. package/dist/zcl/definition/tstype.d.ts +16 -16
  316. package/dist/zcl/definition/tstype.js +2 -2
  317. package/dist/zcl/index.d.ts +15 -15
  318. package/dist/zcl/index.js +55 -55
  319. package/dist/zcl/tstype.d.ts +56 -56
  320. package/dist/zcl/tstype.js +10 -10
  321. package/dist/zcl/utils.d.ts +6 -6
  322. package/dist/zcl/utils.js +165 -165
  323. package/dist/zcl/zclFrame.d.ts +45 -45
  324. package/dist/zcl/zclFrame.js +347 -347
  325. package/dist/zcl/zclStatusError.d.ts +5 -5
  326. package/dist/zcl/zclStatusError.js +13 -13
  327. package/package.json +1 -1
@@ -1,818 +1,817 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- const entity_1 = __importDefault(require("./entity"));
30
- const Zcl = __importStar(require("../../zcl"));
31
- const zclTransactionSequenceNumber_1 = __importDefault(require("../helpers/zclTransactionSequenceNumber"));
32
- const ZclFrameConverter = __importStar(require("../helpers/zclFrameConverter"));
33
- const request_1 = __importDefault(require("../helpers/request"));
34
- const group_1 = __importDefault(require("./group"));
35
- const device_1 = __importDefault(require("./device"));
36
- const debug_1 = __importDefault(require("debug"));
37
- const assert_1 = __importDefault(require("assert"));
38
- const debug = {
39
- info: (0, debug_1.default)('zigbee-herdsman:controller:endpoint'),
40
- error: (0, debug_1.default)('zigbee-herdsman:controller:endpoint'),
41
- };
42
- class Endpoint extends entity_1.default {
43
- // Getters/setters
44
- get binds() {
45
- return this._binds
46
- .map((entry) => {
47
- let target = null;
48
- if (entry.type === 'endpoint') {
49
- const device = device_1.default.byIeeeAddr(this.databaseID, entry.deviceIeeeAddress);
50
- if (device) {
51
- target = device.getEndpoint(entry.endpointID);
52
- }
53
- }
54
- else {
55
- target = group_1.default.byGroupID(entry.groupID, this.databaseID);
56
- }
57
- if (target) {
58
- return { target, cluster: Zcl.Utils.getCluster(entry.cluster) };
59
- }
60
- else {
61
- return undefined;
62
- }
63
- })
64
- .filter((b) => b !== undefined);
65
- }
66
- get configuredReportings() {
67
- return this._configuredReportings.map((entry) => {
68
- const cluster = Zcl.Utils.getCluster(entry.cluster, this.getDevice().manufacturerID);
69
- let attribute;
70
- if (cluster.hasAttribute(entry.attrId)) {
71
- attribute = cluster.getAttribute(entry.attrId);
72
- }
73
- else {
74
- attribute = {
75
- ID: entry.attrId,
76
- name: undefined,
77
- type: undefined,
78
- manufacturerCode: undefined,
79
- };
80
- }
81
- return {
82
- cluster,
83
- attribute,
84
- minimumReportInterval: entry.minRepIntval,
85
- maximumReportInterval: entry.maxRepIntval,
86
- reportableChange: entry.repChange,
87
- };
88
- });
89
- }
90
- constructor(databaseID, ID, profileID, deviceID, inputClusters, outputClusters, deviceNetworkAddress, deviceIeeeAddress, clusters, binds, configuredReportings, meta) {
91
- super();
92
- this.databaseID = databaseID;
93
- this.ID = ID;
94
- this.profileID = profileID;
95
- this.deviceID = deviceID;
96
- this.inputClusters = inputClusters;
97
- this.outputClusters = outputClusters;
98
- this.deviceNetworkAddress = deviceNetworkAddress;
99
- this.deviceIeeeAddress = deviceIeeeAddress;
100
- this.clusters = clusters;
101
- this._binds = binds;
102
- this._configuredReportings = configuredReportings;
103
- this.meta = meta;
104
- this.pendingRequests = new Set();
105
- this.sendInProgress = false;
106
- }
107
- /**
108
- * Get device of this endpoint
109
- */
110
- getDevice() {
111
- return device_1.default.byIeeeAddr(this.databaseID, this.deviceIeeeAddress);
112
- }
113
- /**
114
- * @param {number|string} clusterKey
115
- * @returns {boolean}
116
- */
117
- supportsInputCluster(clusterKey) {
118
- const cluster = Zcl.Utils.getCluster(clusterKey);
119
- return this.inputClusters.includes(cluster.ID);
120
- }
121
- /**
122
- * @param {number|string} clusterKey
123
- * @returns {boolean}
124
- */
125
- supportsOutputCluster(clusterKey) {
126
- const cluster = Zcl.Utils.getCluster(clusterKey);
127
- return this.outputClusters.includes(cluster.ID);
128
- }
129
- /**
130
- * @returns {Zcl.TsType.Cluster[]}
131
- */
132
- getInputClusters() {
133
- return this.clusterNumbersToClusters(this.inputClusters);
134
- }
135
- /**
136
- * @returns {Zcl.TsType.Cluster[]}
137
- */
138
- getOutputClusters() {
139
- return this.clusterNumbersToClusters(this.outputClusters);
140
- }
141
- clusterNumbersToClusters(clusterNumbers) {
142
- return clusterNumbers.map((c) => Zcl.Utils.getCluster(c, this.getDevice().manufacturerID));
143
- }
144
- /*
145
- * CRUD
146
- */
147
- static fromDatabaseRecord(record, deviceNetworkAddress, deviceIeeeAddress, databaseID) {
148
- // Migrate attrs to attributes
149
- for (const entry of Object.values(record.clusters).filter((e) => e.hasOwnProperty('attrs'))) {
150
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
151
- // @ts-ignore
152
- entry.attributes = entry.attrs;
153
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
154
- // @ts-ignore
155
- delete entry.attrs;
156
- }
157
- return new Endpoint(databaseID, record.epId, record.profId, record.devId, record.inClusterList, record.outClusterList, deviceNetworkAddress, deviceIeeeAddress, record.clusters, record.binds || [], record.configuredReportings || [], record.meta || {});
158
- }
159
- toDatabaseRecord() {
160
- return {
161
- profId: this.profileID,
162
- epId: this.ID,
163
- devId: this.deviceID,
164
- inClusterList: this.inputClusters,
165
- outClusterList: this.outputClusters,
166
- clusters: this.clusters,
167
- binds: this._binds,
168
- configuredReportings: this._configuredReportings,
169
- meta: this.meta,
170
- };
171
- }
172
- static create(databaseID, ID, profileID, deviceID, inputClusters, outputClusters, deviceNetworkAddress, deviceIeeeAddress) {
173
- return new Endpoint(databaseID, ID, profileID, deviceID, inputClusters, outputClusters, deviceNetworkAddress, deviceIeeeAddress, {}, [], [], {});
174
- }
175
- saveClusterAttributeKeyValue(clusterKey, list) {
176
- const cluster = Zcl.Utils.getCluster(clusterKey);
177
- if (!this.clusters[cluster.name])
178
- this.clusters[cluster.name] = { attributes: {} };
179
- for (const [attribute, value] of Object.entries(list)) {
180
- this.clusters[cluster.name].attributes[attribute] = value;
181
- }
182
- }
183
- getClusterAttributeValue(clusterKey, attributeKey) {
184
- const cluster = Zcl.Utils.getCluster(clusterKey);
185
- const attribute = cluster.getAttribute(attributeKey);
186
- if (this.clusters[cluster.name] && this.clusters[cluster.name].attributes) {
187
- return this.clusters[cluster.name].attributes[attribute.name];
188
- }
189
- return null;
190
- }
191
- hasPendingRequests() {
192
- return this.pendingRequests.size > 0;
193
- }
194
- async sendPendingRequests(fastPolling) {
195
- if (this.pendingRequests.size === 0)
196
- return;
197
- if (this.sendInProgress) {
198
- debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): sendPendingRequests already in progress`);
199
- return;
200
- }
201
- this.sendInProgress = true;
202
- // Remove expired requests first
203
- const now = Date.now();
204
- for (const request of this.pendingRequests) {
205
- if (now > request.expires) {
206
- debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): discard after timeout. ` +
207
- `Size before: ${this.pendingRequests.size}`);
208
- request.reject();
209
- this.pendingRequests.delete(request);
210
- }
211
- }
212
- debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): send pending requests (` +
213
- `${this.pendingRequests.size}, ${fastPolling})`);
214
- for (const request of this.pendingRequests) {
215
- if (fastPolling ||
216
- (request.sendWhen !== 'fastpoll' && request.sendPolicy !== 'bulk')) {
217
- try {
218
- const result = await request.send();
219
- debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): send success`);
220
- request.resolve(result);
221
- }
222
- catch (error) {
223
- debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): send failed, expires in ` +
224
- `${(request.expires - now) / 1000} seconds`);
225
- request.reject(error);
226
- }
227
- this.pendingRequests.delete(request);
228
- }
229
- }
230
- this.sendInProgress = false;
231
- }
232
- async queueRequest(request) {
233
- debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): Sending when active. ` +
234
- `Timeout ${this.getDevice().pendingRequestTimeout / 1000} seconds`);
235
- return new Promise((resolve, reject) => {
236
- request.addCallbacks(resolve, reject);
237
- this.pendingRequests.add(request);
238
- });
239
- }
240
- filterRequests(newRequest) {
241
- var _a, _b;
242
- if (this.pendingRequests.size === 0 ||
243
- !(typeof newRequest.frame.getCommand === 'function')) {
244
- return;
245
- }
246
- const clusterID = newRequest.frame.Cluster.ID;
247
- const payload = newRequest.frame.Payload;
248
- const commandID = newRequest.frame.getCommand().ID;
249
- debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): ZCL ${newRequest.frame.getCommand().name} ` + `command, filter requests. Before: ${this.pendingRequests.size}`);
250
- for (const request of this.pendingRequests) {
251
- if (((_b = (_a = request === null || request === void 0 ? void 0 : request.frame) === null || _a === void 0 ? void 0 : _a.Cluster) === null || _b === void 0 ? void 0 : _b.ID) === undefined ||
252
- typeof request.frame.getCommand !== 'function') {
253
- continue;
254
- }
255
- if (['bulk', 'queue', 'immediate'].includes(request.sendPolicy)) {
256
- continue;
257
- }
258
- /* istanbul ignore else */
259
- if (request.frame.Cluster.ID === clusterID &&
260
- request.frame.getCommand().ID === commandID) {
261
- /* istanbul ignore else */
262
- if (newRequest.sendPolicy === 'keep-payload' &&
263
- JSON.stringify(request.frame.Payload) === JSON.stringify(payload)) {
264
- debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): Merge duplicate request`);
265
- this.pendingRequests.delete(request);
266
- newRequest.moveCallbacks(request);
267
- }
268
- else if (newRequest.sendPolicy === 'keep-command' ||
269
- newRequest.sendPolicy === 'keep-cmd-undiv') {
270
- const filteredPayload = request.frame.Payload.filter((oldEl) => !payload.find((newEl) => oldEl.attrId === newEl.attrId));
271
- if (filteredPayload.length == 0) {
272
- debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): Remove & reject request`);
273
- if (JSON.stringify(request.frame.Payload) === JSON.stringify(payload)) {
274
- newRequest.moveCallbacks(request);
275
- }
276
- else {
277
- request.reject();
278
- }
279
- this.pendingRequests.delete(request);
280
- }
281
- else if (newRequest.sendPolicy !== 'keep-cmd-undiv') {
282
- // remove all duplicate attributes if we shall not write undivided
283
- request.frame.Payload = filteredPayload;
284
- debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): ` +
285
- `Remove commands from request`);
286
- }
287
- }
288
- }
289
- }
290
- debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): After: ${this.pendingRequests.size}`);
291
- }
292
- async sendRequest(frame, options, func = (d) => {
293
- return entity_1.default.getAdapterByID(this.databaseID).sendZclFrameToEndpoint(this.deviceIeeeAddress, this.deviceNetworkAddress, this.ID, d, options.timeout, options.disableResponse, options.disableRecovery, options.srcEndpoint);
294
- }) {
295
- const logPrefix = `Request Queue (${this.deviceIeeeAddress}/${this.ID}): `;
296
- const request = new request_1.default(func, frame, this.getDevice().pendingRequestTimeout, options.sendWhen, options.sendPolicy);
297
- if (request.sendPolicy !== 'bulk') {
298
- // Check if such a request is already in the queue and remove the old one(s) if necessary
299
- this.filterRequests(request);
300
- }
301
- // send without queueing if sendWhen or sendPolicy is 'immediate' or if the device has no timeout set
302
- if (request.sendWhen === 'immediate' ||
303
- request.sendPolicy === 'immediate' ||
304
- !this.getDevice().pendingRequestTimeout) {
305
- if (this.getDevice().defaultSendRequestWhen !== 'immediate') {
306
- debug.info(logPrefix +
307
- `send ${frame.getCommand().name} request immediately ` +
308
- `(sendWhen=${options.sendWhen})`);
309
- }
310
- return request.send();
311
- }
312
- // If this is a bulk message, we queue directly.
313
- if (request.sendPolicy === 'bulk') {
314
- debug.info(logPrefix +
315
- `queue request (${this.pendingRequests.size} / ${this.sendInProgress})))`);
316
- return this.queueRequest(request);
317
- }
318
- try {
319
- debug.info(logPrefix + `send request`);
320
- return await request.send();
321
- }
322
- catch (error) {
323
- // If we got a failed transaction, the device is likely sleeping.
324
- // Queue for transmission later.
325
- debug.info(logPrefix + `queue request (transaction failed)`);
326
- return this.queueRequest(request);
327
- }
328
- }
329
- /*
330
- * Zigbee functions
331
- */
332
- checkStatus(payload) {
333
- const codes = Array.isArray(payload)
334
- ? payload.map((i) => i.status)
335
- : [payload.statusCode];
336
- const invalid = codes.find((c) => c !== Zcl.Status.SUCCESS);
337
- if (invalid)
338
- throw new Zcl.ZclStatusError(invalid);
339
- }
340
- async report(clusterKey, attributes, options) {
341
- var _a;
342
- const cluster = Zcl.Utils.getCluster(clusterKey);
343
- options = this.getOptionsWithDefaults(options, true, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);
344
- const payload = [];
345
- for (const [nameOrID, value] of Object.entries(attributes)) {
346
- if (cluster.hasAttribute(nameOrID)) {
347
- const attribute = cluster.getAttribute(nameOrID);
348
- payload.push({
349
- attrId: attribute.ID,
350
- attrData: value,
351
- dataType: attribute.type,
352
- });
353
- }
354
- else if (!isNaN(Number(nameOrID))) {
355
- payload.push({
356
- attrId: Number(nameOrID),
357
- attrData: value.value,
358
- dataType: value.type,
359
- });
360
- }
361
- else {
362
- throw new Error(`Unknown attribute '${nameOrID}', specify either an existing attribute or a number`);
363
- }
364
- }
365
- const log = `Report to ${this.deviceIeeeAddress}/${this.ID} ` +
366
- `${cluster.name}(${JSON.stringify(attributes)}, ${JSON.stringify(options)})`;
367
- debug.info(log);
368
- try {
369
- const frame = Zcl.ZclFrame.create(Zcl.FrameType.GLOBAL, options.direction, options.disableDefaultResponse, options.manufacturerCode, (_a = options.transactionSequenceNumber) !== null && _a !== void 0 ? _a : zclTransactionSequenceNumber_1.default.next(), 'report', cluster.ID, payload, options.reservedBits);
370
- await this.sendRequest(frame, options);
371
- }
372
- catch (error) {
373
- error.message = `${log} failed (${error.message})`;
374
- debug.error(error.message);
375
- throw error;
376
- }
377
- }
378
- async write(clusterKey, attributes, options) {
379
- var _a;
380
- const cluster = Zcl.Utils.getCluster(clusterKey);
381
- options = this.getOptionsWithDefaults(options, true, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);
382
- const payload = [];
383
- for (const [nameOrID, value] of Object.entries(attributes)) {
384
- if (cluster.hasAttribute(nameOrID)) {
385
- const attribute = cluster.getAttribute(nameOrID);
386
- payload.push({
387
- attrId: attribute.ID,
388
- attrData: value,
389
- dataType: attribute.type,
390
- });
391
- }
392
- else if (!isNaN(Number(nameOrID))) {
393
- payload.push({
394
- attrId: Number(nameOrID),
395
- attrData: value.value,
396
- dataType: value.type,
397
- });
398
- }
399
- else {
400
- throw new Error(`Unknown attribute '${nameOrID}', specify either an existing attribute or a number`);
401
- }
402
- }
403
- const log = `Write ${this.deviceIeeeAddress}/${this.ID} ` +
404
- `${cluster.name}(${JSON.stringify(attributes)}, ${JSON.stringify(options)})`;
405
- debug.info(log);
406
- try {
407
- const frame = Zcl.ZclFrame.create(Zcl.FrameType.GLOBAL, options.direction, options.disableDefaultResponse, options.manufacturerCode, (_a = options.transactionSequenceNumber) !== null && _a !== void 0 ? _a : zclTransactionSequenceNumber_1.default.next(), options.writeUndiv ? 'writeUndiv' : 'write', cluster.ID, payload, options.reservedBits);
408
- const result = await this.sendRequest(frame, options);
409
- if (!options.disableResponse) {
410
- this.checkStatus(result.frame.Payload);
411
- }
412
- }
413
- catch (error) {
414
- error.message = `${log} failed (${error.message})`;
415
- debug.error(error.message);
416
- throw error;
417
- }
418
- }
419
- async writeResponse(clusterKey, transactionSequenceNumber, attributes, options) {
420
- (0, assert_1.default)(!options || !options.hasOwnProperty('transactionSequenceNumber'), 'Use parameter');
421
- const cluster = Zcl.Utils.getCluster(clusterKey);
422
- options = this.getOptionsWithDefaults(options, true, Zcl.Direction.SERVER_TO_CLIENT, cluster.manufacturerCode);
423
- const payload = [];
424
- for (const [nameOrID, value] of Object.entries(attributes)) {
425
- if (value.hasOwnProperty('status')) {
426
- if (cluster.hasAttribute(nameOrID)) {
427
- const attribute = cluster.getAttribute(nameOrID);
428
- payload.push({ attrId: attribute.ID, status: value.status });
429
- }
430
- else if (!isNaN(Number(nameOrID))) {
431
- payload.push({ attrId: Number(nameOrID), status: value.status });
432
- }
433
- else {
434
- throw new Error(`Unknown attribute '${nameOrID}', specify either an existing attribute or a number`);
435
- }
436
- }
437
- else {
438
- throw new Error(`Missing attribute 'status'`);
439
- }
440
- }
441
- const frame = Zcl.ZclFrame.create(Zcl.FrameType.GLOBAL, options.direction, options.disableDefaultResponse, options.manufacturerCode, transactionSequenceNumber, 'writeRsp', cluster.ID, payload, options.reservedBits);
442
- const log = `WriteResponse ${this.deviceIeeeAddress}/${this.ID} ` +
443
- `${cluster.name}(${JSON.stringify(attributes)}, ${JSON.stringify(options)})`;
444
- debug.info(log);
445
- try {
446
- await this.sendRequest(frame, options);
447
- }
448
- catch (error) {
449
- error.message = `${log} failed (${error.message})`;
450
- debug.error(error.message);
451
- throw error;
452
- }
453
- }
454
- async read(clusterKey, attributes, options) {
455
- var _a;
456
- const cluster = Zcl.Utils.getCluster(clusterKey);
457
- options = this.getOptionsWithDefaults(options, true, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);
458
- const payload = [];
459
- for (const attribute of attributes) {
460
- payload.push({
461
- attrId: typeof attribute === 'number'
462
- ? attribute
463
- : cluster.getAttribute(attribute).ID,
464
- });
465
- }
466
- const frame = Zcl.ZclFrame.create(Zcl.FrameType.GLOBAL, options.direction, options.disableDefaultResponse, options.manufacturerCode, (_a = options.transactionSequenceNumber) !== null && _a !== void 0 ? _a : zclTransactionSequenceNumber_1.default.next(), 'read', cluster.ID, payload, options.reservedBits);
467
- const log = `Read ${this.deviceIeeeAddress}/${this.ID} ` +
468
- `${cluster.name}(${JSON.stringify(attributes)}, ${JSON.stringify(options)})`;
469
- debug.info(log);
470
- try {
471
- const result = await this.sendRequest(frame, options);
472
- if (!options.disableResponse) {
473
- this.checkStatus(result.frame.Payload);
474
- return ZclFrameConverter.attributeKeyValue(result.frame);
475
- }
476
- else {
477
- return null;
478
- }
479
- }
480
- catch (error) {
481
- error.message = `${log} failed (${error.message})`;
482
- debug.error(error.message);
483
- throw error;
484
- }
485
- }
486
- async readResponse(clusterKey, transactionSequenceNumber, attributes, options) {
487
- (0, assert_1.default)(!options || !options.hasOwnProperty('transactionSequenceNumber'), 'Use parameter');
488
- const cluster = Zcl.Utils.getCluster(clusterKey);
489
- options = this.getOptionsWithDefaults(options, true, Zcl.Direction.SERVER_TO_CLIENT, cluster.manufacturerCode);
490
- const payload = [];
491
- for (const [nameOrID, value] of Object.entries(attributes)) {
492
- if (cluster.hasAttribute(nameOrID)) {
493
- const attribute = cluster.getAttribute(nameOrID);
494
- payload.push({
495
- attrId: attribute.ID,
496
- attrData: value,
497
- dataType: attribute.type,
498
- status: 0,
499
- });
500
- }
501
- else if (!isNaN(Number(nameOrID))) {
502
- payload.push({
503
- attrId: Number(nameOrID),
504
- attrData: value.value,
505
- dataType: value.type,
506
- status: 0,
507
- });
508
- }
509
- else {
510
- throw new Error(`Unknown attribute '${nameOrID}', specify either an existing attribute or a number`);
511
- }
512
- }
513
- const frame = Zcl.ZclFrame.create(Zcl.FrameType.GLOBAL, options.direction, options.disableDefaultResponse, options.manufacturerCode, transactionSequenceNumber, 'readRsp', cluster.ID, payload, options.reservedBits);
514
- const log = `ReadResponse ${this.deviceIeeeAddress}/${this.ID} ` +
515
- `${cluster.name}(${JSON.stringify(attributes)}, ${JSON.stringify(options)})`;
516
- debug.info(log);
517
- try {
518
- await this.sendRequest(frame, options);
519
- }
520
- catch (error) {
521
- error.message = `${log} failed (${error.message})`;
522
- debug.error(error.message);
523
- throw error;
524
- }
525
- }
526
- addBinding(clusterKey, target) {
527
- const cluster = Zcl.Utils.getCluster(clusterKey);
528
- if (typeof target === 'number') {
529
- target =
530
- group_1.default.byGroupID(target, this.databaseID) ||
531
- group_1.default.create(target, this.databaseID);
532
- }
533
- if (!this.binds.find((b) => b.cluster.ID === cluster.ID && b.target === target)) {
534
- if (target instanceof group_1.default) {
535
- this._binds.push({
536
- cluster: cluster.ID,
537
- groupID: target.groupID,
538
- type: 'group',
539
- });
540
- }
541
- else {
542
- this._binds.push({
543
- cluster: cluster.ID,
544
- type: 'endpoint',
545
- deviceIeeeAddress: target.deviceIeeeAddress,
546
- endpointID: target.ID,
547
- });
548
- }
549
- this.save();
550
- }
551
- }
552
- async bind(clusterKey, target) {
553
- const cluster = Zcl.Utils.getCluster(clusterKey);
554
- const type = target instanceof Endpoint ? 'endpoint' : 'group';
555
- if (typeof target === 'number') {
556
- target =
557
- group_1.default.byGroupID(target, this.databaseID) ||
558
- group_1.default.create(target, this.databaseID);
559
- }
560
- const destinationAddress = target instanceof Endpoint ? target.deviceIeeeAddress : target.groupID;
561
- const log = `Bind ${this.deviceIeeeAddress}/${this.ID} ${cluster.name} from ` +
562
- `'${target instanceof Endpoint
563
- ? `${destinationAddress}/${target.ID}`
564
- : destinationAddress}'`;
565
- debug.info(log);
566
- try {
567
- await entity_1.default.getAdapterByID(this.databaseID).bind(this.deviceNetworkAddress, this.deviceIeeeAddress, this.ID, cluster.ID, destinationAddress, type, target instanceof Endpoint ? target.ID : null);
568
- this.addBinding(clusterKey, target);
569
- }
570
- catch (error) {
571
- error.message = `${log} failed (${error.message})`;
572
- debug.error(error.message);
573
- throw error;
574
- }
575
- }
576
- save() {
577
- this.getDevice().save();
578
- }
579
- async unbind(clusterKey, target) {
580
- const cluster = Zcl.Utils.getCluster(clusterKey);
581
- const type = target instanceof Endpoint ? 'endpoint' : 'group';
582
- const destinationAddress = target instanceof Endpoint
583
- ? target.deviceIeeeAddress
584
- : target instanceof group_1.default
585
- ? target.groupID
586
- : target;
587
- const log = `Unbind ${this.deviceIeeeAddress}/${this.ID} ${cluster.name} from ` +
588
- `'${target instanceof Endpoint
589
- ? `${destinationAddress}/${target.ID}`
590
- : destinationAddress}'`;
591
- debug.info(log);
592
- try {
593
- await entity_1.default.getAdapterByID(this.databaseID).unbind(this.deviceNetworkAddress, this.deviceIeeeAddress, this.ID, cluster.ID, destinationAddress, type, target instanceof Endpoint ? target.ID : null);
594
- if (typeof target === 'number' &&
595
- group_1.default.byGroupID(target, this.databaseID)) {
596
- target = group_1.default.byGroupID(target, this.databaseID);
597
- }
598
- const index = this.binds.findIndex((b) => b.cluster.ID === cluster.ID && b.target === target);
599
- if (index !== -1) {
600
- this._binds.splice(index, 1);
601
- this.save();
602
- }
603
- }
604
- catch (error) {
605
- error.message = `${log} failed (${error.message})`;
606
- debug.error(error.message);
607
- throw error;
608
- }
609
- }
610
- async defaultResponse(commandID, status, clusterID, transactionSequenceNumber, options) {
611
- (0, assert_1.default)(!options || !options.hasOwnProperty('transactionSequenceNumber'), 'Use parameter');
612
- options = this.getOptionsWithDefaults(options, true, Zcl.Direction.SERVER_TO_CLIENT, null);
613
- const payload = { cmdId: commandID, statusCode: status };
614
- const frame = Zcl.ZclFrame.create(Zcl.FrameType.GLOBAL, options.direction, options.disableDefaultResponse, options.manufacturerCode, transactionSequenceNumber, 'defaultRsp', clusterID, payload, options.reservedBits);
615
- const log = `DefaultResponse ${this.deviceIeeeAddress}/${this.ID} ` +
616
- `${clusterID}(${commandID}, ${JSON.stringify(options)})`;
617
- debug.info(log);
618
- try {
619
- await this.sendRequest(frame, options);
620
- }
621
- catch (error) {
622
- error.message = `${log} failed (${error.message})`;
623
- debug.error(error.message);
624
- throw error;
625
- }
626
- }
627
- async configureReporting(clusterKey, items, options) {
628
- var _a;
629
- const cluster = Zcl.Utils.getCluster(clusterKey);
630
- options = this.getOptionsWithDefaults(options, true, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);
631
- const payload = items.map((item) => {
632
- let dataType, attrId;
633
- if (typeof item.attribute === 'object') {
634
- dataType = item.attribute.type;
635
- attrId = item.attribute.ID;
636
- }
637
- else {
638
- /* istanbul ignore else */
639
- if (cluster.hasAttribute(item.attribute)) {
640
- const attribute = cluster.getAttribute(item.attribute);
641
- dataType = attribute.type;
642
- attrId = attribute.ID;
643
- }
644
- }
645
- return {
646
- direction: Zcl.Direction.CLIENT_TO_SERVER,
647
- attrId,
648
- dataType,
649
- minRepIntval: item.minimumReportInterval,
650
- maxRepIntval: item.maximumReportInterval,
651
- repChange: item.reportableChange,
652
- };
653
- });
654
- const frame = Zcl.ZclFrame.create(Zcl.FrameType.GLOBAL, options.direction, options.disableDefaultResponse, options.manufacturerCode, (_a = options.transactionSequenceNumber) !== null && _a !== void 0 ? _a : zclTransactionSequenceNumber_1.default.next(), 'configReport', cluster.ID, payload, options.reservedBits);
655
- const log = `ConfigureReporting ${this.deviceIeeeAddress}/${this.ID} ` +
656
- `${cluster.name}(${JSON.stringify(items)}, ${JSON.stringify(options)})`;
657
- debug.info(log);
658
- try {
659
- const result = await this.sendRequest(frame, options);
660
- if (!options.disableResponse) {
661
- this.checkStatus(result.frame.Payload);
662
- }
663
- for (const e of payload) {
664
- const match = this._configuredReportings.find((c) => c.attrId === e.attrId && c.cluster === cluster.ID);
665
- if (match) {
666
- this._configuredReportings.splice(this._configuredReportings.indexOf(match), 1);
667
- }
668
- }
669
- for (const entry of payload) {
670
- if (entry.maxRepIntval !== 0xffff) {
671
- this._configuredReportings.push({
672
- cluster: cluster.ID,
673
- attrId: entry.attrId,
674
- minRepIntval: entry.minRepIntval,
675
- maxRepIntval: entry.maxRepIntval,
676
- repChange: entry.repChange,
677
- });
678
- }
679
- }
680
- this.save();
681
- }
682
- catch (error) {
683
- error.message = `${log} failed (${error.message})`;
684
- debug.error(error.message);
685
- throw error;
686
- }
687
- }
688
- async writeStructured(clusterKey, payload, options) {
689
- var _a;
690
- const cluster = Zcl.Utils.getCluster(clusterKey);
691
- options = this.getOptionsWithDefaults(options, true, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);
692
- const frame = Zcl.ZclFrame.create(Zcl.FrameType.GLOBAL, options.direction, options.disableDefaultResponse, options.manufacturerCode, (_a = options.transactionSequenceNumber) !== null && _a !== void 0 ? _a : zclTransactionSequenceNumber_1.default.next(), `writeStructured`, cluster.ID, payload, options.reservedBits);
693
- const log = `WriteStructured ${this.deviceIeeeAddress}/${this.ID} ` +
694
- `${cluster.name}(${JSON.stringify(payload)}, ${JSON.stringify(options)})`;
695
- debug.info(log);
696
- try {
697
- await this.sendRequest(frame, options);
698
- // TODO: support `writeStructuredResponse`
699
- }
700
- catch (error) {
701
- error.message = `${log} failed (${error.message})`;
702
- debug.error(error.message);
703
- throw error;
704
- }
705
- }
706
- async command(clusterKey, commandKey, payload, options) {
707
- var _a;
708
- const cluster = Zcl.Utils.getCluster(clusterKey);
709
- const command = cluster.getCommand(commandKey);
710
- const hasResponse = command.hasOwnProperty('response');
711
- options = this.getOptionsWithDefaults(options, hasResponse, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);
712
- const frame = Zcl.ZclFrame.create(Zcl.FrameType.SPECIFIC, options.direction, options.disableDefaultResponse, options.manufacturerCode, (_a = options.transactionSequenceNumber) !== null && _a !== void 0 ? _a : zclTransactionSequenceNumber_1.default.next(), command.name, cluster.name, payload, options.reservedBits);
713
- const log = `Command ${this.deviceIeeeAddress}/${this.ID} ` +
714
- `${cluster.name}.${command.name}(${JSON.stringify(payload)}, ${JSON.stringify(options)})`;
715
- debug.info(log);
716
- try {
717
- const result = await this.sendRequest(frame, options);
718
- if (result) {
719
- return result.frame.Payload;
720
- }
721
- }
722
- catch (error) {
723
- error.message = `${log} failed (${error.message})`;
724
- debug.error(error.message);
725
- throw error;
726
- }
727
- }
728
- async commandResponse(clusterKey, commandKey, payload, options, transactionSequenceNumber) {
729
- (0, assert_1.default)(!options || !options.hasOwnProperty('transactionSequenceNumber'), 'Use parameter');
730
- const cluster = Zcl.Utils.getCluster(clusterKey);
731
- const command = cluster.getCommandResponse(commandKey);
732
- transactionSequenceNumber =
733
- transactionSequenceNumber || zclTransactionSequenceNumber_1.default.next();
734
- options = this.getOptionsWithDefaults(options, true, Zcl.Direction.SERVER_TO_CLIENT, cluster.manufacturerCode);
735
- const frame = Zcl.ZclFrame.create(Zcl.FrameType.SPECIFIC, options.direction, options.disableDefaultResponse, options.manufacturerCode, transactionSequenceNumber, command.name, cluster.name, payload, options.reservedBits);
736
- const log = `CommandResponse ${this.deviceIeeeAddress}/${this.ID} ` +
737
- `${cluster.name}.${command.name}(${JSON.stringify(payload)}, ${JSON.stringify(options)})`;
738
- debug.info(log);
739
- try {
740
- await this.sendRequest(frame, options, async (f) => {
741
- // Broadcast Green Power responses
742
- if (this.ID === 242) {
743
- await entity_1.default.getAdapterByID(this.databaseID).sendZclFrameToAll(242, f, 242);
744
- }
745
- else {
746
- await entity_1.default.getAdapterByID(this.databaseID).sendZclFrameToEndpoint(this.deviceIeeeAddress, this.deviceNetworkAddress, this.ID, f, options.timeout, options.disableResponse, options.disableRecovery, options.srcEndpoint);
747
- }
748
- });
749
- }
750
- catch (error) {
751
- error.message = `${log} failed (${error.message})`;
752
- debug.error(error.message);
753
- throw error;
754
- }
755
- }
756
- waitForCommand(clusterKey, commandKey, transactionSequenceNumber, timeout) {
757
- const cluster = Zcl.Utils.getCluster(clusterKey);
758
- const command = cluster.getCommand(commandKey);
759
- const waiter = entity_1.default.getAdapterByID(this.databaseID).waitFor(this.deviceNetworkAddress, this.ID, Zcl.FrameType.SPECIFIC, Zcl.Direction.CLIENT_TO_SERVER, transactionSequenceNumber, cluster.ID, command.ID, timeout);
760
- const promise = new Promise((resolve, reject) => {
761
- waiter.promise.then((payload) => resolve({
762
- header: payload.frame.Header,
763
- payload: payload.frame.Payload,
764
- }), (error) => reject(error));
765
- });
766
- return { promise, cancel: waiter.cancel };
767
- }
768
- getOptionsWithDefaults(options, disableDefaultResponse, direction, manufacturerCode) {
769
- const providedOptions = options || {};
770
- return {
771
- sendWhen: this.getDevice().defaultSendRequestWhen,
772
- timeout: 10000,
773
- disableResponse: false,
774
- disableRecovery: false,
775
- disableDefaultResponse,
776
- direction,
777
- srcEndpoint: null,
778
- reservedBits: 0,
779
- manufacturerCode: manufacturerCode ? manufacturerCode : null,
780
- transactionSequenceNumber: null,
781
- writeUndiv: false,
782
- ...providedOptions,
783
- };
784
- }
785
- async addToGroup(group) {
786
- await this.command('genGroups', 'add', {
787
- groupid: group.groupID,
788
- groupname: '',
789
- });
790
- group.addMember(this);
791
- }
792
- /**
793
- * Remove endpoint from a group, accepts both a Group and number as parameter.
794
- * The number parameter type should only be used when removing from a group which is not known
795
- * to zigbee-herdsman.
796
- */
797
- async removeFromGroup(group) {
798
- await this.command('genGroups', 'remove', {
799
- groupid: group instanceof group_1.default ? group.groupID : group,
800
- });
801
- if (group instanceof group_1.default) {
802
- group.removeMember(this);
803
- }
804
- }
805
- async removeFromAllGroups() {
806
- await this.command('genGroups', 'removeAll', {}, { disableDefaultResponse: true });
807
- this.removeFromAllGroupsDatabase();
808
- }
809
- removeFromAllGroupsDatabase() {
810
- for (const group of group_1.default.allByDatabaseID(this.databaseID)) {
811
- if (group.hasMember(this)) {
812
- group.removeMember(this);
813
- }
814
- }
815
- }
816
- }
817
- exports.default = Endpoint;
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const entity_1 = __importDefault(require("./entity"));
30
+ const Zcl = __importStar(require("../../zcl"));
31
+ const zclTransactionSequenceNumber_1 = __importDefault(require("../helpers/zclTransactionSequenceNumber"));
32
+ const ZclFrameConverter = __importStar(require("../helpers/zclFrameConverter"));
33
+ const request_1 = __importDefault(require("../helpers/request"));
34
+ const group_1 = __importDefault(require("./group"));
35
+ const device_1 = __importDefault(require("./device"));
36
+ const debug_1 = __importDefault(require("debug"));
37
+ const assert_1 = __importDefault(require("assert"));
38
+ const debug = {
39
+ info: (0, debug_1.default)('zigbee-herdsman:controller:endpoint'),
40
+ error: (0, debug_1.default)('zigbee-herdsman:controller:endpoint'),
41
+ };
42
+ class Endpoint extends entity_1.default {
43
+ // Getters/setters
44
+ get binds() {
45
+ return this._binds
46
+ .map((entry) => {
47
+ let target = null;
48
+ if (entry.type === 'endpoint') {
49
+ const device = device_1.default.byIeeeAddr(this.databaseID, entry.deviceIeeeAddress);
50
+ if (device) {
51
+ target = device.getEndpoint(entry.endpointID);
52
+ }
53
+ }
54
+ else {
55
+ target = group_1.default.byGroupID(entry.groupID, this.databaseID);
56
+ }
57
+ if (target) {
58
+ return { target, cluster: Zcl.Utils.getCluster(entry.cluster, this.getDevice().manufacturerID) };
59
+ }
60
+ else {
61
+ return undefined;
62
+ }
63
+ }).filter(b => b !== undefined);
64
+ }
65
+ get configuredReportings() {
66
+ return this._configuredReportings.map((entry) => {
67
+ const cluster = Zcl.Utils.getCluster(entry.cluster, this.getDevice().manufacturerID);
68
+ let attribute;
69
+ if (cluster.hasAttribute(entry.attrId)) {
70
+ attribute = cluster.getAttribute(entry.attrId);
71
+ }
72
+ else {
73
+ attribute = {
74
+ ID: entry.attrId,
75
+ name: undefined,
76
+ type: undefined,
77
+ manufacturerCode: undefined,
78
+ };
79
+ }
80
+ return {
81
+ cluster,
82
+ attribute,
83
+ minimumReportInterval: entry.minRepIntval,
84
+ maximumReportInterval: entry.maxRepIntval,
85
+ reportableChange: entry.repChange,
86
+ };
87
+ });
88
+ }
89
+ constructor(databaseID, ID, profileID, deviceID, inputClusters, outputClusters, deviceNetworkAddress, deviceIeeeAddress, clusters, binds, configuredReportings, meta) {
90
+ super();
91
+ this.databaseID = databaseID;
92
+ this.ID = ID;
93
+ this.profileID = profileID;
94
+ this.deviceID = deviceID;
95
+ this.inputClusters = inputClusters;
96
+ this.outputClusters = outputClusters;
97
+ this.deviceNetworkAddress = deviceNetworkAddress;
98
+ this.deviceIeeeAddress = deviceIeeeAddress;
99
+ this.clusters = clusters;
100
+ this._binds = binds;
101
+ this._configuredReportings = configuredReportings;
102
+ this.meta = meta;
103
+ this.pendingRequests = new Set();
104
+ this.sendInProgress = false;
105
+ }
106
+ /**
107
+ * Get device of this endpoint
108
+ */
109
+ getDevice() {
110
+ return device_1.default.byIeeeAddr(this.databaseID, this.deviceIeeeAddress);
111
+ }
112
+ /**
113
+ * @param {number|string} clusterKey
114
+ * @returns {boolean}
115
+ */
116
+ supportsInputCluster(clusterKey) {
117
+ const cluster = Zcl.Utils.getCluster(clusterKey);
118
+ return this.inputClusters.includes(cluster.ID);
119
+ }
120
+ /**
121
+ * @param {number|string} clusterKey
122
+ * @returns {boolean}
123
+ */
124
+ supportsOutputCluster(clusterKey) {
125
+ const cluster = Zcl.Utils.getCluster(clusterKey);
126
+ return this.outputClusters.includes(cluster.ID);
127
+ }
128
+ /**
129
+ * @returns {Zcl.TsType.Cluster[]}
130
+ */
131
+ getInputClusters() {
132
+ return this.clusterNumbersToClusters(this.inputClusters);
133
+ }
134
+ /**
135
+ * @returns {Zcl.TsType.Cluster[]}
136
+ */
137
+ getOutputClusters() {
138
+ return this.clusterNumbersToClusters(this.outputClusters);
139
+ }
140
+ clusterNumbersToClusters(clusterNumbers) {
141
+ return clusterNumbers.map((c) => Zcl.Utils.getCluster(c, this.getDevice().manufacturerID));
142
+ }
143
+ /*
144
+ * CRUD
145
+ */
146
+ static fromDatabaseRecord(record, deviceNetworkAddress, deviceIeeeAddress, databaseID) {
147
+ // Migrate attrs to attributes
148
+ for (const entry of Object.values(record.clusters).filter((e) => e.hasOwnProperty('attrs'))) {
149
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
150
+ // @ts-ignore
151
+ entry.attributes = entry.attrs;
152
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
153
+ // @ts-ignore
154
+ delete entry.attrs;
155
+ }
156
+ return new Endpoint(databaseID, record.epId, record.profId, record.devId, record.inClusterList, record.outClusterList, deviceNetworkAddress, deviceIeeeAddress, record.clusters, record.binds || [], record.configuredReportings || [], record.meta || {});
157
+ }
158
+ toDatabaseRecord() {
159
+ return {
160
+ profId: this.profileID,
161
+ epId: this.ID,
162
+ devId: this.deviceID,
163
+ inClusterList: this.inputClusters,
164
+ outClusterList: this.outputClusters,
165
+ clusters: this.clusters,
166
+ binds: this._binds,
167
+ configuredReportings: this._configuredReportings,
168
+ meta: this.meta,
169
+ };
170
+ }
171
+ static create(databaseID, ID, profileID, deviceID, inputClusters, outputClusters, deviceNetworkAddress, deviceIeeeAddress) {
172
+ return new Endpoint(databaseID, ID, profileID, deviceID, inputClusters, outputClusters, deviceNetworkAddress, deviceIeeeAddress, {}, [], [], {});
173
+ }
174
+ saveClusterAttributeKeyValue(clusterKey, list) {
175
+ const cluster = Zcl.Utils.getCluster(clusterKey);
176
+ if (!this.clusters[cluster.name])
177
+ this.clusters[cluster.name] = { attributes: {} };
178
+ for (const [attribute, value] of Object.entries(list)) {
179
+ this.clusters[cluster.name].attributes[attribute] = value;
180
+ }
181
+ }
182
+ getClusterAttributeValue(clusterKey, attributeKey) {
183
+ const cluster = Zcl.Utils.getCluster(clusterKey);
184
+ const attribute = cluster.getAttribute(attributeKey);
185
+ if (this.clusters[cluster.name] && this.clusters[cluster.name].attributes) {
186
+ return this.clusters[cluster.name].attributes[attribute.name];
187
+ }
188
+ return null;
189
+ }
190
+ hasPendingRequests() {
191
+ return this.pendingRequests.size > 0;
192
+ }
193
+ async sendPendingRequests(fastPolling) {
194
+ if (this.pendingRequests.size === 0)
195
+ return;
196
+ if (this.sendInProgress) {
197
+ debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): sendPendingRequests already in progress`);
198
+ return;
199
+ }
200
+ this.sendInProgress = true;
201
+ // Remove expired requests first
202
+ const now = Date.now();
203
+ for (const request of this.pendingRequests) {
204
+ if (now > request.expires) {
205
+ debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): discard after timeout. ` +
206
+ `Size before: ${this.pendingRequests.size}`);
207
+ request.reject();
208
+ this.pendingRequests.delete(request);
209
+ }
210
+ }
211
+ debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): send pending requests (` +
212
+ `${this.pendingRequests.size}, ${fastPolling})`);
213
+ for (const request of this.pendingRequests) {
214
+ if (fastPolling ||
215
+ (request.sendWhen !== 'fastpoll' && request.sendPolicy !== 'bulk')) {
216
+ try {
217
+ const result = await request.send();
218
+ debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): send success`);
219
+ request.resolve(result);
220
+ }
221
+ catch (error) {
222
+ debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): send failed, expires in ` +
223
+ `${(request.expires - now) / 1000} seconds`);
224
+ request.reject(error);
225
+ }
226
+ this.pendingRequests.delete(request);
227
+ }
228
+ }
229
+ this.sendInProgress = false;
230
+ }
231
+ async queueRequest(request) {
232
+ debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): Sending when active. ` +
233
+ `Timeout ${this.getDevice().pendingRequestTimeout / 1000} seconds`);
234
+ return new Promise((resolve, reject) => {
235
+ request.addCallbacks(resolve, reject);
236
+ this.pendingRequests.add(request);
237
+ });
238
+ }
239
+ filterRequests(newRequest) {
240
+ var _a, _b;
241
+ if (this.pendingRequests.size === 0 ||
242
+ !(typeof newRequest.frame.getCommand === 'function')) {
243
+ return;
244
+ }
245
+ const clusterID = newRequest.frame.Cluster.ID;
246
+ const payload = newRequest.frame.Payload;
247
+ const commandID = newRequest.frame.getCommand().ID;
248
+ debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): ZCL ${newRequest.frame.getCommand().name} ` + `command, filter requests. Before: ${this.pendingRequests.size}`);
249
+ for (const request of this.pendingRequests) {
250
+ if (((_b = (_a = request === null || request === void 0 ? void 0 : request.frame) === null || _a === void 0 ? void 0 : _a.Cluster) === null || _b === void 0 ? void 0 : _b.ID) === undefined ||
251
+ typeof request.frame.getCommand !== 'function') {
252
+ continue;
253
+ }
254
+ if (['bulk', 'queue', 'immediate'].includes(request.sendPolicy)) {
255
+ continue;
256
+ }
257
+ /* istanbul ignore else */
258
+ if (request.frame.Cluster.ID === clusterID &&
259
+ request.frame.getCommand().ID === commandID) {
260
+ /* istanbul ignore else */
261
+ if (newRequest.sendPolicy === 'keep-payload' &&
262
+ JSON.stringify(request.frame.Payload) === JSON.stringify(payload)) {
263
+ debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): Merge duplicate request`);
264
+ this.pendingRequests.delete(request);
265
+ newRequest.moveCallbacks(request);
266
+ }
267
+ else if ((newRequest.sendPolicy === 'keep-command' || newRequest.sendPolicy === 'keep-cmd-undiv') &&
268
+ Array.isArray(request.frame.Payload)) {
269
+ const filteredPayload = request.frame.Payload.filter((oldEl) => !payload.find((newEl) => oldEl.attrId === newEl.attrId));
270
+ if (filteredPayload.length == 0) {
271
+ debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): Remove & reject request`);
272
+ if (JSON.stringify(request.frame.Payload) === JSON.stringify(payload)) {
273
+ newRequest.moveCallbacks(request);
274
+ }
275
+ else {
276
+ request.reject();
277
+ }
278
+ this.pendingRequests.delete(request);
279
+ }
280
+ else if (newRequest.sendPolicy !== 'keep-cmd-undiv') {
281
+ // remove all duplicate attributes if we shall not write undivided
282
+ request.frame.Payload = filteredPayload;
283
+ debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): ` +
284
+ `Remove commands from request`);
285
+ }
286
+ }
287
+ }
288
+ }
289
+ debug.info(`Request Queue (${this.deviceIeeeAddress}/${this.ID}): After: ${this.pendingRequests.size}`);
290
+ }
291
+ async sendRequest(frame, options, func = (d) => {
292
+ return entity_1.default.getAdapterByID(this.databaseID).sendZclFrameToEndpoint(this.deviceIeeeAddress, this.deviceNetworkAddress, this.ID, d, options.timeout, options.disableResponse, options.disableRecovery, options.srcEndpoint);
293
+ }) {
294
+ const logPrefix = `Request Queue (${this.deviceIeeeAddress}/${this.ID}): `;
295
+ const request = new request_1.default(func, frame, this.getDevice().pendingRequestTimeout, options.sendWhen, options.sendPolicy);
296
+ if (request.sendPolicy !== 'bulk') {
297
+ // Check if such a request is already in the queue and remove the old one(s) if necessary
298
+ this.filterRequests(request);
299
+ }
300
+ // send without queueing if sendWhen or sendPolicy is 'immediate' or if the device has no timeout set
301
+ if (request.sendWhen === 'immediate' ||
302
+ request.sendPolicy === 'immediate' ||
303
+ !this.getDevice().pendingRequestTimeout) {
304
+ if (this.getDevice().defaultSendRequestWhen !== 'immediate') {
305
+ debug.info(logPrefix +
306
+ `send ${frame.getCommand().name} request immediately ` +
307
+ `(sendWhen=${options.sendWhen})`);
308
+ }
309
+ return request.send();
310
+ }
311
+ // If this is a bulk message, we queue directly.
312
+ if (request.sendPolicy === 'bulk') {
313
+ debug.info(logPrefix +
314
+ `queue request (${this.pendingRequests.size} / ${this.sendInProgress})))`);
315
+ return this.queueRequest(request);
316
+ }
317
+ try {
318
+ debug.info(logPrefix + `send request`);
319
+ return await request.send();
320
+ }
321
+ catch (error) {
322
+ // If we got a failed transaction, the device is likely sleeping.
323
+ // Queue for transmission later.
324
+ debug.info(logPrefix + `queue request (transaction failed)`);
325
+ return this.queueRequest(request);
326
+ }
327
+ }
328
+ /*
329
+ * Zigbee functions
330
+ */
331
+ checkStatus(payload) {
332
+ const codes = Array.isArray(payload)
333
+ ? payload.map((i) => i.status)
334
+ : [payload.statusCode];
335
+ const invalid = codes.find((c) => c !== Zcl.Status.SUCCESS);
336
+ if (invalid)
337
+ throw new Zcl.ZclStatusError(invalid);
338
+ }
339
+ async report(clusterKey, attributes, options) {
340
+ var _a;
341
+ const cluster = Zcl.Utils.getCluster(clusterKey);
342
+ options = this.getOptionsWithDefaults(options, true, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);
343
+ const payload = [];
344
+ for (const [nameOrID, value] of Object.entries(attributes)) {
345
+ if (cluster.hasAttribute(nameOrID)) {
346
+ const attribute = cluster.getAttribute(nameOrID);
347
+ payload.push({
348
+ attrId: attribute.ID,
349
+ attrData: value,
350
+ dataType: attribute.type,
351
+ });
352
+ }
353
+ else if (!isNaN(Number(nameOrID))) {
354
+ payload.push({
355
+ attrId: Number(nameOrID),
356
+ attrData: value.value,
357
+ dataType: value.type,
358
+ });
359
+ }
360
+ else {
361
+ throw new Error(`Unknown attribute '${nameOrID}', specify either an existing attribute or a number`);
362
+ }
363
+ }
364
+ const log = `Report to ${this.deviceIeeeAddress}/${this.ID} ` +
365
+ `${cluster.name}(${JSON.stringify(attributes)}, ${JSON.stringify(options)})`;
366
+ debug.info(log);
367
+ try {
368
+ const frame = Zcl.ZclFrame.create(Zcl.FrameType.GLOBAL, options.direction, options.disableDefaultResponse, options.manufacturerCode, (_a = options.transactionSequenceNumber) !== null && _a !== void 0 ? _a : zclTransactionSequenceNumber_1.default.next(), 'report', cluster.ID, payload, options.reservedBits);
369
+ await this.sendRequest(frame, options);
370
+ }
371
+ catch (error) {
372
+ error.message = `${log} failed (${error.message})`;
373
+ debug.error(error.message);
374
+ throw error;
375
+ }
376
+ }
377
+ async write(clusterKey, attributes, options) {
378
+ var _a;
379
+ const cluster = Zcl.Utils.getCluster(clusterKey);
380
+ options = this.getOptionsWithDefaults(options, true, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);
381
+ const payload = [];
382
+ for (const [nameOrID, value] of Object.entries(attributes)) {
383
+ if (cluster.hasAttribute(nameOrID)) {
384
+ const attribute = cluster.getAttribute(nameOrID);
385
+ payload.push({
386
+ attrId: attribute.ID,
387
+ attrData: value,
388
+ dataType: attribute.type,
389
+ });
390
+ }
391
+ else if (!isNaN(Number(nameOrID))) {
392
+ payload.push({
393
+ attrId: Number(nameOrID),
394
+ attrData: value.value,
395
+ dataType: value.type,
396
+ });
397
+ }
398
+ else {
399
+ throw new Error(`Unknown attribute '${nameOrID}', specify either an existing attribute or a number`);
400
+ }
401
+ }
402
+ const log = `Write ${this.deviceIeeeAddress}/${this.ID} ` +
403
+ `${cluster.name}(${JSON.stringify(attributes)}, ${JSON.stringify(options)})`;
404
+ debug.info(log);
405
+ try {
406
+ const frame = Zcl.ZclFrame.create(Zcl.FrameType.GLOBAL, options.direction, options.disableDefaultResponse, options.manufacturerCode, (_a = options.transactionSequenceNumber) !== null && _a !== void 0 ? _a : zclTransactionSequenceNumber_1.default.next(), options.writeUndiv ? 'writeUndiv' : 'write', cluster.ID, payload, options.reservedBits);
407
+ const result = await this.sendRequest(frame, options);
408
+ if (!options.disableResponse) {
409
+ this.checkStatus(result.frame.Payload);
410
+ }
411
+ }
412
+ catch (error) {
413
+ error.message = `${log} failed (${error.message})`;
414
+ debug.error(error.message);
415
+ throw error;
416
+ }
417
+ }
418
+ async writeResponse(clusterKey, transactionSequenceNumber, attributes, options) {
419
+ (0, assert_1.default)(!options || !options.hasOwnProperty('transactionSequenceNumber'), 'Use parameter');
420
+ const cluster = Zcl.Utils.getCluster(clusterKey);
421
+ options = this.getOptionsWithDefaults(options, true, Zcl.Direction.SERVER_TO_CLIENT, cluster.manufacturerCode);
422
+ const payload = [];
423
+ for (const [nameOrID, value] of Object.entries(attributes)) {
424
+ if (value.hasOwnProperty('status')) {
425
+ if (cluster.hasAttribute(nameOrID)) {
426
+ const attribute = cluster.getAttribute(nameOrID);
427
+ payload.push({ attrId: attribute.ID, status: value.status });
428
+ }
429
+ else if (!isNaN(Number(nameOrID))) {
430
+ payload.push({ attrId: Number(nameOrID), status: value.status });
431
+ }
432
+ else {
433
+ throw new Error(`Unknown attribute '${nameOrID}', specify either an existing attribute or a number`);
434
+ }
435
+ }
436
+ else {
437
+ throw new Error(`Missing attribute 'status'`);
438
+ }
439
+ }
440
+ const frame = Zcl.ZclFrame.create(Zcl.FrameType.GLOBAL, options.direction, options.disableDefaultResponse, options.manufacturerCode, transactionSequenceNumber, 'writeRsp', cluster.ID, payload, options.reservedBits);
441
+ const log = `WriteResponse ${this.deviceIeeeAddress}/${this.ID} ` +
442
+ `${cluster.name}(${JSON.stringify(attributes)}, ${JSON.stringify(options)})`;
443
+ debug.info(log);
444
+ try {
445
+ await this.sendRequest(frame, options);
446
+ }
447
+ catch (error) {
448
+ error.message = `${log} failed (${error.message})`;
449
+ debug.error(error.message);
450
+ throw error;
451
+ }
452
+ }
453
+ async read(clusterKey, attributes, options) {
454
+ var _a;
455
+ const cluster = Zcl.Utils.getCluster(clusterKey);
456
+ options = this.getOptionsWithDefaults(options, true, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);
457
+ const payload = [];
458
+ for (const attribute of attributes) {
459
+ payload.push({
460
+ attrId: typeof attribute === 'number'
461
+ ? attribute
462
+ : cluster.getAttribute(attribute).ID,
463
+ });
464
+ }
465
+ const frame = Zcl.ZclFrame.create(Zcl.FrameType.GLOBAL, options.direction, options.disableDefaultResponse, options.manufacturerCode, (_a = options.transactionSequenceNumber) !== null && _a !== void 0 ? _a : zclTransactionSequenceNumber_1.default.next(), 'read', cluster.ID, payload, options.reservedBits);
466
+ const log = `Read ${this.deviceIeeeAddress}/${this.ID} ` +
467
+ `${cluster.name}(${JSON.stringify(attributes)}, ${JSON.stringify(options)})`;
468
+ debug.info(log);
469
+ try {
470
+ const result = await this.sendRequest(frame, options);
471
+ if (!options.disableResponse) {
472
+ this.checkStatus(result.frame.Payload);
473
+ return ZclFrameConverter.attributeKeyValue(result.frame);
474
+ }
475
+ else {
476
+ return null;
477
+ }
478
+ }
479
+ catch (error) {
480
+ error.message = `${log} failed (${error.message})`;
481
+ debug.error(error.message);
482
+ throw error;
483
+ }
484
+ }
485
+ async readResponse(clusterKey, transactionSequenceNumber, attributes, options) {
486
+ (0, assert_1.default)(!options || !options.hasOwnProperty('transactionSequenceNumber'), 'Use parameter');
487
+ const cluster = Zcl.Utils.getCluster(clusterKey);
488
+ options = this.getOptionsWithDefaults(options, true, Zcl.Direction.SERVER_TO_CLIENT, cluster.manufacturerCode);
489
+ const payload = [];
490
+ for (const [nameOrID, value] of Object.entries(attributes)) {
491
+ if (cluster.hasAttribute(nameOrID)) {
492
+ const attribute = cluster.getAttribute(nameOrID);
493
+ payload.push({
494
+ attrId: attribute.ID,
495
+ attrData: value,
496
+ dataType: attribute.type,
497
+ status: 0,
498
+ });
499
+ }
500
+ else if (!isNaN(Number(nameOrID))) {
501
+ payload.push({
502
+ attrId: Number(nameOrID),
503
+ attrData: value.value,
504
+ dataType: value.type,
505
+ status: 0,
506
+ });
507
+ }
508
+ else {
509
+ throw new Error(`Unknown attribute '${nameOrID}', specify either an existing attribute or a number`);
510
+ }
511
+ }
512
+ const frame = Zcl.ZclFrame.create(Zcl.FrameType.GLOBAL, options.direction, options.disableDefaultResponse, options.manufacturerCode, transactionSequenceNumber, 'readRsp', cluster.ID, payload, options.reservedBits);
513
+ const log = `ReadResponse ${this.deviceIeeeAddress}/${this.ID} ` +
514
+ `${cluster.name}(${JSON.stringify(attributes)}, ${JSON.stringify(options)})`;
515
+ debug.info(log);
516
+ try {
517
+ await this.sendRequest(frame, options);
518
+ }
519
+ catch (error) {
520
+ error.message = `${log} failed (${error.message})`;
521
+ debug.error(error.message);
522
+ throw error;
523
+ }
524
+ }
525
+ addBinding(clusterKey, target) {
526
+ const cluster = Zcl.Utils.getCluster(clusterKey);
527
+ if (typeof target === 'number') {
528
+ target =
529
+ group_1.default.byGroupID(target, this.databaseID) ||
530
+ group_1.default.create(target, this.databaseID);
531
+ }
532
+ if (!this.binds.find((b) => b.cluster.ID === cluster.ID && b.target === target)) {
533
+ if (target instanceof group_1.default) {
534
+ this._binds.push({
535
+ cluster: cluster.ID,
536
+ groupID: target.groupID,
537
+ type: 'group',
538
+ });
539
+ }
540
+ else {
541
+ this._binds.push({
542
+ cluster: cluster.ID,
543
+ type: 'endpoint',
544
+ deviceIeeeAddress: target.deviceIeeeAddress,
545
+ endpointID: target.ID,
546
+ });
547
+ }
548
+ this.save();
549
+ }
550
+ }
551
+ async bind(clusterKey, target) {
552
+ const cluster = Zcl.Utils.getCluster(clusterKey);
553
+ const type = target instanceof Endpoint ? 'endpoint' : 'group';
554
+ if (typeof target === 'number') {
555
+ target =
556
+ group_1.default.byGroupID(target, this.databaseID) ||
557
+ group_1.default.create(target, this.databaseID);
558
+ }
559
+ const destinationAddress = target instanceof Endpoint ? target.deviceIeeeAddress : target.groupID;
560
+ const log = `Bind ${this.deviceIeeeAddress}/${this.ID} ${cluster.name} from ` +
561
+ `'${target instanceof Endpoint
562
+ ? `${destinationAddress}/${target.ID}`
563
+ : destinationAddress}'`;
564
+ debug.info(log);
565
+ try {
566
+ await entity_1.default.getAdapterByID(this.databaseID).bind(this.deviceNetworkAddress, this.deviceIeeeAddress, this.ID, cluster.ID, destinationAddress, type, target instanceof Endpoint ? target.ID : null);
567
+ this.addBinding(clusterKey, target);
568
+ }
569
+ catch (error) {
570
+ error.message = `${log} failed (${error.message})`;
571
+ debug.error(error.message);
572
+ throw error;
573
+ }
574
+ }
575
+ save() {
576
+ this.getDevice().save();
577
+ }
578
+ async unbind(clusterKey, target) {
579
+ const cluster = Zcl.Utils.getCluster(clusterKey);
580
+ const type = target instanceof Endpoint ? 'endpoint' : 'group';
581
+ const destinationAddress = target instanceof Endpoint
582
+ ? target.deviceIeeeAddress
583
+ : target instanceof group_1.default
584
+ ? target.groupID
585
+ : target;
586
+ const log = `Unbind ${this.deviceIeeeAddress}/${this.ID} ${cluster.name} from ` +
587
+ `'${target instanceof Endpoint
588
+ ? `${destinationAddress}/${target.ID}`
589
+ : destinationAddress}'`;
590
+ debug.info(log);
591
+ try {
592
+ await entity_1.default.getAdapterByID(this.databaseID).unbind(this.deviceNetworkAddress, this.deviceIeeeAddress, this.ID, cluster.ID, destinationAddress, type, target instanceof Endpoint ? target.ID : null);
593
+ if (typeof target === 'number' &&
594
+ group_1.default.byGroupID(target, this.databaseID)) {
595
+ target = group_1.default.byGroupID(target, this.databaseID);
596
+ }
597
+ const index = this.binds.findIndex((b) => b.cluster.ID === cluster.ID && b.target === target);
598
+ if (index !== -1) {
599
+ this._binds.splice(index, 1);
600
+ this.save();
601
+ }
602
+ }
603
+ catch (error) {
604
+ error.message = `${log} failed (${error.message})`;
605
+ debug.error(error.message);
606
+ throw error;
607
+ }
608
+ }
609
+ async defaultResponse(commandID, status, clusterID, transactionSequenceNumber, options) {
610
+ (0, assert_1.default)(!options || !options.hasOwnProperty('transactionSequenceNumber'), 'Use parameter');
611
+ options = this.getOptionsWithDefaults(options, true, Zcl.Direction.SERVER_TO_CLIENT, null);
612
+ const payload = { cmdId: commandID, statusCode: status };
613
+ const frame = Zcl.ZclFrame.create(Zcl.FrameType.GLOBAL, options.direction, options.disableDefaultResponse, options.manufacturerCode, transactionSequenceNumber, 'defaultRsp', clusterID, payload, options.reservedBits);
614
+ const log = `DefaultResponse ${this.deviceIeeeAddress}/${this.ID} ` +
615
+ `${clusterID}(${commandID}, ${JSON.stringify(options)})`;
616
+ debug.info(log);
617
+ try {
618
+ await this.sendRequest(frame, options);
619
+ }
620
+ catch (error) {
621
+ error.message = `${log} failed (${error.message})`;
622
+ debug.error(error.message);
623
+ throw error;
624
+ }
625
+ }
626
+ async configureReporting(clusterKey, items, options) {
627
+ var _a;
628
+ const cluster = Zcl.Utils.getCluster(clusterKey);
629
+ options = this.getOptionsWithDefaults(options, true, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);
630
+ const payload = items.map((item) => {
631
+ let dataType, attrId;
632
+ if (typeof item.attribute === 'object') {
633
+ dataType = item.attribute.type;
634
+ attrId = item.attribute.ID;
635
+ }
636
+ else {
637
+ /* istanbul ignore else */
638
+ if (cluster.hasAttribute(item.attribute)) {
639
+ const attribute = cluster.getAttribute(item.attribute);
640
+ dataType = attribute.type;
641
+ attrId = attribute.ID;
642
+ }
643
+ }
644
+ return {
645
+ direction: Zcl.Direction.CLIENT_TO_SERVER,
646
+ attrId,
647
+ dataType,
648
+ minRepIntval: item.minimumReportInterval,
649
+ maxRepIntval: item.maximumReportInterval,
650
+ repChange: item.reportableChange,
651
+ };
652
+ });
653
+ const frame = Zcl.ZclFrame.create(Zcl.FrameType.GLOBAL, options.direction, options.disableDefaultResponse, options.manufacturerCode, (_a = options.transactionSequenceNumber) !== null && _a !== void 0 ? _a : zclTransactionSequenceNumber_1.default.next(), 'configReport', cluster.ID, payload, options.reservedBits);
654
+ const log = `ConfigureReporting ${this.deviceIeeeAddress}/${this.ID} ` +
655
+ `${cluster.name}(${JSON.stringify(items)}, ${JSON.stringify(options)})`;
656
+ debug.info(log);
657
+ try {
658
+ const result = await this.sendRequest(frame, options);
659
+ if (!options.disableResponse) {
660
+ this.checkStatus(result.frame.Payload);
661
+ }
662
+ for (const e of payload) {
663
+ const match = this._configuredReportings.find((c) => c.attrId === e.attrId && c.cluster === cluster.ID);
664
+ if (match) {
665
+ this._configuredReportings.splice(this._configuredReportings.indexOf(match), 1);
666
+ }
667
+ }
668
+ for (const entry of payload) {
669
+ if (entry.maxRepIntval !== 0xffff) {
670
+ this._configuredReportings.push({
671
+ cluster: cluster.ID,
672
+ attrId: entry.attrId,
673
+ minRepIntval: entry.minRepIntval,
674
+ maxRepIntval: entry.maxRepIntval,
675
+ repChange: entry.repChange,
676
+ });
677
+ }
678
+ }
679
+ this.save();
680
+ }
681
+ catch (error) {
682
+ error.message = `${log} failed (${error.message})`;
683
+ debug.error(error.message);
684
+ throw error;
685
+ }
686
+ }
687
+ async writeStructured(clusterKey, payload, options) {
688
+ var _a;
689
+ const cluster = Zcl.Utils.getCluster(clusterKey);
690
+ options = this.getOptionsWithDefaults(options, true, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);
691
+ const frame = Zcl.ZclFrame.create(Zcl.FrameType.GLOBAL, options.direction, options.disableDefaultResponse, options.manufacturerCode, (_a = options.transactionSequenceNumber) !== null && _a !== void 0 ? _a : zclTransactionSequenceNumber_1.default.next(), `writeStructured`, cluster.ID, payload, options.reservedBits);
692
+ const log = `WriteStructured ${this.deviceIeeeAddress}/${this.ID} ` +
693
+ `${cluster.name}(${JSON.stringify(payload)}, ${JSON.stringify(options)})`;
694
+ debug.info(log);
695
+ try {
696
+ await this.sendRequest(frame, options);
697
+ // TODO: support `writeStructuredResponse`
698
+ }
699
+ catch (error) {
700
+ error.message = `${log} failed (${error.message})`;
701
+ debug.error(error.message);
702
+ throw error;
703
+ }
704
+ }
705
+ async command(clusterKey, commandKey, payload, options) {
706
+ var _a;
707
+ const cluster = Zcl.Utils.getCluster(clusterKey);
708
+ const command = cluster.getCommand(commandKey);
709
+ const hasResponse = command.hasOwnProperty('response');
710
+ options = this.getOptionsWithDefaults(options, hasResponse, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);
711
+ const frame = Zcl.ZclFrame.create(Zcl.FrameType.SPECIFIC, options.direction, options.disableDefaultResponse, options.manufacturerCode, (_a = options.transactionSequenceNumber) !== null && _a !== void 0 ? _a : zclTransactionSequenceNumber_1.default.next(), command.name, cluster.name, payload, options.reservedBits);
712
+ const log = `Command ${this.deviceIeeeAddress}/${this.ID} ` +
713
+ `${cluster.name}.${command.name}(${JSON.stringify(payload)}, ${JSON.stringify(options)})`;
714
+ debug.info(log);
715
+ try {
716
+ const result = await this.sendRequest(frame, options);
717
+ if (result) {
718
+ return result.frame.Payload;
719
+ }
720
+ }
721
+ catch (error) {
722
+ error.message = `${log} failed (${error.message})`;
723
+ debug.error(error.message);
724
+ throw error;
725
+ }
726
+ }
727
+ async commandResponse(clusterKey, commandKey, payload, options, transactionSequenceNumber) {
728
+ (0, assert_1.default)(!options || !options.hasOwnProperty('transactionSequenceNumber'), 'Use parameter');
729
+ const cluster = Zcl.Utils.getCluster(clusterKey);
730
+ const command = cluster.getCommandResponse(commandKey);
731
+ transactionSequenceNumber =
732
+ transactionSequenceNumber || zclTransactionSequenceNumber_1.default.next();
733
+ options = this.getOptionsWithDefaults(options, true, Zcl.Direction.SERVER_TO_CLIENT, cluster.manufacturerCode);
734
+ const frame = Zcl.ZclFrame.create(Zcl.FrameType.SPECIFIC, options.direction, options.disableDefaultResponse, options.manufacturerCode, transactionSequenceNumber, command.name, cluster.name, payload, options.reservedBits);
735
+ const log = `CommandResponse ${this.deviceIeeeAddress}/${this.ID} ` +
736
+ `${cluster.name}.${command.name}(${JSON.stringify(payload)}, ${JSON.stringify(options)})`;
737
+ debug.info(log);
738
+ try {
739
+ await this.sendRequest(frame, options, async (f) => {
740
+ // Broadcast Green Power responses
741
+ if (this.ID === 242) {
742
+ await entity_1.default.getAdapterByID(this.databaseID).sendZclFrameToAll(242, f, 242);
743
+ }
744
+ else {
745
+ await entity_1.default.getAdapterByID(this.databaseID).sendZclFrameToEndpoint(this.deviceIeeeAddress, this.deviceNetworkAddress, this.ID, f, options.timeout, options.disableResponse, options.disableRecovery, options.srcEndpoint);
746
+ }
747
+ });
748
+ }
749
+ catch (error) {
750
+ error.message = `${log} failed (${error.message})`;
751
+ debug.error(error.message);
752
+ throw error;
753
+ }
754
+ }
755
+ waitForCommand(clusterKey, commandKey, transactionSequenceNumber, timeout) {
756
+ const cluster = Zcl.Utils.getCluster(clusterKey);
757
+ const command = cluster.getCommand(commandKey);
758
+ const waiter = entity_1.default.getAdapterByID(this.databaseID).waitFor(this.deviceNetworkAddress, this.ID, Zcl.FrameType.SPECIFIC, Zcl.Direction.CLIENT_TO_SERVER, transactionSequenceNumber, cluster.ID, command.ID, timeout);
759
+ const promise = new Promise((resolve, reject) => {
760
+ waiter.promise.then((payload) => resolve({
761
+ header: payload.frame.Header,
762
+ payload: payload.frame.Payload,
763
+ }), (error) => reject(error));
764
+ });
765
+ return { promise, cancel: waiter.cancel };
766
+ }
767
+ getOptionsWithDefaults(options, disableDefaultResponse, direction, manufacturerCode) {
768
+ const providedOptions = options || {};
769
+ return {
770
+ sendWhen: this.getDevice().defaultSendRequestWhen,
771
+ timeout: 10000,
772
+ disableResponse: false,
773
+ disableRecovery: false,
774
+ disableDefaultResponse,
775
+ direction,
776
+ srcEndpoint: null,
777
+ reservedBits: 0,
778
+ manufacturerCode: manufacturerCode ? manufacturerCode : null,
779
+ transactionSequenceNumber: null,
780
+ writeUndiv: false,
781
+ ...providedOptions,
782
+ };
783
+ }
784
+ async addToGroup(group) {
785
+ await this.command('genGroups', 'add', {
786
+ groupid: group.groupID,
787
+ groupname: '',
788
+ });
789
+ group.addMember(this);
790
+ }
791
+ /**
792
+ * Remove endpoint from a group, accepts both a Group and number as parameter.
793
+ * The number parameter type should only be used when removing from a group which is not known
794
+ * to zigbee-herdsman.
795
+ */
796
+ async removeFromGroup(group) {
797
+ await this.command('genGroups', 'remove', {
798
+ groupid: group instanceof group_1.default ? group.groupID : group,
799
+ });
800
+ if (group instanceof group_1.default) {
801
+ group.removeMember(this);
802
+ }
803
+ }
804
+ async removeFromAllGroups() {
805
+ await this.command('genGroups', 'removeAll', {}, { disableDefaultResponse: true });
806
+ this.removeFromAllGroupsDatabase();
807
+ }
808
+ removeFromAllGroupsDatabase() {
809
+ for (const group of group_1.default.allByDatabaseID(this.databaseID)) {
810
+ if (group.hasMember(this)) {
811
+ group.removeMember(this);
812
+ }
813
+ }
814
+ }
815
+ }
816
+ exports.default = Endpoint;
818
817
  //# sourceMappingURL=endpoint.js.map