-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheditor_apis.js
201 lines (187 loc) · 7.21 KB
/
editor_apis.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const {commandOptions, attributeOptions} = require('./utils')
const { BasicInformationCluster } = require( "@matter/main/clusters");
const os = require('os');
let listComplete = false
let deviceList = {}
module.exports = function(RED) {
//List Interfaces
RED.httpAdmin.get('/_mattercontroller/interfaces', RED.auth.needsPermission('admin.write'), function(req,res){
let interfaces = os.networkInterfaces()
let output = []
for (let i in interfaces) {
for (let i2 in interfaces[i]) {
if (!interfaces[i][i2].internal && interfaces[i][i2].family == "IPv6")
output.push(i)
}
}
uniqueOutput = output.filter(function(elem, pos) {
return output.indexOf(elem) == pos;
})
res.send(uniqueOutput)
})
// List Devices
RED.httpAdmin.get('/_mattercontroller/:id/devices/', RED.auth.needsPermission('admin.write'), function(req,res){
let ctrl_node = RED.nodes.getNode(req.params.id)
listComplete = false
deviceList = {}
if (ctrl_node){
const nodes = ctrl_node.commissioningController.getCommissionedNodes();
nodes.forEach(nodeId => {
ctrl_node.commissioningController.connectNode(nodeId)
.then((conn) => {
let endpoints = conn.getDevices()
if (endpoints.length == 1) { //Simple Device OR Bridge
if (endpoints[0].deviceType == 14) { //Bridge
endpoints[0].childEndpoints.forEach((ep) => {
bridgedinfo = ep.getClusterClientById(57)
bridgedinfo.getNodeLabelAttribute()
.then((nodeLabel) => {
deviceList[`${nodeId}-${ep.number}`] = nodeLabel
})
})
listComplete = true
} else { //Simple Device
info = conn.getRootClusterClient(BasicInformationCluster)
ep = endpoints[0]
info.getNodeLabelAttribute()
.then((nodeLabel) => {
deviceList[`${nodeId}-${ep.number}`] = nodeLabel
listComplete = true
})
}
} else { //Composed Device
info = conn.getRootClusterClient(BasicInformationCluster)
info.getNodeLabelAttribute()
.then((nodeLabel) => {
endpoints.forEach((ep) => {
let name = ep.name.split('-')[1]
deviceList[`${nodeId}-${ep.number}`] = `${nodeLabel}-${name}`
})
listComplete = true
})
}
})
})
listReadytoSend(res)
}
else {
res.sendStatus(404);
}
})
function listReadytoSend(res) {
if (!listComplete) {
setTimeout(listReadytoSend, 100, res)
} else {
res.send(deviceList)
}
}
// List Clusters
RED.httpAdmin.get('/_mattercontroller/:cid/device/:did/clusters', RED.auth.needsPermission('admin.write'), function(req,res){
let ctrl_node = RED.nodes.getNode(req.params.cid)
let nodeID = BigInt(req.params.did.split('-')[0])
let epID = Number(req.params.did.split('-')[1])
if (ctrl_node){
ctrl_node.commissioningController.connectNode(nodeID)
.then((conn) => {
let ep = conn.getDeviceById(epID)
let cl = ep.getAllClusterClients()
clusterList = {}
cl.forEach((c) => {
clusterList[c.id] = c.name
})
res.send(clusterList)
})
}
else {
res.sendStatus(404);
}
})
// List Commands
RED.httpAdmin.get('/_mattercontroller/:cid/device/:did/cluster/:clid/commands', RED.auth.needsPermission('admin.write'), function(req,res){
let ctrl_node = RED.nodes.getNode(req.params.cid)
let nodeID = BigInt(req.params.did.split('-')[0])
let epID = Number(req.params.did.split('-')[1])
if (ctrl_node){
ctrl_node.commissioningController.connectNode(nodeID)
.then((conn) => {
let ep = conn.getDeviceById(epID)
cmds = ep.getClusterClientById(Number(req.params.clid)).commands
res.send(Object.keys(cmds))
})
}
else {
res.sendStatus(404);
}
})
// Get Command options
RED.httpAdmin.get('/_mattermodel/cluster/:clid/command/:cmd/options', RED.auth.needsPermission('admin.write'), function(req,res){
let data = commandOptions(req.params.clid, req.params.cmd)
res.send(data)
})
// List Attributes
RED.httpAdmin.get('/_mattercontroller/:cid/device/:did/cluster/:clid/attributes', RED.auth.needsPermission('admin.write'), function(req,res){
let ctrl_node = RED.nodes.getNode(req.params.cid)
let nodeID = BigInt(req.params.did.split('-')[0])
let epID = Number(req.params.did.split('-')[1])
if (ctrl_node){
ctrl_node.commissioningController.connectNode(nodeID)
.then((conn) => {
let ep = conn.getDeviceById(epID)
atrs = ep.getClusterClientById(Number(req.params.clid)).attributes
res.send(Object.keys(atrs))
})
}
else {
res.sendStatus(404);
}
})
// List Writable Attributes
RED.httpAdmin.get('/_mattercontroller/:cid/device/:did/cluster/:clid/attributes_writable', RED.auth.needsPermission('admin.write'), function(req,res){
let ctrl_node = RED.nodes.getNode(req.params.cid)
let nodeID = BigInt(req.params.did.split('-')[0])
let epID = Number(req.params.did.split('-')[1])
if (ctrl_node){
ctrl_node.commissioningController.connectNode(nodeID)
.then((conn) => {
let ep = conn.getDeviceById(epID)
atrs = ep.getClusterClientById(Number(req.params.clid)).attributes
let response = []
Object.keys(atrs).forEach((k) => {
if (atrs[k].attribute.writable) {
response.push(k)
}
})
res.send(response)
})
}
else {
res.sendStatus(404);
}
})
// List Events
RED.httpAdmin.get('/_mattercontroller/:cid/device/:did/cluster/:clid/events', RED.auth.needsPermission('admin.write'), function(req,res){
let ctrl_node = RED.nodes.getNode(req.params.cid)
let nodeID = BigInt(req.params.did.split('-')[0])
let epID = Number(req.params.did.split('-')[1]) || 0
if (ctrl_node){
ctrl_node.commissioningController.connectNode(nodeID)
.then((conn) => {
let ep = conn.getDeviceById(epID)
events = ep.getClusterClientById(Number(req.params.clid)).events
res.send(Object.keys(events))
})
}
else {
res.sendStatus(404);
}
})
// Get Attribute options
RED.httpAdmin.get('/_mattermodel/cluster/:clid/attribute/:attr/options', RED.auth.needsPermission('admin.write'), function(req,res){
let data = attributeOptions(req.params.clid, req.params.attr)
res.send(data)
})
RED.httpAdmin.get('/_mattercontroller/homedir', RED.auth.needsPermission('admin.write'), function(req,res){
const homedir = require('os').homedir();
res.send(homedir)
})
}