Avoid float calculation artifacts

This commit is contained in:
Odd Stråbø 2021-11-20 10:11:32 +01:00
parent b2f29ec263
commit 0cd5872fb6

View file

@ -5,6 +5,7 @@ import datetime
import struct import struct
import sys import sys
import time import time
from decimal import Decimal
from io import RawIOBase from io import RawIOBase
from typing import Callable, Collection, Optional, cast from typing import Callable, Collection, Optional, cast
@ -403,15 +404,18 @@ if __name__ == "__main__":
if per_voltages(now): if per_voltages(now):
data = try_read_parse(dev, 0x0100, 11, parse_battery_state) data = try_read_parse(dev, 0x0100, 11, parse_battery_state)
if data: if data:
data[DataName.CALCULATED_BATTERY_POWER] = data.get( data[DataName.CALCULATED_BATTERY_POWER] = float(
DataName.BATTERY_VOLTAGE, 0 Decimal(data.get(DataName.BATTERY_VOLTAGE, 0))
) * data.get(DataName.BATTERY_CURRENT) * Decimal(data.get(DataName.BATTERY_CURRENT, 0))
data[DataName.CALCULATED_PANEL_POWER] = data.get( )
DataName.PANEL_VOLTAGE, 0 data[DataName.CALCULATED_PANEL_POWER] = float(
) * data.get(DataName.PANEL_CURRENT) Decimal(data.get(DataName.PANEL_VOLTAGE, 0))
data[DataName.CALCULATED_LOAD_POWER] = data.get( * Decimal(data.get(DataName.PANEL_CURRENT, 0))
DataName.LOAD_VOLTAGE, 0 )
) * data.get(DataName.LOAD_CURRENT) data[DataName.CALCULATED_LOAD_POWER] = float(
Decimal(data.get(DataName.LOAD_VOLTAGE, 0))
* Decimal(data.get(DataName.LOAD_CURRENT, 0))
)
log(data) log(data)
for consumer in consumers: for consumer in consumers:
consumer.write(data) consumer.write(data)