@tsplc/typegen 0.2.17 → 0.2.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/cli.js +798 -544
- package/dist/cli.js.map +15 -9
- package/dist/codegen.js +252 -177
- package/dist/codegen.js.map +5 -5
- package/dist/hardware.js +447 -338
- package/dist/hardware.js.map +10 -5
- package/dist/index.js +848 -602
- package/dist/index.js.map +16 -9
- package/package.json +55 -62
- package/src/cli.ts +68 -51
- package/src/codegen/constants.ts +177 -86
- package/src/codegen/generate.ts +436 -388
- package/src/codegen/parser.ts +358 -287
- package/src/device-tree.ts +152 -140
- package/src/hardware/channels.ts +157 -0
- package/src/hardware/eds.ts +408 -0
- package/src/hardware/mapping-types.ts +88 -0
- package/src/hardware/modbus.ts +105 -0
- package/src/hardware/process-image.ts +102 -0
- package/src/hardware/selection.ts +136 -0
- package/src/hardware.ts +195 -844
- package/src/index.ts +25 -1
- package/src/project.ts +246 -151
package/dist/hardware.js
CHANGED
|
@@ -2,26 +2,7 @@
|
|
|
2
2
|
import { readdir, readFile } from "node:fs/promises";
|
|
3
3
|
import { resolve } from "node:path";
|
|
4
4
|
|
|
5
|
-
// packages/typegen/src/
|
|
6
|
-
var iecTypeBits = {
|
|
7
|
-
BOOL: 1,
|
|
8
|
-
SINT: 8,
|
|
9
|
-
USINT: 8,
|
|
10
|
-
BYTE: 8,
|
|
11
|
-
INT: 16,
|
|
12
|
-
UINT: 16,
|
|
13
|
-
WORD: 16,
|
|
14
|
-
DINT: 32,
|
|
15
|
-
UDINT: 32,
|
|
16
|
-
DWORD: 32,
|
|
17
|
-
REAL: 32,
|
|
18
|
-
LINT: 64,
|
|
19
|
-
ULINT: 64,
|
|
20
|
-
LWORD: 64,
|
|
21
|
-
LREAL: 64
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
// packages/typegen/src/hardware.ts
|
|
5
|
+
// packages/typegen/src/hardware/eds.ts
|
|
25
6
|
var cipTypes = {
|
|
26
7
|
193: "BOOL",
|
|
27
8
|
194: "SINT",
|
|
@@ -39,24 +20,25 @@ var cipTypes = {
|
|
|
39
20
|
211: "DWORD",
|
|
40
21
|
212: "LWORD"
|
|
41
22
|
};
|
|
42
|
-
function
|
|
43
|
-
let clean = ""
|
|
23
|
+
function stripComments(text) {
|
|
24
|
+
let clean = "";
|
|
25
|
+
const offsets = [];
|
|
26
|
+
let quote = false;
|
|
27
|
+
let comment = false;
|
|
44
28
|
for (let i = 0;i < text.length; i++) {
|
|
45
|
-
const c = text
|
|
46
|
-
if (comment
|
|
47
|
-
if (c === `
|
|
29
|
+
const c = text.charAt(i);
|
|
30
|
+
if (comment && c !== `
|
|
48
31
|
`) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
comment = false;
|
|
35
|
+
if (c === '"' && quote && text.charAt(i + 1) === '"') {
|
|
36
|
+
clean += '""';
|
|
37
|
+
offsets.push(i, i + 1);
|
|
38
|
+
i++;
|
|
52
39
|
continue;
|
|
53
40
|
}
|
|
54
41
|
if (c === '"') {
|
|
55
|
-
if (quote && text[i + 1] === '"') {
|
|
56
|
-
clean += '""';
|
|
57
|
-
i++;
|
|
58
|
-
continue;
|
|
59
|
-
}
|
|
60
42
|
quote = !quote;
|
|
61
43
|
}
|
|
62
44
|
if (c === "$" && !quote) {
|
|
@@ -64,98 +46,137 @@ function readEdsRecords(text) {
|
|
|
64
46
|
continue;
|
|
65
47
|
}
|
|
66
48
|
clean += c;
|
|
49
|
+
offsets.push(i);
|
|
67
50
|
}
|
|
68
|
-
if (quote)
|
|
51
|
+
if (quote) {
|
|
69
52
|
throw new Error("Unterminated EDS string.");
|
|
70
|
-
|
|
53
|
+
}
|
|
54
|
+
return { clean, offsets };
|
|
55
|
+
}
|
|
56
|
+
function recordFields(body) {
|
|
57
|
+
const fields = [];
|
|
58
|
+
let field = "";
|
|
59
|
+
let quoted = false;
|
|
60
|
+
for (let i = 0;i < body.length; i++) {
|
|
61
|
+
const c = body.charAt(i);
|
|
62
|
+
if (c === '"') {
|
|
63
|
+
if (quoted && body.charAt(i + 1) === '"') {
|
|
64
|
+
field += '"';
|
|
65
|
+
i++;
|
|
66
|
+
} else {
|
|
67
|
+
quoted = !quoted;
|
|
68
|
+
}
|
|
69
|
+
} else if (c === "," && !quoted) {
|
|
70
|
+
fields.push(field.trim());
|
|
71
|
+
field = "";
|
|
72
|
+
} else {
|
|
73
|
+
field += c;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
fields.push(field.trim());
|
|
77
|
+
return fields;
|
|
78
|
+
}
|
|
79
|
+
function edsSections(text) {
|
|
80
|
+
const { clean, offsets } = stripComments(text);
|
|
81
|
+
const sections = new Map;
|
|
71
82
|
let section;
|
|
72
|
-
const tokens = /\[([^\]\r\n]+)\]|([A-Za-z_][\w]*)\s*=\s*((?:"(?:[^"]|"")*"|[^;])*)\s*;/g;
|
|
73
83
|
let end = 0;
|
|
74
|
-
for (const match of clean.matchAll(
|
|
75
|
-
if (clean.slice(end, match.index).trim())
|
|
84
|
+
for (const match of clean.matchAll(/\[([^\]\r\n]+)\]|([A-Za-z_][\w]*)\s*=\s*((?:"(?:[^"]|"")*"|[^;])*)\s*;/g)) {
|
|
85
|
+
if (clean.slice(end, match.index).trim()) {
|
|
76
86
|
throw new Error("Malformed EDS record or section.");
|
|
87
|
+
}
|
|
77
88
|
end = match.index + match[0].length;
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
89
|
+
const [, header, key, body] = match;
|
|
90
|
+
if (header) {
|
|
91
|
+
const name = header.trim();
|
|
92
|
+
if (sections.has(name)) {
|
|
93
|
+
throw new Error(`Duplicate EDS section ${name}.`);
|
|
94
|
+
}
|
|
95
|
+
section = new Map;
|
|
96
|
+
sections.set(name, section);
|
|
83
97
|
continue;
|
|
84
98
|
}
|
|
85
|
-
if (!section)
|
|
99
|
+
if (!section) {
|
|
86
100
|
throw new Error("EDS record outside a section.");
|
|
87
|
-
|
|
88
|
-
if (section
|
|
101
|
+
}
|
|
102
|
+
if (section.has(key)) {
|
|
89
103
|
throw new Error(`Duplicate EDS record ${key}.`);
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (quoted && body[i + 1] === '"') {
|
|
97
|
-
field += '"';
|
|
98
|
-
i++;
|
|
99
|
-
} else
|
|
100
|
-
quoted = !quoted;
|
|
101
|
-
} else if (c === "," && !quoted) {
|
|
102
|
-
fields.push(field.trim());
|
|
103
|
-
field = "";
|
|
104
|
-
} else
|
|
105
|
-
field += c;
|
|
106
|
-
}
|
|
107
|
-
fields.push(field.trim());
|
|
108
|
-
section[key] = fields;
|
|
104
|
+
}
|
|
105
|
+
section.set(key, {
|
|
106
|
+
fields: recordFields(body),
|
|
107
|
+
start: offsets[match.index],
|
|
108
|
+
end: offsets[end - 1] + 1
|
|
109
|
+
});
|
|
109
110
|
}
|
|
110
|
-
if (clean.slice(end).trim())
|
|
111
|
+
if (clean.slice(end).trim()) {
|
|
111
112
|
throw new Error("Unterminated EDS record.");
|
|
112
|
-
|
|
113
|
+
}
|
|
114
|
+
return sections;
|
|
115
|
+
}
|
|
116
|
+
function readEdsRecords(text) {
|
|
117
|
+
return Object.fromEntries([...edsSections(text)].map(([name, records]) => [
|
|
118
|
+
name,
|
|
119
|
+
Object.fromEntries([...records].map(([key, record]) => [key, record.fields]))
|
|
120
|
+
]));
|
|
113
121
|
}
|
|
114
|
-
function integer(value, label,
|
|
115
|
-
if (!value || !/^(?:\d+|0x[\da-f]+)$/i.test(value))
|
|
122
|
+
function integer(value, label, { minimum }) {
|
|
123
|
+
if (!value || !/^(?:\d+|0x[\da-f]+)$/i.test(value)) {
|
|
116
124
|
throw new Error(`Invalid EDS ${label}: ${value ?? "missing"}.`);
|
|
125
|
+
}
|
|
117
126
|
const n = Number(value);
|
|
118
|
-
if (!Number.isSafeInteger(n) || n <
|
|
127
|
+
if (!Number.isSafeInteger(n) || n < minimum || n > 16777216) {
|
|
119
128
|
throw new Error(`Unsupported EDS ${label}: ${value}.`);
|
|
129
|
+
}
|
|
120
130
|
return n;
|
|
121
131
|
}
|
|
132
|
+
function logicalSegments(bytes) {
|
|
133
|
+
const [segment, ...rest] = bytes;
|
|
134
|
+
if (segment === undefined) {
|
|
135
|
+
return [];
|
|
136
|
+
}
|
|
137
|
+
const width = segment & 3;
|
|
138
|
+
if ((segment & 224) !== 32 || width > 2) {
|
|
139
|
+
throw new Error(`Unsupported EDS logical path segment ${segment.toString(16)}.`);
|
|
140
|
+
}
|
|
141
|
+
if (width && rest[0] !== 0) {
|
|
142
|
+
throw new Error("Invalid EDS logical path padding.");
|
|
143
|
+
}
|
|
144
|
+
const data = width ? rest.slice(1) : rest;
|
|
145
|
+
const length = 1 << width;
|
|
146
|
+
if (length > data.length) {
|
|
147
|
+
throw new Error("Truncated EDS logical path.");
|
|
148
|
+
}
|
|
149
|
+
const value = data.slice(0, length).reduce((sum, byte, index) => sum + byte * 2 ** (index * 8), 0);
|
|
150
|
+
return [
|
|
151
|
+
{ type: segment & 252, value },
|
|
152
|
+
...logicalSegments(data.slice(length))
|
|
153
|
+
];
|
|
154
|
+
}
|
|
122
155
|
function logicalPath(path) {
|
|
123
156
|
const bytes = path.trim() ? path.trim().split(/\s+/).map((token) => {
|
|
124
|
-
if (!/^[0-9a-f]{2}$/i.test(token))
|
|
157
|
+
if (!/^[0-9a-f]{2}$/i.test(token)) {
|
|
125
158
|
throw new Error(`Unsupported EDS path token ${token}.`);
|
|
159
|
+
}
|
|
126
160
|
return parseInt(token, 16);
|
|
127
161
|
}) : [];
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
throw new Error(`Unsupported EDS logical path segment ${segment.toString(16)}.`);
|
|
134
|
-
if (width && bytes[offset++] !== 0)
|
|
135
|
-
throw new Error("Invalid EDS logical path padding.");
|
|
136
|
-
const length = 1 << width;
|
|
137
|
-
if (offset + length > bytes.length)
|
|
138
|
-
throw new Error("Truncated EDS logical path.");
|
|
139
|
-
let value = 0;
|
|
140
|
-
for (let index = 0;index < length; index++)
|
|
141
|
-
value += bytes[offset++] * 2 ** (index * 8);
|
|
142
|
-
if ((segment & 252) === 36)
|
|
143
|
-
result.instance = value;
|
|
144
|
-
if ((segment & 252) === 44)
|
|
145
|
-
result.points.push(value);
|
|
146
|
-
}
|
|
147
|
-
return result;
|
|
162
|
+
const segments = logicalSegments(bytes);
|
|
163
|
+
return {
|
|
164
|
+
instance: segments.filter(({ type }) => type === 36).at(-1)?.value,
|
|
165
|
+
points: segments.filter(({ type }) => type === 44).map(({ value }) => value)
|
|
166
|
+
};
|
|
148
167
|
}
|
|
149
168
|
var optionalNumber = (value) => value?.trim() && Number.isFinite(Number(value)) ? Number(value) : undefined;
|
|
150
169
|
function parseEds(text, file = "device.eds") {
|
|
151
|
-
const records = readEdsRecords(text)
|
|
152
|
-
|
|
170
|
+
const records = readEdsRecords(text);
|
|
171
|
+
const device = records.Device;
|
|
172
|
+
if (!device?.ProdName?.[0]) {
|
|
153
173
|
throw new Error(`${file}: missing EDS Device.ProdName.`);
|
|
174
|
+
}
|
|
154
175
|
const parameters = Object.entries(records.Params ?? {}).filter(([id]) => /^Param\d+$/.test(id)).map(([id, fields]) => ({
|
|
155
176
|
id,
|
|
156
177
|
name: fields[6] || id,
|
|
157
178
|
type: cipTypes[Number(fields[4])] ?? "opaque",
|
|
158
|
-
size: integer(fields[5], `${id} data size`,
|
|
179
|
+
size: integer(fields[5], `${id} data size`, { minimum: 1 }),
|
|
159
180
|
unit: fields[7] ?? "",
|
|
160
181
|
description: fields[8] ?? "",
|
|
161
182
|
minimum: optionalNumber(fields[9]),
|
|
@@ -165,13 +186,16 @@ function parseEds(text, file = "device.eds") {
|
|
|
165
186
|
const params = new Map(parameters.map((parameter) => [parameter.id, parameter]));
|
|
166
187
|
const instances = new Map;
|
|
167
188
|
const size = (value) => {
|
|
168
|
-
if (!value)
|
|
189
|
+
if (!value) {
|
|
169
190
|
return;
|
|
170
|
-
|
|
171
|
-
|
|
191
|
+
}
|
|
192
|
+
if (!value.startsWith("Param")) {
|
|
193
|
+
return { value: integer(value, "connection size", { minimum: 0 }) };
|
|
194
|
+
}
|
|
172
195
|
const parameter = params.get(value);
|
|
173
|
-
if (!parameter || parameter.default === undefined)
|
|
196
|
+
if (!parameter || parameter.default === undefined) {
|
|
174
197
|
throw new Error(`${file}: connection size ${value} has no numeric default.`);
|
|
198
|
+
}
|
|
175
199
|
return {
|
|
176
200
|
value: parameter.default,
|
|
177
201
|
parameter: value,
|
|
@@ -180,18 +204,24 @@ function parseEds(text, file = "device.eds") {
|
|
|
180
204
|
};
|
|
181
205
|
};
|
|
182
206
|
const connections = Object.entries(records["Connection Manager"] ?? {}).filter(([id]) => /^Connection\d+$/.test(id)).map(([id, fields]) => {
|
|
183
|
-
const path = fields[14] ?? ""
|
|
207
|
+
const path = fields[14] ?? "";
|
|
208
|
+
const points = logicalPath(path).points;
|
|
184
209
|
const assembly = (reference, point) => {
|
|
185
|
-
if (!reference)
|
|
210
|
+
if (!reference) {
|
|
186
211
|
return;
|
|
187
|
-
|
|
212
|
+
}
|
|
213
|
+
const record = records.Assembly?.[reference];
|
|
214
|
+
if (!/^Assem\d+$/.test(reference) || !record) {
|
|
188
215
|
throw new Error(`${file}: unknown connection assembly ${reference}.`);
|
|
189
|
-
|
|
190
|
-
|
|
216
|
+
}
|
|
217
|
+
const instance = point ?? logicalPath(record[1] ?? "").instance;
|
|
218
|
+
if (instance === undefined) {
|
|
191
219
|
throw new Error(`${file}: no runtime instance for ${reference}.`);
|
|
220
|
+
}
|
|
192
221
|
const previous = instances.get(reference);
|
|
193
|
-
if (previous !== undefined && previous !== instance)
|
|
222
|
+
if (previous !== undefined && previous !== instance) {
|
|
194
223
|
throw new Error(`${file}: ${reference} has conflicting runtime instances.`);
|
|
224
|
+
}
|
|
195
225
|
instances.set(reference, instance);
|
|
196
226
|
return instance;
|
|
197
227
|
};
|
|
@@ -210,12 +240,17 @@ function parseEds(text, file = "device.eds") {
|
|
|
210
240
|
const members = [];
|
|
211
241
|
let offsetBits = 0;
|
|
212
242
|
for (let index = 6;index < fields.length; index += 2) {
|
|
213
|
-
if (!fields[index] && !fields[index + 1])
|
|
243
|
+
if (!fields[index] && !fields[index + 1]) {
|
|
214
244
|
continue;
|
|
215
|
-
|
|
245
|
+
}
|
|
246
|
+
const bits = integer(fields[index], `${id} member bits`, {
|
|
247
|
+
minimum: 1
|
|
248
|
+
});
|
|
249
|
+
const reference = fields[index + 1];
|
|
216
250
|
const parameter = reference ? params.get(reference) : undefined;
|
|
217
|
-
if (reference && !parameter)
|
|
251
|
+
if (reference && !parameter) {
|
|
218
252
|
throw new Error(`${file}: ${id} references unknown parameter ${reference}.`);
|
|
253
|
+
}
|
|
219
254
|
members.push({
|
|
220
255
|
...reference ? { parameter: reference } : {},
|
|
221
256
|
name: parameter?.name ?? "Padding",
|
|
@@ -227,9 +262,10 @@ function parseEds(text, file = "device.eds") {
|
|
|
227
262
|
});
|
|
228
263
|
offsetBits += bits;
|
|
229
264
|
}
|
|
230
|
-
const size = fields[2] ? integer(fields[2], `${id} size
|
|
231
|
-
if (offsetBits > size * 8)
|
|
265
|
+
const size = fields[2] ? integer(fields[2], `${id} size`, { minimum: 0 }) : Math.ceil(offsetBits / 8);
|
|
266
|
+
if (offsetBits > size * 8) {
|
|
232
267
|
throw new Error(`${file}: ${id} members exceed its ${size} byte payload.`);
|
|
268
|
+
}
|
|
233
269
|
const instance = logicalPath(fields[1] ?? "").instance ?? instances.get(id);
|
|
234
270
|
return { id, instance, name: fields[0] || id, size, members };
|
|
235
271
|
});
|
|
@@ -243,94 +279,80 @@ function parseEds(text, file = "device.eds") {
|
|
|
243
279
|
connections
|
|
244
280
|
};
|
|
245
281
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
else if (entry.isFile() && /\.eds$/i.test(entry.name))
|
|
258
|
-
files.push(path);
|
|
282
|
+
|
|
283
|
+
// packages/typegen/src/hardware/channels.ts
|
|
284
|
+
import { ioScalarBits } from "@tsplc/iec";
|
|
285
|
+
|
|
286
|
+
// packages/typegen/src/hardware/process-image.ts
|
|
287
|
+
import { ioArray, ioScalar, sizePrefix } from "@tsplc/iec";
|
|
288
|
+
function ioLayout(type) {
|
|
289
|
+
const array = ioArray(type.trim());
|
|
290
|
+
if (array) {
|
|
291
|
+
const element = ioLayout(array.element);
|
|
292
|
+
return element && element !== "BOOL" ? `${array.length}x${element}` : undefined;
|
|
259
293
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
const compatible = descriptor.connections.filter((connection) => (settings.inputs?.assembly === undefined || connection.inputAssembly === settings.inputs.assembly) && (settings.outputs?.assembly === undefined || connection.outputAssembly === settings.outputs.assembly));
|
|
264
|
-
if (settings.connection) {
|
|
265
|
-
const connection = compatible.find((item) => item.id === settings.connection);
|
|
266
|
-
if (!connection)
|
|
267
|
-
throw new Error(`${id}: connection ${settings.connection} does not match the selected assemblies.`);
|
|
268
|
-
return connection;
|
|
294
|
+
const scalar = ioScalar(type);
|
|
295
|
+
if (scalar?.family === "integer" || scalar?.family === "bits") {
|
|
296
|
+
return `INT${scalar.bits}`;
|
|
269
297
|
}
|
|
270
|
-
|
|
271
|
-
return compatible[0];
|
|
272
|
-
const owners = compatible.filter((item) => item.owner && item.inputAssembly !== undefined && item.outputAssembly !== undefined);
|
|
273
|
-
if (owners.length === 1)
|
|
274
|
-
return owners[0];
|
|
275
|
-
const choices = compatible.map((item) => `${item.id} "${item.name}" (in ${item.inputAssembly ?? "-"}, out ${item.outputAssembly ?? "-"})`).join(", ");
|
|
276
|
-
throw new Error(`${id}: select connection; ${settings.file} offers ${choices}.`);
|
|
298
|
+
return scalar?.name;
|
|
277
299
|
}
|
|
278
|
-
|
|
279
|
-
const placed = regions.flatMap(({ region, size, address }) => address ? [{ region, start: Number(address.slice(3)), end: Number(address.slice(3)) + size }] : []);
|
|
280
|
-
const cursors = { I: 0, Q: 0 };
|
|
281
|
-
return regions.map((selection) => {
|
|
282
|
-
const { id, region, size, address } = selection;
|
|
283
|
-
if (address)
|
|
284
|
-
return { ...selection, address };
|
|
285
|
-
if (!generated)
|
|
286
|
-
throw new Error(`${id}: set the %${region}B address the template assigns to this assembly, or let codesys.deviceTree place it.`);
|
|
287
|
-
const aligned = (offset) => Math.ceil(offset / 4) * 4;
|
|
288
|
-
let start = aligned(cursors[region]);
|
|
289
|
-
for (let blocked = true;blocked; ) {
|
|
290
|
-
const overlap = placed.find((item) => item.region === region && item.start < start + size && start < item.end);
|
|
291
|
-
blocked = overlap !== undefined;
|
|
292
|
-
if (overlap)
|
|
293
|
-
start = aligned(overlap.end);
|
|
294
|
-
}
|
|
295
|
-
placed.push({ region, start, end: start + size });
|
|
296
|
-
cursors[region] = start + size;
|
|
297
|
-
return { ...selection, address: `%${region}B${start}` };
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
var addressWidths = { 1: "X", 8: "B", 16: "W", 32: "D" };
|
|
300
|
+
var addressSize = (bits) => bits === 64 ? undefined : sizePrefix(bits);
|
|
301
301
|
function directAddress(region, base, offsetBits, bits) {
|
|
302
|
-
const
|
|
303
|
-
if (!
|
|
302
|
+
const size = addressSize(bits);
|
|
303
|
+
if (!size) {
|
|
304
304
|
return;
|
|
305
|
+
}
|
|
305
306
|
const byte = base + Math.floor(offsetBits / 8);
|
|
306
|
-
if (bits === 1)
|
|
307
|
+
if (bits === 1) {
|
|
307
308
|
return `%${region}X${byte}.${offsetBits % 8}`;
|
|
308
|
-
|
|
309
|
+
}
|
|
310
|
+
if ((base * 8 + offsetBits) % bits) {
|
|
309
311
|
return;
|
|
310
|
-
|
|
312
|
+
}
|
|
313
|
+
return `%${region}${size}${byte / (bits / 8)}`;
|
|
311
314
|
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
315
|
+
var byteOffset = (address) => Number(address.slice(3));
|
|
316
|
+
function regionPlacer(regions, { generatedTree }) {
|
|
317
|
+
const taken = regions.flatMap(({ region, size, address }) => address ? [
|
|
318
|
+
{
|
|
319
|
+
region,
|
|
320
|
+
start: byteOffset(address),
|
|
321
|
+
end: byteOffset(address) + size
|
|
322
|
+
}
|
|
323
|
+
] : []);
|
|
324
|
+
const cursors = { I: 0, Q: 0 };
|
|
325
|
+
const aligned = (offset) => Math.ceil(offset / 4) * 4;
|
|
326
|
+
return ({ id, region, size, address }) => {
|
|
327
|
+
if (address) {
|
|
328
|
+
return address;
|
|
329
|
+
}
|
|
330
|
+
if (!generatedTree) {
|
|
331
|
+
throw new Error(`${id}: set the %${region}B address the template assigns to this assembly, or let codesys.deviceTree place it.`);
|
|
332
|
+
}
|
|
333
|
+
const clear = (start) => {
|
|
334
|
+
const overlap = taken.find((item) => item.region === region && item.start < start + size && start < item.end);
|
|
335
|
+
return overlap ? clear(aligned(overlap.end)) : start;
|
|
336
|
+
};
|
|
337
|
+
const start = clear(aligned(cursors[region]));
|
|
338
|
+
taken.push({ region, start, end: start + size });
|
|
339
|
+
cursors[region] = start + size;
|
|
340
|
+
return `%${region}B${start}`;
|
|
341
|
+
};
|
|
331
342
|
}
|
|
332
|
-
|
|
333
|
-
|
|
343
|
+
|
|
344
|
+
// packages/typegen/src/hardware/channels.ts
|
|
345
|
+
function assemblyChannels({
|
|
346
|
+
device,
|
|
347
|
+
direction,
|
|
348
|
+
instance,
|
|
349
|
+
members,
|
|
350
|
+
base,
|
|
351
|
+
payloadSize
|
|
352
|
+
}) {
|
|
353
|
+
const prefix = `${direction}s`;
|
|
354
|
+
const region = direction === "input" ? "I" : "Q";
|
|
355
|
+
const payloadBits = payloadSize * 8;
|
|
334
356
|
const address = (offsetBits, bits) => directAddress(region, base, offsetBits, bits);
|
|
335
357
|
const channels = [];
|
|
336
358
|
for (let i = 0;i < payloadSize; i++) {
|
|
@@ -348,7 +370,7 @@ function assemblyChannels(device, direction, instance, members, base, payloadSiz
|
|
|
348
370
|
unit: "",
|
|
349
371
|
description: "Assembly payload byte"
|
|
350
372
|
});
|
|
351
|
-
for (let bit = 0;bit < 8; bit++)
|
|
373
|
+
for (let bit = 0;bit < 8; bit++) {
|
|
352
374
|
channels.push({
|
|
353
375
|
device,
|
|
354
376
|
id: `${key}.Bit${bit}`,
|
|
@@ -363,19 +385,26 @@ function assemblyChannels(device, direction, instance, members, base, payloadSiz
|
|
|
363
385
|
unit: "",
|
|
364
386
|
description: "Assembly payload bit"
|
|
365
387
|
});
|
|
388
|
+
}
|
|
366
389
|
}
|
|
367
390
|
const named = members.filter((member) => member.parameter).map((member) => ({ member, name: member.name.replace(/[^\w]/g, "_") }));
|
|
368
|
-
const
|
|
369
|
-
|
|
370
|
-
|
|
391
|
+
const seen = new Set;
|
|
392
|
+
const repeated = new Set;
|
|
393
|
+
for (const { name } of named) {
|
|
394
|
+
if (seen.has(name)) {
|
|
395
|
+
repeated.add(name);
|
|
396
|
+
}
|
|
397
|
+
seen.add(name);
|
|
398
|
+
}
|
|
371
399
|
const counts = new Map;
|
|
372
400
|
for (const { member, name } of named) {
|
|
373
|
-
if (member.offsetBits >= payloadBits)
|
|
401
|
+
if (member.offsetBits >= payloadBits) {
|
|
374
402
|
continue;
|
|
403
|
+
}
|
|
375
404
|
const count = counts.get(name) ?? 0;
|
|
376
405
|
counts.set(name, count + 1);
|
|
377
|
-
const key = `${prefix}.${name}${
|
|
378
|
-
const scalarBits =
|
|
406
|
+
const key = `${prefix}.${name}${repeated.has(name) ? `[${count}]` : ""}`;
|
|
407
|
+
const scalarBits = ioScalarBits(member.type);
|
|
379
408
|
const channel = {
|
|
380
409
|
device,
|
|
381
410
|
id: key,
|
|
@@ -389,21 +418,25 @@ function assemblyChannels(device, direction, instance, members, base, payloadSiz
|
|
|
389
418
|
unit: member.unit,
|
|
390
419
|
description: member.description
|
|
391
420
|
};
|
|
392
|
-
const
|
|
393
|
-
if (!
|
|
394
|
-
if (member.offsetBits + member.bits <= payloadBits)
|
|
421
|
+
const spansScalars = scalarBits !== undefined && scalarBits >= 8 && member.bits > scalarBits && member.bits % scalarBits === 0 && member.offsetBits % scalarBits === 0;
|
|
422
|
+
if (!spansScalars) {
|
|
423
|
+
if (member.offsetBits + member.bits <= payloadBits) {
|
|
395
424
|
channels.push(channel);
|
|
425
|
+
}
|
|
396
426
|
continue;
|
|
397
427
|
}
|
|
398
428
|
const length = Math.floor(Math.min(member.bits, payloadBits - member.offsetBits) / scalarBits);
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
429
|
+
const span = {
|
|
430
|
+
...channel,
|
|
431
|
+
bits: length * scalarBits,
|
|
432
|
+
type: `ARRAY[0..${length - 1}] OF ${member.type}`,
|
|
433
|
+
address: address(member.offsetBits, scalarBits)
|
|
434
|
+
};
|
|
435
|
+
channels.push(span);
|
|
403
436
|
for (let index = 0;index < length; index++) {
|
|
404
437
|
const offset = member.offsetBits + index * scalarBits;
|
|
405
438
|
channels.push({
|
|
406
|
-
...
|
|
439
|
+
...span,
|
|
407
440
|
id: `${key}[${index}]`,
|
|
408
441
|
parent: key,
|
|
409
442
|
name: `${member.name}[${index}]`,
|
|
@@ -416,152 +449,118 @@ function assemblyChannels(device, direction, instance, members, base, payloadSiz
|
|
|
416
449
|
}
|
|
417
450
|
return channels;
|
|
418
451
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
const
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
const
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
const region = direction === "input" ? "I" : "Q";
|
|
442
|
-
if (selection?.address && !selection.address.startsWith(`%${region}B`))
|
|
443
|
-
throw new Error(`${id}: ${direction} assembly needs a %${region}B address.`);
|
|
444
|
-
const declaredSize = direction === "input" ? connection?.inputSize : connection?.outputSize;
|
|
445
|
-
const payloadSize = selection?.size ?? declaredSize?.value ?? assembly.size;
|
|
446
|
-
if (!Number.isSafeInteger(payloadSize) || payloadSize <= 0 || payloadSize > assembly.size || declaredSize?.minimum !== undefined && payloadSize < declaredSize.minimum || declaredSize?.maximum !== undefined && payloadSize > declaredSize.maximum || selection?.size !== undefined && declaredSize && !declaredSize.parameter && payloadSize !== declaredSize.value)
|
|
447
|
-
throw new Error(`${id}: ${direction} payload size ${payloadSize} is outside the selected EDS connection's bounds.`);
|
|
448
|
-
return [
|
|
449
|
-
{
|
|
450
|
-
id,
|
|
451
|
-
direction,
|
|
452
|
-
region,
|
|
453
|
-
instance,
|
|
454
|
-
assembly,
|
|
455
|
-
size: payloadSize,
|
|
456
|
-
address: selection?.address
|
|
457
|
-
}
|
|
458
|
-
];
|
|
459
|
-
});
|
|
460
|
-
return { id, settings, descriptor, connection, selections };
|
|
461
|
-
});
|
|
462
|
-
const modbusBytes = { Q: 4, I: 8 };
|
|
463
|
-
const modbusRegions = selected.flatMap(({ id, settings }) => {
|
|
464
|
-
const count = Object.keys(settings.modbus?.registers ?? {}).length;
|
|
465
|
-
return count ? ["Q", "I"].map((region) => ({
|
|
466
|
-
id,
|
|
467
|
-
region,
|
|
468
|
-
size: count * modbusBytes[region],
|
|
469
|
-
modbus: true
|
|
470
|
-
})) : [];
|
|
471
|
-
});
|
|
472
|
-
const placed = placeAssemblies([...selected.flatMap((device) => device.selections), ...modbusRegions], config.codesys?.deviceTree !== undefined);
|
|
473
|
-
const devices = selected.map(({ id, settings, descriptor, connection }) => {
|
|
474
|
-
const assemblies = [];
|
|
475
|
-
const channels = [];
|
|
476
|
-
const modbusBase = (region) => Number(placed.find((item) => item.id === id && ("modbus" in item) && item.region === region).address.slice(3));
|
|
477
|
-
const registers = Object.entries(settings.modbus?.registers ?? {}).map(([name, offset], index) => {
|
|
478
|
-
const registerChannels = modbusChannels(id, name, modbusBase("Q") + index * modbusBytes.Q, modbusBase("I") + index * modbusBytes.I);
|
|
479
|
-
channels.push(...registerChannels);
|
|
480
|
-
const [value, write, status, requests] = registerChannels.map((channel) => channel.address);
|
|
481
|
-
return { name, offset, value, write, status, requests };
|
|
452
|
+
|
|
453
|
+
// packages/typegen/src/hardware/modbus.ts
|
|
454
|
+
var registerBytes = { Q: 4, I: 8 };
|
|
455
|
+
function placeModbusRegisters(device, registers, place) {
|
|
456
|
+
const entries = Object.entries(registers);
|
|
457
|
+
if (!entries.length) {
|
|
458
|
+
return [];
|
|
459
|
+
}
|
|
460
|
+
const outputBase = byteOffset(place({ id: device, region: "Q", size: entries.length * registerBytes.Q }));
|
|
461
|
+
const inputBase = byteOffset(place({ id: device, region: "I", size: entries.length * registerBytes.I }));
|
|
462
|
+
return entries.map(([name, offset], index) => {
|
|
463
|
+
const channel = (field, direction, offsetBits, bits, type, description) => ({
|
|
464
|
+
device,
|
|
465
|
+
id: `modbus.${name}.${field}`,
|
|
466
|
+
name: `${name} ${field}`,
|
|
467
|
+
direction,
|
|
468
|
+
offsetBits,
|
|
469
|
+
bits,
|
|
470
|
+
type,
|
|
471
|
+
address: directAddress(direction === "input" ? "I" : "Q", direction === "input" ? inputBase + index * registerBytes.I : outputBase + index * registerBytes.Q, offsetBits, bits),
|
|
472
|
+
unit: "",
|
|
473
|
+
description
|
|
482
474
|
});
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
assemblies.push({
|
|
488
|
-
direction,
|
|
489
|
-
instance,
|
|
490
|
-
name: assembly.name,
|
|
491
|
-
address,
|
|
492
|
-
size,
|
|
493
|
-
capacity: assembly.size
|
|
494
|
-
});
|
|
495
|
-
channels.push(...assemblyChannels(id, direction, instance, assembly.members, Number(address.slice(3)), size));
|
|
496
|
-
}
|
|
475
|
+
const value = channel("value", "output", 0, 16, "WORD", "Value to write");
|
|
476
|
+
const write = channel("write", "output", 16, 1, "BOOL", "Writes the value when it rises");
|
|
477
|
+
const status = channel("status", "input", 0, 8, "BYTE", "The last answer: 0 accepted, otherwise the exception code");
|
|
478
|
+
const requests = channel("requests", "input", 32, 32, "UDINT", "Requests the device answered");
|
|
497
479
|
return {
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
480
|
+
register: {
|
|
481
|
+
name,
|
|
482
|
+
offset,
|
|
483
|
+
value: value.address,
|
|
484
|
+
write: write.address,
|
|
485
|
+
status: status.address,
|
|
486
|
+
requests: requests.address
|
|
487
|
+
},
|
|
488
|
+
channels: [value, write, status, requests]
|
|
505
489
|
};
|
|
506
490
|
});
|
|
507
|
-
return { descriptors, devices };
|
|
508
491
|
}
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
const
|
|
515
|
-
if (!
|
|
516
|
-
throw new Error(`${
|
|
492
|
+
|
|
493
|
+
// packages/typegen/src/hardware/selection.ts
|
|
494
|
+
function selectConnection(id, settings, descriptor) {
|
|
495
|
+
const compatible = descriptor.connections.filter((connection) => (settings.inputs?.assembly === undefined || connection.inputAssembly === settings.inputs.assembly) && (settings.outputs?.assembly === undefined || connection.outputAssembly === settings.outputs.assembly));
|
|
496
|
+
if (settings.connection) {
|
|
497
|
+
const connection = compatible.find((item) => item.id === settings.connection);
|
|
498
|
+
if (!connection) {
|
|
499
|
+
throw new Error(`${id}: connection ${settings.connection} does not match the selected assemblies.`);
|
|
500
|
+
}
|
|
501
|
+
return connection;
|
|
502
|
+
}
|
|
503
|
+
if (compatible.length <= 1) {
|
|
504
|
+
return compatible[0];
|
|
505
|
+
}
|
|
506
|
+
const owners = compatible.filter((item) => item.owner && item.inputAssembly !== undefined && item.outputAssembly !== undefined);
|
|
507
|
+
if (owners.length === 1) {
|
|
508
|
+
return owners[0];
|
|
509
|
+
}
|
|
510
|
+
const choices = compatible.map((item) => `${item.id} "${item.name}" (in ${item.inputAssembly ?? "-"}, out ${item.outputAssembly ?? "-"})`).join(", ");
|
|
511
|
+
throw new Error(`${id}: select connection; ${settings.file} offers ${choices}.`);
|
|
512
|
+
}
|
|
513
|
+
function selectAssemblies(id, settings, descriptor, connection) {
|
|
514
|
+
return ["input", "output"].flatMap((direction) => {
|
|
515
|
+
const selection = direction === "input" ? settings.inputs : settings.outputs;
|
|
516
|
+
const instance = selection?.assembly ?? (direction === "input" ? connection?.inputAssembly : connection?.outputAssembly);
|
|
517
|
+
if (instance === undefined) {
|
|
518
|
+
if (selection) {
|
|
519
|
+
throw new Error(`${id}: select its ${direction}s assembly.`);
|
|
520
|
+
}
|
|
521
|
+
return [];
|
|
522
|
+
}
|
|
523
|
+
const assembly = descriptor.assemblies.find((a) => a.instance === instance);
|
|
524
|
+
if (!assembly) {
|
|
525
|
+
throw new Error(`${id}: assembly ${instance} is not present in ${settings.file}.`);
|
|
526
|
+
}
|
|
527
|
+
const region = direction === "input" ? "I" : "Q";
|
|
528
|
+
if (selection?.address && !selection.address.startsWith(`%${region}B`)) {
|
|
529
|
+
throw new Error(`${id}: ${direction} assembly needs a %${region}B address.`);
|
|
530
|
+
}
|
|
531
|
+
const declaredSize = direction === "input" ? connection?.inputSize : connection?.outputSize;
|
|
532
|
+
const payloadSize = selection?.size ?? declaredSize?.value ?? assembly.size;
|
|
533
|
+
if (!Number.isSafeInteger(payloadSize) || payloadSize <= 0 || payloadSize > assembly.size || declaredSize?.minimum !== undefined && payloadSize < declaredSize.minimum || declaredSize?.maximum !== undefined && payloadSize > declaredSize.maximum || selection?.size !== undefined && declaredSize && !declaredSize.parameter && payloadSize !== declaredSize.value) {
|
|
534
|
+
throw new Error(`${id}: ${direction} payload size ${payloadSize} is outside the selected EDS connection's bounds.`);
|
|
535
|
+
}
|
|
517
536
|
return [
|
|
518
|
-
symbol,
|
|
519
537
|
{
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
538
|
+
id,
|
|
539
|
+
direction,
|
|
540
|
+
region,
|
|
541
|
+
instance,
|
|
542
|
+
assembly,
|
|
543
|
+
size: payloadSize,
|
|
544
|
+
address: selection?.address
|
|
524
545
|
}
|
|
525
546
|
];
|
|
526
|
-
})
|
|
527
|
-
}
|
|
528
|
-
var timeLayouts = new Map([
|
|
529
|
-
["TIME", "TIME"],
|
|
530
|
-
["DATE", "DATE"],
|
|
531
|
-
["TOD", "TIME_OF_DAY"],
|
|
532
|
-
["TIME_OF_DAY", "TIME_OF_DAY"],
|
|
533
|
-
["DT", "DATE_AND_TIME"],
|
|
534
|
-
["DATE_AND_TIME", "DATE_AND_TIME"]
|
|
535
|
-
]);
|
|
536
|
-
function ioScalarBits(type) {
|
|
537
|
-
const name = type.trim().toUpperCase();
|
|
538
|
-
return timeLayouts.has(name) ? 32 : iecTypeBits[name];
|
|
539
|
-
}
|
|
540
|
-
function ioLayout(type) {
|
|
541
|
-
const array = /^ARRAY\s*\[\s*(-?\d+)\s*\.\.\s*(-?\d+)\s*\]\s*OF\s+(\w+)$/i.exec(type.trim());
|
|
542
|
-
if (array) {
|
|
543
|
-
const element = ioLayout(array[3]);
|
|
544
|
-
return element && element !== "BOOL" ? `${Number(array[2]) - Number(array[1]) + 1}x${element}` : undefined;
|
|
545
|
-
}
|
|
546
|
-
const name = type.trim().toUpperCase();
|
|
547
|
-
if (name === "BOOL" || name === "REAL" || name === "LREAL")
|
|
548
|
-
return name;
|
|
549
|
-
const bits = ioScalarBits(name);
|
|
550
|
-
return timeLayouts.get(name) ?? (bits ? `INT${bits}` : undefined);
|
|
547
|
+
});
|
|
551
548
|
}
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
};
|
|
549
|
+
|
|
550
|
+
// packages/typegen/src/hardware.ts
|
|
551
|
+
import { ioScalarBits as ioScalarBits3 } from "@tsplc/iec";
|
|
552
|
+
|
|
553
|
+
// packages/typegen/src/hardware/mapping-types.ts
|
|
554
|
+
import { ioScalarBits as ioScalarBits2 } from "@tsplc/iec";
|
|
559
555
|
var directBinding = (layout) => {
|
|
560
556
|
const scalar = layout.replace(/^\d+x/, "");
|
|
561
|
-
const
|
|
562
|
-
|
|
557
|
+
const integer = /^INT(\d+)$/.exec(scalar);
|
|
558
|
+
const bits = integer ? Number(integer[1]) : ioScalarBits2(scalar);
|
|
559
|
+
const size = bits === undefined ? undefined : addressSize(bits);
|
|
560
|
+
if (!size) {
|
|
563
561
|
return;
|
|
564
|
-
|
|
562
|
+
}
|
|
563
|
+
return size === "X" ? '{ address: `%${"I" | "Q"}X${number}.${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7}`; type?: string }' : `{ address: \`%\${"I" | "Q"}${size}\${number}\`; type?: string }`;
|
|
565
564
|
};
|
|
566
565
|
function generateHardwareTypes(catalog, targets, publication, ioTypes = true) {
|
|
567
566
|
const selectors = publication?.map((selector) => JSON.stringify(selector)).join(" | ") ?? "string";
|
|
@@ -569,8 +568,9 @@ function generateHardwareTypes(catalog, targets, publication, ioTypes = true) {
|
|
|
569
568
|
export type PublicationSelector = ${selectors || "never"};
|
|
570
569
|
export type PlcPublication = { readonly production: readonly PublicationSelector[]; readonly development: readonly PublicationSelector[] };
|
|
571
570
|
`;
|
|
572
|
-
if (!ioTypes)
|
|
571
|
+
if (!ioTypes) {
|
|
573
572
|
return publicationTypes;
|
|
573
|
+
}
|
|
574
574
|
const channel = (device, id, options) => `{ device: ${JSON.stringify(device)}; channel: ${JSON.stringify(id)}${options} }`;
|
|
575
575
|
const channels = catalog.devices.flatMap((device) => device.channels.filter((item) => item.address).map((item) => ({
|
|
576
576
|
binding: channel(device.id, item.id, item.direction === "input" && item.type === "BOOL" ? "; inverted?: boolean; shared?: boolean" : ""),
|
|
@@ -594,15 +594,124 @@ export type HardwareIoMapping = ${mapping};
|
|
|
594
594
|
export const hardware = ${JSON.stringify(catalog, null, 2)} as const;
|
|
595
595
|
`;
|
|
596
596
|
}
|
|
597
|
+
|
|
598
|
+
// packages/typegen/src/hardware.ts
|
|
599
|
+
async function edsFiles(directory) {
|
|
600
|
+
const entries = await readdir(directory, { withFileTypes: true }).catch((cause) => {
|
|
601
|
+
if (cause && typeof cause === "object" && "code" in cause && cause.code === "ENOENT") {
|
|
602
|
+
return [];
|
|
603
|
+
}
|
|
604
|
+
throw cause;
|
|
605
|
+
});
|
|
606
|
+
const files = [];
|
|
607
|
+
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
608
|
+
const path = resolve(directory, entry.name);
|
|
609
|
+
if (entry.isDirectory()) {
|
|
610
|
+
files.push(...await edsFiles(path));
|
|
611
|
+
} else if (entry.isFile() && /\.eds$/i.test(entry.name)) {
|
|
612
|
+
files.push(path);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
return files;
|
|
616
|
+
}
|
|
617
|
+
async function readDescriptors(config, root) {
|
|
618
|
+
const files = new Set;
|
|
619
|
+
for (const directory of [config.eds].flat()) {
|
|
620
|
+
for (const file of await edsFiles(resolve(root, directory))) {
|
|
621
|
+
files.add(file);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
for (const device of Object.values(config.devices)) {
|
|
625
|
+
files.add(resolve(root, device.file));
|
|
626
|
+
}
|
|
627
|
+
return Promise.all([...files].map(async (file) => parseEds(await readFile(file, "utf8"), file)));
|
|
628
|
+
}
|
|
629
|
+
async function loadHardware(config, root) {
|
|
630
|
+
const descriptors = await readDescriptors(config, root);
|
|
631
|
+
const selected = Object.entries(config.devices).map(([id, settings]) => {
|
|
632
|
+
const descriptor = descriptors.find((d) => d.file === resolve(root, settings.file));
|
|
633
|
+
const connection = selectConnection(id, settings, descriptor);
|
|
634
|
+
const selections = selectAssemblies(id, settings, descriptor, connection);
|
|
635
|
+
return { id, settings, descriptor, connection, selections };
|
|
636
|
+
});
|
|
637
|
+
const place = regionPlacer(selected.flatMap((device) => device.selections), { generatedTree: config.codesys?.deviceTree !== undefined });
|
|
638
|
+
const placed = selected.map((device) => ({
|
|
639
|
+
...device,
|
|
640
|
+
assemblies: device.selections.map((selection) => ({
|
|
641
|
+
...selection,
|
|
642
|
+
address: place(selection)
|
|
643
|
+
}))
|
|
644
|
+
}));
|
|
645
|
+
const devices = placed.map(({ id, settings, descriptor, connection, assemblies }) => {
|
|
646
|
+
const registers = placeModbusRegisters(id, settings.modbus?.registers ?? {}, place);
|
|
647
|
+
return {
|
|
648
|
+
id,
|
|
649
|
+
descriptor: descriptor.file,
|
|
650
|
+
connection: connection && {
|
|
651
|
+
id: connection.id,
|
|
652
|
+
name: connection.name,
|
|
653
|
+
path: connection.path
|
|
654
|
+
},
|
|
655
|
+
assemblies: assemblies.map(({ direction, instance, assembly, size, address }) => ({
|
|
656
|
+
direction,
|
|
657
|
+
instance,
|
|
658
|
+
name: assembly.name,
|
|
659
|
+
address,
|
|
660
|
+
size,
|
|
661
|
+
capacity: assembly.size
|
|
662
|
+
})),
|
|
663
|
+
channels: [
|
|
664
|
+
...registers.flatMap((register) => register.channels),
|
|
665
|
+
...assemblies.flatMap(({ direction, instance, assembly, size, address }) => assemblyChannels({
|
|
666
|
+
device: id,
|
|
667
|
+
direction,
|
|
668
|
+
instance,
|
|
669
|
+
members: assembly.members,
|
|
670
|
+
base: byteOffset(address),
|
|
671
|
+
payloadSize: size
|
|
672
|
+
}))
|
|
673
|
+
],
|
|
674
|
+
statusSymbols: settings.statusSymbols ?? {},
|
|
675
|
+
...settings.modbus && {
|
|
676
|
+
modbus: {
|
|
677
|
+
port: settings.modbus.port,
|
|
678
|
+
registers: registers.map(({ register }) => register)
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
};
|
|
682
|
+
});
|
|
683
|
+
return { descriptors, devices };
|
|
684
|
+
}
|
|
685
|
+
function resolveIo(config, catalog) {
|
|
686
|
+
return Object.fromEntries(Object.entries(config.io).map(([symbol, binding]) => {
|
|
687
|
+
if ("address" in binding) {
|
|
688
|
+
return [symbol, binding];
|
|
689
|
+
}
|
|
690
|
+
const device = catalog.devices.find((d) => d.id === binding.device);
|
|
691
|
+
const channel = device?.channels.find((c) => c.id === binding.channel);
|
|
692
|
+
if (!channel?.address) {
|
|
693
|
+
throw new Error(`${symbol}: unknown or unaddressable channel ${binding.device}.${binding.channel}.`);
|
|
694
|
+
}
|
|
695
|
+
return [
|
|
696
|
+
symbol,
|
|
697
|
+
{
|
|
698
|
+
address: channel.address,
|
|
699
|
+
type: channel.type,
|
|
700
|
+
...binding.inverted && { inverted: true },
|
|
701
|
+
...binding.shared && { shared: true }
|
|
702
|
+
}
|
|
703
|
+
];
|
|
704
|
+
}));
|
|
705
|
+
}
|
|
597
706
|
export {
|
|
598
707
|
generateHardwareTypes,
|
|
599
708
|
ioLayout,
|
|
600
|
-
ioScalarBits,
|
|
709
|
+
ioScalarBits3 as ioScalarBits,
|
|
601
710
|
loadHardware,
|
|
602
711
|
parseEds,
|
|
603
712
|
readEdsRecords,
|
|
604
713
|
resolveIo
|
|
605
714
|
};
|
|
606
715
|
|
|
607
|
-
//# debugId=
|
|
716
|
+
//# debugId=935495DBA5BF1C7C64756E2164756E21
|
|
608
717
|
//# sourceMappingURL=hardware.js.map
|