dlms-protocol
Version:
DLMS Protocol parser for JavaScript
51 lines (50 loc) • 1.41 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
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 DlmsInt8 {
constructor() {
let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
this.tag = _DlmsDataType.default.INT8;
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 2;
}
getValue() {
return this.value;
}
insertData(data) {
if (this.tag !== data[0]) {
throw new Error("Tag mismatch.");
}
this.bodyBytes = [data[1]];
this.value = this._toSignedByte(data[1]);
this.rawValue = [this.tag, ...this.bodyBytes];
this.totalBytes = this.rawValue.length;
}
insertDataFromValue(value) {
this.value = this._toSignedByte(value);
this.bodyBytes = [value & 0xFF];
this.rawValue = [this.tag, ...this.bodyBytes];
this.totalBytes = this.rawValue.length;
}
_toSignedByte(byte) {
return byte > 127 ? byte - 256 : byte;
}
}
exports.default = DlmsInt8;