From 5956101fb67ff14551c86eab209fa03641457ae1 Mon Sep 17 00:00:00 2001 From: Leonardo Gatica Date: Tue, 21 Jun 2016 12:20:36 -0400 Subject: [PATCH] fix(parser): Update regex to variations of RMC --- src/index.js | 24 ++++++++++++------------ test/test.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/src/index.js b/src/index.js index 4691aa34..47621e93 100644 --- a/src/index.js +++ b/src/index.js @@ -33,7 +33,7 @@ const verifyChecksum = (data) => { /** * regex for GPRMC valid data */ -const gprmc = /^\$GP(\w{3})\,(\d{6}[.]\d{3})\,([AV])\,(\d{4}[.]\d{4}\,[NS])\,(\d{5}[.]\d{4}\,[WE])\,(\d{1,3}[.]\d{1,3})?\,(\d{1,3}[.]\d{1,3})?\,(\d{6})\,((\d{1,3}[.]\d{1,3})?\,([WE])?)\,?([ADENS])?\*([0-9A-F]{2})$/; +const gprmc = /^\$GP(\w{3})\,(\d{6}([.]\d+)?)\,([AV])\,(\d{4}([.]\d+)?\,[NS])\,(\d{5}([.]\d+)?\,[WE])\,(\d{1,3}[.]\d{1,3})?\,(\d{1,3}[.]\d{1,3})?\,(\d{6})\,((\d{1,3}[.]\d{1,3})?\,([WE])?)\,?([ADENS])?\*([0-9A-F]{2})$/; /** * regex for GPGGA valid data @@ -161,28 +161,28 @@ const parseRmc = (raw) => { const r = gprmc.exec(raw); if (isValid(raw)) { data.type = r[1]; - data.datetime = moment(`${r[8]}${r[2]}+00:00`, 'DDMMYYHHmmss.SSSZZ').toDate(); + data.datetime = moment(`${r[11]}${r[2]}+00:00`, 'DDMMYYHHmmss.SSSZZ').toDate(); data.loc = { geojson: { type: 'Point', coordinates: [ - degToDec(r[5]), - degToDec(r[4]) + degToDec(r[7]), + degToDec(r[5]) ] }, dmm: { - latitude: r[4], - longitude: r[5] + latitude: r[5], + longitude: r[7] } }; - data.gps = r[3] === 'A'; + data.gps = r[4] === 'A'; data.speed = { - knots: r[6] ? parseFloat(r[6]) : null, - kmh: r[6] ? knotsToKmh(r[6]) : null + knots: r[9] ? parseFloat(r[9]) : null, + kmh: r[9] ? knotsToKmh(r[9]) : null }; - data.track = r[7] ? r[7] : null; - data.magneticVariation = r[9] === ',' ? null : r[9]; - data.mode = r[12] ? faaModes[r[12]] : null; + data.track = r[10] ? r[10] : null; + data.magneticVariation = r[12] === ',' ? null : r[12]; + data.mode = r[15] ? faaModes[r[15]] : null; data.valid = true; } return data; diff --git a/test/test.js b/test/test.js index 97d1ee97..e217165a 100644 --- a/test/test.js +++ b/test/test.js @@ -54,6 +54,55 @@ describe('Nmea', () => { }); }); + describe('GPRMC alternative', () => { + const data = '$GPRMC,192053.00,A,4212.63658,N,00844.30075,W,0.648,295.88,010616,,,A*76'; + const parser = nmea.parse(data); + + it('should return the same value passed in the constructor', () => { + expect(data).to.eql(parser.raw); + }); + + it('should return true if checksum is valid', () => { + expect(nmea.isValid(parser.raw)).to.be.true; + }); + + it('should return true if datetime values is valid', () => { + const datetime = moment.utc(parser.datetime); + const date = datetime.format('DDMMYY'); + expect(date).to.eql('010616'); + const time = datetime.format('HHmmss'); + expect(time).to.eql('192053'); + }); + + it('should return true if position values is valid', () => { + expect(parser.loc.geojson.type).to.eql('Point'); + expect(parser.loc.geojson.coordinates.length).to.eql(2); + expect(parser.loc.dmm.latitude).to.eql('4212.63658,N'); + expect(parser.loc.dmm.longitude).to.eql('00844.30075,W'); + }); + + it('should return true if gps value is valid', () => { + expect(parser.gps).to.be.true; + }); + + it('should return true if speed value is valid', () => { + expect(parser.speed.knots).to.eql(0.648); + expect(parser.speed.kmh).to.eql(1.200096); + }); + + it('should return true if track value is valid', () => { + expect(parser.track).to.eql('295.88'); + }); + + it('should return true if magnetic variation value is valid', () => { + expect(parser.magneticVariation).to.be.null; + }); + + it('should return true if mode value is valid', () => { + expect(parser.mode).to.eql('Autonomous'); + }); + }); + describe('GPGGA', () => { const data = '$GPGGA,172814.0,3723.46587704,N,12202.26957864,W,2,6,1.2,18.893,M,-25.669,M,2.0,0031*4F'; const parser = nmea.parse(data);