@vehicle-sil/gui-client 0.0.0-stage → 0.1.0

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.
@@ -0,0 +1,362 @@
1
+ import { HttpTransport } from './http.js';
2
+ import { Open } from './protocol.js';
3
+ import { ErrorSink, Readable } from './values.js';
4
+ import type { BindOptions, VariableBinding } from './variable-binding.js';
5
+ /**
6
+ * Value of a variable: a number for reals, integers and enums, a boolean for bool. A non-finite real (NaN, infinity) is
7
+ * null.
8
+ */
9
+ export type VariableValue = number | boolean | null;
10
+ /** Names a variable: which ECU (peer), which variable. */
11
+ export interface VariableRef {
12
+ peer: string;
13
+ name: string;
14
+ }
15
+ /** Definition of a variable (from the ECU's declaration). */
16
+ export interface VariableInfo {
17
+ name: string;
18
+ type: Open<'bool' | 'integer' | 'real' | 'enum'>;
19
+ /** input: writable during the run; parameter: writable only before the first step; output: read-only. */
20
+ role: Open<'input' | 'output' | 'parameter'>;
21
+ /** Whether it can be written (role is input or parameter). */
22
+ writable: boolean;
23
+ unit: string;
24
+ min: number | null;
25
+ max: number | null;
26
+ /** Suggested increment for editing (not enforced). */
27
+ step: number | null;
28
+ description: string;
29
+ /** Items of an enum (when type is enum; null otherwise). */
30
+ enum: {
31
+ name: string;
32
+ value: number;
33
+ }[] | null;
34
+ }
35
+ /** A participant that has variables. */
36
+ export interface VariablePeer {
37
+ name: string;
38
+ /** Whether it is connected to the Control Server now. */
39
+ connected: boolean;
40
+ variables: VariableInfo[];
41
+ }
42
+ /**
43
+ * State of the variable API. unknown: not heard from the server yet; off: this run has no variables (reason says
44
+ * why); connecting: connecting to the launcher's Control Server (also while reconnecting after a drop; reason says
45
+ * why); ready: values can be read and written; failed: could not connect (reason says why); ended: the run has ended
46
+ * (the values are the last ones).
47
+ */
48
+ export type VariablesStatus = Open<'unknown' | 'off' | 'connecting' | 'ready' | 'failed' | 'ended'>;
49
+ /** List and state of the variables (of the current run). */
50
+ export interface VariablesState {
51
+ /** Which run they belong to (null without a run). */
52
+ session: number | null;
53
+ status: VariablesStatus;
54
+ /** Why the API is unavailable or cannot connect (the server's English text). null if none. */
55
+ reason: string | null;
56
+ peers: VariablePeer[];
57
+ /** Number of participants and variables beyond the limit that the server did not keep. */
58
+ peersOmitted: number;
59
+ variablesOmitted: number;
60
+ }
61
+ /** One value read, and when it is from. */
62
+ export interface VariableReading {
63
+ value: VariableValue;
64
+ /** Virtual time of the step that read this value (ms). */
65
+ sampleMs: number;
66
+ /** The controller's virtual time when this value arrived (ms; null if unknown). */
67
+ simMs: number | null;
68
+ /**
69
+ * Whether this is the value at the paused time. If true, this value includes the steps that began before simMs (it
70
+ * may be shown as the value at the paused time). If false, it is the last value while running.
71
+ */
72
+ paused: boolean;
73
+ }
74
+ /**
75
+ * The last value of each watched variable: `values[peer]?.[name]`. Variables whose value has not arrived yet are
76
+ * absent.
77
+ */
78
+ export type WatchValues = Readonly<Record<string, Readonly<Record<string, VariableReading>>>>;
79
+ /** One point of the history. */
80
+ export interface HistoryPoint {
81
+ sampleMs: number;
82
+ value: VariableValue;
83
+ }
84
+ /** Options of watch. */
85
+ export interface WatchOptions {
86
+ /**
87
+ * Interval at which values are received (ms; default 100). Never shorter than the server's lower limit
88
+ * (watch_period_ms_min of limits, usually 20). If another watch observes the same variable at a shorter interval,
89
+ * values arrive at that interval.
90
+ */
91
+ periodMs?: number;
92
+ /**
93
+ * Number of history points to keep per variable (default 0: none; limit HISTORY_MAX; a non-number or a negative
94
+ * value counts as 0). The oldest are dropped first. Memory is taken as points arrive. The histories of all variables
95
+ * together hold up to HISTORY_TOTAL_MAX points; once that is reached, every history keeps its current size and drops
96
+ * its oldest points (so it holds fewer than requested). When the run changes, the history starts empty.
97
+ */
98
+ history?: number;
99
+ }
100
+ /** A watch of variables. Call close() when done with it (otherwise values keep arriving). */
101
+ export interface VariableWatch {
102
+ /** The last values of the watched variables. A new object every time a value arrives. */
103
+ readonly values: Readable<WatchValues>;
104
+ /** The last value of the variable (undefined if none yet). */
105
+ value(ref: VariableRef): VariableReading | undefined;
106
+ /** History of the variable (oldest first, up to the number in WatchOptions.history). A new array on every call. */
107
+ history(ref: VariableRef): HistoryPoint[];
108
+ /** Stops. If no other watch observes the same variables, the server subscription is dropped too. */
109
+ close(): void;
110
+ }
111
+ /** The server refused a watch. */
112
+ export interface WatchProblem {
113
+ /** `invalid-watch` or `too-many-watches`. */
114
+ code: string;
115
+ /** The server's English text. */
116
+ message: string;
117
+ }
118
+ /** Per-participant state of a write's result (operation). */
119
+ export interface OperationPeer {
120
+ /** sent, queued, disconnected (in progress); applied, late, rejected, cancelled, invalid, busy, unknown (settled). */
121
+ state: string;
122
+ /** Virtual time of the step that applied it (ms). */
123
+ appliedAtMs: number | null;
124
+ /** Applied in a step after the requested time. */
125
+ late: boolean;
126
+ /** Why it went wrong (English text from the ECU or the Control Server). */
127
+ reason: string | null;
128
+ }
129
+ /** Result of a write (operation). */
130
+ export interface OperationView {
131
+ session: number;
132
+ /** Operation number (operation in the `api/write` answer). */
133
+ seq: number;
134
+ /**
135
+ * Overall state: sent, queued (in progress). Once settled, the common state if every participant has the same one
136
+ * (applied, late, rejected, cancelled, invalid, busy, unknown), mixed if they differ.
137
+ */
138
+ state: string;
139
+ /** Whether it has settled (will not change any more). */
140
+ done: boolean;
141
+ peers: Record<string, OperationPeer>;
142
+ }
143
+ /** Options of a write. */
144
+ export interface WriteOptions {
145
+ /** Virtual time at which to apply it (ms). Omitted: the next step. */
146
+ atMs?: number;
147
+ /** If the requested time cannot be met: reject (default; not applied) or apply (applied in the next step). */
148
+ late?: 'reject' | 'apply';
149
+ /**
150
+ * How long to wait for the result (whether it was applied) (ms; default WRITE_TIMEOUT_MS). While paused, and for a
151
+ * write at a later time (atMs), the write does not settle until the simulation gets there, so make this longer or
152
+ * follow the result in operations. `Infinity` never times out (waits until the run changes or close()). Values
153
+ * longer than about 24.8 days (2^31-1 ms) are capped there. A non-number or a value of 0 or less means the default.
154
+ * Counted from the HTTP answer.
155
+ */
156
+ timeoutMs?: number;
157
+ /**
158
+ * Called once when the server receives the write and returns the operation number (before the result; not called
159
+ * when refused or when no answer came). The number lets you follow the in-progress states (queued and so on) in
160
+ * `operations`, so a widget can show "accepted, waiting to be applied" or know that it may send the next write.
161
+ */
162
+ onAccepted?: (operation: {
163
+ session: number;
164
+ seq: number;
165
+ }) => void;
166
+ }
167
+ /**
168
+ * A write that went wrong.
169
+ * - refused: the server did not accept it (wrong type or range, unknown variable, no run and so on; code names the
170
+ * error). It was not sent, so you may fix it and send it again.
171
+ * - failed: accepted but not applied (operation has the state and reason per participant).
172
+ * - no-answer: unknown whether it was applied. The result did not come within the timeout (operation has the last
173
+ * known state), there was no HTTP answer, the server failed midway (500), the Control Server's answer could not be
174
+ * waited for (code `write-timeout`) or the connection broke midway (`write-failed`), the run changed before the
175
+ * answer, the write settled while reconnecting and its result was lost (not in the summary's list and older than
176
+ * the boundary operations_from_seq), or the server could not keep all unsettled operations and stopped following it.
177
+ * **Sending it again as is may write twice**: check operations and the value first.
178
+ * - closed: the client was closed (unknown whether it was applied).
179
+ */
180
+ export declare class WriteError extends Error {
181
+ readonly kind: 'refused' | 'failed' | 'no-answer' | 'closed';
182
+ /**
183
+ * Name of the server's error (for refused: `unknown-variable`, `not-writable`, `invalid-field` and so on; for
184
+ * no-answer: `write-timeout`, `write-failed`; null unless it is an HTTP error).
185
+ */
186
+ readonly code: string | null;
187
+ /** The operation (once its number is known). */
188
+ readonly operation: OperationView | null;
189
+ constructor(kind: 'refused' | 'failed' | 'no-answer' | 'closed', message: string,
190
+ /**
191
+ * Name of the server's error (for refused: `unknown-variable`, `not-writable`, `invalid-field` and so on; for
192
+ * no-answer: `write-timeout`, `write-failed`; null unless it is an HTTP error).
193
+ */
194
+ code?: string | null,
195
+ /** The operation (once its number is known). */
196
+ operation?: OperationView | null);
197
+ }
198
+ /** Read side and writes of variables (GuiClient.variables). */
199
+ export interface ClientVariables {
200
+ /** List and state of the variables. */
201
+ readonly state: Readable<VariablesState>;
202
+ /** Recent operations (results of writes; oldest first, up to OPERATIONS_KEPT; includes other clients' writes). */
203
+ readonly operations: Readable<readonly OperationView[]>;
204
+ /**
205
+ * The server refused a watch (`invalid-watch`, `too-many-watches`; null if not). Values of the refused subscription
206
+ * do not arrive. Back to null once the watches are recreated or reduced and the subscriptions are sent again. The
207
+ * library keeps its subscriptions within the server's limits, so this normally does not happen (it can with a server
208
+ * that has different limits).
209
+ */
210
+ readonly watchProblem: Readable<WatchProblem | null>;
211
+ /**
212
+ * Starts receiving values of variables. `'all'` means every variable. However many watches observe the same variable,
213
+ * the server gets one subscription per variable. Subscribes again automatically after a reconnect. Continues when the
214
+ * run changes (values of the new run arrive).
215
+ */
216
+ watch(variables: readonly VariableRef[] | 'all', options?: WatchOptions): VariableWatch;
217
+ /**
218
+ * Writes variables: `write({Engine: {'engine.throttle': 50}})`. Sent to the current run (session of state); the
219
+ * server refuses if that run is not running (refused, code session-changed / not-running; if the run is not known
220
+ * yet, nothing is sent and the error is not-running). For an enum, give the value (number) or the item's name.
221
+ * Resolves with the operation once applied. Otherwise rejects with a WriteError (tell the cases apart by kind). The
222
+ * same write is never resent automatically.
223
+ */
224
+ write(writes: Record<string, Record<string, number | boolean | string>>, options?: WriteOptions): Promise<OperationView>;
225
+ /**
226
+ * Binds one variable to one screen widget: exposes the value state (stale, none yet, the value at the paused time and
227
+ * so on), whether it can be written and why not, and the write status as one readable value (state), and writes with
228
+ * write(value). This is all a slider, switch or lamp widget needs. However many places bind the same variable, there
229
+ * is one write per variable and per run (no overlapping sends, the same status everywhere, and the status survives
230
+ * recreating a bind). Call close() when done with it.
231
+ */
232
+ bind(ref: VariableRef, options?: BindOptions): VariableBinding;
233
+ }
234
+ /** Default time to wait for the result of a write (ms). */
235
+ export declare const WRITE_TIMEOUT_MS = 10000;
236
+ /** Limit on the number of history points (per variable). */
237
+ export declare const HISTORY_MAX = 100000;
238
+ /**
239
+ * Limit on the memory taken for histories, for all variables together (in points; one point is 17 bytes). Once it is
240
+ * reached, no variable's history grows further; each keeps its current size and drops its oldest points (so fewer
241
+ * points than requested remain).
242
+ */
243
+ export declare const HISTORY_TOTAL_MAX = 2000000;
244
+ /** Number of recent operations kept. */
245
+ export declare const OPERATIONS_KEPT = 64;
246
+ /** Default watch interval (ms). */
247
+ export declare const WATCH_PERIOD_MS = 100;
248
+ /**
249
+ * Intervals of the server subscriptions (ms). A requested interval is rounded to the longest of these that is not
250
+ * longer (never slower than requested). There is one subscription per interval, so there are at most this many
251
+ * subscriptions (the server's limit is 16 per connection).
252
+ */
253
+ export declare const WATCH_PERIODS_MS: readonly [20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 60000];
254
+ /** Reads `variables` of a summary. Unreadable parts are skipped as if absent (additions are compatible). */
255
+ export declare function readVariables(value: unknown, session: number | null): VariablesState;
256
+ /** Reads an `operation` notification (or one entry of operations in a summary). null if unreadable. */
257
+ export declare function readOperation(value: unknown, session: number): OperationView | null;
258
+ /** What VariablesFeed asks of the connection. */
259
+ export interface VariablesLink {
260
+ /** Sends if connected (does nothing otherwise; resume() sends what is needed on connect). */
261
+ send(message: unknown): unknown;
262
+ onBatch(listener: (batch: readonly unknown[]) => void): () => void;
263
+ }
264
+ /**
265
+ * ECU variables: list and state, value watches, writes and their results. No framework needed.
266
+ *
267
+ * - List and state come from `variables` in hello and in HTTP summaries, and from `variables` notifications (`session`
268
+ * notifications do not carry them). When the run changes, they are unknown until that run's `variables`
269
+ * notification arrives.
270
+ * - Each variable goes into the subscription with the shortest interval requested for it (one subscription per
271
+ * interval). A changed subscription is resent with the same id (the server replaces it); after a reconnect, resume()
272
+ * resends them all.
273
+ * - Per variable, keeps the last value and as many history points as requested (ring buffer). Dropped when the run
274
+ * changes.
275
+ * - Write results arrive in `operation` notifications. They can arrive before the HTTP answer (the operation number),
276
+ * so recent operations are kept and matched once the number is known.
277
+ */
278
+ export declare class VariablesFeed implements Omit<ClientVariables, 'bind'> {
279
+ private readonly link;
280
+ private readonly http;
281
+ private readonly onError;
282
+ /** Limits (from limits of api/app; the defaults until then). */
283
+ private readonly limits;
284
+ private readonly stateValue;
285
+ private readonly operationsValue;
286
+ private readonly handles;
287
+ private readonly buckets;
288
+ private readonly readings;
289
+ private readonly histories;
290
+ private readonly pending;
291
+ /** Variables someone watches (null: a watch observes all). */
292
+ private watched;
293
+ /** History points requested by watches of all variables (the largest request). */
294
+ private allHistory;
295
+ /** History points requested by name (per variable, the largest among the watches that asked). */
296
+ private namedHistory;
297
+ /** Total memory taken for histories (in points). */
298
+ private historyReserved;
299
+ private readonly problemValue;
300
+ private nextId;
301
+ private syncQueued;
302
+ readonly state: Readable<VariablesState>;
303
+ readonly operations: Readable<readonly OperationView[]>;
304
+ readonly watchProblem: Readable<WatchProblem | null>;
305
+ constructor(link: VariablesLink, http: HttpTransport, onError?: ErrorSink,
306
+ /** Limits (from limits of api/app; the defaults until then). */
307
+ limits?: () => {
308
+ watch_period_ms_min?: number;
309
+ watch_period_ms_max?: number;
310
+ watch_variables?: number;
311
+ });
312
+ /** Messages sent right after connecting (after authentication): all current subscriptions. */
313
+ resume(): unknown[];
314
+ /** The client was closed: fail the pending writes (including those waiting for the HTTP answer). Watches stay. */
315
+ dispose(): void;
316
+ private receive;
317
+ /** Applies the variables part of hello and of an HTTP summary (`session`). */
318
+ adoptSummary(summary: unknown): void;
319
+ /**
320
+ * With the summary after a reconnect (or from api/session), settles as "no answer came" (lost) the writes waiting for
321
+ * a result that will no longer arrive. For writes whose number is not known yet (before the HTTP answer), the summary
322
+ * is kept and checked the same way once the number is known.
323
+ */
324
+ private forgetLost;
325
+ /**
326
+ * Switched to another run: drop the previous run's values, histories and operations (histories not requested by name
327
+ * are dropped with their memory, so they do not keep growing as the variables change from run to run). Writes
328
+ * waiting for the previous run's result are failed, since the result will not come.
329
+ */
330
+ private enter;
331
+ private takeValues;
332
+ /** Rebuilds values of the watches that observe the changed variables (all watches if touched is null). */
333
+ private publish;
334
+ private valuesOf;
335
+ private withOperation;
336
+ /**
337
+ * Replaces the list of operations and clears the writes whose result has settled. `fresh` holds the operations that
338
+ * just arrived (a batch of notifications, the list in a summary): the list is cut to OPERATIONS_KEPT entries, so
339
+ * settled operations cut from the list are picked up from here.
340
+ */
341
+ private setOperations;
342
+ private settle;
343
+ write(writes: Record<string, Record<string, number | boolean | string>>, options?: WriteOptions): Promise<OperationView>;
344
+ watch(variables: readonly VariableRef[] | 'all', options?: WatchOptions): VariableWatch;
345
+ /**
346
+ * Turns a requested interval into the longest of WATCH_PERIODS_MS that is not longer, kept within the server's range
347
+ * (lower and upper limit).
348
+ */
349
+ private period;
350
+ /** Recounts who watches what, and drops the values of variables nobody watches and histories nobody requests. */
351
+ private forget;
352
+ /**
353
+ * Per interval, the variables to request (null: all). A variable goes to the shortest interval among the watches that
354
+ * observe it.
355
+ */
356
+ private wanted;
357
+ private bucket;
358
+ private watchMessage;
359
+ /** After watches were added or removed, brings the server subscriptions in line, once for all changes. */
360
+ private queueSync;
361
+ private sync;
362
+ }