UNPKG

dlms-protocol

Version:

DLMS Protocol parser for JavaScript

85 lines (79 loc) 3.12 kB
"use strict"; 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 ActionRequest { constructor(cosemAddress) { let parameters = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; let dlmsData = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; this.SetType = 0x01; this.invokeIdAndPriority = this.generateInvokeId(); this.CosemClassId = cosemAddress.Classid; this.CosemObjectInstanceId = cosemAddress.Instanceid; this.CosemObjectAttributeId = cosemAddress.Attributeid; this.MethodInvocationParameters = 0; if (parameters) { this.MethodInvocationParameters = 1; this.Parameters = this.hexStringToByteArray(parameters); } if (dlmsData) { this.MethodInvocationParameters = 1; this.DlmsData = dlmsData; } } // Helper function to generate a random Invoke ID generateInvokeId() { return 0xC0 + Math.floor(Math.random() * 15) + 1; } // Convert a hex string to a byte array hexStringToByteArray(hexString) { return hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)); } // Insert data method (overloaded) insertData(bytes) { throw new Error("Method not implemented"); } insertDataWithAddress(cosemAddress) { this.SetType = 0x01; this.invokeIdAndPriority = this.generateInvokeId(); this.CosemClassId = cosemAddress.Classid; this.CosemObjectInstanceId = cosemAddress.Instanceid; this.CosemObjectAttributeId = cosemAddress.Attributeid; this.MethodInvocationParameters = 0; } insertDataWithDlms(cosemAddress, dlmsData) { this.SetType = 0x01; this.invokeIdAndPriority = this.generateInvokeId(); this.CosemClassId = cosemAddress.Classid; this.CosemObjectInstanceId = cosemAddress.Instanceid; this.CosemObjectAttributeId = cosemAddress.Attributeid; this.MethodInvocationParameters = 1; this.DlmsData = dlmsData; } // Get the byte array getBytes() { let result = []; result.push(_MessageType.default.ACTION_REQUEST); result.push(this.SetType); result.push(this.invokeIdAndPriority); result.push(...this.CosemClassId); result.push(...this.CosemObjectInstanceId); result.push(...this.CosemObjectAttributeId); result.push(this.MethodInvocationParameters); if (this.MethodInvocationParameters === 1) { result.push(...(this.DlmsData ? this.DlmsData.RawValue : this.Parameters)); } this.Value = [...result]; // Clone the result return result; } // Get the hexadecimal string representation getHexString() { return this.Value.map(dat => dat.toString(16).padStart(2, '0').toUpperCase()).join(' '); } } module.exports = ActionRequest;