@mos-connection/helper
Version:
Helper functions for the MOS-connection library
50 lines • 1.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MosMessage = void 0;
const model_1 = require("@mos-connection/model");
const XMLBuilder = require("xmlbuilder");
const Utils_1 = require("../utils/Utils");
class MosMessage {
constructor(port, strict) {
this.port = port;
this.strict = strict;
this.mosID = undefined;
this.ncsID = undefined;
this._messageID = 0;
this.mosTypes = (0, model_1.getMosTypes)(strict);
}
static getNewMessageID() {
// increments and returns a signed 32-bit int counting from 1, resetting to 1 when wrapping
MosMessage._staticMessageID++;
if (MosMessage._staticMessageID >= MosMessage.MAX_MESSAGE_ID)
MosMessage._staticMessageID = 1;
return MosMessage._staticMessageID;
}
/** */
prepare(messageID) {
if (!this.mosID)
throw new Error(`Can't prepare message: mosID missing`);
if (!this.ncsID)
throw new Error(`Can't prepare message: ncsID missing`);
this._messageID = messageID ?? MosMessage.getNewMessageID();
}
/** */
get messageID() {
return this._messageID;
}
/** */
toString() {
const xml = XMLBuilder.create('mos', undefined, undefined, {
headless: true,
});
(0, Utils_1.addTextElementInternal)(xml, 'ncsID', this.ncsID, undefined, this.strict);
(0, Utils_1.addTextElementInternal)(xml, 'mosID', this.mosID, undefined, this.strict);
(0, Utils_1.addTextElementInternal)(xml, 'messageID', this.messageID, undefined, this.strict);
xml.importDocument(this.messageXMLBlocks);
return xml.end({ pretty: true });
}
}
exports.MosMessage = MosMessage;
MosMessage.MAX_MESSAGE_ID = Math.pow(2, 31) - 2;
MosMessage._staticMessageID = 1;
//# sourceMappingURL=MosMessage.js.map