This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-matter-wot-mapping.ts
244 lines (212 loc) · 6.19 KB
/
generate-matter-wot-mapping.ts
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// this script parses the Matter clusters from the JSON generated by generate-matter-clusters.ts
// and generates the mapping between Matter clusters and WoT TDs, adding ontologies verbs where possible.
import { readFileSync, writeFileSync } from "fs";
import {
CLUSTERS_JSON_FILE_PATH,
MATTER_WOT_MAPPING_FILE_PATH,
} from "./utils/environment.js";
import type {
AttributeDataSchema,
CommandDataSchema,
MatterAvailableClusters,
MatterClusterElement,
MatterClusterId,
MatterOntologiesMapping,
MatterWotMapping,
} from "../../src/models";
/**
* Some clusters mapped manually to **SAREF** ontology.
* TODO: move to a separate file (e.g. a JSON file)
*/
const matterOntologiesMapping: MatterOntologiesMapping = {
"6": {
attributes: {
0: {
"@type": "saref:OnOffState",
},
},
commands: {
0: {
"@type": "saref:OffCommand",
},
1: {
"@type": "saref:OnCommand",
},
2: {
"@type": "saref:ToggleCommand",
},
},
},
"8": {
attributes: {
0: {
"@type": "saref:MultiLevelState",
},
},
commands: {
0: {
"@type": "saref:SetLevelCommand",
},
},
},
};
const parseAttribute = (
clusterId: MatterClusterId,
attribute: MatterClusterElement,
): AttributeDataSchema | undefined => {
if (!attribute._attributes || !attribute._attributes.code) {
return;
}
const code = parseInt(attribute._attributes.code, 16);
const attributeOntologies =
matterOntologiesMapping[clusterId]?.attributes?.[code] || {};
// TODO: some attributes must not be exposed, because of security reasons, we need to handle them
return {
type: "integer",
title: attribute._attributes?.define,
description: attribute.description?._text,
const: code,
"@type": attributeOntologies["@type"],
};
};
const parseCommand = (
clusterId: MatterClusterId,
command: MatterClusterElement,
): CommandDataSchema | undefined => {
if (!command._attributes || !command._attributes.code) {
return;
}
const code = parseInt(command._attributes.code, 16);
const commandOntologies =
matterOntologiesMapping[clusterId]?.commands?.[code] || {};
// TODO: some commands must not be exposed, because of security reasons, we need to handle them
const schema: CommandDataSchema = {
type: "object",
title: command._attributes.define,
description: command.description?._text,
"@type": commandOntologies["@type"],
properties: {
id: {
type: "integer",
const: code,
},
},
};
// if command has arguments, we add them to input schema at payload property, mapping arguments
if (command.arg) {
schema.properties!.payload = {
type: "object",
properties: {},
};
if (!Array.isArray(command.arg)) {
command.arg = [command.arg];
}
for (const arg of command.arg) {
if (!arg._attributes || !arg._attributes.name) {
console.warn(
`\tNo attribute for command: ${command._attributes.define} argument: ${arg}, skipping...`,
);
continue;
}
const name = arg._attributes.name;
// TODO: we need map types in a better way and handle all matter-defined ones
schema.properties!.payload.properties![name] = {
type: "string",
description: arg._attributes.type,
};
}
}
return schema;
};
export const main = () => {
if (!CLUSTERS_JSON_FILE_PATH) {
console.error("Invalid path to the clusters JSON file");
return;
}
console.log("Clusters JSON file:", CLUSTERS_JSON_FILE_PATH);
if (!MATTER_WOT_MAPPING_FILE_PATH) {
console.error("Invalid path to the Matter WoT mapping file");
return;
}
console.log("Matter WoT mapping file:", MATTER_WOT_MAPPING_FILE_PATH);
const mapping: MatterWotMapping = {};
const clusters: MatterAvailableClusters = JSON.parse(
readFileSync(CLUSTERS_JSON_FILE_PATH, "utf8"),
);
for (const clusterId in clusters) {
const cluster = clusters[clusterId as MatterClusterId];
mapping[clusterId] = {
properties: {
[clusterId]: {
title: cluster.name._text,
description: cluster.description?._text,
uriVariables: {
attribute: {
type: "integer",
description: "Attrbute to read from the device",
oneOf: [],
},
},
},
},
actions: {
[clusterId]: {
title: cluster.name._text,
description: cluster.description?._text,
input: {
type: "object",
properties: {
command: {
type: "object",
description: "Command to send to the device",
oneOf: [],
},
},
},
},
},
};
const tdProperties = mapping[clusterId].properties![clusterId];
const tdActions = mapping[clusterId].actions![clusterId];
if (cluster.attribute) {
if (!Array.isArray(cluster.attribute)) {
cluster.attribute = [cluster.attribute];
}
for (const attribute of cluster.attribute) {
const parsedAttribute = parseAttribute(
clusterId as MatterClusterId,
attribute,
);
if (!parsedAttribute) {
console.warn(
`\tNo data for cluster: ${clusterId} attribute: ${attribute}, skipping...`,
);
continue;
}
tdProperties!.uriVariables!.attribute!.oneOf!.push(parsedAttribute);
}
}
if (cluster.command) {
if (!Array.isArray(cluster.command)) {
cluster.command = [cluster.command];
}
for (const command of cluster.command) {
const parsedCommand = parseCommand(
clusterId as MatterClusterId,
command,
);
if (!parsedCommand) {
console.warn(
`\tNo data for cluster: ${clusterId} command: ${command}, skipping...`,
);
continue;
}
tdActions!.input!.properties!.command!.oneOf!.push(parsedCommand);
}
}
}
// TODO: use @node-wot/td-tools to validate the generated TDs
writeFileSync(MATTER_WOT_MAPPING_FILE_PATH, JSON.stringify(mapping, null, 2));
console.log("Done!");
};
main();