dlms-protocol
Version:
DLMS Protocol parser for JavaScript
57 lines (56 loc) • 2.08 kB
JavaScript
;
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 ReadRequest {
constructor() {
let variableNames = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
let variableNamesString = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
this.qty = 0;
this.variableNames = [];
this.value = [];
this.variableParameters = [];
if (Array.isArray(variableNames)) {
this.insertData(variableNames);
} else if (typeof variableNamesString === 'string') {
this.qty = 1;
this.variableParameters = this._stringToBytes(variableNamesString);
}
}
insertData(variableNames) {
this.qty = variableNames.length;
this.variableNames = [...variableNames];
}
getBytes() {
const result = [];
result.push(_MessageType.default.READ_REQUEST);
result.push(this.qty);
if (this.variableNames.length > 0) {
for (const variable of this.variableNames) {
result.push(0x02); // Indicator byte
result.push(variable >> 8 & 0xFF, variable & 0xFF); // Convert variable to two bytes
}
} else if (this.variableParameters.length > 0) {
result.push(0x02); // Indicator byte
result.push(...this.variableParameters);
}
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 = ReadRequest;