UNPKG

@jbroll/nmea-simple

Version:

NMEA 0183 sentence parser and encoder

71 lines (70 loc) 3.16 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); require("should"); var PacketStub_1 = require("../codecs/PacketStub"); var helpers_1 = require("../helpers"); var index_1 = require("../index"); var logSentenceId = "_LOG"; var buttonPressSentenceId = "_BTN"; var CustomPacketFactory = /** @class */ (function (_super) { __extends(CustomPacketFactory, _super); function CustomPacketFactory() { return _super !== null && _super.apply(this, arguments) || this; } CustomPacketFactory.prototype.assembleCustomPacket = function (stub, fields) { if (stub.talkerId === "P") { if (stub.sentenceId === logSentenceId) { return __assign(__assign({}, PacketStub_1.initStubFields(stub, logSentenceId)), { logNum: parseInt(fields[1], 10), logMsg: fields[2] }); } else if (stub.sentenceId === buttonPressSentenceId) { return __assign(__assign({}, PacketStub_1.initStubFields(stub, buttonPressSentenceId)), { buttonId: parseInt(fields[1], 10), longPress: fields[2].charAt(0) === "L" }); } } return null; }; return CustomPacketFactory; }(index_1.DefaultPacketFactory)); var CUSTOM_PACKET_FACTORY = new CustomPacketFactory(); describe("CustomPackets", function () { it("Unknown throws", function () { (function () { index_1.parseGenericPacket(helpers_1.appendChecksumFooter("$--000,data1,data2"), CUSTOM_PACKET_FACTORY); }).should.throw("No known parser for sentence ID \"000\"."); }); it("Log", function () { var packet = index_1.parseGenericPacket(helpers_1.appendChecksumFooter("$P_LOG,5,everything is ok"), CUSTOM_PACKET_FACTORY); index_1.assertPacketIs(logSentenceId, packet); packet.logNum.should.equal(5); packet.logMsg.should.equal("everything is ok"); }); it("Btn", function () { var packet = index_1.parseGenericPacket(helpers_1.appendChecksumFooter("$P_BTN,0,L"), CUSTOM_PACKET_FACTORY); index_1.assertPacketIs(buttonPressSentenceId, packet); packet.buttonId.should.equal(0); packet.longPress.should.equal(true); }); });