Skip to content

Commit 5956101

Browse files
committed
fix(parser): Update regex to variations of RMC
1 parent 09ad277 commit 5956101

2 files changed

Lines changed: 61 additions & 12 deletions

File tree

src/index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const verifyChecksum = (data) => {
3333
/**
3434
* regex for GPRMC valid data
3535
*/
36-
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})$/;
36+
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})$/;
3737

3838
/**
3939
* regex for GPGGA valid data
@@ -161,28 +161,28 @@ const parseRmc = (raw) => {
161161
const r = gprmc.exec(raw);
162162
if (isValid(raw)) {
163163
data.type = r[1];
164-
data.datetime = moment(`${r[8]}${r[2]}+00:00`, 'DDMMYYHHmmss.SSSZZ').toDate();
164+
data.datetime = moment(`${r[11]}${r[2]}+00:00`, 'DDMMYYHHmmss.SSSZZ').toDate();
165165
data.loc = {
166166
geojson: {
167167
type: 'Point',
168168
coordinates: [
169-
degToDec(r[5]),
170-
degToDec(r[4])
169+
degToDec(r[7]),
170+
degToDec(r[5])
171171
]
172172
},
173173
dmm: {
174-
latitude: r[4],
175-
longitude: r[5]
174+
latitude: r[5],
175+
longitude: r[7]
176176
}
177177
};
178-
data.gps = r[3] === 'A';
178+
data.gps = r[4] === 'A';
179179
data.speed = {
180-
knots: r[6] ? parseFloat(r[6]) : null,
181-
kmh: r[6] ? knotsToKmh(r[6]) : null
180+
knots: r[9] ? parseFloat(r[9]) : null,
181+
kmh: r[9] ? knotsToKmh(r[9]) : null
182182
};
183-
data.track = r[7] ? r[7] : null;
184-
data.magneticVariation = r[9] === ',' ? null : r[9];
185-
data.mode = r[12] ? faaModes[r[12]] : null;
183+
data.track = r[10] ? r[10] : null;
184+
data.magneticVariation = r[12] === ',' ? null : r[12];
185+
data.mode = r[15] ? faaModes[r[15]] : null;
186186
data.valid = true;
187187
}
188188
return data;

test/test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,55 @@ describe('Nmea', () => {
5454
});
5555
});
5656

57+
describe('GPRMC alternative', () => {
58+
const data = '$GPRMC,192053.00,A,4212.63658,N,00844.30075,W,0.648,295.88,010616,,,A*76';
59+
const parser = nmea.parse(data);
60+
61+
it('should return the same value passed in the constructor', () => {
62+
expect(data).to.eql(parser.raw);
63+
});
64+
65+
it('should return true if checksum is valid', () => {
66+
expect(nmea.isValid(parser.raw)).to.be.true;
67+
});
68+
69+
it('should return true if datetime values is valid', () => {
70+
const datetime = moment.utc(parser.datetime);
71+
const date = datetime.format('DDMMYY');
72+
expect(date).to.eql('010616');
73+
const time = datetime.format('HHmmss');
74+
expect(time).to.eql('192053');
75+
});
76+
77+
it('should return true if position values is valid', () => {
78+
expect(parser.loc.geojson.type).to.eql('Point');
79+
expect(parser.loc.geojson.coordinates.length).to.eql(2);
80+
expect(parser.loc.dmm.latitude).to.eql('4212.63658,N');
81+
expect(parser.loc.dmm.longitude).to.eql('00844.30075,W');
82+
});
83+
84+
it('should return true if gps value is valid', () => {
85+
expect(parser.gps).to.be.true;
86+
});
87+
88+
it('should return true if speed value is valid', () => {
89+
expect(parser.speed.knots).to.eql(0.648);
90+
expect(parser.speed.kmh).to.eql(1.200096);
91+
});
92+
93+
it('should return true if track value is valid', () => {
94+
expect(parser.track).to.eql('295.88');
95+
});
96+
97+
it('should return true if magnetic variation value is valid', () => {
98+
expect(parser.magneticVariation).to.be.null;
99+
});
100+
101+
it('should return true if mode value is valid', () => {
102+
expect(parser.mode).to.eql('Autonomous');
103+
});
104+
});
105+
57106
describe('GPGGA', () => {
58107
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';
59108
const parser = nmea.parse(data);

0 commit comments

Comments
 (0)