@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.
package/types/run.d.ts ADDED
@@ -0,0 +1,74 @@
1
+ import { HttpTransport } from './http.js';
2
+ import { RunRequest, SessionSummary } from './protocol.js';
3
+ import { SessionFeed } from './session.js';
4
+ import { ErrorSink, Readable } from './values.js';
5
+ /** Stop action: stop in order, or stop without waiting. */
6
+ export type StopAction = 'stop' | 'force';
7
+ /** Run control and its state (GuiClient.run). */
8
+ export interface ClientRun {
9
+ /** A start was sent and its answer is pending. */
10
+ readonly starting: Readable<boolean>;
11
+ /** A stop was sent and its answer is pending (which kind of stop). */
12
+ readonly stopping: Readable<StopAction | null>;
13
+ /** A stop was asked for while the answer to a start was pending. */
14
+ readonly stopWhileStarting: Readable<boolean>;
15
+ /** Why the last action was refused (the server's text, unchanged). null once a run starts or ends. */
16
+ readonly error: Readable<string | null>;
17
+ /**
18
+ * Starts a run (`POST api/run`). Resolves with the summary in the answer; rejects with an HttpError if refused.
19
+ * The server may check the configuration file again as part of the start, so this waits up to twice the check limit
20
+ * (waiting its turn, then checking) plus a margin. If there is still no answer, it rejects with an HttpError with
21
+ * `timedOut` and error says that no answer came (the WebSocket state tells whether the run started). The server may
22
+ * have started it.
23
+ */
24
+ start(request: RunRequest): Promise<SessionSummary | null>;
25
+ /** Stops in order (`POST api/stop` `{force: false}`). Asking again has no further effect. */
26
+ stop(): Promise<SessionSummary | null>;
27
+ /** Stops without waiting (`POST api/stop` `{force: true}`). */
28
+ forceStop(): Promise<SessionSummary | null>;
29
+ }
30
+ /**
31
+ * Run control: start (`POST api/run`), stop in order or without waiting (`POST api/stop`), and the state for them.
32
+ *
33
+ * - The answer to an action (the current summary) is applied to the session copy without waiting for the notification
34
+ * (no stale display right after a click).
35
+ * - Start and stop are sent separately. A stop can be asked for while the answer to a start is pending (the server
36
+ * delivers it once the launcher has started; stopping in order has no further effect when asked again, so if the
37
+ * summary in the start's answer does not show the stop as asked for yet, it is asked for again).
38
+ * - error holds the reason for a refusal (the server's English text). It is cleared once a run starts or ends.
39
+ * - The Promises of start() and stop() resolve with the summary in the answer (null if unreadable) and reject with an
40
+ * HttpError when refused. No answer (`timedOut`) is not a refusal: the session copy tells whether the run started.
41
+ * The state (starting and so on) has already changed when the Promise resolves.
42
+ */
43
+ export declare class RunControl implements ClientRun {
44
+ private readonly feed;
45
+ private readonly transport;
46
+ /**
47
+ * How long to wait for the answer to a start (ms). Long, because the server may check again as part of the start.
48
+ */
49
+ private readonly startTimeoutMs;
50
+ private readonly startSending;
51
+ private readonly stopSending;
52
+ private readonly stopAskedWhileStarting;
53
+ private readonly failure;
54
+ readonly starting: Readable<boolean>;
55
+ readonly stopping: Readable<StopAction | null>;
56
+ readonly stopWhileStarting: Readable<boolean>;
57
+ readonly error: Readable<string | null>;
58
+ constructor(feed: SessionFeed, transport: HttpTransport, onError?: ErrorSink,
59
+ /**
60
+ * How long to wait for the answer to a start (ms). Long, because the server may check again as part of the start.
61
+ */
62
+ startTimeoutMs?: () => number);
63
+ /** Starts a run. */
64
+ start(request: RunRequest): Promise<SessionSummary | null>;
65
+ /** Stops in order (waits for the participants to end). Asking again never turns it into a stop without waiting. */
66
+ stop(): Promise<SessionSummary | null>;
67
+ /** Stops without waiting (kills the participants). */
68
+ forceStop(): Promise<SessionSummary | null>;
69
+ /**
70
+ * Sends and applies the summary in the answer. done is called after the answer (failures too), before the Promise
71
+ * settles.
72
+ */
73
+ private send;
74
+ }
@@ -0,0 +1,255 @@
1
+ import { CheckResult, ControlLink, Dropped, Finding, LauncherEvent, ParticipantState, Phase, RunResult, ScenarioStatus, ScenarioStep, ServerProblem, SessionSummary, Speed, TimeReport } from './protocol.js';
2
+ /** Log lines kept per source (participant or launcher). The same number the server keeps. */
3
+ export declare const MAX_LOG_LINES_PER_SOURCE = 2000;
4
+ /** Log lines kept in total. Beyond this, the oldest lines of the source holding the most are dropped. */
5
+ export declare const MAX_LOG_LINES = 20000;
6
+ /** Server notices kept. Beyond this, only the number is counted. */
7
+ export declare const MAX_PROBLEMS = 200;
8
+ /** Findings of the run kept. */
9
+ export declare const MAX_FINDINGS = 500;
10
+ /** One log line. */
11
+ export interface LogLine {
12
+ /** seq of the event. A marker for dropped lines has the seq of the line before it + 0.5 (markers may share a seq). */
13
+ seq: number;
14
+ /** Key the screen tells lines apart by (unique within the session, markers included). */
15
+ key: string;
16
+ /** The participant name, or `vehicle_sil_launch`. */
17
+ source: string;
18
+ line: string;
19
+ /**
20
+ * Only on a marker for dropped lines ("N lines were dropped here"). launcher: lines the launcher could not write
21
+ * (log_dropped).
22
+ */
23
+ gap?: {
24
+ count: number;
25
+ atLeast: boolean;
26
+ launcher?: boolean;
27
+ };
28
+ }
29
+ /** Log counts per source. */
30
+ export interface LogSource {
31
+ /** Lines held now (markers for dropped lines not counted). */
32
+ lines: number;
33
+ /** Old lines dropped (those the server no longer kept, and those the screen dropped at its limit). */
34
+ dropped: number;
35
+ }
36
+ /** State of a participant during the run. */
37
+ export interface ParticipantView {
38
+ name: string;
39
+ /** The last state received. null if nothing has arrived yet. */
40
+ state: ParticipantState | null;
41
+ required: boolean | null;
42
+ pid: number | null;
43
+ exitCode: number | null;
44
+ signal: string | null;
45
+ /**
46
+ * Why the launcher decided the participant failed (reason of the failed event; may arrive before the process ends).
47
+ * Kept even if ended arrives later. null unless it was decided to have failed.
48
+ */
49
+ failure: string | null;
50
+ /** Why it could not be started (not_started), or the text of the failure finding (failed). In English, unchanged. */
51
+ message: string | null;
52
+ /**
53
+ * The launcher asked it to stop (stop_requested; ended in the result is stopped). How it ends after that is not
54
+ * colored as a failure.
55
+ */
56
+ stopRequested: boolean;
57
+ /** Names of fields the server left out because the summary was full (empty once a later event arrives). */
58
+ omittedFields: readonly string[];
59
+ /**
60
+ * State of the connection to the ECU's variables (the control_link event, or control_link of the result). null while
61
+ * unknown.
62
+ */
63
+ controlLink: ControlLink | null;
64
+ /** A participant the user starts (manual of the check result, or the manual event). */
65
+ manual: boolean;
66
+ /** manual: the command line that starts it (as the launcher wrote it). null if it has not arrived. */
67
+ commandLine: string | null;
68
+ /** manual: the directory to run that command in. null if it has not arrived. */
69
+ workingDirectory: string | null;
70
+ /**
71
+ * manual: environment variables to set when starting it (names and values, as the launcher wrote them). Empty if
72
+ * there are none. When the registry is not on a loopback address, it includes `VEHICLE_SIL_LISTEN_ON_NETWORK` with
73
+ * `1`.
74
+ */
75
+ environment: Readonly<Record<string, string>>;
76
+ }
77
+ /** State of the launcher process (as the server sees it). */
78
+ export interface LauncherView {
79
+ /** Whether it is running. Stays true after the result arrives, until it exits. Decides whether it can be stopped. */
80
+ running: boolean;
81
+ exitCode: number | null;
82
+ /** Number of the signal that ended it (POSIX only). */
83
+ signal: number | null;
84
+ /**
85
+ * Seconds it has been running without writing anything (the server watches with a heartbeat). null while it writes.
86
+ */
87
+ silentSeconds: number | null;
88
+ }
89
+ /**
90
+ * Virtual time and how it advances (the last time event, or time of the summary). Fields an older launcher does not
91
+ * write are null. TimeControl (time.ts) derives from this what the time controls need (whether they can be used now
92
+ * and so on).
93
+ */
94
+ export interface TimeView {
95
+ simMs: number;
96
+ /** The current speed (a number or 'max'; null if not written). */
97
+ speed: Speed;
98
+ /** Whether it is paused (null if not written). */
99
+ paused: boolean | null;
100
+ /** Actual speed over roughly the last second (null while paused and when starting to move). */
101
+ actualSpeed: number | null;
102
+ /** Number of late time steps. */
103
+ lateSteps: number | null;
104
+ /** Max lateness (ms). */
105
+ maxLateMs: number | null;
106
+ /** Total slip (ms). */
107
+ slippedMs: number | null;
108
+ /** Lateness of a time step that is past its deadline and has not started yet (ms). */
109
+ lateMs: number | null;
110
+ }
111
+ /** The last command event received (stop included). */
112
+ export interface CommandView {
113
+ /** Name of the command (null for an unreadable line). */
114
+ name: string | null;
115
+ accepted: boolean;
116
+ /** Why it was not accepted (in English, unchanged). */
117
+ message: string | null;
118
+ /** For an accepted step, the virtual time it actually advances (ms). */
119
+ ms: number | null;
120
+ /** seq of that event (-1 if unknown). Tells a new answer from the same answer arriving again in a summary. */
121
+ seq: number;
122
+ }
123
+ export declare function timeView(time: TimeReport): TimeView;
124
+ /**
125
+ * Progress of a scenario run (from scenario of the summary, scenario_step events, and scenario of the check and the
126
+ * result).
127
+ */
128
+ export interface ScenarioView {
129
+ /** The scenario file (relative to root). */
130
+ file: string;
131
+ name: string | null;
132
+ /** Number of steps (from the check result; null until then). */
133
+ stepCount: number | null;
134
+ /** Steps received (in index order, the last state of each step). After the result, every step. */
135
+ steps: readonly ScenarioStep[];
136
+ /** Number of steps beyond the limit that the server did not keep. */
137
+ stepsOmitted: number;
138
+ /** Result of the scenario (passed, failed, incomplete, not_run). null until there is a result. */
139
+ status: ScenarioStatus | null;
140
+ /** Virtual time at which the scenario ended. */
141
+ overAtMs: number | null;
142
+ }
143
+ /** State of one session (one run of the launcher), as the screen sees it. */
144
+ export interface SessionView {
145
+ /** Session number the server assigned. */
146
+ id: number;
147
+ /** seq of the last event received (-1 if none). On reconnect, the resend is requested from the next one. */
148
+ lastSeq: number;
149
+ /** The configuration file (relative to root). */
150
+ file: string;
151
+ /** Participants left out. */
152
+ disable: readonly string[];
153
+ /**
154
+ * Participants the user chose on the screen to start themselves (manual: true in the configuration file is in check).
155
+ */
156
+ manual: readonly string[];
157
+ /** Speed given at the start (null means as in the configuration file). The current speed is time.speed. */
158
+ speed: Speed;
159
+ /** Whether the run started paused. */
160
+ startPaused: boolean;
161
+ phase: Phase | null;
162
+ /** Position of the last known phase received (so an unknown phase never moves the phase backwards). */
163
+ phaseRank: number;
164
+ check: CheckResult | null;
165
+ participants: readonly ParticipantView[];
166
+ /** Log lines (in seq order). */
167
+ logs: readonly LogLine[];
168
+ /** Per source, the number of lines held and dropped. */
169
+ logSources: Readonly<Record<string, LogSource>>;
170
+ /** Findings decided during the run (the check's findings are in check). */
171
+ findings: readonly Finding[];
172
+ /** t_ms of the last event received (real time since the launcher started). */
173
+ elapsedMs: number;
174
+ /** Virtual time (only once a time event has arrived). */
175
+ simMs: number | null;
176
+ /**
177
+ * Names of the fields of the last time that the server left out because the summary was full (empty once a later
178
+ * event arrives).
179
+ */
180
+ timeOmittedFields: readonly string[];
181
+ /** Virtual time and how it advances (once a time event has arrived). */
182
+ time: TimeView | null;
183
+ /** Number of stop requests (1: "stopping"; 2: after "stop without waiting" was requested). */
184
+ stopRequests: number;
185
+ /** The last command event received (whether it was accepted, and why not). */
186
+ lastCommand: CommandView | null;
187
+ result: RunResult | null;
188
+ launcher: LauncherView;
189
+ /** What the server noticed (shown in English, unchanged). */
190
+ problems: readonly ServerProblem[];
191
+ /** Number of server notices beyond the limit that were not kept. */
192
+ problemsOmitted: number;
193
+ /**
194
+ * Names of the parts that could not be read (`result.participants` of the summary, `finding.code` of an event and so
195
+ * on). The screen shows "some parts cannot be displayed" for the parts listed here.
196
+ */
197
+ unreadable: readonly string[];
198
+ /**
199
+ * Events not applied because their seq was not after the previous event's (overlap with the server's resend, or seq
200
+ * went back).
201
+ */
202
+ staleEvents: number;
203
+ /** Number of markers placed for lines the server did not send (dropped); used for the markers' keys. */
204
+ droppedMarks: number;
205
+ /** Progress of the scenario (null for a run without a scenario). */
206
+ scenario: ScenarioView | null;
207
+ }
208
+ /** Where the run stands now (used by the status bar and the result display). */
209
+ export type RunOutcome =
210
+ /** Running, with no stop requested. */
211
+ 'running'
212
+ /** A stop was requested (waiting for the participants to end). */
213
+ | 'stopping'
214
+ /** A stop without waiting was requested. */
215
+ | 'forcing'
216
+ /** There is a result, but the launcher is still running (waiting for it to exit). */
217
+ | 'finishing'
218
+ /** The launcher printed a result and exited. */
219
+ | 'ended'
220
+ /** The launcher exited without a result. */
221
+ | 'crashed';
222
+ export declare function runOutcome(session: SessionView): RunOutcome;
223
+ /** Builds a session from the server's summary (hello, a new session). The log arrives in the resend that follows. */
224
+ export declare function sessionFromSummary(summary: SessionSummary): SessionView;
225
+ /**
226
+ * Counts log lines that were not sent (dropped) and places an "N lines were dropped here" marker at the current
227
+ * position of that source's log. Before the resend (not-kept), it goes before the lines being resent.
228
+ */
229
+ export declare function addDropped(session: SessionView, dropped: Dropped): SessionView;
230
+ /**
231
+ * Applies a summary. The local log is kept.
232
+ *
233
+ * - force (WebSocket hello and session): arrives in the same order as events, so everything is taken as correct at
234
+ * the time it arrives.
235
+ * - Without force (an HTTP answer): its order relative to notifications is unknown. Whether the launcher runs and the
236
+ * stop requests are taken, but a launcher that has ended in the same run is never set back to running. Server
237
+ * notices are taken only when there are no fewer than held locally (the server only adds, so a summary with fewer is
238
+ * old). Phase, check, participants, findings, result and virtual time are taken only when the summary is newer than
239
+ * the local events, and not at all with controlOnly (the WebSocket is connected and the same things arrive in
240
+ * notifications), so findings do not appear twice, once from the summary and once from a notification.
241
+ */
242
+ export declare function applySummary(session: SessionView, summary: SessionSummary, force?: boolean, controlOnly?: boolean): SessionView;
243
+ /**
244
+ * Adds a server notice (later summaries carry the same one and replace it). Beyond the limit, only the number is
245
+ * counted.
246
+ */
247
+ export declare function addProblem(session: SessionView, problem: ServerProblem): SessionView;
248
+ /** Position of the phase (-1 for an unknown phase). */
249
+ export declare function phaseIndex(phase: Phase | null): number;
250
+ /**
251
+ * Applies a batch of events. Events with the same or an earlier seq are not applied (overlap with the resend: what
252
+ * arrived before the reconnect). With countStale (events that are not a resend), those not applied are counted in
253
+ * staleEvents (seq went back). The log array is rebuilt once per batch (not copied for every event).
254
+ */
255
+ export declare function applyEvents(session: SessionView, events: readonly LauncherEvent[], countStale?: boolean): SessionView;
@@ -0,0 +1,118 @@
1
+ import { Connection } from './connection.js';
2
+ import { AuthMessage, ServerMessage, SessionSummary } from './protocol.js';
3
+ import { SessionView } from './session-state.js';
4
+ import { ErrorSink, Readable } from './values.js';
5
+ /** How many kinds of notices not tied to a session are kept. */
6
+ export declare const MAX_NOTICES = 20;
7
+ /**
8
+ * Notice for an event not applied because its seq was not after the previous event's (English, shown as is like the
9
+ * other notices).
10
+ */
11
+ export declare const STALE_EVENTS_NOTICE: string;
12
+ /**
13
+ * Notice not tied to a session (an unreadable notification and so on; the English text is shown as is). Identical texts
14
+ * are counted together.
15
+ */
16
+ export interface Notice {
17
+ id: number;
18
+ message: string;
19
+ /** How many times the same text arrived. */
20
+ count: number;
21
+ }
22
+ /** Read side of the session copy (GuiClient.session). */
23
+ export interface ClientSession {
24
+ /** The current (or last) session. null while the server has none yet. */
25
+ readonly session: Readable<SessionView | null>;
26
+ /**
27
+ * The session copy matches the server: connected, and hello and the resent events have all arrived. Until then,
28
+ * Start and Stop are disabled so that nobody acts on a stale display. Other features (values and so on) have their
29
+ * own ready.
30
+ */
31
+ readonly ready: Readable<boolean>;
32
+ /** When an event was last applied (Date.now()). The starting point for advancing elapsed time with the clock. */
33
+ readonly lastEventAt: Readable<number>;
34
+ /**
35
+ * Notices not tied to a session. Notices about the session (a gap in seq and so on) are in the problems of session.
36
+ */
37
+ readonly notices: Readable<readonly Notice[]>;
38
+ /**
39
+ * The server refused the token (WebSocket unauthorized). The connection no longer reconnects. Once set, it stays
40
+ * set (to use another token, create a new client).
41
+ */
42
+ readonly rejected: Readable<boolean>;
43
+ /**
44
+ * The text (English) saying why the server API's version differs from the version this library reads
45
+ * (PROTOCOL_VERSION). The connection no longer reconnects (waiting will not fix it). Once set, it stays set. null if
46
+ * the versions match.
47
+ */
48
+ readonly incompatible: Readable<string | null>;
49
+ /**
50
+ * Receives the readable notifications, one batch at a time, after they are applied to the state. Call the returned
51
+ * function to unsubscribe.
52
+ */
53
+ onMessages(listener: (batch: readonly ServerMessage[]) => void): () => void;
54
+ /**
55
+ * Adds something the user noticed (an unreadable answer and so on) to the notices. Identical texts are counted
56
+ * together.
57
+ */
58
+ report(message: string, count?: number): void;
59
+ /** Removes a notice. */
60
+ dismissNotice(id: number): void;
61
+ }
62
+ /**
63
+ * Copy of a session (one run of the launcher). Reads the notifications of the connection (Connection) and builds the
64
+ * state from the server's summary and events.
65
+ *
66
+ * - Rebuilds the state from the summary in hello (the summary is authoritative), then applies the events that follow
67
+ * (the resent log and new events).
68
+ * - On every connect, puts into the authentication message where in the session it holds the server should resend
69
+ * from (resumeFields).
70
+ * - On a hello or session whose summary core is unreadable, marks itself not in sync and asks the connection to
71
+ * restart (receive again from hello).
72
+ * - Passes unknown notifications and unknown events through without applying them (onMessages gets only the readable
73
+ * ones, the connection's onBatch gets all of them).
74
+ * - Requests to the connection (disconnect after a refusal, restart) are made before subscribers are notified (so
75
+ * they happen even if a subscriber throws).
76
+ */
77
+ export declare class SessionFeed implements ClientSession {
78
+ private readonly connection;
79
+ private readonly onError;
80
+ private readonly current;
81
+ private readonly synced;
82
+ private readonly receivedAt;
83
+ private readonly noticeList;
84
+ private readonly rejectedValue;
85
+ private readonly incompatibleValue;
86
+ private readonly messageListeners;
87
+ private noticeId;
88
+ private stoppedFlag;
89
+ readonly session: Readable<SessionView | null>;
90
+ readonly ready: Readable<boolean>;
91
+ readonly lastEventAt: Readable<number>;
92
+ readonly notices: Readable<readonly Notice[]>;
93
+ readonly rejected: Readable<boolean>;
94
+ readonly incompatible: Readable<string | null>;
95
+ constructor(connection: Connection, onError?: ErrorSink);
96
+ /** Refused, or the versions differ: never connect again (set before the readable values and before disconnecting). */
97
+ get stopped(): boolean;
98
+ /**
99
+ * Fields to add to the authentication message: where in the session it holds the server should resend from. Called
100
+ * on every connect (after the call, the copy is not in sync until hello and the resent events have arrived). Used
101
+ * only inside the library.
102
+ */
103
+ resumeFields(): Pick<AuthMessage, 'session' | 'from_seq'>;
104
+ onMessages(listener: (batch: readonly ServerMessage[]) => void): () => void;
105
+ /**
106
+ * Applies the summary in an HTTP answer without waiting for the notification (the answers to start, stop and
107
+ * fetchSession(); used inside the library). While the WebSocket is connected, takes only the launcher and the stop
108
+ * request for the same run (the rest arrives in notifications). A summary of an earlier run never brings back an old
109
+ * run over a new one.
110
+ */
111
+ adopt(summary: SessionSummary | null): void;
112
+ report(message: string, count?: number): void;
113
+ dismissNotice(id: number): void;
114
+ /** Reads and applies a batch from the connection. */
115
+ private receive;
116
+ private apply;
117
+ private notify;
118
+ }
@@ -0,0 +1,162 @@
1
+ import { HttpTransport } from './http.js';
2
+ import { AppInfo, TimeCommand, TimeCommandName } from './protocol.js';
3
+ import { CommandView, SessionView, TimeView } from './session-state.js';
4
+ import { ErrorSink, Readable } from './values.js';
5
+ import { VariablesState } from './variables.js';
6
+ /** The launcher features that concern time. */
7
+ export declare const FEATURE_SPEED = "speed";
8
+ export declare const FEATURE_START_PAUSED = "start-paused";
9
+ export declare const FEATURE_TIME_COMMANDS = "time-commands";
10
+ /**
11
+ * How long to wait for the launcher's answer (the command event) to a command sent (ms). After that, the client stops
12
+ * waiting, enables the controls again and reports the missing answer as an error (a launcher that does not read its
13
+ * input).
14
+ */
15
+ export declare const ANSWER_TIMEOUT_MS = 5000;
16
+ /** Why nothing was sent: no run, or waiting for the answer to the previous command. */
17
+ export type NotSentReason = 'no-session' | 'waiting';
18
+ /** Result of send() and the like: whether it was sent and, if not, why. */
19
+ export type SendResult = {
20
+ sent: true;
21
+ } | {
22
+ sent: false;
23
+ reason: NotSentReason;
24
+ };
25
+ /** A time command that went wrong. */
26
+ export interface TimeFailure {
27
+ /** server: the server refused (message is the server's English text); no-answer: the launcher did not answer. */
28
+ kind: 'server' | 'no-answer';
29
+ /**
30
+ * English text to show a person. To put it in the screen's language, build it from kind, command and answerTimeoutMs.
31
+ */
32
+ message: string;
33
+ command: TimeCommandName;
34
+ /** Number of the run it was sent to. */
35
+ session: number | null;
36
+ }
37
+ /** Lateness figures (meaningful only while the speed is paced, such as at real-time speed). */
38
+ export interface Lateness {
39
+ lateSteps: number;
40
+ maxLateMs: number;
41
+ slippedMs: number;
42
+ lateMs: number;
43
+ }
44
+ /**
45
+ * Time state and the commands that may be used now. All values come from the session copy and the server information.
46
+ */
47
+ export interface TimeState {
48
+ /** Whether the launcher has time commands (pause, resume, step, speed). If not, no controls are shown. */
49
+ supported: boolean;
50
+ /** Time of the current session (null until a time event arrives). */
51
+ time: TimeView | null;
52
+ /** The speed is paced (a numeric speed). Lateness is not counted when running as fast as possible. */
53
+ paced: boolean;
54
+ /** Lateness figures (null when the launcher does not write them). */
55
+ lateness: Lateness | null;
56
+ /**
57
+ * Whether the run fell behind real time (some time steps were later than the tolerance). Small late_ms within the
58
+ * tolerance come and go with every step synchronization, so they are not counted here. Lateness so far is kept after
59
+ * switching to as fast as possible.
60
+ */
61
+ behind: boolean;
62
+ /** The last command event received (stop included). */
63
+ lastCommand: CommandView | null;
64
+ /**
65
+ * Whether time commands can be sent: the launcher supports them, the session copy is in sync, the phase is running,
66
+ * a time event has arrived, there is no result yet and no stop has been requested. Once the phase moves on to
67
+ * stopping, the launcher no longer accepts them. Commands can also be sent during beforeStart.
68
+ */
69
+ controllable: boolean;
70
+ /**
71
+ * Whether the run is before its first step (not started yet), waiting for a resume or a step: a run started paused
72
+ * (start_paused) whose launcher has a Control Server (the variable status is not off) and that has not started
73
+ * moving. Meanwhile the launcher writes no time events but accepts resume and step (it accepts pause too, which
74
+ * changes nothing), and variables written are applied in the step at time 0.
75
+ */
76
+ beforeStart: boolean;
77
+ /**
78
+ * A command sent whose answer has not been seen yet (waiting for the HTTP answer or the launcher's command event).
79
+ * Meanwhile no further command is sent (so a double click or a repeated Enter does not step twice).
80
+ */
81
+ sending: TimeCommandName | null;
82
+ /** A step is in progress (meanwhile the launcher counts as paused and accepts resume and step). */
83
+ stepping: boolean;
84
+ /** Whether Pause can be used (while running, also during a step). */
85
+ canPause: boolean;
86
+ /** Whether Resume can be used (while paused, or during a step). */
87
+ canResume: boolean;
88
+ /** Whether Step can be used (the same as canResume; the launcher's rule). */
89
+ canStep: boolean;
90
+ /** The last command that went wrong. null once the run changes. */
91
+ failure: TimeFailure | null;
92
+ }
93
+ /** Read side and commands of time (GuiClient.time). */
94
+ export interface ClientTime {
95
+ /** Time state and the commands that may be used now. */
96
+ readonly state: Readable<TimeState>;
97
+ /** How long to wait for the answer to a command (ms). */
98
+ readonly answerTimeoutMs: number;
99
+ pause(): SendResult;
100
+ resume(): SendResult;
101
+ /**
102
+ * While paused, advances virtual time by ms and pauses again (the launcher rounds up to a multiple of the time step).
103
+ */
104
+ step(ms: number): SendResult;
105
+ /** Changes the speed (a pause stays in effect). */
106
+ setSpeed(value: number | 'max'): SendResult;
107
+ /**
108
+ * Sends a command. The result arrives as the launcher's command and time events. With no run, or while waiting for
109
+ * the answer to the previous command, nothing is sent and `{sent: false, reason}` is returned. Check whether a
110
+ * command can be used (canPause in state and so on) before sending.
111
+ */
112
+ send(command: TimeCommand): SendResult;
113
+ }
114
+ /**
115
+ * Simulation time: exposes the state (TimeState) as a readable value and sends time commands (pause, resume, step,
116
+ * speed change) to the GUI server (`POST api/time`).
117
+ *
118
+ * - The state is built from the session copy (summary and launcher events; the summary is authoritative). A client
119
+ * that connects midway gets the same state from the summary.
120
+ * - The server passes commands to the launcher and returns at once. lastCommand shows whether the launcher accepted a
121
+ * command, time shows that it took effect.
122
+ */
123
+ export declare class TimeControl implements ClientTime {
124
+ private readonly session;
125
+ private readonly transport;
126
+ /** How long to wait for an answer (ms). */
127
+ readonly answerTimeoutMs: number;
128
+ /** Variable state (used to decide beforeStart; if absent, always false). */
129
+ private readonly variables;
130
+ private readonly inFlight;
131
+ private readonly pending;
132
+ private readonly failureValue;
133
+ private answerTimer;
134
+ private lastLateness;
135
+ /** Time state and the commands that may be used now. */
136
+ readonly state: Readable<TimeState>;
137
+ constructor(session: Readable<SessionView | null>, ready: Readable<boolean>, app: Readable<AppInfo | null>, transport: HttpTransport,
138
+ /** How long to wait for an answer (ms). */
139
+ answerTimeoutMs?: number, onError?: ErrorSink,
140
+ /** Variable state (used to decide beforeStart; if absent, always false). */
141
+ variables?: Readable<VariablesState> | null);
142
+ pause(): SendResult;
143
+ resume(): SendResult;
144
+ /**
145
+ * While paused, advances virtual time by ms and pauses again (the launcher rounds up to a multiple of the time step).
146
+ */
147
+ step(ms: number): SendResult;
148
+ /** Changes the speed (a pause stays in effect). */
149
+ setSpeed(value: number | 'max'): SendResult;
150
+ /**
151
+ * Sends a command. The result arrives as the launcher's command and time events (the content of the HTTP answer is
152
+ * not used). With no run, or while waiting for the answer to the previous command (sending), nothing is sent and
153
+ * `{sent: false, reason}` is returned. If no answer comes within answerTimeoutMs, stops waiting and reports it in
154
+ * failure. Check whether a command can be used (canPause and so on) before sending.
155
+ */
156
+ send(command: TimeCommand): SendResult;
157
+ /** Stops the wait timer and stops waiting for the answer (when the client closes). */
158
+ dispose(): void;
159
+ /** The answer timed out: if no answer has come yet, stop waiting and say so. */
160
+ private giveUp;
161
+ private compute;
162
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Takes the token from the URL the server printed (`http://127.0.0.1:<port>/#token=<...>`).
3
+ *
4
+ * Every screen the GUI server serves (the sample screens, and your own screen served with `--web`) reads `#token=`
5
+ * from the URL it was opened with, removes it from the URL right away (also from the tab's back/forward history; the
6
+ * browsing history may still keep it) and keeps it in the tab's sessionStorage (it survives a reload and is gone when
7
+ * the tab closes). Browser objects come in as arguments (this library runs on Node too, so it does not read `window`
8
+ * directly). In the browser, call `takeToken(window)`.
9
+ */
10
+ /** sessionStorage key. Screens of the same server (same origin) share this key. */
11
+ export declare const TOKEN_STORAGE_KEY = "vehicle-sil-gui-token";
12
+ /** Where to read the token from. The browser's `window` has this shape. */
13
+ export interface TokenPlace {
14
+ readonly location: {
15
+ readonly hash: string;
16
+ readonly pathname: string;
17
+ readonly search: string;
18
+ };
19
+ readonly history: {
20
+ readonly state: unknown;
21
+ replaceState(data: unknown, unused: string, url?: string | null): void;
22
+ };
23
+ /** Even reading it can throw (when the browser blocks site data); the token is then kept in memory only. */
24
+ readonly sessionStorage?: {
25
+ getItem(key: string): string | null;
26
+ setItem(key: string, value: string): void;
27
+ } | null;
28
+ }
29
+ /**
30
+ * Reads `#token=` from the URL, removes it from the URL, stores it in sessionStorage and returns it. If the URL has
31
+ * none, returns the one in sessionStorage (received earlier in the same tab). If neither has one, returns null (the
32
+ * page was not opened from the URL the server printed).
33
+ * Call it before anything rewrites the URL (when the screen's router rewrites the URL, `#token=` is lost).
34
+ */
35
+ export declare function takeToken(place: TokenPlace): string | null;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Builds the URLs to the server.
3
+ *
4
+ * URLs have no leading / and are relative to <base href> (`api/app`, `ws`), so a screen served under a path other
5
+ * than / (for example /gui/) works unchanged.
6
+ */
7
+ /** WebSocket URL relative to the base href (ws for http, wss for https). */
8
+ export declare function webSocketUrl(baseUri: string, path: string): string;