nmea-web-serial 1.1.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,7 +12,6 @@ import { EventObject } from 'xstate';
12
12
  import { GGAPacket } from 'nmea-simple';
13
13
  import { GLLPacket } from 'nmea-simple';
14
14
  import { HDGPacket } from 'nmea-simple';
15
- import { HDMPacket } from 'nmea-simple';
16
15
  import { HDTPacket } from 'nmea-simple';
17
16
  import { MachineSnapshot } from 'xstate';
18
17
  import { MetaObject } from 'xstate';
@@ -28,61 +27,29 @@ import { Values } from 'xstate';
28
27
  import { VTGPacket } from 'nmea-simple';
29
28
  import { ZDAPacket } from 'nmea-simple';
30
29
 
31
- /**
32
- * Append the correct trailing "*xx" footer for an NMEA string and return the result.
33
- */
34
- export declare function appendChecksumFooter(sentenceWithoutChecksum: string): string;
35
-
36
- /**
37
- * Computes navigation data from stored packets using a fallback strategy.
38
- * Each navigation property (time, position, speed, heading, depth) is computed
39
- * with priority-based fallback to the best available data source.
40
- */
41
- export declare function computeNavigationData(packets: StoredPackets, externalVariation?: number): NavigationData;
42
-
43
- /**
44
- * Generate a checksum for an NMEA sentence without the trailing "*xx".
45
- */
46
- export declare function computeNmeaChecksum(sentenceWithoutChecksum: string): number;
47
-
48
- /**
49
- * Creates a navigation adapter function with the specified magnetic variation.
50
- * The adapter is a closure that captures the variation value.
51
- *
52
- * @param externalVariation - Optional magnetic variation for heading calculations.
53
- * @returns An adapter function that transforms packets into navigation data.
54
- */
55
- export declare function createNavigationAdapter(externalVariation?: number): (packets: StoredPackets) => NavigationData;
56
-
57
- /**
58
- * Configuration for creating a navigation-focused NMEA machine.
59
- * This is a convenience configuration that uses the navigation adapter.
60
- */
61
- export declare function createNavigationNmeaConfig(options?: {
62
- allowedSentenceIds?: readonly string[];
63
- externalVariation?: number;
64
- }): NmeaMachineConfig<NavigationData, StoredPackets>;
65
-
66
30
  /**
67
31
  * Convenience function to create a navigation-focused NMEA machine.
68
- * This is a pre-configured machine that computes navigation data from NMEA packets.
69
32
  *
70
- * @param options - Optional configuration for the navigation machine.
71
- * @param options.allowedSentenceIds - Optional array of allowed sentence IDs.
72
- * @param options.externalVariation - Optional magnetic variation for heading calculations.
73
- * @returns An NMEA machine configured for navigation data computation.
33
+ * This is a pre-configured machine that computes navigation data (position, time, speed, heading, depth)
34
+ * from NMEA packets. It uses the navigation adapter which automatically handles priority-based fallback
35
+ * between different sentence types.
74
36
  *
75
37
  * @example
76
38
  * ```typescript
77
- * const machine = createNavigationNmeaMachine({
78
- * externalVariation: 5.5,
79
- * });
39
+ * const machine = createNavigationNmeaMachine();
40
+ * const actor = createActor(machine);
41
+ * actor.start();
42
+ * actor.send({ type: 'CONNECT' });
80
43
  * ```
44
+ *
45
+ * @returns An XState state machine configured for navigation data computation.
46
+ *
47
+ * @remarks
48
+ * The returned machine is an XState state machine. Use it with XState's `createActor` to create
49
+ * an actor instance, or use the {@link NavigationNmeaClient} class for a simpler API that doesn't
50
+ * require direct XState knowledge.
81
51
  */
82
- export declare function createNavigationNmeaMachine(options?: {
83
- allowedSentenceIds?: readonly string[];
84
- externalVariation?: number;
85
- }): StateMachine<NmeaContext<NavigationData, StoredPackets>, {
52
+ export declare function createNavigationNmeaMachine(): StateMachine<NmeaContext<NavigationData, StoredPackets>, {
86
53
  type: "CONNECT";
87
54
  } | {
88
55
  type: "DISCONNECT";
@@ -608,18 +575,11 @@ readonly CONNECT: "connecting";
608
575
  };
609
576
  }>;
