Power factor new calculation; new bytesToInt function with uint and bigendian

This commit is contained in:
rasta5man 2025-08-06 12:00:17 +02:00
parent 5540cee8ec
commit 1012544193
2 changed files with 114 additions and 127 deletions

View file

@ -2616,7 +2616,7 @@ exports.install = function(instance) {
//účinník
else if (register == 77) {
let power_factor = Math.cos(bytesToInt(bytes) * 0.1).toFixed(2);
let power_factor = Math.cos(bytesToInt(bytes) * 0.1 * (Math.PI / 180)).toFixed(2);
values["power_factor"] = Number(power_factor);
}
@ -2629,8 +2629,7 @@ exports.install = function(instance) {
//energia
else if (register == 79) {
let energy = bytesToInt(bytes);
//Energiu treba reportovať v kWh -> delit 1000
values["energy"] = energy / 1000;
values["energy"] = energy / 1000; //energia v kWh -> delit 1000
}
//doba života

View file

@ -1,31 +1,19 @@
function bytesToInt(bytes, numberOfBytes)
{
function bytesToInt(bytes, numberOfBytes) {
let buffer = [];
if(Array.isArray(bytes))
{
if (Array.isArray(bytes)) {
buffer = bytes.slice(0);
if(numberOfBytes != undefined)
{
if (numberOfBytes != undefined) {
buffer = bytes.slice(bytes.length - numberOfBytes);
}
}
else buffer.push(bytes);
//var decimal = (buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3];
let l = (buffer.length - 1) * 8;
let decimal = 0;
for(let i = 0; i < buffer.length; i++)
{
var s = buffer[i] << l;
if(l < 8) s = buffer[i]
decimal = decimal + s;
l = l - 8;
let result = 0;
for (let i = 0; i < buffer.length; i++) {
result = (result << 8) | buffer[i];
}
return decimal;
return result >>> 0; //ensure it's an unsigned 32-bit number
}
function resizeArray(arr, newSize, defaultValue) {