UNPKG

dlms-protocol

Version:

DLMS Protocol parser for JavaScript

66 lines (65 loc) 2.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; require("core-js/modules/es.array-buffer.constructor.js"); require("core-js/modules/es.array-buffer.slice.js"); require("core-js/modules/es.typed-array.uint8-array.js"); require("core-js/modules/es.typed-array.fill.js"); require("core-js/modules/es.typed-array.set.js"); require("core-js/modules/es.typed-array.sort.js"); require("core-js/modules/es.typed-array.to-locale-string.js"); require("core-js/modules/web.dom-collections.iterator.js"); var _DlmsDataType = _interopRequireDefault(require("../enum/DlmsDataType")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } class DlmsUint32 { constructor() { let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; this.tag = _DlmsDataType.default.UINT32; this.totalBytes = 0; this.rawValue = []; this.bodyBytes = []; this.value = 0; if (data) { if (Array.isArray(data)) { this.insertData(data); } else { this.insertDataFromValue(data); } } } getStandardLength() { return 5; } getValue() { return this.value; } insertData(data) { if (this.tag !== data[0]) { throw new Error("Tag mismatch."); } this.bodyBytes = data.slice(1, 5); // Extract 4 bytes after the tag this.value = this._toUint32(this.bodyBytes); this.rawValue = [this.tag, ...this.bodyBytes]; this.totalBytes = this.rawValue.length; } insertDataFromValue(value) { this.value = value; this.bodyBytes = this._fromUint32(value); this.rawValue = [this.tag, ...this.bodyBytes]; this.totalBytes = this.rawValue.length; } _toUint32(bytes) { const buffer = new Uint8Array(bytes); const dataView = new DataView(buffer.buffer); return dataView.getUint32(0, false); // Big-endian } _fromUint32(value) { const buffer = new ArrayBuffer(4); const dataView = new DataView(buffer); dataView.setUint32(0, value, false); // Big-endian return Array.from(new Uint8Array(buffer)); } } exports.default = DlmsUint32;