Skip to content

Commit

Permalink
NMEA-PARSER: v2.1.1 added GGA metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
crisconru committed Jul 23, 2024
1 parent 17cf1d9 commit 3892bb2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/nmea-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@coremarine/nmea-parser",
"version": "2.1.0",
"version": "2.1.1",
"description": "Library to parse NMEA 0183 sentences",
"author": "CoreMarine",
"license": "MIT",
Expand Down
38 changes: 34 additions & 4 deletions packages/nmea-parser/src/nmea-metadata.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import { Float64, NMEASentence, Uint8 } from './types'
import { Float64, NMEASentence, Uint32, Uint8 } from './types'

const metadataGGA = (sentence: NMEASentence): NMEASentence => {
const getUTCPosition = /**
* Description placeholder
*
* @param {string} utcPosition hhmmss.ss where hh hours, mm minutes and ss.ss seconds
* @returns {Uint32 | null}
*/
(utcPosition: string): Uint32 | null => {
if (utcPosition.length !== 9) { return null }
if (isNaN(Number(utcPosition))) { return null }
const hours = Number(utcPosition.slice(0, 2))
const minutes = Number(utcPosition.slice(2, 4))
const seconds = Number(utcPosition.slice(4, 6))
const millis = Number(utcPosition.slice(7))

const date = new Date()
date.setHours(hours, minutes, seconds, millis)
return date.getTime()
}

const getLatitudeDegrees = (latitude: string, letter: string): Float64 => {
const [left, minutesRight] = latitude.split('.')
const degrees = left.slice(0, -2)
Expand Down Expand Up @@ -39,25 +58,36 @@ const metadataGGA = (sentence: NMEASentence): NMEASentence => {
}

sentence.payload.forEach((field, index) => {
// UTC Position
if (field.name === 'utc_position') {
const utcPosition = field.value as string
const timestamp = getUTCPosition(utcPosition)
if (timestamp !== null) {
sentence.payload[index].metadata = { timestamp }
sentence.metadata = { ...sentence.metadata, timestamp }
}
}
// Latitude
if (field.name === 'latitude') {
const latitude = field.value as string
const letter = sentence.payload[index + 1].value as string
const degrees = getLatitudeDegrees(latitude, letter)
sentence.metadata = { latitude: degrees }
sentence.payload[index].metadata = { degrees }
sentence.metadata = { ...sentence.metadata, latitude: degrees }
return
}
// Longitude
if (field.name === 'longitude') {
const longitude = field.value as string
const letter = sentence.payload[index + 1].value as string
const degrees = getLongitudeDegrees(longitude, letter)
sentence.metadata = { longitude: degrees }
sentence.payload[index].metadata = { degrees }
sentence.metadata = { ...sentence.metadata, longitude: degrees }
return
}
// Quality
if (field.name === 'quality') {
sentence.metadata = { quality: getQuality(field.value as Uint8) }
sentence.metadata = { ...sentence.metadata, quality: getQuality(field.value as Uint8) }
// return
}
// Rest
Expand Down
13 changes: 13 additions & 0 deletions packages/nmea-parser/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,16 @@ describe('Parser', () => {
})
})
})

test('GGA sentence', () => {
const sample = '$INGGA,132247.95,7118.690092,N,02215.039776,E,2,12,0.8,66.48,M,26.96,M,20.0,1006*56\r\n'
const parser = new Parser()
const output = parser.parseData(sample)
expect(output).toHaveLength(1)
const gga = output[0]
expect(gga).toHaveProperty('metadata')
expect(gga.metadata).toHaveProperty('timestamp')
expect(gga.metadata).toHaveProperty('latitude')
expect(gga.metadata).toHaveProperty('longitude')
expect(gga.metadata).toHaveProperty('quality')
})

0 comments on commit 3892bb2

Please sign in to comment.