Skip to content

Commit

Permalink
fix(parser): Update regex to variations of RMC
Browse files Browse the repository at this point in the history
  • Loading branch information
lgaticaq committed Jun 21, 2016
1 parent 09ad277 commit 5956101
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
49 changes: 49 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5956101

Please sign in to comment.