@signalk/nmea0183-signalk
Version:
A node.js/javascript parser for NMEA0183 sentences. Sentences are parsed to Signal K format.
178 lines • 6.86 kB
JavaScript
;
/**
* Copyright 2016 Signal K and Fabian Tollenaar <fabian@signalk.org>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Parser = void 0;
const debug_1 = __importDefault(require("debug"));
const utils = __importStar(require("@signalk/nmea0183-utilities"));
const getTagBlock_1 = __importDefault(require("./getTagBlock"));
const transformSource_1 = __importDefault(require("./transformSource"));
const hooks_1 = __importDefault(require("../hooks"));
// package.json loaded via resolveJsonModule. Using a typed local shape avoids
// leaking the full json schema into the Parser class surface.
const package_json_1 = __importDefault(require("../../package.json"));
const debug = (0, debug_1.default)('signalk-parser-nmea0183');
class Parser {
options;
session;
name;
version;
author;
license;
hooks;
constructor(opts) {
if (opts === null) {
throw new TypeError('Parser options must not be null; pass undefined or an options object');
}
// Fall back to an empty options object when the caller passes a
// non-object sentinel (legacy behaviour covered by info.js tests).
const resolved = typeof opts === 'object' && opts !== undefined
? opts
: {};
this.options = resolved;
if (!Object.keys(this.options).includes('validateChecksum')) {
this.options.validateChecksum = true;
}
this.session = {};
this.name = package_json_1.default.name;
this.version = package_json_1.default.version;
this.author = package_json_1.default.author;
this.license = package_json_1.default.license;
this.hooks = { ...hooks_1.default };
if (resolved.onPropertyValues) {
resolved.onPropertyValues('nmea0183sentenceParser', (propertyValues_) => {
if (propertyValues_ === undefined) {
return;
}
propertyValues_
.filter((v) => v)
.map((propValue) => propValue.value)
.filter(isValidSentenceParserEntry)
.forEach(({ sentence, parser }) => {
debug(`setting custom parser ${sentence}`);
this.hooks[sentence] = parser;
});
});
}
}
parse(sentence) {
const maybeTagBlock = (0, getTagBlock_1.default)(sentence);
let tags;
let body;
if (maybeTagBlock !== false) {
body = maybeTagBlock.sentence;
tags = maybeTagBlock.tags;
}
else {
body = sentence;
tags = {};
}
const valid = utils.valid(body, this.options.validateChecksum);
if (valid === false) {
throw new Error(`Sentence "${body.trim()}" is invalid`);
}
if (body.charCodeAt(body.length - 1) === 10) {
// in case there's a newline
body = body.substr(0, body.length - 1);
}
const data = body.split('*')[0];
const dataParts = data.split(',');
const header = dataParts[0];
let id = '';
let talker = '';
let internalId = '';
if (header.charAt(1).toUpperCase() === 'P') {
// proprietary sentence
id = header.substr(-3, header.length).toUpperCase();
talker = header.substr(1, 2).toUpperCase();
internalId = header.substr(1, header.length);
}
else {
id = header.substr(3, header.length).toUpperCase();
talker = header.substr(1, 2);
internalId = header.substr(3, 3).toUpperCase();
}
const split = dataParts.slice(1, dataParts.length);
if (typeof tags.source === 'undefined') {
tags.source = ':';
}
else {
tags.source = `${tags.source}:${id}`;
}
const hook = this.hooks[internalId];
if (typeof hook === 'function') {
const result = hook({
id,
sentence: body,
parts: split,
tags,
talker
}, this.session);
return (0, transformSource_1.default)(result ?? null, id, talker);
}
return null;
}
}
exports.Parser = Parser;
function isValidSentenceParserEntry(entry) {
const candidate = entry;
const isValid = candidate !== null &&
candidate !== undefined &&
typeof candidate.sentence === 'string' &&
typeof candidate.parser === 'function';
if (!isValid) {
console.error(`Invalid sentence parser entry:${JSON.stringify(entry)}`);
}
return isValid;
}
exports.default = Parser;
//# sourceMappingURL=index.js.map