@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/src/device-tree.ts
CHANGED
|
@@ -1,43 +1,9 @@
|
|
|
1
1
|
import { readFile } from "node:fs/promises";
|
|
2
2
|
import type { DeviceTree, ResolvedTsplcConfig } from "@tsplc/config";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
/** Section records with their spans in the original text; comments cannot match. */
|
|
8
|
-
function edsRecords(text: string) {
|
|
9
|
-
let masked = "",
|
|
10
|
-
quote = false,
|
|
11
|
-
comment = false;
|
|
12
|
-
for (const c of text) {
|
|
13
|
-
if (comment && c !== "\n") {
|
|
14
|
-
masked += " ".repeat(c.length);
|
|
15
|
-
continue;
|
|
16
|
-
}
|
|
17
|
-
comment = false;
|
|
18
|
-
if (c === '"') quote = !quote;
|
|
19
|
-
if (c === "$" && !quote) {
|
|
20
|
-
comment = true;
|
|
21
|
-
masked += " ";
|
|
22
|
-
continue;
|
|
23
|
-
}
|
|
24
|
-
masked += c;
|
|
25
|
-
}
|
|
26
|
-
const records: { section: string; key: string; start: number; end: number; fields: string[] }[] =
|
|
27
|
-
[];
|
|
28
|
-
let section = "";
|
|
29
|
-
for (const match of masked.matchAll(recordPattern)) {
|
|
30
|
-
if (match[1]) {
|
|
31
|
-
section = match[1].trim();
|
|
32
|
-
continue;
|
|
33
|
-
}
|
|
34
|
-
const start = match.index!,
|
|
35
|
-
end = start + match[0].length;
|
|
36
|
-
const fields = readEdsRecords(`[R]\n${masked.slice(start, end)}`).R![match[2]!]!;
|
|
37
|
-
records.push({ section, key: match[2]!, start, end, fields });
|
|
38
|
-
}
|
|
39
|
-
return records;
|
|
40
|
-
}
|
|
3
|
+
import { iecName } from "@tsplc/config/build";
|
|
4
|
+
import type { HardwareCatalog } from "./hardware.ts";
|
|
5
|
+
import { edsSections } from "./hardware/eds.ts";
|
|
6
|
+
import { byteOffset } from "./hardware/process-image.ts";
|
|
41
7
|
|
|
42
8
|
const quoted = (value: string) => `"${value.replaceAll('"', '""')}"`;
|
|
43
9
|
|
|
@@ -49,54 +15,76 @@ const quoted = (value: string) => `"${value.replaceAll('"', '""')}"`;
|
|
|
49
15
|
* first byte of a multi-byte member such as a reader's result data.
|
|
50
16
|
*/
|
|
51
17
|
export function normalizeEds(
|
|
52
|
-
|
|
53
|
-
|
|
18
|
+
text: string,
|
|
19
|
+
selection: { connection: string; inputSize?: number; outputSize?: number },
|
|
54
20
|
): string {
|
|
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
|
-
|
|
21
|
+
const sections = edsSections(text);
|
|
22
|
+
const connections = [...(sections.get("Connection Manager") ?? [])].filter(
|
|
23
|
+
([key]) => /^Connection\d+$/.test(key),
|
|
24
|
+
);
|
|
25
|
+
const selected = connections.find(
|
|
26
|
+
([key]) => key === selection.connection,
|
|
27
|
+
)?.[1];
|
|
28
|
+
if (!selected) {
|
|
29
|
+
throw new Error(`EDS has no ${selection.connection}.`);
|
|
30
|
+
}
|
|
31
|
+
const fields = [...selected.fields];
|
|
32
|
+
const resized = new Map<string, number>();
|
|
33
|
+
for (const [sizeField, formatField, size] of [
|
|
34
|
+
[3, 4, selection.outputSize],
|
|
35
|
+
[6, 7, selection.inputSize],
|
|
36
|
+
] as const) {
|
|
37
|
+
const assembly = fields[formatField];
|
|
38
|
+
if (!assembly) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (size === undefined) {
|
|
42
|
+
throw new Error(`Select a payload size for ${assembly}.`);
|
|
43
|
+
}
|
|
44
|
+
fields[sizeField] = String(size);
|
|
45
|
+
resized.set(assembly, size);
|
|
46
|
+
}
|
|
47
|
+
const edits = connections.map(([, record]) => ({
|
|
48
|
+
start: record.start,
|
|
49
|
+
end: record.end,
|
|
50
|
+
text:
|
|
51
|
+
record === selected
|
|
52
|
+
? `Connection1 = ${fields.map((value, index) => (index >= 12 ? quoted(value) : value)).join(",")};`
|
|
53
|
+
: "",
|
|
54
|
+
}));
|
|
55
|
+
for (const [key, record] of sections.get("Assembly") ?? []) {
|
|
56
|
+
const size = resized.get(key);
|
|
57
|
+
if (size === undefined) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const [name = "", path = "", , descriptor = ""] = record.fields;
|
|
61
|
+
const rawBytes = [
|
|
62
|
+
quoted(name),
|
|
63
|
+
path && quoted(path),
|
|
64
|
+
size,
|
|
65
|
+
descriptor,
|
|
66
|
+
"",
|
|
67
|
+
"",
|
|
68
|
+
size * 8,
|
|
69
|
+
"",
|
|
70
|
+
];
|
|
71
|
+
edits.push({
|
|
72
|
+
start: record.start,
|
|
73
|
+
end: record.end,
|
|
74
|
+
text: `${key} = ${rawBytes.join(",")};`,
|
|
75
|
+
});
|
|
76
|
+
resized.delete(key);
|
|
77
|
+
}
|
|
78
|
+
if (resized.size) {
|
|
79
|
+
throw new Error(`EDS has no ${[...resized.keys()].join(", ")} assembly.`);
|
|
80
|
+
}
|
|
81
|
+
return edits
|
|
82
|
+
.sort((a, b) => b.start - a.start)
|
|
83
|
+
.reduce(
|
|
84
|
+
(result, edit) =>
|
|
85
|
+
result.slice(0, edit.start) + edit.text + result.slice(edit.end),
|
|
86
|
+
text,
|
|
87
|
+
);
|
|
100
88
|
}
|
|
101
89
|
|
|
102
90
|
/**
|
|
@@ -105,60 +93,84 @@ export function normalizeEds(
|
|
|
105
93
|
* on each pinned byte or bit naming the logical IO bound there.
|
|
106
94
|
*/
|
|
107
95
|
export async function deviceTree(
|
|
108
|
-
|
|
109
|
-
|
|
96
|
+
config: ResolvedTsplcConfig,
|
|
97
|
+
catalog: HardwareCatalog,
|
|
110
98
|
): Promise<DeviceTree | undefined> {
|
|
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
|
-
|
|
99
|
+
const settings = config.codesys?.deviceTree;
|
|
100
|
+
if (!settings) {
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
const devices = await Promise.all(
|
|
104
|
+
catalog.devices.map(async (device) => {
|
|
105
|
+
if (!iecName.safeParse(device.id).success) {
|
|
106
|
+
throw new Error(
|
|
107
|
+
`${device.id}: a device tree adapter needs an IEC identifier name.`,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
const connection = device.connection;
|
|
111
|
+
if (!connection) {
|
|
112
|
+
throw new Error(`${device.id}: select the connection to configure.`);
|
|
113
|
+
}
|
|
114
|
+
// The catalog selects every assembly the connection carries.
|
|
115
|
+
const region = (direction: "input" | "output") => {
|
|
116
|
+
const assembly = device.assemblies.find(
|
|
117
|
+
(item) => item.direction === direction,
|
|
118
|
+
);
|
|
119
|
+
return assembly && { address: assembly.address, size: assembly.size };
|
|
120
|
+
};
|
|
121
|
+
const inputs = region("input");
|
|
122
|
+
const outputs = region("output");
|
|
123
|
+
// One comment per binding, on its bit or first byte: every comment is a
|
|
124
|
+
// CODESYS write that revalidates the whole tree.
|
|
125
|
+
const comments = Object.entries(config.io).flatMap(([key, binding]) => {
|
|
126
|
+
if (!("device" in binding) || binding.device !== device.id) {
|
|
127
|
+
return [];
|
|
128
|
+
}
|
|
129
|
+
const channel = device.channels.find(
|
|
130
|
+
(item) => item.id === binding.channel,
|
|
131
|
+
)!;
|
|
132
|
+
if (channel.assembly === undefined) {
|
|
133
|
+
return [];
|
|
134
|
+
}
|
|
135
|
+
if (channel.bits === 1) {
|
|
136
|
+
return [{ address: channel.address!, text: key }];
|
|
137
|
+
}
|
|
138
|
+
const area = channel.direction === "input" ? inputs! : outputs!;
|
|
139
|
+
const bytes = channel.bits / 8;
|
|
140
|
+
return [
|
|
141
|
+
{
|
|
142
|
+
address: `${area.address.slice(0, 3)}${byteOffset(area.address) + channel.offsetBits / 8}`,
|
|
143
|
+
text: bytes > 1 ? `${key} (${bytes} bytes)` : key,
|
|
144
|
+
},
|
|
145
|
+
];
|
|
146
|
+
});
|
|
147
|
+
const network = config.devices[device.id]?.network;
|
|
148
|
+
if (device.modbus && !network?.address) {
|
|
149
|
+
throw new Error(
|
|
150
|
+
`${device.id}: Modbus registers need its network address.`,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
name: device.id,
|
|
155
|
+
eds: normalizeEds(await readFile(device.descriptor, "utf8"), {
|
|
156
|
+
connection: connection.id,
|
|
157
|
+
inputSize: inputs?.size,
|
|
158
|
+
outputSize: outputs?.size,
|
|
159
|
+
}),
|
|
160
|
+
...(network?.address && { address: network.address }),
|
|
161
|
+
...(network?.rpi && {
|
|
162
|
+
rpiMicroseconds: Number(BigInt(network.rpi.nanoseconds) / 1000n),
|
|
163
|
+
}),
|
|
164
|
+
...(inputs && { inputs }),
|
|
165
|
+
...(outputs && { outputs }),
|
|
166
|
+
...(device.modbus && { modbus: device.modbus }),
|
|
167
|
+
comments,
|
|
168
|
+
};
|
|
169
|
+
}),
|
|
170
|
+
);
|
|
171
|
+
const [first, ...rest] = devices;
|
|
172
|
+
if (!first) {
|
|
173
|
+
throw new Error("A generated device tree needs at least one device.");
|
|
174
|
+
}
|
|
175
|
+
return { ethernet: settings.ethernet ?? {}, devices: [first, ...rest] };
|
|
164
176
|
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import type { EdsMember } from "./eds.ts";
|
|
2
|
+
import { ioScalarBits } from "@tsplc/iec";
|
|
3
|
+
import { directAddress } from "./process-image.ts";
|
|
4
|
+
|
|
5
|
+
export interface IoChannel {
|
|
6
|
+
device: string;
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
direction: "input" | "output";
|
|
10
|
+
/** Absent on a Modbus channel, which belongs to no EtherNet/IP assembly. */
|
|
11
|
+
assembly?: number;
|
|
12
|
+
offsetBits: number;
|
|
13
|
+
bits: number;
|
|
14
|
+
type: string;
|
|
15
|
+
address?: string;
|
|
16
|
+
parent?: string;
|
|
17
|
+
unit: string;
|
|
18
|
+
description: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Where a selected assembly sits, and how much of it the connection transfers. */
|
|
22
|
+
export interface AssemblyPlacement {
|
|
23
|
+
device: string;
|
|
24
|
+
direction: "input" | "output";
|
|
25
|
+
instance: number;
|
|
26
|
+
members: EdsMember[];
|
|
27
|
+
/** The assembly's byte offset in its %I or %Q region. */
|
|
28
|
+
base: number;
|
|
29
|
+
payloadSize: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Every payload byte and bit, then each named member the payload carries. A member
|
|
34
|
+
* spanning several scalars is an array channel with a channel per element.
|
|
35
|
+
*/
|
|
36
|
+
export function assemblyChannels({
|
|
37
|
+
device,
|
|
38
|
+
direction,
|
|
39
|
+
instance,
|
|
40
|
+
members,
|
|
41
|
+
base,
|
|
42
|
+
payloadSize,
|
|
43
|
+
}: AssemblyPlacement): IoChannel[] {
|
|
44
|
+
const prefix = `${direction}s`;
|
|
45
|
+
const region = direction === "input" ? "I" : "Q";
|
|
46
|
+
const payloadBits = payloadSize * 8;
|
|
47
|
+
const address = (offsetBits: number, bits: number) =>
|
|
48
|
+
directAddress(region, base, offsetBits, bits);
|
|
49
|
+
const channels: IoChannel[] = [];
|
|
50
|
+
for (let i = 0; i < payloadSize; i++) {
|
|
51
|
+
const key = `${prefix}.Byte${i}`;
|
|
52
|
+
channels.push({
|
|
53
|
+
device,
|
|
54
|
+
id: key,
|
|
55
|
+
name: `Byte ${i}`,
|
|
56
|
+
direction,
|
|
57
|
+
assembly: instance,
|
|
58
|
+
offsetBits: i * 8,
|
|
59
|
+
bits: 8,
|
|
60
|
+
type: "BYTE",
|
|
61
|
+
address: address(i * 8, 8),
|
|
62
|
+
unit: "",
|
|
63
|
+
description: "Assembly payload byte",
|
|
64
|
+
});
|
|
65
|
+
for (let bit = 0; bit < 8; bit++) {
|
|
66
|
+
channels.push({
|
|
67
|
+
device,
|
|
68
|
+
id: `${key}.Bit${bit}`,
|
|
69
|
+
parent: key,
|
|
70
|
+
name: `Bit ${bit}`,
|
|
71
|
+
direction,
|
|
72
|
+
assembly: instance,
|
|
73
|
+
offsetBits: i * 8 + bit,
|
|
74
|
+
bits: 1,
|
|
75
|
+
type: "BOOL",
|
|
76
|
+
address: address(i * 8 + bit, 1),
|
|
77
|
+
unit: "",
|
|
78
|
+
description: "Assembly payload bit",
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// Repeated parameter names are indexed in assembly order.
|
|
83
|
+
const named = members
|
|
84
|
+
.filter((member) => member.parameter)
|
|
85
|
+
.map((member) => ({ member, name: member.name.replace(/[^\w]/g, "_") }));
|
|
86
|
+
const seen = new Set<string>();
|
|
87
|
+
const repeated = new Set<string>();
|
|
88
|
+
for (const { name } of named) {
|
|
89
|
+
if (seen.has(name)) {
|
|
90
|
+
repeated.add(name);
|
|
91
|
+
}
|
|
92
|
+
seen.add(name);
|
|
93
|
+
}
|
|
94
|
+
const counts = new Map<string, number>();
|
|
95
|
+
for (const { member, name } of named) {
|
|
96
|
+
if (member.offsetBits >= payloadBits) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const count = counts.get(name) ?? 0;
|
|
100
|
+
counts.set(name, count + 1);
|
|
101
|
+
const key = `${prefix}.${name}${repeated.has(name) ? `[${count}]` : ""}`;
|
|
102
|
+
const scalarBits = ioScalarBits(member.type);
|
|
103
|
+
const channel: IoChannel = {
|
|
104
|
+
device,
|
|
105
|
+
id: key,
|
|
106
|
+
name: member.name,
|
|
107
|
+
direction,
|
|
108
|
+
assembly: instance,
|
|
109
|
+
offsetBits: member.offsetBits,
|
|
110
|
+
bits: member.bits,
|
|
111
|
+
type: member.type,
|
|
112
|
+
address:
|
|
113
|
+
scalarBits === member.bits
|
|
114
|
+
? address(member.offsetBits, member.bits)
|
|
115
|
+
: undefined,
|
|
116
|
+
unit: member.unit,
|
|
117
|
+
description: member.description,
|
|
118
|
+
};
|
|
119
|
+
const spansScalars =
|
|
120
|
+
scalarBits !== undefined &&
|
|
121
|
+
scalarBits >= 8 &&
|
|
122
|
+
member.bits > scalarBits &&
|
|
123
|
+
member.bits % scalarBits === 0 &&
|
|
124
|
+
member.offsetBits % scalarBits === 0;
|
|
125
|
+
if (!spansScalars) {
|
|
126
|
+
if (member.offsetBits + member.bits <= payloadBits) {
|
|
127
|
+
channels.push(channel);
|
|
128
|
+
}
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const length = Math.floor(
|
|
132
|
+
Math.min(member.bits, payloadBits - member.offsetBits) / scalarBits,
|
|
133
|
+
);
|
|
134
|
+
const span: IoChannel = {
|
|
135
|
+
...channel,
|
|
136
|
+
bits: length * scalarBits,
|
|
137
|
+
type: `ARRAY[0..${length - 1}] OF ${member.type}`,
|
|
138
|
+
// The whole span binds to a logical FixedArray at its first element's address.
|
|
139
|
+
address: address(member.offsetBits, scalarBits),
|
|
140
|
+
};
|
|
141
|
+
channels.push(span);
|
|
142
|
+
for (let index = 0; index < length; index++) {
|
|
143
|
+
const offset = member.offsetBits + index * scalarBits;
|
|
144
|
+
channels.push({
|
|
145
|
+
...span,
|
|
146
|
+
id: `${key}[${index}]`,
|
|
147
|
+
parent: key,
|
|
148
|
+
name: `${member.name}[${index}]`,
|
|
149
|
+
type: member.type,
|
|
150
|
+
bits: scalarBits,
|
|
151
|
+
offsetBits: offset,
|
|
152
|
+
address: address(offset, scalarBits),
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return channels;
|
|
157
|
+
}
|