Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
122 lines (106 loc) · 3.89 KB
/
Copy pathutils.js
File metadata and controls
122 lines (106 loc) · 3.89 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
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
const 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){
let data = {}
let clusterName = Models.MatterModel.standard.get(Models.ClusterModel, Number(clusterID)).name
let cluster = Models[clusterName]
cluster.commands.forEach((cmd) => {
if (cmd.name == cap(commandName)){
let vars = (cmd.children || []).flat()
vars.forEach(f => {
let key = deCap(f.name)
let val
if ('default' in f && f.default !== undefined){
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){
let data = {}
let clusterName = Models.MatterModel.standard.get(Models.ClusterModel, Number(clusterID)).name
let cluster = 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){
let simpleCommands = {}
clusterList.forEach((clusterID) => {
simpleCommands[clusterID] = []
let clusterName = Models.MatterModel.standard.get(Models.ClusterModel, Number(clusterID)).name
let cluster = Models[clusterName]
cluster.commands.forEach((cmd, i) => {
simpleCommands[clusterID].push(deCap(cmd.name))
})
})
return simpleCommands
}
function getAttributes(clusterList){
let simpleAttributes = {}
clusterList.forEach((clusterID) => {
simpleAttributes[clusterID] = []
let clusterName = Models.MatterModel.standard.get(Models.ClusterModel, Number(clusterID)).name
let cluster = 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)
}
})
}
})
}
// Types that should be evaluated against the runtime context (msg/flow/global/etc).
// Any other type (the editor's custom select keys, undefined, or 'str') is a literal.
const EVAL_TYPES = ['msg', 'flow', 'global', 'env', 'jsonata']
// Resolve a typed field: evaluate it when its type is one of EVAL_TYPES, otherwise
// return the stored value verbatim (a dropdown-selected literal or a legacy value
// with no stored type). Always returns a Promise.
function resolveField(RED, value, type, node, msg){
if (type && EVAL_TYPES.includes(type)){
return resolveTyped(RED, value, type, node, msg)
}
return Promise.resolve(value)
}
module.exports = {commandOptions, attributeOptions, deCap, cap, resolveTyped, resolveField, listClusters, getCommands, getAttributes}