UNPKG

extended-nmea

Version:

A TypeScript library for parsing NMEA0183-like sentences with support for custom and proprietary sentences.

68 lines 2.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Decoder = void 0; const QuerySentence_1 = require("./types/sentences/QuerySentence"); const Sentence = require("./sentences"); class Decoder { static register(id, sentence) { Decoder.TalkerCodecs.set(id, sentence); } static unregister(id) { return Decoder.TalkerCodecs.delete(id); } static registerProprietary(manufacturerId, sentence) { Decoder.ProprietaryCodecs.set(manufacturerId, sentence); } static unregisterProprietary(manufacturerId) { return Decoder.ProprietaryCodecs.delete(manufacturerId); } static decode(data) { if (typeof data !== 'string') throw new Error(`Unable to decode sentence: invalid data type: ${typeof data}. Only strings are supported.`); // check proprietary sentences first to prevent false positives if (data.length > 1 && data[1] === 'P') { return this.decodeProprietary(data); } if (data.length > 5 && data[5] === 'Q') { return this.decodeQuery(data); } return this.decodeTalker(data); } static decodeQuery(data) { return new QuerySentence_1.QuerySentence(data); } static decodeProprietary(data) { const manufacturerId = data.substr(2, data.indexOf(',') - 2); if (!Decoder.ProprietaryCodecs.has(manufacturerId)) throw new Error(`Unable to decode sentence: unknown manufacturer id for proprietary sentence: ${manufacturerId}`); const sentenceConstructor = Decoder.ProprietaryCodecs.get(manufacturerId); return new sentenceConstructor(data, manufacturerId); } static decodeTalker(data) { if (data.length < 6) throw new Error(`Unable to decode sentence: invalid format. Expected at least 6 characters, got: ${data} (${data.length} characters)`); const talkerIdLength = 2; const sentenceId = data.substr(talkerIdLength + 1, data.indexOf(',') - talkerIdLength - 1); if (!Decoder.TalkerCodecs.has(sentenceId)) throw new Error(`Unable to decode sentence: unknown sentence id: ${sentenceId}`); const sentenceConstructor = Decoder.TalkerCodecs.get(sentenceId); return new sentenceConstructor(data, talkerIdLength); } } exports.Decoder = Decoder; Decoder.TalkerCodecs = new Map([ [Sentence.GGA.ID, Sentence.GGA], [Sentence.ROT.ID, Sentence.ROT], [Sentence.HDT.ID, Sentence.HDT], [Sentence.RMC.ID, Sentence.RMC], [Sentence.VTG.ID, Sentence.VTG], [Sentence.GSA.ID, Sentence.GSA], [Sentence.GSV.ID, Sentence.GSV], [Sentence.RSA.ID, Sentence.RSA], [Sentence.DPT.ID, Sentence.DPT], [Sentence.MTW.ID, Sentence.MTW], [Sentence.GLL.ID, Sentence.GLL], [Sentence.DTM.ID, Sentence.DTM], ]); Decoder.ProprietaryCodecs = new Map(); //# sourceMappingURL=decoder.js.map