-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
108 lines (94 loc) · 3.18 KB
/
utils.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
var Models = require( "@matter/main/model")
function deCap(string) {
return string.charAt(0).toLowerCase() + string.slice(1);
}
function cap(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function commandOptions(clusterID, commandName){
data = {}
clusterName = Models.MatterModel.standard.get(Models.ClusterModel, Number(clusterID)).name
cluster = eval('Models.'+clusterName)
cluster.commands.forEach((cmd, i) => {
if (cmd.name == cap(commandName)){
let vars = cluster.commands[i].children.flat()
vars.forEach(f => {
let key = deCap(f.name)
let val
if ('default' in f){
val = f.default
} else if
('constraint' in f && f.constraint != 'all'){
val = `[${f.constraint}]`
} else if
('type' in f){
val = `[${f.type}]`
} else {
val = ''
}
data[key] = val
});
}
});
return data
}
function attributeOptions(clusterID, attributeName){
data = {}
clusterName = Models.MatterModel.standard.get(Models.ClusterModel, Number(clusterID)).name
cluster = eval('Models.'+clusterName)
cluster.attributes.forEach((attr, i) => {
if (attr.name == cap(attributeName)){
data.name = attr.name
data.default = attr.default || ''
data.constraint = attr.constraint || ''
data.type = attr.type
data.details = attr.details || ''
}
})
return data
}
// Functions to update the simples lists
function listClusters(){
Models.MatterModel.standard.clusters.forEach((cl) => {
console.log(`"${cl.id}", // ${cl.name}`)
})
}
function getCommands(clusterList){
simpleCommands = {}
clusterList.forEach((clusterID) => {
simpleCommands[clusterID] = []
clusterName = Models.MatterModel.standard.get(Models.ClusterModel, Number(clusterID)).name
cluster = eval('Models.'+clusterName)
cluster.commands.forEach((cmd, i) => {
simpleCommands[clusterID].push(deCap(cmd.name))
})
})
return simpleCommands
}
function getAttributes(clusterList){
simpleAttributes = {}
clusterList.forEach((clusterID) => {
simpleAttributes[clusterID] = []
clusterName = Models.MatterModel.standard.get(Models.ClusterModel, Number(clusterID)).name
cluster = eval('Models.'+clusterName)
cluster.attributes.forEach((attr, i) => {
simpleAttributes[clusterID].push(deCap(attr.name))
})
})
return simpleAttributes
}
function resolveTyped(RED, data, dataType, node, msg){
return new Promise(function(resolve, reject) {
if (dataType == 'null'){
resolve(null)
} else {
RED.util.evaluateNodeProperty(data, dataType, node, msg, (err, result) => {
if (err) {reject(err) }
else {
resolve(result)
}
})
}
})
}
module.exports = {commandOptions, attributeOptions, deCap, cap, resolveTyped}