Skip to content

Commit

Permalink
implement basic NMEA stream parsing
Browse files Browse the repository at this point in the history
take into account only GGA packets and export all locations as a single
track
  • Loading branch information
pirxpilot committed Sep 25, 2023
1 parent 8b74f73 commit 1577117
Show file tree
Hide file tree
Showing 6 changed files with 2,622 additions and 6 deletions.
30 changes: 29 additions & 1 deletion lib/import.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
const makeStream = require('./stream');

module.exports = importNmea;

function importNmea() {
/* global WritableStream */

/**
* Parses ASCII stream containing NMEA messages into Furkot's trip
* @param {ReadableStream} from
*/
async function importNmea(from) {
const stream = makeStream({ GGA: true });
const track = [];

await from.pipeThrough(stream).pipeTo(new WritableStream({
write: packet => track.push(toStop(packet))
}));

return {
track: [track]
};
}

function toStop({ longitude, latitude, time }) {
return {
coordinates: {
lon: longitude,
lat: latitude
},
timestamp: time.toISOString()
};
}
49 changes: 49 additions & 0 deletions lib/stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const nmea = require("nmea-simple");

module.exports = makeStream;

/* global TransformStream */

function makeStream(types) {
return new TransformStream(makeSource(types));
}

function makeSource(types) {
let prefix = '';
return {
transform,
flush
};

/**
* @param {String} chunk
* @param {TransformStreamDefaultController} controller
*/
function transform(chunk, controller) {
const lines = chunk.split(/[\n\r]+/);
if (prefix) {
lines[0] = prefix + lines[0];
}
prefix = lines.length > 0 ? lines.pop() : '';
for (const line of lines) {
processSentence(line, controller);
}
}

/**
* @param {TransformStreamDefaultController} controller
*/
function flush(controller) {
processSentence(prefix, controller);
}

function processSentence(line, controller) {
if (line[0] === '$') {
const packet = nmea.parseNmeaSentence(line);
const { sentenceId } = packet;
if (types[sentenceId]) {
controller.enqueue(packet);
}
}
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"nmea",
"furkot"
],
"dependencies": {},
"dependencies": {
"nmea-simple": "^3.3.0"
},
"devDependencies": {
"@pirxpilot/jshint": "^3.0.1"
},
Expand Down
Loading

0 comments on commit 1577117

Please sign in to comment.