610
577
 
611
- /**
612
- * Generate the correct trailing "*xx" footer for an NMEA sentence.
613
- */
614
- export declare function createNmeaChecksumFooter(sentenceWithoutChecksum: string): string;
615
-
616
578
  /**
617
579
  * Factory function to create an NMEA state machine with a generic adapter pattern.
618
580
  *
619
- * @template TData - The type of computed/translated data stored in context.
620
- * @template TPackets - The type of stored packets (typically a record of sentence ID to packet).
621
- * @param config - Configuration for the machine including adapter function and initial values.
622
- * @returns An XState machine configured with the provided adapter and types.
581
+ * Creates an XState state machine that manages serial port connections, parses NMEA sentences,
582
+ * and transforms them into your application's data format using the provided adapter function.
623
583
  *
624
584
  * @example
625
585
  * ```typescript
@@ -629,9 +589,23 @@ export declare function createNmeaChecksumFooter(sentenceWithoutChecksum: string
629
589
  * initialData: { time: null, position: null },
630
590
  * initialPackets: {},
631
591
  * });
592
+ *
593
+ * const actor = createActor(machine);
594
+ * actor.start();
595
+ * actor.send({ type: 'CONNECT' });
632
596
  * ```
597
+ *
598
+ * @template TData - The type of computed/translated data stored in context.
599
+ * @template TPackets - The type of stored packets (typically a record of sentence ID to packet).
600
+ * @param config - Configuration for the machine including adapter function and initial values.
601
+ * @returns An XState state machine that can be used with `createActor` from XState.
602
+ *
603
+ * @remarks
604
+ * The returned machine is an XState state machine. Use it with XState's `createActor` to create
605
+ * an actor instance, or use the {@link NmeaClient} class for a simpler API that doesn't require
606
+ * direct XState knowledge.
633
607
  */
