Skip to content

Commit

Permalink
Fix parse whit gps status void. Add gps to parse data. Fix #6
Browse files Browse the repository at this point in the history
  • Loading branch information
lgaticaq committed Aug 28, 2015
1 parent c55dd4c commit 48aea87
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 52 deletions.
55 changes: 28 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ import nmea from "node-nmea"

const raw = "$GPRMC,161006.425,A,7855.6020,S,13843.8900,E,154.89,84.62,110715,173.1,W,A*30"
const data = nmea.parse(raw)
data.isValid() // true
data.valid // true
data.raw // '$GPRMC,161006.425,A,7855.6020,S,13843.8900,E,154.89,84.62,110715,173.1,W,A*30'
data.type // RMC
data.gps // true
data.datetime // Sat Jul 11 2015 13:10:06 GMT-0300 (CLT)
data.loc // { type: 'Point', coordinates: [ 138.73149999999998, -78.9267 ] }
data.speed // 286.85627999999997
Expand All @@ -59,19 +60,19 @@ data.mode // 'Autonomous'
import nmea from "node-nmea"

const raw = nmea.randomData()
const data = nmea.parse(raw)
data.isValid() // true
data.raw // '$GPRMC,161006.425,A,7855.6020,S,13843.8900,E,154.89,84.62,110715,173.1,W,A*30'
data.time // '161006.425'
data.gpsStatus // 'A'
data.latitude // '7855.6020,S'
data.longitude // '13843.8900,E'
data.speed // '154.89'
data.track // '84.62'
data.date // '110715'
data.magneticVariation // '173.1,W'
data.faa // 'A'
data.checkSum // '30'
raw.raw // '$GPRMC,161006.425,A,7855.6020,S,13843.8900,E,154.89,84.62,110715,173.1,W,A*30'
raw.time // '161006.425'
raw.gpsStatus // 'A'
raw.latitude // '7855.6020,S'
raw.longitude // '13843.8900,E'
raw.speed // '154.89'
raw.track // '84.62'
raw.date // '110715'
raw.magneticVariation // '173.1,W'
raw.faa // 'A'
raw.checkSum // '30'
const data = nmea.parse(raw.raw)
data.valid // true
```

## Random data with options
Expand All @@ -98,17 +99,17 @@ const opts = {
longitude: -70.52955843508244
}
const raw = nmea.randomData(opts)
const data = nmea.parse(raw)
data.isValid() // true
data.raw // '$GPRMC,171200.000,A,3321.1740,S,7031.7735,S,120.50,38.34,150715,0.8,E,A*0C'
data.time // '171200.000'
data.gpsStatus // 'A'
data.latitude // '3321.1740,S'
data.longitude // '7031.7735,S'
data.speed // '120.50'
data.track // '38.34'
data.date // '150715'
data.magneticVariation // '0.8,E'
data.faa // 'A'
data.checkSum // '0C'
raw.raw // '$GPRMC,171200.000,A,3321.1740,S,7031.7735,S,120.50,38.34,150715,0.8,E,A*0C'
raw.time // '171200.000'
raw.gpsStatus // 'A'
raw.latitude // '3321.1740,S'
raw.longitude // '7031.7735,S'
raw.speed // '120.50'
raw.track // '38.34'
raw.date // '150715'
raw.magneticVariation // '0.8,E'
raw.faa // 'A'
raw.checkSum // '0C'
const data = nmea.parse(raw.raw)
data.valid // true
```
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-nmea",
"version": "0.2.4",
"version": "0.3.0",
"description": "Parser for NMEA sentences.",
"main": "lib",
"scripts": {
Expand All @@ -27,10 +27,10 @@
},
"homepage": "https://github.com/lgaticaq/node-nmea#readme",
"devDependencies": {
"babel": "^5.8.21",
"babel": "^5.8.23",
"eslint": "^1.2.1",
"mocha": "^2.2.5",
"rimraf": "^2.4.2"
"rimraf": "^2.4.3"
},
"dependencies": {
"chance": "^0.7.6",
Expand Down
45 changes: 23 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ const params = {
gpsStatus: /[AV]/,
latitude: /\d{4}[.]\d{4}\,[NS]/,
longitude: /\d{5}[.]\d{4}\,[WE]/,
speed: /\d{1,3}[.]\d{1,3}/,
track: /\d{1,3}[.]\d{1,3}/,
speed: /(\d{1,3}[.]\d{1,3})?/,
track: /(\d{1,3}[.]\d{1,3})?/,
date: /\d{6}/,
magneticVariation: /(\d{1,3}[.]\d{1,3})?\,([WE])?/,
faa: /([ADENS])?/,
Expand Down Expand Up @@ -76,7 +76,7 @@ const gprmc = XRegExp.build(`(?x)^
*/
function isValid(data) {
const r = XRegExp.exec(data, gprmc)
return gprmc.test(data) && verifyChecksum(data) && r.gpsStatus === "A"
return gprmc.test(data) && verifyChecksum(data)
}

/**
Expand Down Expand Up @@ -140,7 +140,7 @@ function degToDec(data) {
* @return {number} km/h
*/
function knotsToKmh(knots) {
let kmh = 0.0
let kmh = null
if (knots) {
kmh = parseFloat(knots) * 1.852
}
Expand Down Expand Up @@ -182,29 +182,30 @@ const faaModes = {
* @return {object} data parse
*/
function parse(raw) {
let data = {raw: raw, valid: false}
const r = XRegExp.exec(raw, gprmc)
if (isValid(raw)) {
const r = XRegExp.exec(raw, gprmc)
const datetime = `${r.date} ${r.time} +00:00`
const track = r.track === "" ? null : r.track
const mv = r.magneticVariation === "," ? null : r.magneticVariation
return {
raw: raw,
type: r.type,
datetime: moment(datetime, "DDMMYY HHmmss.SSS ZZ").toDate(),
loc: {
type: "Point",
coordinates: [
degToDec(r.longitude),
degToDec(r.latitude)
]
},
speed: knotsToKmh(r.speed),
track: r.track,
magneticVariation: mv,
mode: r.faa ? faaModes[r.faa] : null
data.raw = raw
data.type = r.type
data.datetime = moment(datetime, "DDMMYY HHmmss.SSS ZZ").toDate()
data.loc = {
type: "Point",
coordinates: [
degToDec(r.longitude),
degToDec(r.latitude)
]
}
} else {
return null
data.gps = r.gpsStatus === "A"
data.speed = knotsToKmh(r.speed)
data.track = track
data.magneticVariation = mv
data.mode = r.faa ? faaModes[r.faa] : null,
data.valid = true
}
return data
}

/**
Expand Down

0 comments on commit 48aea87

Please sign in to comment.