Skip to content

Commit

Permalink
NMEA-PARSER: v1.7.0 update deps to isolate from validators like Valibot
Browse files Browse the repository at this point in the history
  • Loading branch information
crisconru committed Jul 9, 2024
1 parent a00f7d0 commit f599167
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 302 deletions.
9 changes: 5 additions & 4 deletions packages/nmea-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@coremarine/nmea-parser",
"version": "1.6.15",
"version": "1.7.0",
"description": "Library to parse NMEA 0183 sentences",
"author": "CoreMarine",
"license": "MIT",
Expand Down Expand Up @@ -50,12 +50,13 @@
"test:coverage": "vitest run --coverage"
},
"dependencies": {
"@gcornut/valibot-json-schema": "^0.0.28",
"@schemasjs/valibot-numbers": "^1.0.7",
"@gcornut/valibot-json-schema": "^0.31.0",
"@schemasjs/valibot-numbers": "^1.0.13",
"@schemasjs/validator": "^1.0.1",
"js-yaml": "4.1.0"
},
"peerDependencies": {
"valibot": ">=0.30.0"
"valibot": ">=0.31.0"
},
"devDependencies": {
"@types/js-yaml": "4.0.9"
Expand Down
194 changes: 97 additions & 97 deletions packages/nmea-parser/src/nmea.ts
Original file line number Diff line number Diff line change
@@ -1,165 +1,165 @@
export const PROTOCOLS = {
protocols: [
"protocols": [
{
protocol: 'NMEA',
version: '3.1',
standard: true,
sentences: [
"protocol": "NMEA",
"version": "3.1",
"standard": true,
"sentences": [
{
sentence: 'AAM',
description: 'Waypoint Arrival Alarm',
fields: [
"sentence": "AAM",
"description": "Waypoint Arrival Alarm",
"fields": [
{
name: 'status',
type: 'string',
note: 'BOOLEAN\n\nA = arrival circle entered\n\nV = arrival circle not passed'
"name": "status",
"type": "string",
"note": "BOOLEAN\n\nA = arrival circle entered\n\nV = arrival circle not passed"
},
{
name: 'status',
type: 'string',
note: 'BOOLEAN\n\nA = perpendicular passed at waypoint\n\nV = perpendicular not passed'
"name": "status",
"type": "string",
"note": "BOOLEAN\n\nA = perpendicular passed at waypoint\n\nV = perpendicular not passed"
},
{
name: 'arrival_circle_radius',
type: 'float32'
"name": "arrival_circle_radius",
"type": "float32"
},
{
name: 'radius_units',
type: 'string',
units: 'nautic miles'
"name": "radius_units",
"type": "string",
"units": "nautic miles"
},
{
name: 'waypoint_id',
type: 'string'
"name": "waypoint_id",
"type": "string"
}
]
},
{
sentence: 'GGA',
description: 'Global Positioning System Fix Data',
fields: [
"sentence": "GGA",
"description": "Global Positioning System Fix Data",
"fields": [
{
name: 'utc_position',
type: 'string',
units: 'ms'
"name": "utc_position",
"type": "string",
"units": "ms"
},
{
name: 'latitude',
type: 'string',
units: 'deg'
"name": "latitude",
"type": "string",
"units": "deg"
},
{
name: 'latitude_direction',
type: 'string',
note: 'N: North\n S: South'
"name": "latitude_direction",
"type": "string",
"note": "N: North\n S: South"
},
{
name: 'longitude',
type: 'string',
units: 'deg'
"name": "longitude",
"type": "string",
"units": "deg"
},
{
name: 'longitude_direction',
type: 'string',
note: 'E - East\n W - West'
"name": "longitude_direction",
"type": "string",
"note": "E - East\n W - West"
},
{
name: 'gps_quality',
type: 'int8',
note: '0: Fix not valid\n 1: GPS fix\n 2: Differential GPS fix (DGNSS), SBAS, OmniSTAR VBS, Beacon, RTX in GVBS mode\n 3: Not applicable\n 4: RTK Fixed, xFill\n 5: RTK Float, OmniSTAR XP/HP, Location RTK, RTX\n 6: INS Dead reckoning\n 7: Manual Input Mode\n 8: Simulator Mode'
"name": "gps_quality",
"type": "int8",
"note": "0: Fix not valid\n 1: GPS fix\n 2: Differential GPS fix (DGNSS), SBAS, OmniSTAR VBS, Beacon, RTX in GVBS mode\n 3: Not applicable\n 4: RTK Fixed, xFill\n 5: RTK Float, OmniSTAR XP/HP, Location RTK, RTX\n 6: INS Dead reckoning\n 7: Manual Input Mode\n 8: Simulator Mode"
},
{
name: 'satellites',
type: 'uint8'
"name": "satellites",
"type": "uint8"
},
{
name: 'hdop',
type: 'float64'
"name": "hdop",
"type": "float64"
},
{
name: 'altitude',
type: 'float64',
units: 'm',
note: 'Orthometric height Mean-Sea-Level (MSL reference)'
"name": "altitude",
"type": "float64",
"units": "m",
"note": "Orthometric height Mean-Sea-Level (MSL reference)"
},
{
name: 'altitude_units',
type: 'string',
units: 'm'
"name": "altitude_units",
"type": "string",
"units": "m"
},
{
name: 'geoid_separation',
type: 'float64',
units: 'm',
note: 'Geoidal Separation: the difference between the WGS-84 earth ellipsoid surface and mean-sea-level (geoid) surface, "-" = mean-sea-level surface below WGS-84 ellipsoid surface.'
"name": "geoid_separation",
"type": "float64",
"units": "m",
"note": "Geoidal Separation: the difference between the WGS-84 earth ellipsoid surface and mean-sea-level (geoid) surface, \"-\" = mean-sea-level surface below WGS-84 ellipsoid surface."
},
{
name: 'geoid_separation_units',
type: 'string',
units: 'm'
"name": "geoid_separation_units",
"type": "string",
"units": "m"
},
{
name: 'age_of_differential_gps_data',
type: 'uint32',
units: 'sec',
note: 'Time in seconds since last SC104 Type 1 or 9 update, null field when DGPS is not used300'
"name": "age_of_differential_gps_data",
"type": "uint32",
"units": "sec",
"note": "Time in seconds since last SC104 Type 1 or 9 update, null field when DGPS is not used300"
},
{
name: 'reference_station_id',
type: 'uint16',
note: 'Reference station ID, range 0000 to 4095. A null field when any reference station ID is selected and no corrections are received. See table below for a description of the field values.\n\n0002 CenterPoint or ViewPoint RTX\n\n0005 RangePoint RTX\n\n0006 FieldPoint RTX\n\n0100 VBS\n\n1000 HP\n\n1001 HP/XP (Orbits)\n\n1002 HP/G2 (Orbits)\n\n1008 XP (GPS)\n\n1012 G2 (GPS)\n\n1013 G2 (GPS/GLONASS)\n\n1014 G2 (GLONASS)\n\n1016 HP/XP (GPS)\n\n1020 HP/G2 (GPS)\n\n1021 HP/G2 (GPS/GLONASS)'
"name": "reference_station_id",
"type": "uint16",
"note": "Reference station ID, range 0000 to 4095. A null field when any reference station ID is selected and no corrections are received. See table below for a description of the field values.\n\n0002 CenterPoint or ViewPoint RTX\n\n0005 RangePoint RTX\n\n0006 FieldPoint RTX\n\n0100 VBS\n\n1000 HP\n\n1001 HP/XP (Orbits)\n\n1002 HP/G2 (Orbits)\n\n1008 XP (GPS)\n\n1012 G2 (GPS)\n\n1013 G2 (GPS/GLONASS)\n\n1014 G2 (GLONASS)\n\n1016 HP/XP (GPS)\n\n1020 HP/G2 (GPS)\n\n1021 HP/G2 (GPS/GLONASS)"
}
]
},
{
sentence: 'HDT',
description: 'Heading - True',
fields: [
"sentence": "HDT",
"description": "Heading - True",
"fields": [
{
name: 'heading',
type: 'float32',
note: 'Heading, degrees True'
"name": "heading",
"type": "float32",
"note": "Heading, degrees True"
},
{
name: 'true',
type: 'string',
note: 'T = True'
"name": "true",
"type": "string",
"note": "T = True"
}
]
},
{
sentence: 'ZDA',
description: 'Time & Date - UTC, day, month, year and local time zone',
fields: [
"sentence": "ZDA",
"description": "Time & Date - UTC, day, month, year and local time zone",
"fields": [
{
name: 'utc_time',
type: 'string',
note: 'UTC time (hours, minutes, seconds, may have fractional subseconds)'
"name": "utc_time",
"type": "string",
"note": "UTC time (hours, minutes, seconds, may have fractional subseconds)"
},
{
name: 'day',
type: 'int8',
note: 'Day, 01 to 31'
"name": "day",
"type": "int8",
"note": "Day, 01 to 31"
},
{
name: 'month',
type: 'int8',
note: 'Month, 01 to 12'
"name": "month",
"type": "int8",
"note": "Month, 01 to 12"
},
{
name: 'year',
type: 'int16',
note: 'Year (4 digits)'
"name": "year",
"type": "int16",
"note": "Year (4 digits)"
},
{
name: 'local_zone_hours',
type: 'int8',
note: 'Local zone description, 00 to +- 13 hours'
"name": "local_zone_hours",
"type": "int8",
"note": "Local zone description, 00 to +- 13 hours"
},
{
name: 'local_zone_minutes',
type: 'int8',
note: 'Local zone minutes description, 00 to 59, apply same sign as local hours'
"name": "local_zone_minutes",
"type": "int8",
"note": "Local zone minutes description, 00 to 59, apply same sign as local hours"
}
]
}
Expand Down
25 changes: 12 additions & 13 deletions packages/nmea-parser/src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import * as v from 'valibot'
import { END_FLAG, END_FLAG_LENGTH, MAX_CHARACTERS, NMEA_ID_LENGTH, START_FLAG, START_FLAG_LENGTH } from './constants'
import { BooleanSchema, NMEALikeSchema, ProtocolsInputSchema, StringSchema, UnsignedIntegerSchema } from './schemas'
import type { Data, FieldType, FieldUnknown, NMEAKnownSentence, NMEALike, NMEAParser, NMEAPreParsed, NMEASentence, NMEAUknownSentence, ParserSentences, ProtocolOutput, ProtocolsFile, ProtocolsInput, Sentence, StoredSentences } from './types'
import { PROTOCOLS } from './nmea'
import { getSentencesByProtocol, getStoreSentences, readProtocolsFile, readProtocolsString } from './protocols'
import { BooleanSchema, NMEALikeSchema, ProtocolsInputSchema, StringSchema, UnsignedIntegerSchema } from './schemas'
import { generateSentenceFromModel, getFakeSentence, getNMEAUnparsedSentence } from './sentences'
import type { Data, FieldType, FieldUnknown, NMEAKnownSentence, NMEALike, NMEAParser, NMEAPreParsed, NMEASentence, NMEAUknownSentence, ParserSentences, ProtocolOutput, ProtocolsFile, ProtocolsInput, Sentence, StoredSentences } from './types'
import { getTalker } from './utils'
import { PROTOCOLS } from './nmea'

export class Parser implements NMEAParser {
// Memory - Buffer
protected _memory: boolean = true
get memory (): typeof this._memory { return this._memory }
set memory (mem: boolean) { this._memory = v.parse(BooleanSchema, mem) }
set memory (mem: boolean) { this._memory = BooleanSchema.parse(mem) }
protected _buffer: string = ''
protected _bufferLength: number = MAX_CHARACTERS
get bufferLimit (): typeof this._bufferLength { return this._bufferLength }
set bufferLimit (limit: number) { this._bufferLength = v.parse(UnsignedIntegerSchema, limit) }
set bufferLimit (limit: number) { this._bufferLength = UnsignedIntegerSchema.parse(limit) }
// Sentences
protected _sentences: StoredSentences = new Map()
// get sentences() { return this._sentences }
Expand All @@ -28,7 +27,7 @@ export class Parser implements NMEAParser {
}

private readInternalProtocols (): void {
const parsed = v.parse(ProtocolsInputSchema, PROTOCOLS)
const parsed = ProtocolsInputSchema.parse(PROTOCOLS)
this.addProtocols(parsed)
}

Expand All @@ -44,7 +43,7 @@ export class Parser implements NMEAParser {
}

getSentence (id: string): Sentence {
if (!v.is(StringSchema, id) || id.length < NMEA_ID_LENGTH) { return null }
if (!StringSchema.is(id) || id.length < NMEA_ID_LENGTH) { return null }
const aux = this._sentences.get(id) ?? null
if (aux !== null) { return aux }
const [talk, sent] = [id.slice(0, id.length - NMEA_ID_LENGTH), id.slice(-NMEA_ID_LENGTH)]
Expand All @@ -55,7 +54,7 @@ export class Parser implements NMEAParser {
}

addProtocols (input: ProtocolsInput): void {
if (!v.is(ProtocolsInputSchema, input)) {
if (!ProtocolsInputSchema.is(input)) {
const error = 'Parser: invalid protocols to parse'
console.error(error)
console.error(input)
Expand All @@ -73,7 +72,7 @@ export class Parser implements NMEAParser {
}

getFakeSentenceByID (id: string): NMEALike | null {
if (!v.is(StringSchema, id) || id.length < NMEA_ID_LENGTH) { return null }
if (!StringSchema.is(id) || id.length < NMEA_ID_LENGTH) { return null }
const aux = this._sentences.get(id) ?? null
if (aux !== null) { return generateSentenceFromModel(aux) }
// const [_, sent] = [id.slice(0, id.length - NMEA_ID_LENGTH), id.slice(-NMEA_ID_LENGTH)]
Expand All @@ -85,7 +84,7 @@ export class Parser implements NMEAParser {
}

parseData (text: string): NMEASentence[] {
if (!v.is(StringSchema, text)) return []
if (!StringSchema.is(text)) return []
const data = (this.memory) ? this._buffer + text : text
return this.getFrames(data)
}
Expand Down Expand Up @@ -177,7 +176,7 @@ export class Parser implements NMEAParser {
if (this._sentences.has(preparsedSentence.sentence)) {
const sentence = this.getKnownFrame(preparsedSentence)
if (sentence !== null) return sentence
// Probably known sentence by talker ID
// Probably known sentence by talker ID
} else {
const sentence = this.getKnownTalkerFrame(preparsedSentence)
if (sentence !== null) return sentence
Expand Down Expand Up @@ -207,7 +206,7 @@ export class Parser implements NMEAParser {

const possibleFrame = text.slice(start, end + END_FLAG_LENGTH)

if (!v.is(NMEALikeSchema, possibleFrame)) {
if (!NMEALikeSchema.is(possibleFrame)) {
pivot = start + START_FLAG_LENGTH
continue
}
Expand Down
Loading

0 comments on commit f599167

Please sign in to comment.