UNPKG

dlms-protocol

Version:

DLMS Protocol parser for JavaScript

45 lines (43 loc) 1.5 kB
"use strict"; 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 _DlmsDataType = _interopRequireDefault(require("../enum/DlmsDataType")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } class DlmsBitstring { constructor() { let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; this.tag = _DlmsDataType.default.BITSTRING; this.totalBytes = 0; this.rawValue = []; this.bodyBytes = []; this.value = ""; this.size = 0; 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."); } this.size = data[1]; this.bodyBytes = Array.from(data.slice(2, 2 + Math.ceil(this.size / 8))); this.value = this.bodyBytes.map(byte => byte.toString(2).padStart(8, '0')) // Convert each byte to binary and pad to 8 bits .join('') // Combine all bits into a single string .slice(0, this.size); // Trim the bitstring to the specified size this.rawValue = [this.tag, this.size, ...this.bodyBytes]; this.totalBytes = this.rawValue.length; } } exports.default = DlmsBitstring;