UNPKG

dlms-protocol

Version:

DLMS Protocol parser for JavaScript

76 lines (75 loc) 2.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; require("core-js/modules/web.dom-collections.iterator.js"); var _DlmsDataType = _interopRequireDefault(require("../enum/DlmsDataType")); var _DlmsDatas = _interopRequireDefault(require("./DlmsDatas")); var _DlmsArrayStruct = _interopRequireDefault(require("./DlmsArrayStruct")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } class DlmsCompactArray { constructor() { let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; this.tag = _DlmsDataType.default.COMPACT_ARRAY; this.totalBytes = 0; this.rawValue = []; this.bodyBytes = []; this.value = []; if (data) { this.insertData(data); } } getStandardLength() { throw new Error("Method not implemented."); } getValue() { return this.value; } insertData(data) { if (this.tag !== data[0]) { throw new Error("Tag mismatch."); } const descriptor = _DlmsDatas.default.factory(data[1]); let isStruct = false; const typesDlms = []; let structLen = 0; if (descriptor instanceof _DlmsArrayStruct.default) { isStruct = true; structLen = data[2]; this.totalBytes = structLen + data[3 + structLen] + 4; this.bodyBytes = data.slice(4 + structLen); for (let i = 1; i <= structLen; i++) { typesDlms.push(data[2 + i]); } } else { this.totalBytes = data[2] + 3; this.bodyBytes = data.slice(3); typesDlms.push(data[1]); } let bytelist = Array.from(this.bodyBytes); while (bytelist.length > 0) { if (isStruct) { let index = 0; typesDlms.forEach(type => { bytelist.splice(index, 0, type); const step = _DlmsDatas.default.factory(type).getStandardLength(); index += step; }); bytelist.unshift(2, structLen); const descriptorInstance = new _DlmsArrayStruct.default(bytelist); this.value.push(descriptorInstance); bytelist = bytelist.slice(descriptorInstance.totalBytes); } else { const dlmsData = _DlmsDatas.default.factory(data[1]); bytelist.unshift(data[1]); dlmsData.insertData(bytelist); this.value.push(dlmsData); bytelist = bytelist.slice(dlmsData.totalBytes); } } this.rawValue = [this.tag, ...this.bodyBytes]; this.totalBytes = this.rawValue.length; } } exports.default = DlmsCompactArray;