dlms-protocol
Version:
DLMS Protocol parser for JavaScript
66 lines (65 loc) • 2.39 kB
JavaScript
"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 _Enums = require("./Enums.js");
class WriteRequest {
constructor() {
let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
let variableName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
let parameters = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
this.variableAccessSpecificationQty = 0x01;
this.variableAccessSpecification = 0x02;
this.variableName = variableName;
this.dataQty = 0x00;
this.data = data;
this.value = [];
this.parameters = parameters ? this._stringToBytes(parameters) : [];
this.variableParameters = variableName ? this._stringToBytes(variableName) : [];
if (data) {
this.dataQty = 0x01;
}
}
insertData(dataBytes) {
throw new Error("Method not implemented.");
}
getBytes() {
const result = [];
result.push(_Enums.MessageType.WRITE_REQUEST);
result.push(this.variableAccessSpecificationQty);
result.push(this.variableAccessSpecification);
if (this.variableParameters.length > 0) {
result.push(...this.variableParameters);
} else {
const variableNameValue = Number(this.variableName);
result.push(variableNameValue >> 8 & 0xFF, variableNameValue & 0xFF);
}
result.push(this.dataQty);
if (this.data || this.parameters.length > 0) {
result.push(...(this.data ? this.data.rawValue : this.parameters));
}
this.value = [...result];
return result;
}
insertDataFromObjects(data, variableName) {
this.variableAccessSpecificationQty = 0x01;
this.variableAccessSpecification = 0x02;
this.variableName = variableName;
this.dataQty = 0x01;
this.data = data;
}
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 = WriteRequest;