UNPKG

dlms-protocol

Version:

DLMS Protocol parser for JavaScript

73 lines (72 loc) 3.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; require("core-js/modules/es.parse-int.js"); 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 _MessageType = _interopRequireDefault(require("../enum/MessageType")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } class GlobalBlockTransfer { constructor() { let bytes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; let lastBlock = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; let streaming = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; let window = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0; let quantity = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0; let index = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 0; let blockNumberAck = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : 0; let blockData = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : []; this.blockControl = 0; this.lastBlock = lastBlock; this.streaming = streaming; this.window = window; this.blockNumber = 0; this.quantity = quantity; this.index = index; this.blockNumberAck = blockNumberAck; this.blockData = blockData; this.value = []; this.dataLength = blockData.length; if (bytes) { this.insertFromBytes(bytes); } else { this.constructFromParameters(); } } insertFromBytes(bytes) { this.value = [...bytes]; this.blockControl = bytes[1]; const blockControlBinary = this._toBinary(bytes[1], 8); this.lastBlock = blockControlBinary[0] === '1'; this.streaming = blockControlBinary[1] === '1'; this.window = parseInt(blockControlBinary.substring(2, 8), 2); this.blockNumber = bytes[2]; const blockNumberBinary = this._toBinary(bytes[2], 8); this.quantity = parseInt(blockNumberBinary.substring(0, 3), 2); this.index = parseInt(blockNumberBinary.substring(3, 8), 2); this.blockNumberAck = bytes[3]; this.dataLength = bytes[4]; this.blockData = bytes.slice(5, 5 + this.dataLength); } constructFromParameters() { const blockControlBinary = "".concat(this.lastBlock ? '1' : '0').concat(this.streaming ? '1' : '0').concat(this._toBinary(this.window, 6)); this.blockControl = parseInt(blockControlBinary, 2); const blockNumberBinary = "".concat(this._toBinary(this.quantity, 3)).concat(this._toBinary(this.index, 5)); this.blockNumber = parseInt(blockNumberBinary, 2); } getBytes() { const result = [_MessageType.default.GLOBAL_BLOCK_TRANSFER, this.blockControl, this.blockNumber, this.blockNumberAck, this.dataLength, ...this.blockData]; this.value = [...result]; return result; } getHexString() { return this.value.map(byte => byte.toString(16).toUpperCase().padStart(2, '0')).join(' '); } _toBinary(value, length) { return value.toString(2).padStart(length, '0'); } } exports.default = GlobalBlockTransfer;