@2702rebels/ntcore 1.0.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,428 @@
1
+ //#region src/NT4Types.d.ts
2
+ /**
3
+ * Topic properties, including well-known properties
4
+ *
5
+ * https://github.com/wpilibsuite/allwpilib/blob/main/ntcore/doc/networktables4.adoc#properties
6
+ */
7
+ type TopicProperties = Record<string, unknown> & {
8
+ /**
9
+ * **Persistent flag**
10
+ *
11
+ * If `true`, the last set value will be periodically saved to persistent storage on the server
12
+ * and be restored during server startup. Topics with this property set to `true` will not
13
+ * be deleted by the server when the last publisher stops publishing.
14
+ */
15
+ persistent?: boolean;
16
+ /**
17
+ * **Retained flag**
18
+ *
19
+ * Topics with this property set to `true` will not be deleted by the server when
20
+ * the last publisher stops publishing.
21
+ */
22
+ retained?: boolean;
23
+ /**
24
+ * **Cached flag**
25
+ *
26
+ * If `false`, the server and clients will not store the value of the topic.
27
+ * This means that only value updates will be available for the topic.
28
+ */
29
+ cached?: boolean;
30
+ };
31
+ /**
32
+ * Subscription options, including well-known options
33
+ */
34
+ type SubscriptionOptions = Record<string, unknown> & {
35
+ /**
36
+ * **Periodic sweep time (in seconds)**
37
+ *
38
+ * How frequently the server should send changes. The server may send more frequently than this
39
+ * (e.g. use a combined minimum period for all values) or apply a restricted range to this value
40
+ * The default if unspecified is 100 ms (same as NT 3.0).
41
+ */
42
+ periodic?: number;
43
+ /**
44
+ * **All changes flag**
45
+ *
46
+ * If `true`, the server should send all value changes over the wire. If `false`, only
47
+ * the most recent value is sent (same as NT 3.0 behavior). If not specified, defaults to `false`.
48
+ */
49
+ all?: boolean;
50
+ /**
51
+ * **No value changes flag**
52
+ *
53
+ * If `true`, the server should not send any value changes over the wire regardless of other options.
54
+ * This is useful for only getting topic announcements. If `false`, value changes are sent in accordance
55
+ * with other options. If not specified, defaults to `false`.
56
+ */
57
+ topicsonly?: boolean;
58
+ /**
59
+ * **Prefix flag**
60
+ *
61
+ * If `true`, any topic starting with the name in the subscription topics list is subscribed to,
62
+ * not just exact matches. If not specified, defaults to `false`.
63
+ */
64
+ prefix?: boolean;
65
+ };
66
+ //#endregion
67
+ //#region src/NT4Subscription.d.ts
68
+ /** Contains information about NT4 subscription. */
69
+ declare class NT4Subscription {
70
+ /** Subscription UID. */
71
+ readonly uid: number;
72
+ /** Subscription options. */
73
+ readonly options: SubscriptionOptions;
74
+ /** Subscribed topic names or prefixes. */
75
+ readonly topics: Set<string>;
76
+ constructor(uid: number, topics: Iterable<string>, options?: SubscriptionOptions);
77
+ }
78
+ //#endregion
79
+ //#region src/NT4Topic.d.ts
80
+ /** Contains information about NT4 topic. */
81
+ declare class NT4Topic {
82
+ /** Topic UID. */
83
+ readonly uid: number;
84
+ /** Topic name. */
85
+ readonly name: string;
86
+ /** Topic data type. */
87
+ readonly type: string;
88
+ /** Topic properties. */
89
+ readonly properties: TopicProperties;
90
+ /** Publisher UID. */
91
+ pubuid?: number;
92
+ /** Last retained value. */
93
+ retainedValue?: unknown;
94
+ /** Timestamp of {@link retainedValue}. */
95
+ retainedTimestamp?: number;
96
+ constructor(uid: number, name: string, type: string, properties?: TopicProperties, pubuid?: number);
97
+ /** Merges properties with this topic. */
98
+ mergeProperties(properties: TopicProperties): void;
99
+ /** Updates retained value if the incoming timestamp is newer (greater-than or equal-to). */
100
+ updateRetainedValue(value: unknown, timestamp: number): void;
101
+ /** Removes retained value except for "weak" default value. */
102
+ removeRetainedValue(): void;
103
+ /** Resets retained value timestamp except for "weak" default value. */
104
+ resetRetainedTimestamp(): void;
105
+ /** Topic type code. For internal use only. */
106
+ readonly typeCode: number;
107
+ }
108
+ //#endregion
109
+ //#region src/NT4Spec.d.ts
110
+
111
+ /**
112
+ * Supported data types mapped to protocol identifiers.
113
+ *
114
+ * https://github.com/wpilibsuite/allwpilib/blob/main/ntcore/doc/networktables4.adoc#supported-data-types
115
+ */
116
+ declare const DataTypeCodes: {
117
+ readonly boolean: 0;
118
+ readonly double: 1;
119
+ readonly int: 2;
120
+ readonly float: 3;
121
+ readonly string: 4;
122
+ readonly json: 4;
123
+ readonly raw: 5;
124
+ readonly rpc: 5;
125
+ readonly msgpack: 5;
126
+ readonly protobuf: 5;
127
+ readonly "boolean[]": 16;
128
+ readonly "double[]": 17;
129
+ readonly "int[]": 18;
130
+ readonly "float[]": 19;
131
+ readonly "string[]": 20;
132
+ };
133
+ type DataType = keyof typeof DataTypeCodes;
134
+ //#endregion
135
+ //#region src/NT4Client.d.ts
136
+ type NT4ClientConnectionState = "disconnected" | "connecting" | "connected";
137
+ type NT4ClientOptions = {
138
+ /** Indicates that secure channel should be used. */
139
+ secure?: boolean;
140
+ /**
141
+ * Invoked when the client connects.
142
+ */
143
+ onConnect?: () => void;
144
+ /**
145
+ * Invoked when the client disconnects.
146
+ */
147
+ onDisconnect?: () => void;
148
+ /**
149
+ * Invoked when new topic is announced by the server.
150
+ *
151
+ * @param topic topic announced
152
+ */
153
+ onTopicAnnounced?: (topic: NT4Topic) => void;
154
+ /**
155
+ * Invoked when a topic is removed by the server.
156
+ *
157
+ * @param topic topic removed
158
+ */
159
+ onTopicRemoved?: (topic: NT4Topic) => void;
160
+ /**
161
+ * Invoked when a topic properties are updated by the server.
162
+ *
163
+ * @param topic topic updated
164
+ */
165
+ onTopicUpdated?: (topic: NT4Topic) => void;
166
+ /**
167
+ * Invoked when the topic receives new data.
168
+ *
169
+ * @param topic topic receiving data
170
+ * @param value value received
171
+ * @param timestamp server timestamp in microseconds
172
+ */
173
+ onDataReceived?: (topic: NT4Topic, value: unknown, timestamp: number) => void;
174
+ /**
175
+ * Retry policy that accepts the current number of connection attempts
176
+ * and returns the timeout in milliseconds or -1 to abort.
177
+ *
178
+ * @param attempts number of subsequent connection attempts
179
+ * @returns timeout in milliseconds or -1 to abort
180
+ */
181
+ retryPolicy?: (attempts: number) => number;
182
+ /**
183
+ * Overrides default timeout for ping-pong packets.
184
+ *
185
+ * Defaults:
186
+ * - NT 4.0: 3000 milliseconds
187
+ * - NT 4.1: 1000 milliseconds
188
+ */
189
+ pingTimeoutMilliseconds?: number;
190
+ };
191
+ /**
192
+ * Implements a NetworkTable4 client that is responsible for establishing and maintaining a connection.
193
+ */
194
+ declare class NT4Client {
195
+ private readonly port;
196
+ private readonly clientId;
197
+ private readonly dataChannel;
198
+ private readonly rttChannel;
199
+ private readonly decoder;
200
+ private readonly encoder;
201
+ private readonly onConnect?;
202
+ private readonly onDisconnect?;
203
+ private readonly onTopicAnnounced?;
204
+ private readonly onTopicRemoved?;
205
+ private readonly onTopicUpdated?;
206
+ private readonly onDataReceived?;
207
+ private readonly retryPolicy;
208
+ private pingTimeoutOverride_ms?;
209
+ private subscriptions;
210
+ private publishedTopics;
211
+ private announcedTopics;
212
+ private announcedTopicsIndex;
213
+ private connectionEstablished;
214
+ private connectionInitiated;
215
+ private connectionAttempts;
216
+ private connectionState;
217
+ private networkLatency_μs;
218
+ private serverTimeOffset_μs;
219
+ private pingTimeout_ms;
220
+ private pongTimestamp_μs;
221
+ private pingIntervalId;
222
+ private pongIntervalId;
223
+ /** Truncated at 15 seconds exponential backoff retry policy starting at 1 second. */
224
+ static defaultRetryPolicy(attempts: number): number;
225
+ /** Cleans up closed data channel. */
226
+ private cleanup;
227
+ /** Attempts to establish a connection, continuously retrying using policy. */
228
+ private connectWithRetry;
229
+ /** Invoked once the client-server clock is synchronized. */
230
+ private onClockSynchronized;
231
+ /** Consumes received data frame. */
232
+ private processDataFrame;
233
+ /** Handles general data frame received. */
234
+ private onChannelMessage;
235
+ /** Handles RTT data frame received. */
236
+ private onChannelMessageRTT;
237
+ /** Handles channel closing for whatever reason. */
238
+ private onChannelClose;
239
+ /** Returns current timestamp in microseconds. This is NOT wall clock time. */
240
+ private getClientTimestamp_μs;
241
+ /** Sends data frame to publish the topic. */
242
+ private publishCore;
243
+ /** Sends data frame to unpublish the topic. */
244
+ private unpublishCore;
245
+ /** Sends data frame to set topic properties. */
246
+ private setPropertiesCore;
247
+ /** Sends data frame to subscribe the subscription. */
248
+ private subscribeCore;
249
+ /** Sends data frame to unsubscribe the subscription. */
250
+ private unsubscribeCore;
251
+ /** Sends data frame to update topic value. */
252
+ private sendValueCore;
253
+ /**
254
+ * Sends RTT measurement binary frame.
255
+ * Uses RTT channel if available, falls back to data channel in NT4.0.
256
+ */
257
+ private sendRTT;
258
+ /**
259
+ * Determines whether connection times out if no message has been received
260
+ * within the timeout interval since the last pong.
261
+ */
262
+ private checkTimeout;
263
+ /** Generates random UID. */
264
+ private generateUID;
265
+ /** Generates random safe UID avoiding possible clashes. */
266
+ private generateSafeUID;
267
+ /** Determines whether published topic with the specified identifier exists. */
268
+ private readonly hasPublishedTopicWithId;
269
+ /** Determines whether subscription with the specified identifier exists. */
270
+ private readonly hasSubscriptionWithId;
271
+ /**
272
+ * Constructs an instance of NT4Client without initiating a connection.
273
+ *
274
+ * @param serverAddress NT4 server network address
275
+ * @param clientId this client identifier (cannot contain '@')
276
+ * @param options options
277
+ */
278
+ constructor(serverAddress: string, clientId: string, options: NT4ClientOptions);
279
+ /**
280
+ * Returns NT4 server network address the client is using.
281
+ */
282
+ readonly serverAddress: string;
283
+ /**
284
+ * Returns current connection state.
285
+ */
286
+ get state(): NT4ClientConnectionState;
287
+ /**
288
+ * Returns current network latency in microseconds if known.
289
+ */
290
+ get networkLatencyMicroseconds(): number | null;
291
+ /**
292
+ * Returns server time in microseconds that is current or
293
+ * based on the specified client (local) time.
294
+ *
295
+ * Returns `null` if the server time offset is not established.
296
+ */
297
+ serverTimeMicroseconds(clientTimeMicroseconds?: number): number | null;
298
+ /**
299
+ * Initiates the connection.
300
+ * Once established the connection will be automatically kept alive by reconnecting.
301
+ */
302
+ connect(): void;
303
+ /**
304
+ * Terminates the connection.
305
+ * The client can be reconnected manually by calling {@link connect} again.
306
+ */
307
+ disconnect(): void;
308
+ /**
309
+ * Creates a new subscription.
310
+ *
311
+ * The subscription can be created regardless of connectivity state.
312
+ * All client subscriptions are automatically restored once connection
313
+ * has been established. This is also true for reconnects following
314
+ * loss of connectivity or deliberate disconnects.
315
+ *
316
+ * Caller can {@link unsubscribe} by passing the return value of this call or
317
+ * invoke {@link unsubscribeAll} to remove all existing client subscriptions.
318
+ *
319
+ * @param topics topics or prefixes to include in the subscription
320
+ * @param options subscription options
321
+ * @returns A subscription instance
322
+ */
323
+ subscribe(topics: Iterable<string>, options: SubscriptionOptions): NT4Subscription;
324
+ /**
325
+ * Unsubscribes an existing subscription.
326
+ *
327
+ * @param arg subscription or subscription identifier
328
+ * @returns `true` if the subscription was successfully unsubscribed, `false` if the subscription was not found
329
+ */
330
+ unsubscribe(arg: NT4Subscription | number): boolean;
331
+ /**
332
+ * Unsubscribes all existing subscriptions.
333
+ */
334
+ unsubscribeAll(): void;
335
+ /**
336
+ * Publishes a topic with the specified name and type.
337
+ *
338
+ * This method registers this client as a publisher for the topic.
339
+ * It should be called before {@link setValue} or {@link setValueAt}
340
+ * can be used.
341
+ *
342
+ * Publishing a topic on a disconnected client is allowed.
343
+ * Client will automatically register such publishers once the connection
344
+ * has been established and send default or retained value as appropriate.
345
+ *
346
+ * @param topic topic name
347
+ * @param type data type
348
+ * @param properties properties
349
+ */
350
+ publishTopic(topic: string, type: DataType | string, properties?: TopicProperties): NT4Topic;
351
+ /**
352
+ * Un-publishes previously published topic.
353
+ *
354
+ * @param topic topic name
355
+ * @returns `true` if the topic was successfully unpublished, `false` if the topic was not published
356
+ */
357
+ unpublishTopic(topic: string): boolean;
358
+ /**
359
+ * Determines whether client already publishes the topic.
360
+ *
361
+ * @param topic topic name
362
+ */
363
+ isTopicPublished(topic: string): boolean;
364
+ /**
365
+ * Sets properties for the specified topic.
366
+ *
367
+ * @param topic topic name
368
+ * @param properties new properties
369
+ */
370
+ setTopicProperties(topic: string, properties: TopicProperties): void;
371
+ /**
372
+ * Sets persistent flag for the topic.
373
+ *
374
+ * @param topic topic name
375
+ * @param value value to set
376
+ */
377
+ setTopicPersistent(topic: string, value: boolean): void;
378
+ /**
379
+ * Sets retained flag for the topic.
380
+ *
381
+ * @param topic topic name
382
+ * @param value value to set
383
+ */
384
+ setTopicRetained(topic: string, value: boolean): void;
385
+ /**
386
+ * Sends a value to the published topic using the current time.
387
+ *
388
+ * This client must publish the topic with {@link publishTopic} first.
389
+ *
390
+ * @param topic topic name
391
+ * @param value value to send
392
+ */
393
+ setValue(topic: string, value: unknown): void;
394
+ /**
395
+ * Sends a default ("weak") value to the published topic.
396
+ *
397
+ * This client must publish the topic with {@link publishTopic} first.
398
+ *
399
+ * @param topic topic name
400
+ * @param value value to send
401
+ */
402
+ setDefaultValue(topic: string, value: unknown): void;
403
+ /**
404
+ * Sends a value to the published topic using the specified timestamp.
405
+ *
406
+ * This client must publish the topic with {@link publishTopic} first.
407
+ *
408
+ * @param topic topic name
409
+ * @param value value to send
410
+ * @param timestamp timestamp
411
+ */
412
+ setValueAt(topic: string, value: unknown, timestamp: number): void;
413
+ /**
414
+ * Returns current ping timeout.
415
+ *
416
+ * @returns A value in milliseconds or 0 if the timeout has not been set yet.
417
+ */
418
+ get pingTimeoutMilliseconds(): number;
419
+ /**
420
+ * Sets ping timeout.
421
+ *
422
+ * @param value Timeout in milliseconds or `undefined` to use default protocol timeout value.
423
+ */
424
+ setPingTimeoutMilliseconds(value: number | undefined): void;
425
+ }
426
+ //#endregion
427
+ export { NT4Client, NT4ClientConnectionState, NT4ClientOptions, NT4Subscription, NT4Topic, SubscriptionOptions, TopicProperties };
428
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/NT4Types.ts","../src/NT4Subscription.ts","../src/NT4Topic.ts","../src/NT4Spec.ts","../src/NT4Client.ts"],"sourcesContent":[],"mappings":";;AAKA;AA8BA;;;KA9BY,eAAA,GAAkB;ECFjB;;;;;;;;;ACGb;;;;;;;;ACiCA;AAkBA;;;;AC7CA,CAAA;AAEA;;;AAiC2B,KJZf,mBAAA,GAAsB,MIYP,CAAA,MAA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