-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubscribe.js
44 lines (36 loc) · 1.38 KB
/
subscribe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const {cap} = require('./utils')
module.exports = function(RED) {
function MatterSubscribe(config) {
RED.nodes.createNode(this, config);
var node = this;
node.controller = RED.nodes.getNode(config.controller);
node._id = BigInt(config.device.split('-')[0])
node._ep = config.device.split('-')[1] || 1
node.cluster = Number(config.cluster)
node.attr = cap(config.attr)
node.topic = config.topic
function subscribe(node){
node.controller.commissioningController.connectNode(node._id)
.then((conn) => {
const ep = conn.getDeviceById(Number(node._ep))
const clc = ep.getClusterClientById(Number(node.cluster))
let command = eval(`clc.add${node.attr}AttributeListener`)
command(value => {
msg = {topic: node.topic}
msg.payload = value
node.send(msg)
})
})
}
function waitforserver(node) {
if (!node.controller.started) {
setTimeout(waitforserver, 100, node)
} else {
node.log('Setting Subscription...')
subscribe(node)
}
}
waitforserver(node)
}
RED.nodes.registerType("mattersubscribe",MatterSubscribe);
}