-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_attribute.js
37 lines (32 loc) · 1.34 KB
/
write_attribute.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
const {cap} = require('./utils')
module.exports = function(RED) {
function MatterWriteAttr(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)
this.on('input', function(msg) {
_data = RED.util.evaluateNodeProperty(config.data, config.dataType, node, msg);
node.controller.commissioningController.connectNode(node._id).then((conn) => {
const ep = conn.getDeviceById(Number(node._ep))
const clc = ep.getClusterClientById(Number(node.cluster))
try {
let command = eval(`clc.set${node.attr}Attribute`)
command(_data)
.then((attr_resp) => {
node.log(attr_resp)
msg.payload = 'ok'
node.send(msg)
})
.catch((e) => node.error(e))
} catch (error) {
node.error(error)
}
})
})
}
RED.nodes.registerType("matterwriteattr",MatterWriteAttr);
}