Repository navigation
Expand file tree
/
Copy pathcommand.js
More file actions
80 lines (69 loc) · 3.35 KB
/
Copy pathcommand.js
File metadata and controls
80 lines (69 loc) · 3.35 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const { resolveField, resolveTyped } = require('./utils')
module.exports = function(RED) {
function MatterCommand(config) {
RED.nodes.createNode(this, config);
const node = this;
node.controller = RED.nodes.getNode(config.controller);
this.on('input', async function(msg, send, done) {
try {
if (!node.controller || !node.controller.commissioningController) {
done('Matter controller not available — check that the controller is configured and deployed')
return
}
// Resolve device (dropdown literal, or msg/flow/global/env/jsonata)
const deviceVal = await resolveField(RED, config.device, config.deviceType, node, msg)
if (deviceVal == null || deviceVal === '' || deviceVal === '__SELECT__') {
done('Device not configured — set in editor or provide via msg/flow/global')
return
}
const deviceId = BigInt(String(deviceVal).split('-')[0])
const ep = String(deviceVal).split('-')[1] || 1
// Resolve cluster
const clusterVal = await resolveField(RED, config.cluster, config.clusterType, node, msg)
if (clusterVal == null || clusterVal === '' || clusterVal === '__SELECT__') {
done('Cluster not configured — set in editor or provide via msg/flow/global')
return
}
const cluster = Number(clusterVal)
if (Number.isNaN(cluster)) {
done('Cluster is not a valid number')
return
}
// Resolve command
const commandVal = await resolveField(RED, config.command, config.commandType, node, msg)
if (commandVal == null || commandVal === '' || commandVal === '__SELECT__') {
done('Command not configured — set in editor or provide via msg/flow/global')
return
}
const command = commandVal
// Resolve data: typed config value, or fall back to msg.payload
let _data
if (config.data || config.dataType) {
_data = await resolveTyped(RED, config.data, config.dataType, node, msg)
} else {
_data = msg.payload
}
await node.controller.whenStarted()
const conn = await node.controller.getConnection(deviceId)
const epObj = conn.getDeviceById(Number(ep))
const clc = epObj.getClusterClientById(cluster)
const cmdFn = clc.commands[command]
if (typeof cmdFn !== 'function') {
done(`Command '${command}' not found on cluster ${cluster}`)
return
}
if (_data == null || (typeof _data === 'object' && Object.keys(_data).length === 0)){
await cmdFn()
} else {
await cmdFn(_data)
}
msg.payload = 'ok'
node.send(msg)
done()
} catch (e) {
done(e)
}
})
}
RED.nodes.registerType("mattercommand",MatterCommand);
}