Avoid float calculation artifacts
This commit is contained in:
parent
b2f29ec263
commit
0cd5872fb6
1 changed files with 13 additions and 9 deletions
22
solar_ble.py
22
solar_ble.py
|
@ -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)
|
||||||
|
|
Loading…
Reference in a new issue