634
- export declare function createNmeaMachine<TData, TPackets extends Record<string, unknown>>(config: NmeaMachineConfig<TData, TPackets>): StateMachine<NmeaContext<TData, TPackets>, {
608
+ export declare function createNmeaMachine<TData, TPackets extends Record<string, PacketStub | undefined>>(config: NmeaMachineConfig<TData, TPackets>): StateMachine<NmeaContext<TData, TPackets>, {
635
609
  type: "CONNECT";
636
610
  } | {
637
611
  type: "DISCONNECT";
@@ -1159,74 +1133,26 @@ readonly CONNECT: "connecting";
1159
1133
 
1160
1134
  declare type CustomPackets = DBSPacket | DBKPacket | DPTPacket;
1161
1135
 
1162
- export declare interface DBKPacket extends PacketStub<typeof sentenceId_3> {
1136
+ declare interface DBKPacket extends PacketStub<typeof sentenceId_3> {
1163
1137
  depthFeet: number;
1164
1138
  depthMeters: number;
1165
1139
  depthFathoms: number;
1166
1140
  }
1167
1141
 
1168
- export declare interface DBSPacket extends PacketStub<typeof sentenceId_2> {
1142
+ declare interface DBSPacket extends PacketStub<typeof sentenceId_2> {
1169
1143
  depthFeet: number;
1170
1144
  depthMeters: number;
1171
1145
  depthFathoms: number;
1172
1146
  }
1173
1147
 
1174
- export declare interface DPTPacket extends PacketStub<typeof sentenceId> {
1148
+ declare interface DPTPacket extends PacketStub<typeof sentenceId> {
1175
1149
  depthMeters: number;
1176
1150
  offsetMeters: number;
1177
1151
  maximumRangeScale: number;
1178
1152
  }
1179
1153
 
1180
- export declare function encodeAltitude(alt: number): string;
1181
-
1182
- export declare function encodeAltitudeNoUnits(alt: number): string;
1183
-
1184
- export declare function encodeDate(date?: Date): string;
1185
-
1186
- export declare function encodeDegrees(degrees?: number): string;
1187
-
1188
- export declare function encodeFixed(value: number | undefined, decimalPlaces: number): string;
1189
-
1190
- export declare function encodeGeoidalSeperation(geoidalSep: number): string;
1191
-
1192
- export declare function encodeGeoidalSeperationNoUnits(geoidalSep: number): string;
1193
-
1194
- /**
1195
- * Encodes the latitude in the standard NMEA format "ddmm.mmmmmm".
1196
- *
1197
- * @param latitude Latitude in decimal degrees.
1198
- */
1199
- export declare function encodeLatitude(latitude?: number): string;
1200
-
1201
- /**
1202
- * Encodes the longitude in the standard NMEA format "dddmm.mmmmmm".
1203
- *
1204
- * @param longitude Longitude in decimal degrees.
1205
- */
1206
- export declare function encodeLongitude(longitude?: number): string;
1207
-
1208
- export declare function encodeTime(date?: Date): string;
1209
-
1210
- export declare function encodeValue(value?: unknown): string;
1211
-
1212
1154
  declare type ExtendedNmeaPacket = Packet | CustomPackets;
1213
1155
 
1214
- /**
1215
- * Initial navigation data state.
1216
- */
1217
- export declare const initialNavigationData: NavigationData;
1218
-
1219
- /**
1220
- * Initial stored packets state.
1221
- */
1222
- export declare const initialNavigationPackets: StoredPackets;
1223
-
1224
- /**
1225
- * Default sentence IDs used for navigation data computation.
1226
- * Includes standard NMEA sentences and custom depth sentences.
1227
- */
1228
- export declare const NAVIGATION_SENTENCE_IDS: readonly ["GGA", "RMC", "GLL", "VTG", "HDT", "HDG", "HDM", "DPT", "DBT", "DBS", "DBK", "ZDA"];
1229
-
1230
1156
  /**
1231
1157
  * Types for navigation data.
1232
1158
  */
@@ -1252,7 +1178,7 @@ export declare interface NavigationData {
1252
1178
  } | null;
1253
1179
  heading: {
1254
1180
  degreesTrue: number;
1255
- source: 'HDT' | 'HDG' | 'HDM' | 'COG' | null;
1181
+ source: 'HDT' | 'HDG' | 'COG' | null;
1256
1182
  isDerived: boolean;
1257
1183
  } | null;
1258
1184
  depth: {
@@ -1261,12 +1187,112 @@ export declare interface NavigationData {
1261
1187
  } | null;
1262
1188
  }
1263
1189
 
1190
+ /**
1191
+ * Simple client for navigation NMEA data.
1192
+ * Creates a machine and manages connection state automatically.
1193
+ *
1194
+ * @example
1195
+ * ```typescript
1196
+ * const client = new NavigationNmeaClient({
1197
+ * enableLogging: true,
1198
+ * onData: (navigation) => {
1199
+ * console.log('Position:', navigation.position);
1200
+ * },
1201
+ * });
1202
+ *
1203
+ * client.connect();
1204
+ * ```
1205
+ */
1206
+ export declare class NavigationNmeaClient extends NmeaClient<NavigationData, StoredPackets> {
1207
+ /**
1208
+ * Creates a new navigation NMEA client.
1209
+ *
1210
+ * @param options - Optional configuration and callbacks.
1211
+ */
1212
+ constructor(options?: NmeaClientOptions<NavigationData>);
1213
+ }
1214
+
1215
+ /**
1216
+ * Simple client for managing NMEA connections.
1217
+ * Abstracts away XState details while still exposing the machine for advanced use.
1218
+ */
1219
+ export declare class NmeaClient<TData, TPackets extends Record<string, PacketStub | undefined>> {
1220
+ private actor;
1221
+ private subscription;
1222
+ private options?;
1223
+ /**
1224
+ * Creates a new NMEA client.
1225
+ *
1226
+ * @param machine - The NMEA machine instance (created with createNmeaMachine).
1227
+ * @param options - Optional configuration and callbacks.
1228
+ */
1229
+ constructor(machine: ReturnType<typeof createNmeaMachine<TData, TPackets>>, options?: NmeaClientOptions<TData>);
1230
+ private setupSubscriptions;
1231
+ /**
1232
+ * Gets the current data.
1233
+ */
1234
+ get data(): TData;
1235
+ /**
1236
+ * Gets whether the connection is currently active.
1237
+ */
1238
+ get isConnected(): boolean;
1239
+ /**
1240
+ * Gets whether the connection is in progress.
1241
+ */
1242
+ get isConnecting(): boolean;
1243
+ /**
1244
+ * Gets the current error message, if any.
1245
+ */
1246
+ get error(): string | null;
1247
+ /**
1248
+ * Connects to the serial port.
1249
+ */
1250
+ connect(): void;
1251
+ /**
1252
+ * Disconnects from the serial port.
1253
+ */
1254
+ disconnect(): void;
1255
+ /**
1256
+ * Sets logging enabled/disabled.
1257
+ */
1258
+ setLogging(enabled: boolean): void;
1259
+ /**
1260
+ * Sets the baud rate (requires reconnection to take effect).
1261
+ */
1262
+ setBaudRate(baudRate: number): void;
1263
+ /**
1264
+ * Gets the underlying XState actor (for advanced users).
1265
+ * Allows access to full XState functionality.
1266
+ */
1267
+ get machine(): NmeaMachineActor<TData, TPackets>;
1268
+ /**
1269
+ * Disposes of the client and cleans up resources.
1270
+ */
1271
+ dispose(): void;
1272
+ }
1273
+
1274
+ /**
1275
+ * Configuration for the NMEA client.
1276
+ */
1277
+ export declare interface NmeaClientOptions<TData = unknown> {
1278
+ /** Whether to enable logging of parsed packets. */
1279
+ enableLogging?: boolean;
1280
+ /** Baud rate for serial communication. Default is 4800. */
1281
+ baudRate?: number;
1282
+ /** Callback function called when data is updated. */
1283
+ onData?: (data: TData) => void;
1284
+ /** Callback function called when connection state changes. */
1285
+ onStateChange?: (isConnected: boolean) => void;
1286
+ /** Callback function called when an error occurs. */
1287
+ onError?: (error: string) => void;
1288
+ }
1289
+
1264
1290
  /**
1265
1291
  * Generic context for the NMEA state machine.
1266
1292
  * @template TData - The type of computed/translated data.
1267
1293
  * @template TPackets - The type of stored packets.
1268
1294
  */
1269
- export declare interface NmeaContext<TData, TPackets extends Record<string, unknown>> {
1295
+ declare interface NmeaContext<TData, TPackets extends Record<string, PacketStub | undefined>> {
1270
1296
  /** The active Web Serial port connection, or null if disconnected. */
1271
1297
  port: SerialPort | null;
1272
1298
  /** Current error message, or null if no error. */
@@ -1284,7 +1310,7 @@ export declare interface NmeaContext<TData, TPackets extends Record<string, unkn
1284
1310
  /**
1285
1311
  * Events that the NMEA state machine can handle.
1286
1312
  */
1287
- export declare type NmeaEvent = {
1313
+ declare type NmeaEvent = {
1288
1314
  type: 'CONNECT';
1289
1315
  } | {
1290
1316
  type: 'DISCONNECT';
@@ -1314,14 +1340,14 @@ export declare type NmeaEvent = {
1314
1340
  * @template TData - The type of computed/translated data.
1315
1341
  * @template TPackets - The type of stored packets.
1316
1342
  */
1317
- export declare type NmeaMachineActor<TData, TPackets extends Record<string, unknown>> = ActorRefFrom<ReturnType<typeof createNmeaMachine<TData, TPackets>>>;
1343
+ export declare type NmeaMachineActor<TData, TPackets extends Record<string, PacketStub | undefined>> = ActorRefFrom<ReturnType<typeof createNmeaMachine<TData, TPackets>>>;
1318
1344
 
1319
1345
  /**
1320
1346
  * Configuration for creating an NMEA machine.
1321
1347
  * @template TData - The type of computed/translated data stored in context.
1322
1348
  * @template TPackets - The type of stored packets (typically a record of sentence ID to packet).
1323
1349
  */
1324
- export declare interface NmeaMachineConfig<TData, TPackets extends Record<string, unknown>> {
1350
+ export declare interface NmeaMachineConfig<TData, TPackets extends Record<string, PacketStub | undefined>> {
1325
1351
  /** Function that transforms stored packets into the computed data type. */
1326
1352
  adapter: (packets: TPackets) => TData;
1327
1353
  /** Optional list of sentence IDs to filter and store. If not provided, all parsed sentences are stored. */
@@ -1332,54 +1358,8 @@ export declare interface NmeaMachineConfig<TData, TPackets extends Record<string
1332
1358
  initialPackets: TPackets;
1333
1359
  }
1334
1360
 
1335
- export declare function padLeft(value: string | number, length: number, paddingCharacter: string): string;
1336
-
1337
- /**
1338
- * Parses coordinate given as "dddmm.mm", "ddmm.mm", "dmm.mm" or "mm.mm"
1339
- */
1340
- export declare function parseDmCoordinate(coordinate: string): number;
1341
-
1342
- /**
1343
- * Parse the given string to a float, returning 0 for an empty string.
1344
- */
1345
- export declare function parseFloatSafe(str: string): number;
1346
-
1347
- /**
1348
- * Parse the given string to a integer, returning 0 for an empty string.
1349
- */
1350
- export declare function parseIntSafe(i: string): number;
1351
-
1352
- /**
1353
- * Parses latitude given as "ddmm.mm", "dmm.mm" or "mm.mm" (assuming zero
1354
- * degrees) along with a given hemisphere of "N" or "S" into decimal degrees,
1355
- * where north is positive and south is negative.
1356
- */
1357
- export declare function parseLatitude(lat: string, hemi: string): number;
1358
-
1359
- /**
1360
- * Parses latitude given as "dddmm.mm", "ddmm.mm", "dmm.mm" or "mm.mm" (assuming
1361
- * zero degrees) along with a given hemisphere of "E" or "W" into decimal
1362
- * degrees, where east is positive and west is negative.
1363
- */
1364
- export declare function parseLongitude(lon: string, hemi: string): number;
1365
-
1366
1361
  export declare function parseNmeaSentence(sentence: string): ExtendedNmeaPacket;
1367
1362
 
1368
- /**
1369
- * Parse the given string to a float if possible, returning 0 for an undefined
1370
- * value and a string the the given string cannot be parsed.
1371
- */
1372
- export declare function parseNumberOrString(str?: string): number | string;
1373
-
1374
- /**
1375
- * Parses a UTC time and optionally a date and returns a Date
1376
- * object.
1377
- * @param {string} time Time the format "hhmmss" or "hhmmss.ss"
1378
- * @param {string=} date Optional date in format the ddmmyyyy or ddmmyy
1379
- * @returns {Date}
1380
- */
1381
- export declare function parseTime(time: string, date?: string, rmcCompatible?: boolean): Date;
1382
-
1383
1363
  export declare function parseUnsafeNmeaSentence(sentence: string): ExtendedNmeaPacket | UnknownPacket;
1384
1364
 
1385
1365
  declare const sentenceId: "DPT";
@@ -1391,14 +1371,13 @@ declare const sentenceId_3: "DBK";
1391
1371
  /**
1392
1372
  * Types for stored packets by sentence type.
1393
1373
  */
1394
- export declare interface StoredPackets extends Record<string, unknown> {
1374
+ export declare interface StoredPackets extends Record<string, PacketStub | undefined> {
1395
1375
  GGA?: GGAPacket;
1396
1376
  RMC?: RMCPacket;
1397
1377
  GLL?: GLLPacket;
1398
1378
  VTG?: VTGPacket;
1399
1379
  HDT?: HDTPacket;
1400
1380
  HDG?: HDGPacket;
1401
- HDM?: HDMPacket;
1402
1381
  DPT?: DPTPacket;
1403
1382
  DBT?: DBTPacket;
1404
1383
  DBS?: DBSPacket;
@@ -1406,16 +1385,6 @@ export declare interface StoredPackets extends Record<string, unknown> {
1406
1385
  ZDA?: ZDAPacket;
1407
1386
  }
1408
1387
 
1409
- /**
1410
- * Utility functions for NMEA encoding, decoding, and validation.
1411
- */
1412
- export declare function toHexString(v: number): string;
1413
-
1414
1388
  export declare type UnsafeAndCustomPackets = CustomPackets | UnknownPacket;
1415
1389
 
1416
- /**
1417
- * Checks that the given NMEA sentence has a valid checksum.
1418
- */
1419
- export declare function validNmeaChecksum(nmeaSentence: string): boolean;
1420
-
1421
1390
  export { }