knxultimate
Version:
KNX IP protocol implementation for Node. This is the ENGINE of Node-Red KNX-Ultimate node.
92 lines • 2.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasProp = hasProp;
exports.hex2bin = hex2bin;
exports.hexToDec = hexToDec;
exports.ldexp = ldexp;
exports.frexp = frexp;
exports.getHex = getHex;
exports.getFloat = getFloat;
exports.round = round;
exports.getTimestamp = getTimestamp;
exports.wait = wait;
function hasProp(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
function hex2bin(hex) {
return parseInt(hex, 16).toString(2).padStart(8, '0');
}
function hexToDec(hex) {
let result = 0;
let digitValue;
hex = hex.toLowerCase();
for (let i = 0; i < hex.length; i++) {
digitValue = '0123456789abcdefgh'.indexOf(hex[i]);
result = result * 16 + digitValue;
}
return result;
}
function ldexp(mantissa, exponent) {
return exponent > 1023
? mantissa * 2 ** 1023 * 2 ** (exponent - 1023)
: exponent < -1074
? mantissa * 2 ** -1074 * 2 ** (exponent + 1074)
: mantissa * 2 ** exponent;
}
function frexp(value) {
if (value === 0)
return [value, 0];
const data = new DataView(new ArrayBuffer(8));
data.setFloat64(0, value);
let bits = (data.getUint32(0) >>> 20) & 0x7ff;
if (bits === 0) {
data.setFloat64(0, value * 2 ** 64);
bits = ((data.getUint32(0) >>> 20) & 0x7ff) - 64;
}
const exponent = bits - 1022;
const mantissa = ldexp(value, -exponent);
return [mantissa, exponent];
}
function getHex(_value) {
try {
const arr = frexp(_value);
const mantissa = arr[0];
const exponent = arr[1];
let max_mantissa = 0;
let e;
for (e = exponent; e >= -15; e--) {
max_mantissa = ldexp(100 * mantissa, e);
if (max_mantissa > -2048 && max_mantissa < 2047)
break;
}
const sign = mantissa < 0 ? 1 : 0;
const mant = mantissa < 0 ? ~(max_mantissa ^ 2047) : max_mantissa;
const exp = exponent - e;
return [(sign << 7) + (exp << 3) + (mant >> 8), mant % 256];
}
catch (error) {
return null;
}
}
function getFloat(_value0, _value1) {
const sign = _value0 >> 7;
const exponent = (_value0 & 0b01111000) >> 3;
let mantissa = 256 * (_value0 & 0b00000111) + _value1;
mantissa = sign === 1 ? ~(mantissa ^ 2047) : mantissa;
return parseFloat(ldexp(0.01 * mantissa, exponent).toPrecision(15));
}
function round(value, decimals) {
return Number(`${Math.round(Number(`${value}e${decimals}`))}e-${decimals}`);
}
function getTimestamp() {
const now = new Date();
const seconds = now.getSeconds().toString().padStart(2, '0');
const milliseconds = now.getMilliseconds().toString().padStart(3, '0');
return `${seconds}.${milliseconds}`;
}
function wait(ms) {
return new Promise((r) => {
setTimeout(r, ms);
});
}
//# sourceMappingURL=utils.js.map