-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand.js
48 lines (43 loc) · 1.62 KB
/
command.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module.exports = function(RED) {
function MatterCommand(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]
node.cluster = Number(config.cluster)
node.command = config.command
this.on('input', function(msg) {
let _data
RED.util.evaluateNodeProperty(config.data, config.dataType, node, msg, (err, result) => {
if (err) {
node.error(err)
} else {
_data = result
}
})
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.commands.${node.command}`)
if (Object.keys(_data).length == 0){
command()
.then(() => {
msg.payload = 'ok'
node.send(msg)
})
.catch((e) => node.error(e))
} else {
command(_data)
.then(() => {
msg.payload = 'ok'
node.send(msg)
})
.catch((e) => node.error(e))
}
})
})
}
RED.nodes.registerType("mattercommand",MatterCommand);
}