extended-nmea
Version:
A TypeScript library for parsing NMEA0183-like sentences with support for custom and proprietary sentences.
66 lines • 2.36 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChecksumSentence = void 0;
const NmeaSentence_1 = require("./NmeaSentence");
const helpers_1 = require("../../helpers");
class ChecksumSentence extends NmeaSentence_1.NmeaSentence {
/**
* Whether or not this sentence contains a checksum value (a "*" and two hexadecimal characters at the end).
*/
get hasChecksum() {
return this.dataNoFixtures.charAt(this.dataNoFixtures.length - 3) === ChecksumSentence.ChecksumSeparator;
}
/**
* Returns all characters between "$" and "*" if there is one or between "$" and "<CR><LF>".
*/
get dataNoChecksum() {
if (this.hasChecksum)
return this.dataNoFixtures.substr(0, this.dataNoFixtures.length - 3);
else
return this.dataNoFixtures;
}
/**
* Returns the last two characters at the end of the sentence, excluding "<CR><LF>".
*/
get checksum() {
return this.dataNoFixtures.substr(this.dataNoFixtures.length - 2);
}
/**
* If this sentence has a checksum, returns whether it is valid or not. Otherwise returns true.
*/
get checksumValid() {
// MAYBE: return false if no checksum exists
if (!this.hasChecksum)
return true;
return this.checksum.toUpperCase() === helpers_1.Helpers.xorChecksum(this.dataNoChecksum);
}
/**
* Returns all fields (including the first field in the sentence) separated by comma. Excludes checksum.
*/
get fields() {
return this.dataNoChecksum.split(',');
}
/**
* Whether or not this sentence is a valid NMEA0183 talker sentence.
*
* This getter also validates the checksum, if there is one.
*/
get valid() {
return super.valid && this.checksumValid;
}
get invalidReason() {
if (!super.valid) {
return super.invalidReason;
}
if (!this.checksumValid) {
return 'The checksum is invalid.';
}
return null;
}
}
exports.ChecksumSentence = ChecksumSentence;
/**
* The optional checksum in a valid "talker sentence" is separated by a "*" character.
*/
ChecksumSentence.ChecksumSeparator = "*";
//# sourceMappingURL=ChecksumSentence.js.map