dlms-protocol
Version:
DLMS Protocol parser for JavaScript
66 lines (65 loc) • 2.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("core-js/modules/es.regexp.to-string.js");
require("core-js/modules/esnext.iterator.map.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _DlmsDateTime = _interopRequireDefault(require("../typesDLMS/DlmsDateTime.js"));
var _DlmsDatas = _interopRequireDefault(require("../typesDLMS/DlmsDatas.js"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
class InformationReport {
constructor() {
let bytes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
this.currentTime = null;
this.variableAccessSpecificationQty = 0;
this.variableAccessSpecification = 0;
this.variableName = [];
this.dataQty = 0;
this.data = [];
this.value = [];
if (bytes) {
this.insertData(bytes);
}
}
insertData(bytes) {
this.value = [...bytes]; // Clone the byte array
let variablesIndex = 0;
if (bytes[1] !== 0) {
this.currentTime = new _DlmsDateTime.default();
const currentTimeBytes = bytes.slice(2, 2 + bytes[1]);
this.currentTime.insertData(currentTimeBytes);
variablesIndex = bytes[1] + 2;
} else {
variablesIndex = 2;
}
this.variableAccessSpecificationQty = bytes[variablesIndex];
variablesIndex++;
for (let i = 0; i < this.variableAccessSpecificationQty; i++) {
variablesIndex++;
const variableNameValue = bytes[variablesIndex] << 8 | bytes[variablesIndex + 1];
this.variableName.push(variableNameValue);
variablesIndex += 2;
}
this.dataQty = bytes[variablesIndex];
variablesIndex++;
if (variablesIndex <= bytes.length - 1) {
for (let i = 0; i < this.dataQty; i++) {
const dataType = bytes[variablesIndex];
const dlmsData = _DlmsDatas.default.factory(dataType);
const temp = bytes.slice(variablesIndex);
dlmsData.insertData(temp);
variablesIndex += dlmsData.totalBytes;
this.data.push(dlmsData);
}
}
}
getBytes() {
return this.value;
}
getHexString() {
return this.value.map(byte => byte.toString(16).toUpperCase().padStart(2, '0')).join(' ');
}
}
exports.default = InformationReport;