@tsplc/tmp-compiler 0.0.0-stage → 0.6.3
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/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/cli.js +2325 -0
- package/dist/cli.js.map +26 -0
- package/dist/clock.js +11 -0
- package/dist/clock.js.map +11 -0
- package/dist/codesys.js +6563 -0
- package/dist/codesys.js.map +51 -0
- package/dist/collections.js +399 -0
- package/dist/collections.js.map +10 -0
- package/dist/index.js +16198 -0
- package/dist/index.js.map +62 -0
- package/dist/ir.js +3948 -0
- package/dist/ir.js.map +35 -0
- package/dist/retain.js +10 -0
- package/dist/retain.js.map +10 -0
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +121 -0
- package/dist/telemetry.js.map +12 -0
- package/package.json +122 -4
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +655 -0
- package/src/codesys/emit.ts +185 -0
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +96 -0
- package/src/codesys/explain/expressions.ts +445 -0
- package/src/codesys/explain/flags.ts +134 -0
- package/src/codesys/explain/parameters.ts +53 -0
- package/src/codesys/explain/rows.ts +182 -0
- package/src/codesys/explain/tables.ts +79 -0
- package/src/codesys/explain/tasks.ts +68 -0
- package/src/codesys/explain/writes.ts +497 -0
- package/src/codesys/explain.ts +149 -0
- package/src/codesys/expressions.ts +100 -0
- package/src/codesys/global-lists.ts +284 -0
- package/src/codesys/global-names.ts +66 -0
- package/src/codesys/http.ts +1090 -0
- package/src/codesys/index.ts +13 -0
- package/src/codesys/instrumentation.ts +574 -0
- package/src/codesys/io-paths.ts +149 -0
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +684 -0
- package/src/codesys/names.ts +58 -0
- package/src/codesys/numeric.ts +516 -0
- package/src/codesys/plan.ts +688 -0
- package/src/codesys/programs.ts +138 -0
- package/src/codesys/publication.ts +99 -0
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +247 -0
- package/src/codesys/scan-clock.ts +103 -0
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +24 -0
- package/src/codesys/sources.ts +118 -0
- package/src/codesys/st-files.ts +219 -0
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +150 -0
- package/src/codesys/symbols.ts +240 -0
- package/src/codesys/task-access.ts +186 -0
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +852 -0
- package/src/codesys/types.ts +310 -0
- package/src/codesys/vendor.ts +113 -0
- package/src/frontend/analyzer.ts +779 -0
- package/src/frontend/async.ts +530 -0
- package/src/frontend/blocks.ts +578 -0
- package/src/frontend/configuration.ts +83 -0
- package/src/frontend/const-enums.ts +196 -0
- package/src/frontend/const-functions.ts +204 -0
- package/src/frontend/control-flow.ts +787 -0
- package/src/frontend/cooperative.ts +114 -0
- package/src/frontend/coroutines.ts +1344 -0
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +224 -0
- package/src/frontend/elaborate/assignments.ts +343 -0
- package/src/frontend/elaborate/calls.ts +1342 -0
- package/src/frontend/elaborate/capture-protocol.ts +237 -0
- package/src/frontend/elaborate/capture.ts +388 -0
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +473 -0
- package/src/frontend/elaborate/expressions.ts +2177 -0
- package/src/frontend/elaborate/filtered-arrays.ts +643 -0
- package/src/frontend/elaborate/frame.ts +145 -0
- package/src/frontend/elaborate/generics.ts +123 -0
- package/src/frontend/elaborate/graph.ts +141 -0
- package/src/frontend/elaborate/imports.ts +105 -0
- package/src/frontend/elaborate/index.ts +851 -0
- package/src/frontend/elaborate/initializers.ts +235 -0
- package/src/frontend/elaborate/iterators.ts +1541 -0
- package/src/frontend/elaborate/layouts.ts +1025 -0
- package/src/frontend/elaborate/model.ts +478 -0
- package/src/frontend/elaborate/names.ts +134 -0
- package/src/frontend/elaborate/reduce.ts +900 -0
- package/src/frontend/elaborate/references.ts +727 -0
- package/src/frontend/elaborate/retention.ts +284 -0
- package/src/frontend/elaborate/specialize.ts +1137 -0
- package/src/frontend/elaborate/statements.ts +1220 -0
- package/src/frontend/elaborate/storage.ts +836 -0
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +200 -0
- package/src/frontend/elaborate/writes.ts +415 -0
- package/src/frontend/expressions.ts +1382 -0
- package/src/frontend/generated-names.ts +95 -0
- package/src/frontend/intrinsics.ts +382 -0
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +156 -0
- package/src/frontend/positions.ts +359 -0
- package/src/frontend/promise-chains.ts +872 -0
- package/src/frontend/promises.ts +419 -0
- package/src/frontend/routines.ts +239 -0
- package/src/frontend/sdk.ts +187 -0
- package/src/frontend/services.ts +612 -0
- package/src/frontend/statements.ts +871 -0
- package/src/frontend/tasks.ts +526 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +1073 -0
- package/src/frontend/typescript.ts +659 -0
- package/src/index.ts +9 -0
- package/src/ir/aggregates.ts +291 -0
- package/src/ir/allocation.ts +96 -0
- package/src/ir/conditions.ts +855 -0
- package/src/ir/core-model.ts +289 -0
- package/src/ir/declarations.ts +281 -0
- package/src/ir/diagnostics.ts +68 -0
- package/src/ir/domains.ts +151 -0
- package/src/ir/fold.ts +279 -0
- package/src/ir/identity.ts +19 -0
- package/src/ir/index.ts +137 -0
- package/src/ir/job-errors.ts +69 -0
- package/src/ir/layouts.ts +211 -0
- package/src/ir/link.ts +302 -0
- package/src/ir/model.ts +394 -0
- package/src/ir/normalization-error.ts +10 -0
- package/src/ir/normalize.ts +52 -0
- package/src/ir/numeric.ts +163 -0
- package/src/ir/operands.ts +160 -0
- package/src/ir/ranges.ts +420 -0
- package/src/ir/routine.ts +1116 -0
- package/src/ir/scalars.ts +453 -0
- package/src/ir/share.ts +790 -0
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +138 -0
- package/src/ir/types.ts +151 -0
- package/src/project/artifacts.ts +424 -0
- package/src/project/cli.ts +211 -0
- package/src/project/debugger.ts +111 -0
- package/src/project/deployments.ts +387 -0
- package/src/project/flags.ts +14 -0
- package/src/project/git.ts +35 -0
- package/src/project/plant.ts +629 -0
- package/src/project/project-commands.ts +507 -0
- package/src/project/project.ts +559 -0
- package/src/project/release.ts +152 -0
- package/src/project/server-check.ts +54 -0
- package/src/project/server.ts +165 -0
- package/src/project/terminal.ts +45 -0
- package/src/runtime/async.ts +207 -0
- package/src/runtime/launch.ts +59 -0
- package/src/runtime/main.ts +59 -0
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +177 -0
- package/src/runtime/program.ts +109 -0
- package/src/runtime/retain.ts +108 -0
- package/src/runtime/runtime.ts +324 -0
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +456 -0
- package/src/runtime/symbols.ts +97 -0
- package/src/runtime/telemetry.ts +154 -0
- package/src/runtime/transport.ts +168 -0
- package/stdlib/clock.ts +8 -0
- package/stdlib/collections.ts +534 -0
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +4 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +89 -0
- package/types/async.d.ts +87 -0
- package/types/clock.d.ts +8 -0
- package/types/config.d.ts +2 -0
- package/types/data-types.d.ts +31 -0
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +10 -0
- package/types/index.d.ts +212 -0
- package/types/retain.d.ts +12 -0
- package/types/telemetry.d.ts +47 -0
|
@@ -0,0 +1,852 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type CoreStatement,
|
|
3
|
+
coreTables,
|
|
4
|
+
type Deployment,
|
|
5
|
+
errorName,
|
|
6
|
+
type GeneratedObject,
|
|
7
|
+
httpErrors,
|
|
8
|
+
instancedBlock,
|
|
9
|
+
jobErrors,
|
|
10
|
+
nestedBodies,
|
|
11
|
+
type PlcType,
|
|
12
|
+
type SourceSpan,
|
|
13
|
+
type VerifiedCoreModule,
|
|
14
|
+
} from "@tsplc/compiler/ir";
|
|
15
|
+
import {
|
|
16
|
+
type Environment,
|
|
17
|
+
type ResolvedTelemetryConfig,
|
|
18
|
+
serverBaseUrl,
|
|
19
|
+
} from "@tsplc/config";
|
|
20
|
+
import { elementaryType } from "@tsplc/iec";
|
|
21
|
+
import {
|
|
22
|
+
buildIdLength,
|
|
23
|
+
taskNameLength,
|
|
24
|
+
telemetryPath,
|
|
25
|
+
} from "@tsplc/server/telemetry";
|
|
26
|
+
import { plcBufferBytes } from "@tsplc/server/wire";
|
|
27
|
+
import type { CodesysOptions } from "./config";
|
|
28
|
+
import { blockInstances, type InstancePath } from "./debug";
|
|
29
|
+
import {
|
|
30
|
+
type HttpEndpoint,
|
|
31
|
+
type NativeEndpoint,
|
|
32
|
+
resolveHttpEndpoint,
|
|
33
|
+
routeBase,
|
|
34
|
+
} from "./endpoints";
|
|
35
|
+
import {
|
|
36
|
+
decimalLength,
|
|
37
|
+
httpCertificateVerifier,
|
|
38
|
+
httpClientMethods,
|
|
39
|
+
jsonEncoders,
|
|
40
|
+
jsonScalar,
|
|
41
|
+
nativeHttp,
|
|
42
|
+
opcuaClient,
|
|
43
|
+
opcuaClientMethods,
|
|
44
|
+
} from "./http";
|
|
45
|
+
import type { Report } from "./plan";
|
|
46
|
+
import { required } from "./required";
|
|
47
|
+
import { Sources } from "./sources";
|
|
48
|
+
import { routineTasks } from "./task-access";
|
|
49
|
+
import { ringSettings, telemetrySendIntervalMs } from "./telemetry-settings";
|
|
50
|
+
import { type CodesysTypes, stStringLiteral } from "./types";
|
|
51
|
+
|
|
52
|
+
/** The id of the sender's client block. */
|
|
53
|
+
export const telemetryClient = "tsplc:telemetry";
|
|
54
|
+
const telemetryWriter = "tsplc:telemetryWriter";
|
|
55
|
+
/** Stands for the build id in the sender until the id, a hash over everything else, is known. */
|
|
56
|
+
const buildPlaceholder = "<build>";
|
|
57
|
+
|
|
58
|
+
type TelemetryStatement = Extract<CoreStatement, { kind: "telemetry" }>;
|
|
59
|
+
|
|
60
|
+
/** A telemetry call: `record` is the JSON its method writes before the properties' values. */
|
|
61
|
+
interface CallSite {
|
|
62
|
+
id: number;
|
|
63
|
+
task: string;
|
|
64
|
+
record: string;
|
|
65
|
+
call: TelemetryStatement;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* A root job's unhandled failure; its source is that of the step it failed at. Failures that
|
|
70
|
+
* differ only in their instance share a method, which takes `instance` as its JSON member.
|
|
71
|
+
*/
|
|
72
|
+
interface FailureSite {
|
|
73
|
+
id: number;
|
|
74
|
+
task: string;
|
|
75
|
+
record: string;
|
|
76
|
+
/** The `"instance":…,` member this job passes, or null when its function has no instance. */
|
|
77
|
+
instance: string | null;
|
|
78
|
+
sources: readonly { source: string; steps: readonly number[] }[];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface TelemetryPlan extends ResolvedTelemetryConfig {
|
|
82
|
+
/** Where the senders post their batches. */
|
|
83
|
+
endpoint: HttpEndpoint;
|
|
84
|
+
calls: ReadonlyMap<CoreStatement, CallSite>;
|
|
85
|
+
/** By job block id. */
|
|
86
|
+
failures: ReadonlyMap<string, FailureSite>;
|
|
87
|
+
/** `debug.trace`'s operand count, when the trace is on. */
|
|
88
|
+
traceOperands?: number;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A string as JSON text in ASCII, so it can sit in an ST literal. */
|
|
92
|
+
const asciiJson = (value: string) =>
|
|
93
|
+
JSON.stringify(value).replace(
|
|
94
|
+
/[^ -~]/g,
|
|
95
|
+
(character) =>
|
|
96
|
+
`\\u${character.charCodeAt(0).toString(16).padStart(4, "0")}`,
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
/** Compiler-made jobs, such as a service call's own, carry this prefix. */
|
|
100
|
+
export const generated = (name: string) => name.startsWith("tsplc");
|
|
101
|
+
|
|
102
|
+
const maximum = (values: readonly number[]) =>
|
|
103
|
+
values.reduce((a, b) => Math.max(a, b), 0);
|
|
104
|
+
|
|
105
|
+
const digits = {
|
|
106
|
+
uint: decimalLength(elementaryType("UINT")),
|
|
107
|
+
dint: decimalLength(elementaryType("DINT")),
|
|
108
|
+
udint: decimalLength(elementaryType("UDINT")),
|
|
109
|
+
ulint: decimalLength(elementaryType("ULINT")),
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/** The members `Commit` ends every record with, and the writer's UDINT each one prints. */
|
|
113
|
+
const commitMembers = [
|
|
114
|
+
{ member: ',"scan":', value: "scan" },
|
|
115
|
+
{ member: ',"time":', value: "now" },
|
|
116
|
+
] as const;
|
|
117
|
+
const commitSize =
|
|
118
|
+
commitMembers.reduce(
|
|
119
|
+
(total, { member }) => total + member.length + digits.udint,
|
|
120
|
+
0,
|
|
121
|
+
) + "}".length;
|
|
122
|
+
|
|
123
|
+
const codeMember = ',"code":';
|
|
124
|
+
const nameMember = ',"name":';
|
|
125
|
+
const errorNames = Object.keys(jobErrors).flatMap((code) => {
|
|
126
|
+
const name = errorName(Number(code));
|
|
127
|
+
return name === undefined ? [] : [[code, name] as const];
|
|
128
|
+
});
|
|
129
|
+
/** What an exception record's error code and its name take after its properties. */
|
|
130
|
+
const codeSize =
|
|
131
|
+
codeMember.length +
|
|
132
|
+
digits.udint +
|
|
133
|
+
nameMember.length +
|
|
134
|
+
maximum([
|
|
135
|
+
...errorNames.map(([, name]) => asciiJson(name).length),
|
|
136
|
+
asciiJson(`HTTP ${httpErrors.last - httpErrors.base}`).length,
|
|
137
|
+
]);
|
|
138
|
+
|
|
139
|
+
const nullJson = "null";
|
|
140
|
+
|
|
141
|
+
/** A property's value in a record: the most bytes it takes, and the ST that appends parameter `value`. */
|
|
142
|
+
function propertyJson(
|
|
143
|
+
type: PlcType,
|
|
144
|
+
text: boolean,
|
|
145
|
+
): { size: number; encode: (value: string, target: CodesysTypes) => string } {
|
|
146
|
+
if (text && type.kind === "array") {
|
|
147
|
+
return {
|
|
148
|
+
size: type.length * 6 + 2,
|
|
149
|
+
encode: (value) =>
|
|
150
|
+
`WriteText(ADR(${value}), LINT_TO_DINT(LIMIT(0, TO_LINT(${value}Length), ${type.length})));`,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
if (type.kind === "enum") {
|
|
154
|
+
const texts = type.members.map((member) =>
|
|
155
|
+
asciiJson(member.text ?? member.name),
|
|
156
|
+
);
|
|
157
|
+
return {
|
|
158
|
+
size: maximum([nullJson.length, ...texts.map((item) => item.length)]),
|
|
159
|
+
encode: (value, target) =>
|
|
160
|
+
`CASE ${value} OF ${type.members.map((member, index) => `${target.name(type.id)}.${target.field(type.id, member.name)}: Append(${stStringLiteral(required(texts[index]))});`).join(" ")} ELSE Append('${nullJson}'); END_CASE;`,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
if (type.kind === "float") {
|
|
164
|
+
return {
|
|
165
|
+
size: type.bits === 32 ? 16 : 25,
|
|
166
|
+
encode: (value) =>
|
|
167
|
+
`IF ${value} = ${value} AND ABS(${value}) < ${type.id}#${type.bits === 32 ? "3.4E38" : "1.7E308"} THEN Append(${type.id}_TO_STRING(${value})); ELSE Append('${nullJson}'); END_IF;`,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
const scalar = jsonScalar(type);
|
|
171
|
+
if (!scalar) {
|
|
172
|
+
throw new Error(`Unverified telemetry value type ${type.id}.`);
|
|
173
|
+
}
|
|
174
|
+
return scalar;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** A call's record up to its source: its kind and what it names. */
|
|
178
|
+
const recordHead = (record: TelemetryStatement["record"]): string => {
|
|
179
|
+
switch (record.kind) {
|
|
180
|
+
case "event":
|
|
181
|
+
return `{"kind":"event","event":${asciiJson(record.event)}`;
|
|
182
|
+
case "log":
|
|
183
|
+
return `{"kind":"log","level":"${record.level}","message":${asciiJson(record.message)}`;
|
|
184
|
+
case "exception":
|
|
185
|
+
return '{"kind":"exception","handled":true';
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/** A `debug.trace` record with `operands` operands: the ST of the writer's `Trace`, and the most bytes it takes. */
|
|
190
|
+
const traceRecord = (operands: number) => {
|
|
191
|
+
const site = '{"kind":"trace","site":';
|
|
192
|
+
const value = ',"value":';
|
|
193
|
+
const list = ',"operands":[';
|
|
194
|
+
return {
|
|
195
|
+
implementation: `${[
|
|
196
|
+
"payload := '';",
|
|
197
|
+
`Append(${stStringLiteral(site)});`,
|
|
198
|
+
"Append(UINT_TO_STRING(site));",
|
|
199
|
+
`Append(${stStringLiteral(value)});`,
|
|
200
|
+
"Append(DINT_TO_STRING(value));",
|
|
201
|
+
`Append(${stStringLiteral(list)});`,
|
|
202
|
+
...Array.from(
|
|
203
|
+
{ length: operands },
|
|
204
|
+
(_, index) =>
|
|
205
|
+
`IF operands > ${index} THEN ${index ? "Append(','); " : ""}Append(DINT_TO_STRING(operand${index})); END_IF;`,
|
|
206
|
+
),
|
|
207
|
+
"Append(']');",
|
|
208
|
+
"Commit();",
|
|
209
|
+
].join("\n")}\n`,
|
|
210
|
+
size:
|
|
211
|
+
site.length +
|
|
212
|
+
digits.uint +
|
|
213
|
+
value.length +
|
|
214
|
+
digits.dint +
|
|
215
|
+
list.length +
|
|
216
|
+
operands * (digits.dint + ",".length) +
|
|
217
|
+
"]".length +
|
|
218
|
+
commitSize,
|
|
219
|
+
};
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
function telemetryCalls(core: VerifiedCoreModule) {
|
|
223
|
+
const routines = [
|
|
224
|
+
...core.functions,
|
|
225
|
+
...core.blocks.filter((block) => !block.standard),
|
|
226
|
+
];
|
|
227
|
+
const statements: { routine: string; call: TelemetryStatement }[] = [];
|
|
228
|
+
const find = (routine: string, items: readonly CoreStatement[]): void => {
|
|
229
|
+
for (const item of items) {
|
|
230
|
+
if (item.kind === "telemetry") {
|
|
231
|
+
statements.push({ routine, call: item });
|
|
232
|
+
}
|
|
233
|
+
for (const body of nestedBodies(item)) {
|
|
234
|
+
find(routine, body);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
for (const routine of routines) {
|
|
239
|
+
find(routine.id, routine.body);
|
|
240
|
+
}
|
|
241
|
+
return statements;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
interface JobFailure {
|
|
245
|
+
task: string;
|
|
246
|
+
function: string;
|
|
247
|
+
instance?: string;
|
|
248
|
+
span: SourceSpan;
|
|
249
|
+
sources: { source: string; steps: readonly number[] }[];
|
|
250
|
+
fallback: string;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** Root jobs own the unhandled record. Awaiting jobs own their children's errors. */
|
|
254
|
+
function planJobFailures(
|
|
255
|
+
core: VerifiedCoreModule,
|
|
256
|
+
tasks: ReadonlyMap<string, string | undefined>,
|
|
257
|
+
location: (span: SourceSpan) => string,
|
|
258
|
+
): ReadonlyMap<string, JobFailure> {
|
|
259
|
+
const { types, blockOf } = coreTables(core);
|
|
260
|
+
const failures = new Map<string, JobFailure>();
|
|
261
|
+
const instances = blockInstances(core);
|
|
262
|
+
const awaited = (path: InstancePath) =>
|
|
263
|
+
path.slice(0, -1).some(({ variable }) => {
|
|
264
|
+
const type = instancedBlock(variable.type, types);
|
|
265
|
+
return type !== undefined && blockOf(type.block).job !== undefined;
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
for (const block of core.blocks) {
|
|
269
|
+
const { job } = block;
|
|
270
|
+
const task = tasks.get(block.id);
|
|
271
|
+
const paths = required(instances.get(block.id));
|
|
272
|
+
if (!job || !task || !paths.length || paths.some(awaited)) {
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// A generated detached wrapper reports its one authored child by name.
|
|
277
|
+
const children = block.state.flatMap((child) => {
|
|
278
|
+
const type = types.get(child.type);
|
|
279
|
+
const called =
|
|
280
|
+
type?.kind === "block" ? blockOf(type.block).job : undefined;
|
|
281
|
+
return called && !generated(called.name) ? [called] : [];
|
|
282
|
+
});
|
|
283
|
+
const [onlyChild] = children;
|
|
284
|
+
const named =
|
|
285
|
+
generated(job.name) && children.length === 1 && onlyChild
|
|
286
|
+
? onlyChild
|
|
287
|
+
: job;
|
|
288
|
+
const fallback = location(job.span);
|
|
289
|
+
const steps = new Map<string, number[]>();
|
|
290
|
+
for (const step of job.steps) {
|
|
291
|
+
const source = location(step.span) || fallback;
|
|
292
|
+
steps.set(source, [...(steps.get(source) ?? []), step.step]);
|
|
293
|
+
}
|
|
294
|
+
failures.set(block.id, {
|
|
295
|
+
task,
|
|
296
|
+
function: named.name,
|
|
297
|
+
...(named.instance && { instance: named.instance }),
|
|
298
|
+
span: job.span,
|
|
299
|
+
sources: [...steps].map(([source, numbers]) => ({
|
|
300
|
+
source,
|
|
301
|
+
steps: numbers,
|
|
302
|
+
})),
|
|
303
|
+
fallback,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
return failures;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Whether a build has telemetry: the program calls `@tsplc/compiler/telemetry`, `telemetry` is
|
|
312
|
+
* configured, or `debug.trace` is on. Then every task with something to record gets a ring and
|
|
313
|
+
* a sender, root jobs' unhandled failures included; otherwise nothing is added.
|
|
314
|
+
*/
|
|
315
|
+
export function planTelemetry(
|
|
316
|
+
core: VerifiedCoreModule,
|
|
317
|
+
deployment: Deployment,
|
|
318
|
+
options: CodesysOptions,
|
|
319
|
+
project: {
|
|
320
|
+
root: string;
|
|
321
|
+
telemetry?: ResolvedTelemetryConfig;
|
|
322
|
+
trace?: { capacity: number; operands: number } | false;
|
|
323
|
+
server?: { transport: "http" | "opcua"; baseUrlEnv?: string };
|
|
324
|
+
environment: Environment;
|
|
325
|
+
},
|
|
326
|
+
report: Report,
|
|
327
|
+
): TelemetryPlan | undefined {
|
|
328
|
+
const { typeOf } = coreTables(core);
|
|
329
|
+
const statements = telemetryCalls(core);
|
|
330
|
+
if (!statements.length && !project.telemetry && !project.trace) {
|
|
331
|
+
return undefined;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const settings = ringSettings(project.telemetry, project.trace);
|
|
335
|
+
const sources = new Sources(project.root);
|
|
336
|
+
const location = (span: SourceSpan) => {
|
|
337
|
+
const range = sources.range(span);
|
|
338
|
+
return range ? `${sources.files[range.file]}:${range.line}` : "";
|
|
339
|
+
};
|
|
340
|
+
const tasks = routineTasks(core, deployment);
|
|
341
|
+
const tooLarge = (size: number, what: string, span?: SourceSpan) => {
|
|
342
|
+
if (size > settings.recordBytes) {
|
|
343
|
+
report(
|
|
344
|
+
`${what} can take ${size} bytes of JSON, more than telemetry.recordBytes (${settings.recordBytes}). Use fewer or shorter properties, smaller STRING or byte array bounds, or raise recordBytes.`,
|
|
345
|
+
span,
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
const calls = new Map<CoreStatement, CallSite>();
|
|
351
|
+
const recordIds = new Map<string, number>();
|
|
352
|
+
for (const { routine, call } of statements) {
|
|
353
|
+
const task = tasks.get(routine);
|
|
354
|
+
if (!task) {
|
|
355
|
+
report(
|
|
356
|
+
"Telemetry calls must be in code that one task runs: each task writes its own records.",
|
|
357
|
+
call.span,
|
|
358
|
+
);
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
const source = asciiJson(location(call.span));
|
|
362
|
+
const { record } = call;
|
|
363
|
+
const prefix = `${recordHead(record)},"source":${source},"properties":{`;
|
|
364
|
+
const size =
|
|
365
|
+
prefix.length +
|
|
366
|
+
call.properties.reduce(
|
|
367
|
+
(total, { name, value, length }, index) =>
|
|
368
|
+
total +
|
|
369
|
+
(index ? 1 : 0) +
|
|
370
|
+
asciiJson(name).length +
|
|
371
|
+
1 +
|
|
372
|
+
propertyJson(typeOf(value.type), length !== undefined).size,
|
|
373
|
+
0,
|
|
374
|
+
) +
|
|
375
|
+
1 +
|
|
376
|
+
(record.kind === "exception" ? codeSize : 0) +
|
|
377
|
+
commitSize;
|
|
378
|
+
tooLarge(size, "This telemetry record", call.span);
|
|
379
|
+
// Calls that write the same record with the same parameters, such as one call site in
|
|
380
|
+
// several instances, share a method.
|
|
381
|
+
const key = JSON.stringify([
|
|
382
|
+
prefix,
|
|
383
|
+
record.kind,
|
|
384
|
+
call.properties.map(({ name }) => name),
|
|
385
|
+
callParameters(call).map(({ name, value, text }) => [
|
|
386
|
+
name,
|
|
387
|
+
value.type,
|
|
388
|
+
text,
|
|
389
|
+
]),
|
|
390
|
+
]);
|
|
391
|
+
const id = recordIds.get(key) ?? recordIds.size + 1;
|
|
392
|
+
recordIds.set(key, id);
|
|
393
|
+
calls.set(call, { id, task, record: prefix, call });
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const failures = new Map<string, FailureSite>();
|
|
397
|
+
const failureIds = new Map<string, number>();
|
|
398
|
+
for (const [block, job] of planJobFailures(core, tasks, location)) {
|
|
399
|
+
const record = `{"kind":"exception","handled":false,"function":${asciiJson(job.function)},`;
|
|
400
|
+
const instance = job.instance
|
|
401
|
+
? `"instance":${asciiJson(job.instance)},`
|
|
402
|
+
: null;
|
|
403
|
+
tooLarge(
|
|
404
|
+
record.length +
|
|
405
|
+
(instance?.length ?? 0) +
|
|
406
|
+
'"properties":{}'.length +
|
|
407
|
+
codeSize +
|
|
408
|
+
',"source":""'.length +
|
|
409
|
+
maximum(
|
|
410
|
+
[...job.sources.map(({ source }) => source), job.fallback].map(
|
|
411
|
+
(source) => asciiJson(source).length - 2,
|
|
412
|
+
),
|
|
413
|
+
) +
|
|
414
|
+
commitSize,
|
|
415
|
+
`The failure record of ${job.function}`,
|
|
416
|
+
job.span,
|
|
417
|
+
);
|
|
418
|
+
const key = JSON.stringify([record, instance !== null, job.sources]);
|
|
419
|
+
const id = failureIds.get(key) ?? failureIds.size + 1;
|
|
420
|
+
failureIds.set(key, id);
|
|
421
|
+
failures.set(block, {
|
|
422
|
+
id,
|
|
423
|
+
task: job.task,
|
|
424
|
+
record,
|
|
425
|
+
instance,
|
|
426
|
+
sources: job.sources,
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
if (project.trace) {
|
|
431
|
+
tooLarge(
|
|
432
|
+
traceRecord(project.trace.operands).size,
|
|
433
|
+
"A debug.trace record with its operands",
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
const base = serverBaseUrl(project, project.environment);
|
|
438
|
+
const plan = {
|
|
439
|
+
...settings,
|
|
440
|
+
calls,
|
|
441
|
+
failures,
|
|
442
|
+
...(project.trace && { traceOperands: project.trace.operands }),
|
|
443
|
+
};
|
|
444
|
+
if (project.server?.transport === "opcua") {
|
|
445
|
+
return {
|
|
446
|
+
...plan,
|
|
447
|
+
endpoint: {
|
|
448
|
+
transport: "opcua",
|
|
449
|
+
path: telemetryPath,
|
|
450
|
+
timeoutMs: options.http.timeoutMs,
|
|
451
|
+
},
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
if (!base) {
|
|
455
|
+
report(
|
|
456
|
+
`Telemetry posts to the app server: set server.baseUrlEnv and that environment variable to its base URL.`,
|
|
457
|
+
);
|
|
458
|
+
return undefined;
|
|
459
|
+
}
|
|
460
|
+
const endpoint = resolveHttpEndpoint(
|
|
461
|
+
`${routeBase(base)}${telemetryPath}`,
|
|
462
|
+
options.http,
|
|
463
|
+
project.environment,
|
|
464
|
+
);
|
|
465
|
+
if ("error" in endpoint) {
|
|
466
|
+
report(endpoint.error);
|
|
467
|
+
return undefined;
|
|
468
|
+
}
|
|
469
|
+
return { ...plan, endpoint };
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/** A call's method parameters, in declaration order: `p<n>` for each property, `code` for an exception. */
|
|
473
|
+
export const callParameters = ({ record, properties }: TelemetryStatement) => [
|
|
474
|
+
...properties.flatMap(({ value, length }, index) => [
|
|
475
|
+
{ name: `p${index}`, value, text: length !== undefined },
|
|
476
|
+
...(length
|
|
477
|
+
? [{ name: `p${index}Length`, value: length, text: false }]
|
|
478
|
+
: []),
|
|
479
|
+
]),
|
|
480
|
+
...(record.kind === "exception"
|
|
481
|
+
? [{ name: "code", value: record.code, text: false }]
|
|
482
|
+
: []),
|
|
483
|
+
];
|
|
484
|
+
|
|
485
|
+
/** The first site of each method id, in id order. */
|
|
486
|
+
function firstOfEach<Site extends { id: number }>(sites: Iterable<Site>) {
|
|
487
|
+
const first = new Map<number, Site>();
|
|
488
|
+
for (const site of sites) {
|
|
489
|
+
if (!first.has(site.id)) {
|
|
490
|
+
first.set(site.id, site);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
return [...first.values()].sort((a, b) => a.id - b.id);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/** The batch header's literal parts, before and after the task's name. */
|
|
497
|
+
const header = (build: string) => ({
|
|
498
|
+
start: `{"build":${asciiJson(build)},"task":"`,
|
|
499
|
+
rest: [
|
|
500
|
+
'","now":',
|
|
501
|
+
',"scan":',
|
|
502
|
+
',"first":',
|
|
503
|
+
',"dropped":',
|
|
504
|
+
',"records":[',
|
|
505
|
+
] as const,
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
/** Records that always fit a batch beside its header: a sender with this many unsent sends at once. */
|
|
509
|
+
const batchRecords = (recordBytes: number) => {
|
|
510
|
+
const { start, rest } = header("0".repeat(buildIdLength));
|
|
511
|
+
const fixed =
|
|
512
|
+
start.length +
|
|
513
|
+
taskNameLength +
|
|
514
|
+
rest.join("").length +
|
|
515
|
+
2 * digits.udint +
|
|
516
|
+
2 * digits.ulint +
|
|
517
|
+
"]}".length;
|
|
518
|
+
return Math.max(1, Math.floor((plcBufferBytes - fixed) / (recordBytes + 1)));
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
/** How the sender reaches the server: its HTTP client's endpoint and network, or the OPC UA slots it posts through. */
|
|
522
|
+
export type TelemetryTransport =
|
|
523
|
+
| { kind: "http"; endpoint: NativeEndpoint; network: string }
|
|
524
|
+
| { kind: "opcua"; timeoutMs: number; rpcGlobal: string };
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* The ring and sender of a task, `FB_TsplcTelemetry`: one method per call site and root job, a
|
|
528
|
+
* shared `Trace`, and a body each scan runs first that counts the scan, reads the clock and
|
|
529
|
+
* sends. It encodes with the HTTP blocks' JSON methods, and sends through a native HTTP block
|
|
530
|
+
* or over OPC UA. Its batches name the build once `fillBuildId` has run.
|
|
531
|
+
*/
|
|
532
|
+
export function telemetryObjects({
|
|
533
|
+
plan,
|
|
534
|
+
names,
|
|
535
|
+
target,
|
|
536
|
+
transport,
|
|
537
|
+
types,
|
|
538
|
+
}: {
|
|
539
|
+
plan: TelemetryPlan;
|
|
540
|
+
names: { writer: string; record: string; client: string };
|
|
541
|
+
target: CodesysTypes;
|
|
542
|
+
transport: TelemetryTransport;
|
|
543
|
+
types: ReadonlyMap<string, PlcType>;
|
|
544
|
+
}): GeneratedObject[] {
|
|
545
|
+
const { capacity, recordBytes } = plan;
|
|
546
|
+
const method = (
|
|
547
|
+
name: string,
|
|
548
|
+
declaration: string,
|
|
549
|
+
implementation: string,
|
|
550
|
+
access = "",
|
|
551
|
+
): GeneratedObject => ({
|
|
552
|
+
id: `${telemetryWriter}:${name}`,
|
|
553
|
+
parent: telemetryWriter,
|
|
554
|
+
kind: "method",
|
|
555
|
+
name,
|
|
556
|
+
declaration: `METHOD ${access}${name}\n${declaration}`,
|
|
557
|
+
implementation,
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
const calls = firstOfEach(plan.calls.values()).map(({ id, record, call }) => {
|
|
561
|
+
const parameters = callParameters(call);
|
|
562
|
+
const inputs = parameters.filter((parameter) => !parameter.text);
|
|
563
|
+
const references = parameters.filter((parameter) => parameter.text);
|
|
564
|
+
const properties = call.properties.map(({ name, value, length }, index) => {
|
|
565
|
+
const code = propertyJson(
|
|
566
|
+
required(types.get(value.type)),
|
|
567
|
+
length !== undefined,
|
|
568
|
+
).encode(`p${index}`, target);
|
|
569
|
+
return `Append(${stStringLiteral(`${index ? "," : ""}${asciiJson(name)}:`)});\n${code}`;
|
|
570
|
+
});
|
|
571
|
+
return method(
|
|
572
|
+
`Record${id}`,
|
|
573
|
+
[
|
|
574
|
+
...(inputs.length
|
|
575
|
+
? [
|
|
576
|
+
"VAR_INPUT",
|
|
577
|
+
...inputs.map(
|
|
578
|
+
(input) =>
|
|
579
|
+
` ${input.name} : ${target.name(input.value.type)};`,
|
|
580
|
+
),
|
|
581
|
+
"END_VAR",
|
|
582
|
+
]
|
|
583
|
+
: []),
|
|
584
|
+
...(references.length
|
|
585
|
+
? [
|
|
586
|
+
"VAR_IN_OUT",
|
|
587
|
+
...references.map(
|
|
588
|
+
(input) =>
|
|
589
|
+
` ${input.name} : ${target.name(input.value.type)};`,
|
|
590
|
+
),
|
|
591
|
+
"END_VAR",
|
|
592
|
+
]
|
|
593
|
+
: []),
|
|
594
|
+
]
|
|
595
|
+
.map((line) => `${line}\n`)
|
|
596
|
+
.join(""),
|
|
597
|
+
`${[
|
|
598
|
+
"payload := '';",
|
|
599
|
+
`Append(${stStringLiteral(record)});`,
|
|
600
|
+
...properties,
|
|
601
|
+
"Append('}');",
|
|
602
|
+
...(call.record.kind === "exception"
|
|
603
|
+
? [
|
|
604
|
+
`Append(${stStringLiteral(codeMember)});`,
|
|
605
|
+
"Append(UDINT_TO_STRING(code));",
|
|
606
|
+
"ErrorName(code);",
|
|
607
|
+
]
|
|
608
|
+
: []),
|
|
609
|
+
"Commit();",
|
|
610
|
+
].join("\n")}\n`,
|
|
611
|
+
);
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
const failures = firstOfEach(plan.failures.values()).map(
|
|
615
|
+
({ id, record, sources }) => {
|
|
616
|
+
const instances = [...plan.failures.values()].flatMap((failure) =>
|
|
617
|
+
failure.id === id && failure.instance !== null
|
|
618
|
+
? [failure.instance.length]
|
|
619
|
+
: [],
|
|
620
|
+
);
|
|
621
|
+
return method(
|
|
622
|
+
`Failure${id}`,
|
|
623
|
+
`VAR_INPUT\n code : UDINT;\n step : UINT;\n${instances.length ? ` instance : STRING(${maximum(instances)});\n` : ""}END_VAR\n`,
|
|
624
|
+
`${[
|
|
625
|
+
"payload := '';",
|
|
626
|
+
`Append(${stStringLiteral(record)});`,
|
|
627
|
+
...(instances.length ? ["Append(instance);"] : []),
|
|
628
|
+
`Append(${stStringLiteral(`"properties":{}${codeMember}`)});`,
|
|
629
|
+
"Append(UDINT_TO_STRING(code));",
|
|
630
|
+
"ErrorName(code);",
|
|
631
|
+
`Append(',"source":"');`,
|
|
632
|
+
"CASE step OF",
|
|
633
|
+
...sources.map(
|
|
634
|
+
({ source, steps }) =>
|
|
635
|
+
`${steps.join(", ")}: Append(${stStringLiteral(asciiJson(source).slice(1, -1))});`,
|
|
636
|
+
),
|
|
637
|
+
"END_CASE;",
|
|
638
|
+
"Append('\"');",
|
|
639
|
+
"Commit();",
|
|
640
|
+
].join("\n")}\n`,
|
|
641
|
+
);
|
|
642
|
+
},
|
|
643
|
+
);
|
|
644
|
+
|
|
645
|
+
const operands = plan.traceOperands;
|
|
646
|
+
const trace =
|
|
647
|
+
operands === undefined
|
|
648
|
+
? []
|
|
649
|
+
: [
|
|
650
|
+
method(
|
|
651
|
+
"Trace",
|
|
652
|
+
`${[
|
|
653
|
+
"VAR_INPUT",
|
|
654
|
+
" site : UINT;",
|
|
655
|
+
" value : DINT;",
|
|
656
|
+
" operands : USINT;",
|
|
657
|
+
...Array.from(
|
|
658
|
+
{ length: operands },
|
|
659
|
+
(_, index) => ` operand${index} : DINT;`,
|
|
660
|
+
),
|
|
661
|
+
"END_VAR",
|
|
662
|
+
].join("\n")}\n`,
|
|
663
|
+
traceRecord(operands).implementation,
|
|
664
|
+
),
|
|
665
|
+
];
|
|
666
|
+
|
|
667
|
+
const { start, rest } = header(buildPlaceholder);
|
|
668
|
+
const clientPorts = `VAR_INPUT\nstart : BOOL;\ncancel : BOOL;\nEND_VAR\nVAR_IN_OUT\nbody : STRING(${plcBufferBytes});\nEND_VAR\nVAR_OUTPUT\nbusy : BOOL;\ndone : BOOL;\nerror : UDINT;\nEND_VAR`;
|
|
669
|
+
const clientObjects =
|
|
670
|
+
transport.kind === "opcua"
|
|
671
|
+
? [
|
|
672
|
+
opcuaClient({
|
|
673
|
+
id: telemetryClient,
|
|
674
|
+
name: names.client,
|
|
675
|
+
ports: clientPorts,
|
|
676
|
+
port: (label) => label,
|
|
677
|
+
timeoutMs: transport.timeoutMs,
|
|
678
|
+
rpcGlobal: transport.rpcGlobal,
|
|
679
|
+
body: "Append(body);",
|
|
680
|
+
decode: "",
|
|
681
|
+
}),
|
|
682
|
+
...opcuaClientMethods(telemetryClient),
|
|
683
|
+
]
|
|
684
|
+
: [
|
|
685
|
+
...nativeHttp({
|
|
686
|
+
id: telemetryClient,
|
|
687
|
+
name: names.client,
|
|
688
|
+
ports: clientPorts,
|
|
689
|
+
port: (label) => label,
|
|
690
|
+
endpoint: transport.endpoint,
|
|
691
|
+
network: transport.network,
|
|
692
|
+
method: "POST",
|
|
693
|
+
json: true,
|
|
694
|
+
body: "Append(body);",
|
|
695
|
+
decode: "",
|
|
696
|
+
}),
|
|
697
|
+
...httpClientMethods(telemetryClient),
|
|
698
|
+
...httpCertificateVerifier(
|
|
699
|
+
telemetryClient,
|
|
700
|
+
names.client,
|
|
701
|
+
transport.endpoint,
|
|
702
|
+
),
|
|
703
|
+
];
|
|
704
|
+
return [
|
|
705
|
+
{
|
|
706
|
+
id: "tsplc:telemetryRecord",
|
|
707
|
+
kind: "dataType",
|
|
708
|
+
name: names.record,
|
|
709
|
+
declaration: `TYPE ${names.record} :\nSTRUCT\n sequence : ULINT;\n json : STRING(${recordBytes});\nEND_STRUCT\nEND_TYPE\n`,
|
|
710
|
+
},
|
|
711
|
+
{
|
|
712
|
+
id: telemetryWriter,
|
|
713
|
+
kind: "functionBlock",
|
|
714
|
+
name: names.writer,
|
|
715
|
+
declaration: `FUNCTION_BLOCK ${names.writer}
|
|
716
|
+
VAR_INPUT
|
|
717
|
+
task : STRING(${taskNameLength});
|
|
718
|
+
END_VAR
|
|
719
|
+
VAR_OUTPUT
|
|
720
|
+
scan : UDINT;
|
|
721
|
+
now : UDINT;
|
|
722
|
+
END_VAR
|
|
723
|
+
VAR
|
|
724
|
+
head : ULINT;
|
|
725
|
+
sent : ULINT;
|
|
726
|
+
dropped : ULINT;
|
|
727
|
+
records : ARRAY[0..${capacity - 1}] OF ${names.record};
|
|
728
|
+
payload : STRING(${plcBufferBytes});
|
|
729
|
+
malformed : BOOL;
|
|
730
|
+
text : STRING(${recordBytes});
|
|
731
|
+
batch : STRING(${plcBufferBytes});
|
|
732
|
+
batched : ULINT;
|
|
733
|
+
sending : BOOL;
|
|
734
|
+
lastSend : UDINT;
|
|
735
|
+
client : ${names.client};
|
|
736
|
+
END_VAR
|
|
737
|
+
`,
|
|
738
|
+
// Idle, a scan costs the clock and two comparisons. A 400 means the server can't take
|
|
739
|
+
// these records, so they are dropped rather than sent forever.
|
|
740
|
+
implementation: `scan := scan + 1;
|
|
741
|
+
now := TIME_TO_UDINT(TIME());
|
|
742
|
+
IF sending THEN
|
|
743
|
+
client(start := TRUE, cancel := FALSE, body := batch);
|
|
744
|
+
IF client.done THEN
|
|
745
|
+
IF client.error = ${httpErrors.refused} THEN
|
|
746
|
+
dropped := dropped + (batched - sent);
|
|
747
|
+
END_IF;
|
|
748
|
+
IF client.error = 0 OR client.error = ${httpErrors.refused} THEN
|
|
749
|
+
sent := batched;
|
|
750
|
+
END_IF;
|
|
751
|
+
sending := FALSE;
|
|
752
|
+
lastSend := now;
|
|
753
|
+
client(start := FALSE, cancel := FALSE, body := batch);
|
|
754
|
+
END_IF;
|
|
755
|
+
ELSIF head <> sent AND_THEN (head - sent >= ${batchRecords(recordBytes)} OR now - lastSend >= ${telemetrySendIntervalMs}) THEN
|
|
756
|
+
Send();
|
|
757
|
+
END_IF;
|
|
758
|
+
`,
|
|
759
|
+
},
|
|
760
|
+
method(
|
|
761
|
+
"Send",
|
|
762
|
+
"VAR\n slot : UDINT;\nEND_VAR\n",
|
|
763
|
+
`IF head - sent > ${capacity} THEN
|
|
764
|
+
dropped := dropped + (head - sent - ${capacity});
|
|
765
|
+
sent := head - ${capacity};
|
|
766
|
+
END_IF;
|
|
767
|
+
payload := '';
|
|
768
|
+
Append(${stStringLiteral(start)});
|
|
769
|
+
Append(task);
|
|
770
|
+
Append(${stStringLiteral(rest[0])});
|
|
771
|
+
Append(UDINT_TO_STRING(now));
|
|
772
|
+
Append(${stStringLiteral(rest[1])});
|
|
773
|
+
Append(UDINT_TO_STRING(scan));
|
|
774
|
+
Append(${stStringLiteral(rest[2])});
|
|
775
|
+
Append(ULINT_TO_STRING(sent + 1));
|
|
776
|
+
Append(${stStringLiteral(rest[3])});
|
|
777
|
+
Append(ULINT_TO_STRING(dropped));
|
|
778
|
+
Append(${stStringLiteral(rest[4])});
|
|
779
|
+
batched := sent;
|
|
780
|
+
WHILE batched <> head DO
|
|
781
|
+
slot := ULINT_TO_UDINT((batched + 1) MOD ${capacity});
|
|
782
|
+
IF ByteLength(payload) + ByteLength(records[slot].json) + 3 > ${plcBufferBytes} THEN
|
|
783
|
+
EXIT;
|
|
784
|
+
END_IF;
|
|
785
|
+
IF batched <> sent THEN
|
|
786
|
+
Append(',');
|
|
787
|
+
END_IF;
|
|
788
|
+
Append(records[slot].json);
|
|
789
|
+
batched := batched + 1;
|
|
790
|
+
END_WHILE;
|
|
791
|
+
Append(']}');
|
|
792
|
+
batch := payload;
|
|
793
|
+
sending := TRUE;
|
|
794
|
+
client(start := TRUE, cancel := FALSE, body := batch);
|
|
795
|
+
`,
|
|
796
|
+
"PRIVATE ",
|
|
797
|
+
),
|
|
798
|
+
// The whole record is written before \`head\` moves, so a reader of the ring can tell a torn one.
|
|
799
|
+
method(
|
|
800
|
+
"Commit",
|
|
801
|
+
"VAR\n slot : UDINT;\nEND_VAR\n",
|
|
802
|
+
`${commitMembers.map(({ member, value }) => `Append(${stStringLiteral(member)});\nAppend(UDINT_TO_STRING(${value}));\n`).join("")}Append('}');
|
|
803
|
+
slot := ULINT_TO_UDINT((head + 1) MOD ${capacity});
|
|
804
|
+
records[slot].json := payload;
|
|
805
|
+
records[slot].sequence := head + 1;
|
|
806
|
+
head := head + 1;
|
|
807
|
+
`,
|
|
808
|
+
"PRIVATE ",
|
|
809
|
+
),
|
|
810
|
+
method(
|
|
811
|
+
"ErrorName",
|
|
812
|
+
"VAR_INPUT\n code : UDINT;\nEND_VAR\n",
|
|
813
|
+
`CASE code OF
|
|
814
|
+
${errorNames.map(([code, name]) => `${code}: Append(${stStringLiteral(`${nameMember}${asciiJson(name)}`)});`).join("\n")}
|
|
815
|
+
${httpErrors.base + 1}..${httpErrors.last}: Append(${stStringLiteral(`${nameMember}"HTTP `)}); Append(UDINT_TO_STRING(code - ${httpErrors.base})); Append('"');
|
|
816
|
+
END_CASE;
|
|
817
|
+
`,
|
|
818
|
+
"PRIVATE ",
|
|
819
|
+
),
|
|
820
|
+
method(
|
|
821
|
+
"WriteText",
|
|
822
|
+
"VAR_INPUT\n data : POINTER TO BYTE;\n length : DINT;\nEND_VAR\nVAR\n index : DINT;\nEND_VAR\n",
|
|
823
|
+
`index := 0;
|
|
824
|
+
WHILE index < length AND_THEN data[index] <> 0 DO
|
|
825
|
+
text[index] := data[index];
|
|
826
|
+
index := index + 1;
|
|
827
|
+
END_WHILE;
|
|
828
|
+
text[index] := 0;
|
|
829
|
+
WriteString(text);
|
|
830
|
+
`,
|
|
831
|
+
"PRIVATE ",
|
|
832
|
+
),
|
|
833
|
+
...jsonEncoders(telemetryWriter),
|
|
834
|
+
...calls,
|
|
835
|
+
...failures,
|
|
836
|
+
...trace,
|
|
837
|
+
...clientObjects,
|
|
838
|
+
];
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/** Writes the build id into the sender's batch header, where `telemetryObjects` left a placeholder. */
|
|
842
|
+
export function fillBuildId(
|
|
843
|
+
objects: readonly GeneratedObject[],
|
|
844
|
+
build: string,
|
|
845
|
+
): void {
|
|
846
|
+
const send = objects.find(
|
|
847
|
+
(object) => object.id === `${telemetryWriter}:Send`,
|
|
848
|
+
);
|
|
849
|
+
if (send?.implementation !== undefined) {
|
|
850
|
+
send.implementation = send.implementation.replace(buildPlaceholder, build);
|
|
851
|
+
}
|
|
852
|
+
}
|