nmea-web-serial 1.1.2 → 1.1.4

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.
@@ -1,19 +1,12 @@
1
- import { ActionFunction } from 'xstate';
2
- import { ActorRef } from 'xstate';
3
1
  import { ActorRefFrom } from 'xstate';
4
2
  import { ActorRefFromLogic } from 'xstate';
5
- import { AnyActorRef } from 'xstate';
6
- import { AnyEventObject } from 'xstate';
7
3
  import { CallbackActorLogic } from 'xstate';
8
4
  import { DBTPacket } from 'nmea-simple';
9
- import { DoneActorEvent } from 'xstate';
10
- import { ErrorActorEvent } from 'xstate';
11
5
  import { EventObject } from 'xstate';
12
6
  import { GGAPacket } from 'nmea-simple';
13
7
  import { GLLPacket } from 'nmea-simple';
14
8
  import { HDGPacket } from 'nmea-simple';
15
9
  import { HDTPacket } from 'nmea-simple';
16
- import { MachineSnapshot } from 'xstate';
17
10
  import { MetaObject } from 'xstate';
18
11
  import { NonReducibleUnknown } from 'xstate';
19
12
  import { Packet } from 'nmea-simple';
@@ -21,910 +14,33 @@ import { PacketStub } from 'nmea-simple/dist/codecs/PacketStub';
21
14
  import { PromiseActorLogic } from 'xstate';
22
15
  import { RMCPacket } from 'nmea-simple';
23
16
  import { StateMachine } from 'xstate';
24
- import { StateValue } from 'xstate';
25
17
  import { UnknownPacket } from 'nmea-simple/dist/codecs/UnknownPacket';
26
- import { Values } from 'xstate';
27
18
  import { VTGPacket } from 'nmea-simple';
28
19
  import { ZDAPacket } from 'nmea-simple';
29
20
 
