UNPKG

dlms-protocol

Version:

DLMS Protocol parser for JavaScript

81 lines (80 loc) 3.41 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.exec.js"); require("core-js/modules/es.regexp.to-string.js"); require("core-js/modules/es.string.match.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 SetRequest { constructor() { let dlmsData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; let cosemAddress = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; let selectiveAccessDescriptor = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; let parameters = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null; this.setType = 0; this.invokeIdAndPriority = 0; this.cosemClassId = new Array(2).fill(0); this.cosemObjectInstanceId = new Array(6).fill(0); this.cosemObjectAttributeId = new Array(1).fill(0); this.selectiveAccessDescriptor = 0; this.dlmsData = null; this.value = []; this.parameters = []; if (dlmsData && cosemAddress) { this.insertData(dlmsData, cosemAddress, selectiveAccessDescriptor); } else if (cosemAddress && parameters) { this.insertDataWithParameters(cosemAddress, parameters); } } insertData(dlmsData, cosemAddress, selectiveAccessDescriptor) { const invokeId = Math.floor(Math.random() * 15) + 1; // Generate random invokeId this.setType = dlmsData.totalBytes < 127 ? 0x01 : 0x02; this.invokeIdAndPriority = 0xC0 + invokeId; this.cosemClassId = [...cosemAddress.classId]; this.cosemObjectInstanceId = [...cosemAddress.instanceId]; this.cosemObjectAttributeId = [...cosemAddress.attributeId]; this.selectiveAccessDescriptor = selectiveAccessDescriptor; this.dlmsData = dlmsData; } insertDataWithParameters(cosemAddress, parameters) { const invokeId = Math.floor(Math.random() * 15) + 1; // Generate random invokeId console.log("invoke id", invokeId); this.setType = 0x01; this.invokeIdAndPriority = 0xC0 + invokeId; this.cosemClassId = [...cosemAddress.classId]; this.cosemObjectInstanceId = [...cosemAddress.instanceId]; this.cosemObjectAttributeId = [...cosemAddress.attributeId]; this.selectiveAccessDescriptor = 0x00; this.parameters = this._stringToBytes(parameters); } getBytes() { const result = []; result.push(_MessageType.default.SET_REQUEST); result.push(this.setType); result.push(this.invokeIdAndPriority); result.push(...this.cosemClassId); result.push(...this.cosemObjectInstanceId); result.push(...this.cosemObjectAttributeId); result.push(this.selectiveAccessDescriptor); if (this.dlmsData) { result.push(...this.dlmsData.rawValue); } else { result.push(...this.parameters); } this.value = [...result]; return result; } getHexString() { return this.value.map(byte => byte.toString(16).toUpperCase().padStart(2, '0')).join(' '); } _stringToBytes(hexString) { return hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)); } } exports.default = SetRequest;