dlms-protocol
Version:
DLMS Protocol parser for JavaScript
71 lines (70 loc) • 2.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("core-js/modules/es.array-buffer.constructor.js");
require("core-js/modules/es.array-buffer.slice.js");
require("core-js/modules/es.typed-array.uint8-array.js");
require("core-js/modules/es.typed-array.fill.js");
require("core-js/modules/es.typed-array.set.js");
require("core-js/modules/es.typed-array.sort.js");
require("core-js/modules/es.typed-array.to-locale-string.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _DlmsDataType = _interopRequireDefault(require("../enum/DlmsDataType"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
class DlmsUint64 {
constructor() {
let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
this.tag = _DlmsDataType.default.UINT64;
this.totalBytes = 0;
this.rawValue = [];
this.bodyBytes = [];
this.value = BigInt(0);
if (data) {
if (Array.isArray(data)) {
this.insertData(data);
} else {
this.insertDataFromValue(data);
}
}
}
getStandardLength() {
return 9;
}
getValue() {
return this.value;
}
insertData(data) {
if (this.tag !== data[0]) {
throw new Error("Tag mismatch.");
}
this.bodyBytes = data.slice(1, 9); // Extract 8 bytes after the tag
this.value = this._toUint64(this.bodyBytes);
this.rawValue = [this.tag, ...this.bodyBytes];
this.totalBytes = this.rawValue.length;
}
insertDataFromValue(value) {
this.value = BigInt(value);
this.bodyBytes = this._fromUint64(value);
this.rawValue = [this.tag, ...this.bodyBytes];
this.totalBytes = this.rawValue.length;
}
_toUint64(bytes) {
const buffer = new Uint8Array(bytes);
const dataView = new DataView(buffer.buffer);
const high = BigInt(dataView.getUint32(0, false)); // Big-endian
const low = BigInt(dataView.getUint32(4, false)); // Big-endian
return high << BigInt(32) | low;
}
_fromUint64(value) {
const buffer = new ArrayBuffer(8);
const dataView = new DataView(buffer);
const high = Number(value >> BigInt(32));
const low = Number(value & BigInt(0xFFFFFFFF));
dataView.setUint32(0, high, false); // Big-endian
dataView.setUint32(4, low, false); // Big-endian
return Array.from(new Uint8Array(buffer));
}
}
exports.default = DlmsUint64;