30
21
  /**
31
- * Simple client for navigation NMEA data.
32
- * Creates a machine and manages connection state automatically.
33
- *
34
- * @param options - Configuration options.
35
- * @returns A client instance for managing the NMEA connection.
36
- *
37
- * @example
38
- * ```typescript
39
- * const client = createNavigationNmeaClient({
40
- * enableLogging: true,
41
- * onData: (navigation) => {
42
- * console.log('Position:', navigation.position);
43
- * },
44
- * });
45
- *
46
- * client.connect();
47
- * ```
48
- */
49
- export declare function createNavigationNmeaClient(options?: NmeaClientOptions<NavigationData>): NmeaClient<NavigationData, StoredPackets>;
50
-
51
- /**
52
- * Convenience function to create a navigation-focused NMEA machine.
53
- * This is a pre-configured machine that computes navigation data from NMEA packets.
54
- *
55
- * @returns An NMEA machine configured for navigation data computation.
56
- *
57
- * @example
58
- * ```typescript
59
- * const machine = createNavigationNmeaMachine();
60
- * ```
61
- */
62
- export declare function createNavigationNmeaMachine(): StateMachine<NmeaContext<NavigationData, StoredPackets>, {
63
- type: "CONNECT";
64
- } | {
65
- type: "DISCONNECT";
66
- } | {
67
- type: "SERIAL.DATA";
68
- data: Packet;
69
- } | {
70
- type: "SERIAL.ERROR";
71
- error: string;
72
- } | {
73
- type: "FATAL_ERROR";
74
- error: string;
75
- } | {
76
- type: "SERIAL.DISCONNECTED";
77
- } | {
78
- type: "STOP";
79
- } | {
80
- type: "SET_LOGGING";
81
- enabled: boolean;
82
- } | {
83
- type: "SET_BAUD_RATE";
84
- baudRate: number;
85
- }, {
86
- [x: string]: ActorRefFromLogic<PromiseActorLogic<SerialPort, {
87
- baudRate: number;
88
- }, EventObject>> | ActorRefFromLogic<CallbackActorLogic<NmeaEvent, {
89
- port: SerialPort;
90
- }, EventObject>> | ActorRefFromLogic<PromiseActorLogic<void, {
91
- port: SerialPort | null;
92
- }, EventObject>> | undefined;
93
- }, Values< {
94
- connectToSerial: {
95
- src: "connectToSerial";
96
- logic: PromiseActorLogic<SerialPort, {
97
- baudRate: number;
98
- }, EventObject>;
99
- id: string | undefined;
100
- };
101
- readNmeaStream: {
102
- src: "readNmeaStream";
103
- logic: CallbackActorLogic<NmeaEvent, {
104
- port: SerialPort;
105
- }, EventObject>;
106
- id: string | undefined;
107
- };
108
- closePort: {
109
- src: "closePort";
110
- logic: PromiseActorLogic<void, {
111
- port: SerialPort | null;
112
- }, EventObject>;
113
- id: string | undefined;
114
- };
115
- }>, Values< {
116
- setPort: {
117
- type: "setPort";
118
- params: {
119
- port: SerialPort;
120
- };
121
- };
122
- setError: {
123
- type: "setError";
124
- params: {
125
- error: unknown;
126
- };
127
- };
128
- logParsedData: {
129
- type: "logParsedData";
130
- params: {
131
- packet: Packet;
132
- };
133
- };
134
- storePacket: {
135
- type: "storePacket";
136
- params: {
137
- packet: Packet;
138
- };
139
- };
140
- clearError: {
141
- type: "clearError";
142
- params: {
143
- error: string;
144
- };
145
- };
146
- setFatalError: {
147
- type: "setFatalError";
148
- params: {
149
- error: string;
150
- };
151
- };
152
- disconnect: {
153
- type: "disconnect";
154
- params: NonReducibleUnknown;
155
- };
156
- setLogging: {
157
- type: "setLogging";
158
- params: {
159
- enabled: boolean;
160
- };
161
- };
162
- setBaudRate: {
163
- type: "setBaudRate";
164
- params: {
165
- baudRate: number;
166
- };
167
- };
168
- }>, never, never, "error" | "disconnected" | "connecting" | "connected" | "disconnecting", string, NonReducibleUnknown, NonReducibleUnknown, EventObject, MetaObject, {
169
- readonly id: "nmea";
170
- readonly initial: "disconnected";
171
- readonly context: {
172
- readonly port: null;
173
- readonly error: null;
174
- readonly packets: StoredPackets;
175
- readonly data: NavigationData;
176
- readonly enableLogging: false;
177
- readonly baudRate: 4800;
178
- };
179
- readonly on: {
180
- readonly SET_LOGGING: {
181
- readonly actions: {
182
- readonly type: "setLogging";
183
- readonly params: ({ event }: {
184
- context: NmeaContext<NavigationData, StoredPackets>;
185
- event: {
186
- type: "SET_LOGGING";
187
- enabled: boolean;
188
- };
189
- }) => {
190
- enabled: boolean;
191
- };
192
- };
193
- };
194
- };
195
- readonly states: {
196
- readonly disconnected: {
197
- readonly on: {
198
- readonly CONNECT: "connecting";
199
- readonly SET_BAUD_RATE: {
200
- readonly actions: {
201
- readonly type: "setBaudRate";
202
- readonly params: ({ event }: {
203
- context: NmeaContext<NavigationData, StoredPackets>;
204
- event: {
205
- type: "SET_BAUD_RATE";
206
- baudRate: number;
207
- };
208
- }) => {
209
- baudRate: number;
210
- };
211
- };
212
- };
213
- };
214
- };
215
- readonly connecting: {
216
- readonly invoke: {
217
- readonly id: "connectToSerial";
218
- readonly src: "connectToSerial";
219
- readonly input: ({ context }: {
220
- context: NmeaContext<NavigationData, StoredPackets>;
221
- event: {
222
- type: "CONNECT";
223
- } | {
224
- type: "DISCONNECT";
225
- } | {
226
- type: "SERIAL.DATA";
227
- data: Packet;
228
- } | {
229
- type: "SERIAL.ERROR";
230
- error: string;
231
- } | {
232
- type: "FATAL_ERROR";
233
- error: string;
234
- } | {
235
- type: "SERIAL.DISCONNECTED";
236
- } | {
237
- type: "STOP";
238
- } | {
239
- type: "SET_LOGGING";
240
- enabled: boolean;
241
- } | {
242
- type: "SET_BAUD_RATE";
243
- baudRate: number;
244
- };
245
- self: ActorRef<MachineSnapshot<NmeaContext<NavigationData, StoredPackets>, {
246
- type: "CONNECT";
247
- } | {
248
- type: "DISCONNECT";
249
- } | {
250
- type: "SERIAL.DATA";
251
- data: Packet;
252
- } | {
253
- type: "SERIAL.ERROR";
254
- error: string;
255
- } | {
256
- type: "FATAL_ERROR";
257
- error: string;
258
- } | {
259
- type: "SERIAL.DISCONNECTED";
260
- } | {
261
- type: "STOP";
262
- } | {
263
- type: "SET_LOGGING";
264
- enabled: boolean;
265
- } | {
266
- type: "SET_BAUD_RATE";
267
- baudRate: number;
268
- }, Record<string, AnyActorRef>, StateValue, string, unknown, any, any>, {
269
- type: "CONNECT";
270
- } | {
271
- type: "DISCONNECT";
272
- } | {
273
- type: "SERIAL.DATA";
274
- data: Packet;
275
- } | {
276
- type: "SERIAL.ERROR";
277
- error: string;
278
- } | {
279
- type: "FATAL_ERROR";
280
- error: string;
281
- } | {
282
- type: "SERIAL.DISCONNECTED";
283
- } | {
284
- type: "STOP";
285
- } | {
286
- type: "SET_LOGGING";
287
- enabled: boolean;
288
- } | {
289
- type: "SET_BAUD_RATE";
290
- baudRate: number;
291
- }, AnyEventObject>;
292
- }) => {
293
- baudRate: number;
294
- };
295
- readonly onDone: {
296
- readonly target: "connected";
297
- readonly actions: {
298
- readonly type: "setPort";
299
- readonly params: ({ event }: {
300
- context: NmeaContext<NavigationData, StoredPackets>;
301
- event: DoneActorEvent<SerialPort, string>;
302
- }) => {
303
- port: SerialPort;
304
- };
305
- };
306
- };
307
- readonly onError: {
308
- readonly target: "error";
309
- readonly actions: {
310
- readonly type: "setError";
311
- readonly params: ({ event }: {
312
- context: NmeaContext<NavigationData, StoredPackets>;
313
- event: ErrorActorEvent<unknown, string>;
314
- }) => {
315
- error: unknown;
316
- };
317
- };
318
- };
319
- };
320
- };
321
- readonly connected: {
322
- readonly on: {
323
- readonly 'SERIAL.DATA': {
324
- readonly actions: readonly [{
325
- readonly type: "logParsedData";
326
- readonly params: ({ event }: {
327
- context: NmeaContext<NavigationData, StoredPackets>;
328
- event: {
329
- type: "SERIAL.DATA";
330
- data: Packet;
331
- };
332
- }) => {
333
- packet: Packet;
334
- };
335
- }, {
336
- readonly type: "storePacket";
337
- readonly params: ({ event }: {
338
- context: NmeaContext<NavigationData, StoredPackets>;
339
- event: {
340
- type: "SERIAL.DATA";
341
- data: Packet;
342
- };
343
- }) => {
344
- packet: Packet;
345
- };
346
- }];
347
- };
348
- readonly 'SERIAL.ERROR': {
349
- readonly actions: {
350
- readonly type: "clearError";
351
- readonly params: ({ event }: {
352
- context: NmeaContext<NavigationData, StoredPackets>;
353
- event: {
354
- type: "SERIAL.ERROR";
355
- error: string;
356
- };
357
- }) => {
358
- error: string;
359
- };
360
- };
361
- };
362
- readonly FATAL_ERROR: {
363
- readonly target: "error";
364
- readonly actions: {
365
- readonly type: "setFatalError";
366
- readonly params: ({ event }: {
367
- context: NmeaContext<NavigationData, StoredPackets>;
368
- event: {
369
- type: "FATAL_ERROR";
370
- error: string;
371
- };
372
- }) => {
373
- error: string;
374
- };
375
- };
376
- };
377
- readonly 'SERIAL.DISCONNECTED': {
378
- readonly target: "disconnecting";
379
- };
380
- readonly DISCONNECT: {
381
- readonly actions: ActionFunction<NmeaContext<NavigationData, StoredPackets>, {
382
- type: "DISCONNECT";
383
- }, {
384
- type: "CONNECT";
385
- } | {
386
- type: "DISCONNECT";
387
- } | {
388
- type: "SERIAL.DATA";
389
- data: Packet;
390
- } | {
391
- type: "SERIAL.ERROR";
392
- error: string;
393
- } | {
394
- type: "FATAL_ERROR";
395
- error: string;
396
- } | {
397
- type: "SERIAL.DISCONNECTED";
398
- } | {
399
- type: "STOP";
400
- } | {
401
- type: "SET_LOGGING";
402
- enabled: boolean;
403
- } | {
404
- type: "SET_BAUD_RATE";
405
- baudRate: number;
406
- }, undefined, never, never, never, never, never>;
407
- };
408
- };
409
- readonly invoke: {
410
- readonly id: "streamReader";
411
- readonly src: "readNmeaStream";
412
- readonly input: ({ context }: {
413
- context: NmeaContext<NavigationData, StoredPackets>;
414
- event: {
415
- type: "CONNECT";
416
- } | {
417
- type: "DISCONNECT";
418
- } | {
419
- type: "SERIAL.DATA";
420
- data: Packet;
421
- } | {
422
- type: "SERIAL.ERROR";
423
- error: string;
424
- } | {
425
- type: "FATAL_ERROR";
426
- error: string;
427
- } | {
428
- type: "SERIAL.DISCONNECTED";
429
- } | {
430
- type: "STOP";
431
- } | {
432
- type: "SET_LOGGING";
433
- enabled: boolean;
434
- } | {
435
- type: "SET_BAUD_RATE";
436
- baudRate: number;
437
- };
438
- self: ActorRef<MachineSnapshot<NmeaContext<NavigationData, StoredPackets>, {
439
- type: "CONNECT";
440
- } | {
441
- type: "DISCONNECT";
442
- } | {
443
- type: "SERIAL.DATA";
444
- data: Packet;
445
- } | {
446
- type: "SERIAL.ERROR";
447
- error: string;
448
- } | {
449
- type: "FATAL_ERROR";
450
- error: string;
451
- } | {
452
- type: "SERIAL.DISCONNECTED";
453
- } | {
454
- type: "STOP";
455
- } | {
456
- type: "SET_LOGGING";
457
- enabled: boolean;
458
- } | {
459
- type: "SET_BAUD_RATE";
460
- baudRate: number;
461
- }, Record<string, AnyActorRef>, StateValue, string, unknown, any, any>, {
462
- type: "CONNECT";
463
- } | {
464
- type: "DISCONNECT";
465
- } | {
466
- type: "SERIAL.DATA";
467
- data: Packet;
468
- } | {
469
- type: "SERIAL.ERROR";
470
- error: string;
471
- } | {
472
- type: "FATAL_ERROR";
473
- error: string;
474
- } | {
475
- type: "SERIAL.DISCONNECTED";
476
- } | {
477
- type: "STOP";
478
- } | {
479
- type: "SET_LOGGING";
480
- enabled: boolean;
481
- } | {
482
- type: "SET_BAUD_RATE";
483
- baudRate: number;
484
- }, AnyEventObject>;
485
- }) => {
486
- port: SerialPort;
487
- };
488
- };
489
- };
490
- readonly disconnecting: {
491
- readonly invoke: {
492
- readonly id: "closePort";
493
- readonly src: "closePort";
494
- readonly input: ({ context }: {
495
- context: NmeaContext<NavigationData, StoredPackets>;
496
- event: {
497
- type: "CONNECT";
498
- } | {
499
- type: "DISCONNECT";
500
- } | {
501
- type: "SERIAL.DATA";
502
- data: Packet;
503
- } | {
504
- type: "SERIAL.ERROR";
505
- error: string;
506
- } | {
507
- type: "FATAL_ERROR";
508
- error: string;
509
- } | {
510
- type: "SERIAL.DISCONNECTED";
511
- } | {
512
- type: "STOP";
513
- } | {
514
- type: "SET_LOGGING";
515
- enabled: boolean;
516
- } | {
517
- type: "SET_BAUD_RATE";
518
- baudRate: number;
519
- };
520
- self: ActorRef<MachineSnapshot<NmeaContext<NavigationData, StoredPackets>, {
521
- type: "CONNECT";
522
- } | {
523
- type: "DISCONNECT";
524
- } | {
525
- type: "SERIAL.DATA";
526
- data: Packet;
527
- } | {
528
- type: "SERIAL.ERROR";
529
- error: string;
530
- } | {
531
- type: "FATAL_ERROR";
532
- error: string;
533
- } | {
534
- type: "SERIAL.DISCONNECTED";
535
- } | {
536
- type: "STOP";
537
- } | {
538
- type: "SET_LOGGING";
539
- enabled: boolean;
540
- } | {
541
- type: "SET_BAUD_RATE";
542
- baudRate: number;
543
- }, Record<string, AnyActorRef>, StateValue, string, unknown, any, any>, {
544
- type: "CONNECT";
545
- } | {
546
- type: "DISCONNECT";
547
- } | {
548
- type: "SERIAL.DATA";
549
- data: Packet;
550
- } | {
551
- type: "SERIAL.ERROR";
552
- error: string;
553
- } | {
554
- type: "FATAL_ERROR";
555
- error: string;
556
- } | {
557
- type: "SERIAL.DISCONNECTED";
558
- } | {
559
- type: "STOP";
560
- } | {
561
- type: "SET_LOGGING";
562
- enabled: boolean;
563
- } | {
564
- type: "SET_BAUD_RATE";
565
- baudRate: number;
566
- }, AnyEventObject>;
567
- }) => {
568
- port: SerialPort | null;
569
- };
570
- readonly onDone: {
571
- readonly target: "disconnected";
572
- readonly actions: "disconnect";
573
- };
574
- readonly onError: {
575
- readonly target: "disconnected";
576
- readonly actions: "disconnect";
577
- };
578
- };
579
- };
580
- readonly error: {
581
- readonly on: {
582
- readonly CONNECT: "connecting";
583
- };
584
- };
585
- };
586
- }>;
587
-
588
- /**
589
- * Factory function to create an NMEA state machine with a generic adapter pattern.
590
- *
591
- * @template TData - The type of computed/translated data stored in context.
592
- * @template TPackets - The type of stored packets (typically a record of sentence ID to packet).
593
- * @param config - Configuration for the machine including adapter function and initial values.
594
- * @returns An XState machine configured with the provided adapter and types.
595
- *
596
- * @example
597
- * ```typescript
598
- * const machine = createNmeaMachine({
599
- * adapter: computeNavigationData,
600
- * allowedSentenceIds: ['GGA', 'RMC', 'VTG'],
601
- * initialData: { time: null, position: null },
602
- * initialPackets: {},
603
- * });
604
- * ```
605
- */
606
- export declare function createNmeaMachine<TData, TPackets extends Record<string, unknown>>(config: NmeaMachineConfig<TData, TPackets>): StateMachine<NmeaContext<TData, TPackets>, {
607
- type: "CONNECT";
608
- } | {
609
- type: "DISCONNECT";
610
- } | {
611
- type: "SERIAL.DATA";
612
- data: Packet;
613
- } | {
614
- type: "SERIAL.ERROR";
615
- error: string;
616
- } | {
617
- type: "FATAL_ERROR";
618
- error: string;
619
- } | {
620
- type: "SERIAL.DISCONNECTED";
621
- } | {
622
- type: "STOP";
623
- } | {
624
- type: "SET_LOGGING";
625
- enabled: boolean;
626
- } | {
627
- type: "SET_BAUD_RATE";
628
- baudRate: number;
629
- }, {
630
- [x: string]: ActorRefFromLogic<PromiseActorLogic<SerialPort, {
631
- baudRate: number;
632
- }, EventObject>> | ActorRefFromLogic<CallbackActorLogic<NmeaEvent, {
633
- port: SerialPort;
634
- }, EventObject>> | ActorRefFromLogic<PromiseActorLogic<void, {
635
- port: SerialPort | null;
636
- }, EventObject>> | undefined;
637
- }, Values< {
638
- connectToSerial: {
639
- src: "connectToSerial";
640
- logic: PromiseActorLogic<SerialPort, {
641
- baudRate: number;
642
- }, EventObject>;
643
- id: string | undefined;
644
- };
645
- readNmeaStream: {
646
- src: "readNmeaStream";
647
- logic: CallbackActorLogic<NmeaEvent, {
648
- port: SerialPort;
649
- }, EventObject>;
650
- id: string | undefined;
651
- };
652
- closePort: {
653
- src: "closePort";
654
- logic: PromiseActorLogic<void, {
655
- port: SerialPort | null;
656
- }, EventObject>;
657
- id: string | undefined;
658
- };
659
- }>, Values< {
660
- setPort: {
661
- type: "setPort";
662
- params: {
663
- port: SerialPort;
664
- };
665
- };
666
- setError: {
667
- type: "setError";
668
- params: {
669
- error: unknown;
670
- };
671
- };
672
- logParsedData: {
673
- type: "logParsedData";
674
- params: {
675
- packet: Packet;
676
- };
677
- };
678
- storePacket: {
679
- type: "storePacket";
680
- params: {
681
- packet: Packet;
682
- };
683
- };
684
- clearError: {
685
- type: "clearError";
686
- params: {
687
- error: string;
688
- };
689
- };
690
- setFatalError: {
691
- type: "setFatalError";
692
- params: {
693
- error: string;
694
- };
695
- };
696
- disconnect: {
697
- type: "disconnect";
698
- params: NonReducibleUnknown;
699
- };
700
- setLogging: {
701
- type: "setLogging";
702
- params: {
703
- enabled: boolean;
704
- };
705
- };
706
- setBaudRate: {
707
- type: "setBaudRate";
708
- params: {
709
- baudRate: number;
710
- };
711
- };
712
- }>, never, never, "error" | "disconnected" | "connecting" | "connected" | "disconnecting", string, NonReducibleUnknown, NonReducibleUnknown, EventObject, MetaObject, {
713
- readonly id: "nmea";
714
- readonly initial: "disconnected";
715
- readonly context: {
716
- readonly port: null;
717
- readonly error: null;
718
- readonly packets: TPackets;
719
- readonly data: TData;
720
- readonly enableLogging: false;
721
- readonly baudRate: 4800;
722
- };
723
- readonly on: {
724
- readonly SET_LOGGING: {
725
- readonly actions: {
726
- readonly type: "setLogging";
727
- readonly params: ({ event }: {
728
- context: NmeaContext<TData, TPackets>;
729
- event: {
730
- type: "SET_LOGGING";
731
- enabled: boolean;
732
- };
733
- }) => {
734
- enabled: boolean;
735
- };
736
- };
737
- };
738
- };
739
- readonly states: {
740
- readonly disconnected: {
741
- readonly on: {
742
- readonly CONNECT: "connecting";
743
- readonly SET_BAUD_RATE: {
744
- readonly actions: {
745
- readonly type: "setBaudRate";
746
- readonly params: ({ event }: {
747
- context: NmeaContext<TData, TPackets>;
748
- event: {
749
- type: "SET_BAUD_RATE";
750
- baudRate: number;
751
- };
752
- }) => {
753
- baudRate: number;
754
- };
755
- };
756
- };
757
- };
758
- };
759
- readonly connecting: {
760
- readonly invoke: {
761
- readonly id: "connectToSerial";
762
- readonly src: "connectToSerial";
763
- readonly input: ({ context }: {
764
- context: NmeaContext<TData, TPackets>;
765
- event: {
766
- type: "CONNECT";
767
- } | {
768
- type: "DISCONNECT";
769
- } | {
770
- type: "SERIAL.DATA";
771
- data: Packet;
772
- } | {
773
- type: "SERIAL.ERROR";
774
- error: string;
775
- } | {
776
- type: "FATAL_ERROR";
777
- error: string;
778
- } | {
779
- type: "SERIAL.DISCONNECTED";
780
- } | {
781
- type: "STOP";
782
- } | {
783
- type: "SET_LOGGING";
784
- enabled: boolean;
785
- } | {
786
- type: "SET_BAUD_RATE";
787
- baudRate: number;
788
- };
789
- self: ActorRef<MachineSnapshot<NmeaContext<TData, TPackets>, {
790
- type: "CONNECT";
791
- } | {
792
- type: "DISCONNECT";
793
- } | {
794
- type: "SERIAL.DATA";
795
- data: Packet;
796
- } | {
797
- type: "SERIAL.ERROR";
798
- error: string;
799
- } | {
800
- type: "FATAL_ERROR";
801
- error: string;
802
- } | {
803
- type: "SERIAL.DISCONNECTED";
804
- } | {
805
- type: "STOP";
806
- } | {
807
- type: "SET_LOGGING";
808
- enabled: boolean;
809
- } | {
810
- type: "SET_BAUD_RATE";
811
- baudRate: number;
812
- }, Record<string, AnyActorRef>, StateValue, string, unknown, any, any>, {
813
- type: "CONNECT";
814
- } | {
815
- type: "DISCONNECT";
816
- } | {
817
- type: "SERIAL.DATA";
818
- data: Packet;
819
- } | {
820
- type: "SERIAL.ERROR";
821
- error: string;
822
- } | {
823
- type: "FATAL_ERROR";
824
- error: string;
825
- } | {
826
- type: "SERIAL.DISCONNECTED";
827
- } | {
828
- type: "STOP";
829
- } | {
830
- type: "SET_LOGGING";
831
- enabled: boolean;
832
- } | {
833
- type: "SET_BAUD_RATE";
834
- baudRate: number;
835
- }, AnyEventObject>;
836
- }) => {
837
- baudRate: number;
838
- };
839
- readonly onDone: {
840
- readonly target: "connected";
841
- readonly actions: {
842
- readonly type: "setPort";
843
- readonly params: ({ event }: {
844
- context: NmeaContext<TData, TPackets>;
845
- event: DoneActorEvent<SerialPort, string>;
846
- }) => {
847
- port: SerialPort;
848
- };
849
- };
850
- };
851
- readonly onError: {
852
- readonly target: "error";
853
- readonly actions: {
854
- readonly type: "setError";
855
- readonly params: ({ event }: {
856
- context: NmeaContext<TData, TPackets>;
857
- event: ErrorActorEvent<unknown, string>;
858
- }) => {
859
- error: unknown;
860
- };
861
- };
862
- };
863
- };
864
- };
865
- readonly connected: {
866
- readonly on: {
867
- readonly 'SERIAL.DATA': {
868
- readonly actions: readonly [{
869
- readonly type: "logParsedData";
870
- readonly params: ({ event }: {
871
- context: NmeaContext<TData, TPackets>;
872
- event: {
873
- type: "SERIAL.DATA";
874
- data: Packet;
875
- };
876
- }) => {
877
- packet: Packet;
878
- };
879
- }, {
880
- readonly type: "storePacket";
881
- readonly params: ({ event }: {
882
- context: NmeaContext<TData, TPackets>;
883
- event: {
884
- type: "SERIAL.DATA";
885
- data: Packet;
886
- };
887
- }) => {
888
- packet: Packet;
889
- };
890
- }];
891
- };
892
- readonly 'SERIAL.ERROR': {
893
- readonly actions: {
894
- readonly type: "clearError";
895
- readonly params: ({ event }: {
896
- context: NmeaContext<TData, TPackets>;
897
- event: {
898
- type: "SERIAL.ERROR";
899
- error: string;
900
- };
901
- }) => {
902
- error: string;
903
- };
904
- };
905
- };
906
- readonly FATAL_ERROR: {
907
- readonly target: "error";
908
- readonly actions: {
909
- readonly type: "setFatalError";
910
- readonly params: ({ event }: {
911
- context: NmeaContext<TData, TPackets>;
912
- event: {
913
- type: "FATAL_ERROR";
914
- error: string;
915
- };
916
- }) => {
917
- error: string;
918
- };
919
- };
920
- };
921
- readonly 'SERIAL.DISCONNECTED': {
922
- readonly target: "disconnecting";
923
- };
924
- readonly DISCONNECT: {
925
- readonly actions: ActionFunction<NmeaContext<TData, TPackets>, {
926
- type: "DISCONNECT";
927
- }, {
22
+ * Convenience function to create a navigation-focused NMEA machine.
23
+ *
24
+ * This is a pre-configured machine that computes navigation data (position, time, speed, heading, depth)
25
+ * from NMEA packets. It uses the navigation adapter which automatically handles priority-based fallback
26
+ * between different sentence types.
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * const machine = createNavigationNmeaMachine();
31
+ * const actor = createActor(machine);
32
+ * actor.start();
33
+ * actor.send({ type: 'CONNECT' });
34
+ * ```
35
+ *
36
+ * @returns An XState state machine configured for navigation data computation.
37
+ *
38
+ * @remarks
39
+ * The returned machine is an XState state machine. Use it with XState's `createActor` to create
40
+ * an actor instance, or use the {@link NavigationNmeaClient} class for a simpler API that doesn't
41
+ * require direct XState knowledge.
42
+ */
43
+ export declare function createNavigationNmeaMachine(): StateMachine<NmeaContext<NavigationData, StoredPackets>, {
928
44
  type: "CONNECT";
929
45
  } | {
930
46
  type: "DISCONNECT";
@@ -947,97 +63,117 @@ enabled: boolean;
947
63
  } | {
948
64
  type: "SET_BAUD_RATE";
949
65
  baudRate: number;
950
- }, undefined, never, never, never, never, never>;
951
- };
952
- };
953
- readonly invoke: {
954
- readonly id: "streamReader";
955
- readonly src: "readNmeaStream";
956
- readonly input: ({ context }: {
957
- context: NmeaContext<TData, TPackets>;
958
- event: {
959
- type: "CONNECT";
960
- } | {
961
- type: "DISCONNECT";
962
- } | {
963
- type: "SERIAL.DATA";
964
- data: Packet;
965
- } | {
966
- type: "SERIAL.ERROR";
967
- error: string;
968
- } | {
969
- type: "FATAL_ERROR";
970
- error: string;
971
- } | {
972
- type: "SERIAL.DISCONNECTED";
973
- } | {
974
- type: "STOP";
975
- } | {
976
- type: "SET_LOGGING";
977
- enabled: boolean;
978
- } | {
979
- type: "SET_BAUD_RATE";
66
+ }, {
67
+ [x: string]: ActorRefFromLogic<PromiseActorLogic<SerialPort, {
980
68
  baudRate: number;
981
- };
982
- self: ActorRef<MachineSnapshot<NmeaContext<TData, TPackets>, {
983
- type: "CONNECT";
984
- } | {
985
- type: "DISCONNECT";
986
- } | {
987
- type: "SERIAL.DATA";
988
- data: Packet;
989
- } | {
990
- type: "SERIAL.ERROR";
991
- error: string;
992
- } | {
993
- type: "FATAL_ERROR";
994
- error: string;
995
- } | {
996
- type: "SERIAL.DISCONNECTED";
69
+ }, EventObject>> | ActorRefFromLogic<CallbackActorLogic<NmeaEvent, {
70
+ port: SerialPort;
71
+ }, EventObject>> | ActorRefFromLogic<PromiseActorLogic<void, {
72
+ port: SerialPort | null;
73
+ }, EventObject>> | undefined;
74
+ }, {
75
+ src: "connectToSerial";
76
+ logic: PromiseActorLogic<SerialPort, {
77
+ baudRate: number;
78
+ }, EventObject>;
79
+ id: string | undefined;
997
80
  } | {
998
- type: "STOP";
81
+ src: "readNmeaStream";
82
+ logic: CallbackActorLogic<NmeaEvent, {
83
+ port: SerialPort;
84
+ }, EventObject>;
85
+ id: string | undefined;
999
86
  } | {
1000
- type: "SET_LOGGING";
1001
- enabled: boolean;
87
+ src: "closePort";
88
+ logic: PromiseActorLogic<void, {
89
+ port: SerialPort | null;
90
+ }, EventObject>;
91
+ id: string | undefined;
92
+ }, {
93
+ type: "setPort";
94
+ params: {
95
+ port: SerialPort;
96
+ };
1002
97
  } | {
1003
- type: "SET_BAUD_RATE";
1004
- baudRate: number;
1005
- }, Record<string, AnyActorRef>, StateValue, string, unknown, any, any>, {
1006
- type: "CONNECT";
98
+ type: "setError";
99
+ params: {
100
+ error: unknown;
101
+ };
1007
102
  } | {
1008
- type: "DISCONNECT";
103
+ type: "logParsedData";
104
+ params: {
105
+ packet: Packet;
106
+ };
1009
107
  } | {
1010
- type: "SERIAL.DATA";
1011
- data: Packet;
108
+ type: "storePacket";
109
+ params: {
110
+ packet: Packet;
111
+ };
1012
112
  } | {
1013
- type: "SERIAL.ERROR";
113
+ type: "clearError";
114
+ params: {
1014
115
  error: string;
116
+ };
1015
117
  } | {
1016
- type: "FATAL_ERROR";
118
+ type: "setFatalError";
119
+ params: {
1017
120
  error: string;
121
+ };
1018
122
  } | {
1019
- type: "SERIAL.DISCONNECTED";
1020
- } | {
1021
- type: "STOP";
123
+ type: "disconnect";
124
+ params: NonReducibleUnknown;
1022
125
  } | {
1023
- type: "SET_LOGGING";
126
+ type: "setLogging";
127
+ params: {
1024
128
  enabled: boolean;
129
+ };
1025
130
  } | {
1026
- type: "SET_BAUD_RATE";
131
+ type: "setBaudRate";
132
+ params: {
1027
133
  baudRate: number;
1028
- }, AnyEventObject>;
1029
- }) => {
1030
- port: SerialPort;
1031
- };
1032
134
  };
135
+ }, never, never, "error" | "disconnected" | "connecting" | "connected" | "disconnecting", string, NonReducibleUnknown, NonReducibleUnknown, EventObject, MetaObject, {
136
+ id: "nmea";
137
+ states: {
138
+ readonly disconnected: {};
139
+ readonly connecting: {};
140
+ readonly connected: {};
141
+ readonly disconnecting: {};
142
+ readonly error: {};
1033
143
  };
1034
- readonly disconnecting: {
1035
- readonly invoke: {
1036
- readonly id: "closePort";
1037
- readonly src: "closePort";
1038
- readonly input: ({ context }: {
1039
- context: NmeaContext<TData, TPackets>;
1040
- event: {
144
+ }>;
145
+
146
+ /**
147
+ * Factory function to create an NMEA state machine with a generic adapter pattern.
148
+ *
149
+ * Creates an XState state machine that manages serial port connections, parses NMEA sentences,
150
+ * and transforms them into your application's data format using the provided adapter function.
151
+ *
152
+ * @example
153
+ * ```typescript
154
+ * const machine = createNmeaMachine({
155
+ * adapter: computeNavigationData,
156
+ * allowedSentenceIds: ['GGA', 'RMC', 'VTG'],
157
+ * initialData: { time: null, position: null },
158
+ * initialPackets: {},
159
+ * });
160
+ *
161
+ * const actor = createActor(machine);
162
+ * actor.start();
163
+ * actor.send({ type: 'CONNECT' });
164
+ * ```
165
+ *
166
+ * @template TData - The type of computed/translated data stored in context.
167
+ * @template TPackets - The type of stored packets (typically a record of sentence ID to packet).
168
+ * @param config - Configuration for the machine including adapter function and initial values.
169
+ * @returns An XState state machine that can be used with `createActor` from XState.
170
+ *
171
+ * @remarks
172
+ * The returned machine is an XState state machine. Use it with XState's `createActor` to create
173
+ * an actor instance, or use the {@link NmeaClient} class for a simpler API that doesn't require
174
+ * direct XState knowledge.
175
+ */
176
+ export declare function createNmeaMachine<TData, TPackets extends Record<string, PacketStub | undefined>>(config: NmeaMachineConfig<TData, TPackets>): StateMachine<NmeaContext<TData, TPackets>, {
1041
177
  type: "CONNECT";
1042
178
  } | {
1043
179
  type: "DISCONNECT";
@@ -1060,90 +196,101 @@ enabled: boolean;
1060
196
  } | {
1061
197
  type: "SET_BAUD_RATE";
1062
198
  baudRate: number;
1063
- };
1064
- self: ActorRef<MachineSnapshot<NmeaContext<TData, TPackets>, {
1065
- type: "CONNECT";
1066
- } | {
1067
- type: "DISCONNECT";
1068
- } | {
1069
- type: "SERIAL.DATA";
1070
- data: Packet;
1071
- } | {
1072
- type: "SERIAL.ERROR";
1073
- error: string;
1074
- } | {
1075
- type: "FATAL_ERROR";
1076
- error: string;
1077
- } | {
1078
- type: "SERIAL.DISCONNECTED";
199
+ }, {
200
+ [x: string]: ActorRefFromLogic<PromiseActorLogic<SerialPort, {
201
+ baudRate: number;
202
+ }, EventObject>> | ActorRefFromLogic<CallbackActorLogic<NmeaEvent, {
203
+ port: SerialPort;
204
+ }, EventObject>> | ActorRefFromLogic<PromiseActorLogic<void, {
205
+ port: SerialPort | null;
206
+ }, EventObject>> | undefined;
207
+ }, {
208
+ src: "connectToSerial";
209
+ logic: PromiseActorLogic<SerialPort, {
210
+ baudRate: number;
211
+ }, EventObject>;
212
+ id: string | undefined;
1079
213
  } | {
1080
- type: "STOP";
214
+ src: "readNmeaStream";
215
+ logic: CallbackActorLogic<NmeaEvent, {
216
+ port: SerialPort;
217
+ }, EventObject>;
218
+ id: string | undefined;
1081
219
  } | {
1082
- type: "SET_LOGGING";
1083
- enabled: boolean;
220
+ src: "closePort";
221
+ logic: PromiseActorLogic<void, {
222
+ port: SerialPort | null;
223
+ }, EventObject>;
224
+ id: string | undefined;
225
+ }, {
226
+ type: "setPort";
227
+ params: {
228
+ port: SerialPort;
229
+ };
1084
230
  } | {
1085
- type: "SET_BAUD_RATE";
1086
- baudRate: number;
1087
- }, Record<string, AnyActorRef>, StateValue, string, unknown, any, any>, {
1088
- type: "CONNECT";
231
+ type: "setError";
232
+ params: {
233
+ error: unknown;
234
+ };
1089
235
  } | {
1090
- type: "DISCONNECT";
236
+ type: "logParsedData";
237
+ params: {
238
+ packet: Packet;
239
+ };
1091
240
  } | {
1092
- type: "SERIAL.DATA";
1093
- data: Packet;
241
+ type: "storePacket";
242
+ params: {
243
+ packet: Packet;
244
+ };
1094
245
  } | {
1095
- type: "SERIAL.ERROR";
246
+ type: "clearError";
247
+ params: {
1096
248
  error: string;
249
+ };
1097
250
  } | {
1098
- type: "FATAL_ERROR";
251
+ type: "setFatalError";
252
+ params: {
1099
253
  error: string;
254
+ };
1100
255
  } | {
1101
- type: "SERIAL.DISCONNECTED";
1102
- } | {
1103
- type: "STOP";
256
+ type: "disconnect";
257
+ params: NonReducibleUnknown;
1104
258
  } | {
1105
- type: "SET_LOGGING";
259
+ type: "setLogging";
260
+ params: {
1106
261
  enabled: boolean;
262
+ };
1107
263
  } | {
1108
- type: "SET_BAUD_RATE";
264
+ type: "setBaudRate";
265
+ params: {
1109
266
  baudRate: number;
1110
- }, AnyEventObject>;
1111
- }) => {
1112
- port: SerialPort | null;
1113
- };
1114
- readonly onDone: {
1115
- readonly target: "disconnected";
1116
- readonly actions: "disconnect";
1117
- };
1118
- readonly onError: {
1119
- readonly target: "disconnected";
1120
- readonly actions: "disconnect";
1121
- };
1122
- };
1123
- };
1124
- readonly error: {
1125
- readonly on: {
1126
- readonly CONNECT: "connecting";
1127
- };
1128
267
  };
268
+ }, never, never, "error" | "disconnected" | "connecting" | "connected" | "disconnecting", string, NonReducibleUnknown, NonReducibleUnknown, EventObject, MetaObject, {
269
+ id: "nmea";
270
+ states: {
271
+ readonly disconnected: {};
272
+ readonly connecting: {};
273
+ readonly connected: {};
274
+ readonly disconnecting: {};
275
+ readonly error: {};
1129
276
  };
1130
277
  }>;
1131
278
 
1132
279
  declare type CustomPackets = DBSPacket | DBKPacket | DPTPacket;
1133
280
 
1134
- export declare interface DBKPacket extends PacketStub<typeof sentenceId_3> {
281
+ declare interface DBKPacket extends PacketStub<typeof sentenceId_3> {
1135
282
  depthFeet: number;
1136
283
  depthMeters: number;
1137
284
  depthFathoms: number;
1138
285
  }
1139
286
 
1140
- export declare interface DBSPacket extends PacketStub<typeof sentenceId_2> {
287
+ declare interface DBSPacket extends PacketStub<typeof sentenceId_2> {
1141
288
  depthFeet: number;
1142
289
  depthMeters: number;
1143
290
  depthFathoms: number;
1144
291
  }
1145
292
 
1146
- export declare interface DPTPacket extends PacketStub<typeof sentenceId> {
293
+ declare interface DPTPacket extends PacketStub<typeof sentenceId> {
1147
294
  depthMeters: number;
1148
295
  offsetMeters: number;
1149
296
  maximumRangeScale: number;
@@ -1185,11 +332,36 @@ export declare interface NavigationData {
1185
332
  } | null;
1186
333
  }
1187
334
 
335
+ /**
336
+ * Simple client for navigation NMEA data.
337
+ * Creates a machine and manages connection state automatically.
338
+ *
339
+ * @example
340
+ * ```typescript
341
+ * const client = new NavigationNmeaClient({
342
+ * enableLogging: true,
343
+ * onData: (navigation) => {
344
+ * console.log('Position:', navigation.position);
345
+ * },
346
+ * });
347
+ *
348
+ * client.connect();
349
+ * ```
350
+ */
351
+ export declare class NavigationNmeaClient extends NmeaClient<NavigationData, StoredPackets> {
352
+ /**
353
+ * Creates a new navigation NMEA client.
354
+ *
355
+ * @param options - Optional configuration and callbacks.
356
+ */
357
+ constructor(options?: NmeaClientOptions<NavigationData>);
358
+ }
359
+
1188
360
  /**
1189
361
  * Simple client for managing NMEA connections.
1190
362
  * Abstracts away XState details while still exposing the machine for advanced use.
1191
363
  */
1192
- export declare class NmeaClient<TData, TPackets extends Record<string, unknown>> {
364
+ export declare class NmeaClient<TData, TPackets extends Record<string, PacketStub | undefined>> {
1193
365
  private actor;
1194
366
  private subscription;
1195
367
  private options?;
@@ -1265,7 +437,7 @@ export declare interface NmeaClientOptions<TData = unknown> {
1265
437
  * @template TData - The type of computed/translated data.
1266
438
  * @template TPackets - The type of stored packets.
1267
439
  */
1268
- export declare interface NmeaContext<TData, TPackets extends Record<string, unknown>> {
440
+ declare interface NmeaContext<TData, TPackets extends Record<string, PacketStub | undefined>> {
1269
441
  /** The active Web Serial port connection, or null if disconnected. */
1270
442
  port: SerialPort | null;
1271
443
  /** Current error message, or null if no error. */
@@ -1283,7 +455,7 @@ export declare interface NmeaContext<TData, TPackets extends Record<string, unkn
1283
455
  /**
1284
456
  * Events that the NMEA state machine can handle.
1285
457
  */
1286
- export declare type NmeaEvent = {
458
+ declare type NmeaEvent = {
1287
459
  type: 'CONNECT';
1288
460
  } | {
1289
461
  type: 'DISCONNECT';
@@ -1313,14 +485,14 @@ export declare type NmeaEvent = {
1313
485
  * @template TData - The type of computed/translated data.
1314
486
  * @template TPackets - The type of stored packets.
1315
487
  */
1316
- export declare type NmeaMachineActor<TData, TPackets extends Record<string, unknown>> = ActorRefFrom<ReturnType<typeof createNmeaMachine<TData, TPackets>>>;
488
+ export declare type NmeaMachineActor<TData, TPackets extends Record<string, PacketStub | undefined>> = ActorRefFrom<ReturnType<typeof createNmeaMachine<TData, TPackets>>>;
1317
489
 
1318
490
  /**
1319
491
  * Configuration for creating an NMEA machine.
1320
492
  * @template TData - The type of computed/translated data stored in context.
1321
493
  * @template TPackets - The type of stored packets (typically a record of sentence ID to packet).
1322
494
  */
1323
- export declare interface NmeaMachineConfig<TData, TPackets extends Record<string, unknown>> {
495
+ export declare interface NmeaMachineConfig<TData, TPackets extends Record<string, PacketStub | undefined>> {
1324
496
  /** Function that transforms stored packets into the computed data type. */
1325
497
  adapter: (packets: TPackets) => TData;
1326
498
  /** Optional list of sentence IDs to filter and store. If not provided, all parsed sentences are stored. */
@@ -1331,8 +503,6 @@ export declare interface NmeaMachineConfig<TData, TPackets extends Record<string
1331
503
  initialPackets: TPackets;
1332
504
  }
1333
505
 
1334
- export { Packet }
1335
-
1336
506
  export declare function parseNmeaSentence(sentence: string): ExtendedNmeaPacket;
1337
507
 
1338
508
  export declare function parseUnsafeNmeaSentence(sentence: string): ExtendedNmeaPacket | UnknownPacket;
@@ -1346,7 +516,7 @@ declare const sentenceId_3: "DBK";
1346
516
  /**
1347
517
  * Types for stored packets by sentence type.
1348
518
  */
1349
- export declare interface StoredPackets extends Record<string, unknown> {
519
+ export declare interface StoredPackets extends Record<string, PacketStub | undefined> {
1350
520
  GGA?: GGAPacket;
1351
521
  RMC?: RMCPacket;
1352
522
  GLL?: GLLPacket;