dlms-protocol
Version:
DLMS Protocol parser for JavaScript
65 lines (64 loc) • 2.12 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("core-js/modules/esnext.iterator.constructor.js");
require("core-js/modules/esnext.iterator.for-each.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _DlmsDataType = _interopRequireDefault(require("../enum/DlmsDataType"));
var _DlmsDatas = _interopRequireDefault(require("./DlmsDatas"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
class DlmsArrayStruct {
constructor() {
let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
let dlmsDatas = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
this.tag = _DlmsDataType.default.STRUCT;
this.totalBytes = 0;
this.rawValue = [];
this.bodyBytes = [];
this.size = 0;
this.value = [];
if (data) {
this.insertData(data);
} else if (dlmsDatas) {
this.insertDataFromList(dlmsDatas);
}
}
getStandardLength() {
throw new Error("Method not implemented.");
}
getValue() {
return this.value;
}
insertData(data) {
if (this.tag !== data[0]) {
throw new Error("Tag mismatch.");
}
this.size = data[1];
this.bodyBytes = data.slice(2); // Skip tag and size
let temp = [...this.bodyBytes];
for (let i = 0; i < this.size; i++) {
if (temp.length > 0) {
let tempValue = _DlmsDatas.default.factory(temp[0]);
tempValue.insertData(temp);
this.value.push(tempValue);
temp = temp.slice(tempValue.totalBytes);
}
}
this.rawValue = [this.tag, this.size, ...this.bodyBytes];
this.totalBytes = this.rawValue.length;
}
insertDataFromList(dlmsDatas) {
this.size = dlmsDatas.length;
this.bodyBytes = [];
this.value = [];
dlmsDatas.forEach(dlmsData => {
this.value.push(dlmsData);
this.bodyBytes.push(...dlmsData.rawValue);
});
this.rawValue = [this.tag, this.size, ...this.bodyBytes];
this.totalBytes = this.rawValue.length;
}
}
exports.default = DlmsArrayStruct;