dlms-protocol
Version:
DLMS Protocol parser for JavaScript
66 lines (65 loc) • 2.12 kB
JavaScript
"use strict";
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 DlmsUint16 {
constructor() {
let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
this.tag = _DlmsDataType.default.UINT16;
this.totalBytes = 0;
this.rawValue = [];
this.bodyBytes = [];
this.value = 0;
if (data) {
if (Array.isArray(data)) {
this.insertData(data);
} else {
this.insertDataFromValue(data);
}
}
}
getStandardLength() {
return 3;
}
getValue() {
return this.value;
}
insertData(data) {
if (this.tag !== data[0]) {
throw new Error("Tag mismatch.");
}
this.bodyBytes = [data[1], data[2]];
this.value = this._toUint16(this.bodyBytes);
this.rawValue = [this.tag, ...this.bodyBytes];
this.totalBytes = this.rawValue.length;
}
insertDataFromValue(value) {
this.value = value;
this.bodyBytes = this._fromUint16(value);
this.rawValue = [this.tag, ...this.bodyBytes];
this.totalBytes = this.rawValue.length;
}
_toUint16(bytes) {
const buffer = new Uint8Array(bytes);
const dataView = new DataView(buffer.buffer);
return dataView.getUint16(0, false); // Big-endian
}
_fromUint16(value) {
const buffer = new ArrayBuffer(2);
const dataView = new DataView(buffer);
dataView.setUint16(0, value, false); // Big-endian
return Array.from(new Uint8Array(buffer));
}
}
exports.default = DlmsUint16;