Skip to content

Commit

Permalink
when storing anything in a ValueStore, log the value
Browse files Browse the repository at this point in the history
  • Loading branch information
yankee42 committed Jan 21, 2022
1 parent ac9009f commit a7e71a2
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 20 deletions.
14 changes: 14 additions & 0 deletions packages/helpermodules/auto_str.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from typing import TypeVar

T = TypeVar('T')


def auto_str(cls: T) -> T:
def __str__(self):
return '%s(%s)' % (
type(self).__name__,
', '.join('%s=%s' % item for item in vars(self).items())
)

cls.__str__ = __str__
return cls
6 changes: 6 additions & 0 deletions packages/modules/common/component_state.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from typing import List

from helpermodules.auto_str import auto_str


@auto_str
class BatState:
def __init__(self, imported: float = 0, exported: float = 0, power: float = 0, soc: float = 0):
self.imported = imported
Expand All @@ -9,6 +12,7 @@ def __init__(self, imported: float = 0, exported: float = 0, power: float = 0, s
self.soc = soc


@auto_str
class CounterState:
def __init__(self,
imported: float = 0,
Expand Down Expand Up @@ -40,6 +44,7 @@ def __init__(self,
self.frequency = frequency


@auto_str
class InverterState:
def __init__(
self,
Expand All @@ -54,6 +59,7 @@ def __init__(
self.counter = counter


@auto_str
class CarState:
def __init__(self, soc: float):
self.soc = soc
11 changes: 11 additions & 0 deletions packages/modules/common/store/_api.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import logging
from abc import abstractmethod
from typing import Generic, TypeVar

T = TypeVar("T")
log = logging.getLogger("ValueStore")


class ValueStore(Generic[T]):
@abstractmethod
def set(self, state: T) -> None:
pass


class LoggingValueStore(Generic[T], ValueStore[T]):
def __init__(self, delegate: ValueStore[T]):
self.delegate = delegate

def set(self, state: T) -> None:
log.debug("Saving %s", state)
self.delegate.set(state)
14 changes: 6 additions & 8 deletions packages/modules/common/store/_battery.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from helpermodules import log, compatibility
from helpermodules import compatibility
from modules.common.component_state import BatState
from modules.common.store import ValueStore
from modules.common.store.ramdisk import files
from modules.common.store._api import LoggingValueStore
from modules.common.store._broker import pub_to_broker
from modules.common.store._util import process_error
from modules.common.store.ramdisk import files


class BatteryValueStoreRamdisk(ValueStore[BatState]):
Expand All @@ -16,9 +17,6 @@ def set(self, bat_state: BatState):
files.battery.soc.write(bat_state.soc)
files.battery.energy_imported.write(bat_state.imported)
files.battery.energy_exported.write(bat_state.exported)
log.MainLogger().info('BAT Watt: ' + str(bat_state.power))
log.MainLogger().info('BAT Einspeisung: ' + str(bat_state.exported))
log.MainLogger().info('BAT Bezug: ' + str(bat_state.imported))
except Exception as e:
process_error(e)

Expand All @@ -38,6 +36,6 @@ def set(self, bat_state: BatState):


def get_bat_value_store(component_num: int) -> ValueStore[BatState]:
if compatibility.is_ramdisk_in_use():
return BatteryValueStoreRamdisk(component_num)
return BatteryValueStoreBroker(component_num)
return LoggingValueStore(
(BatteryValueStoreRamdisk if compatibility.is_ramdisk_in_use() else BatteryValueStoreBroker)(component_num)
)
12 changes: 5 additions & 7 deletions packages/modules/common/store/_counter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from helpermodules import log, compatibility
from helpermodules import compatibility
from modules.common.component_state import CounterState
from modules.common.store import ValueStore
from modules.common.store._api import LoggingValueStore
from modules.common.store._broker import pub_to_broker
from modules.common.store._util import process_error
from modules.common.store.ramdisk import files
Expand All @@ -17,9 +18,6 @@ def set(self, counter_state: CounterState):
files.evu.energy_export.write(counter_state.exported)
files.evu.power_import.write(counter_state.power)
files.evu.frequency.write(counter_state.frequency)
log.MainLogger().info('EVU Watt: ' + str(counter_state.power))
log.MainLogger().info('EVU Bezug: ' + str(counter_state.imported))
log.MainLogger().info('EVU Einspeisung: ' + str(counter_state.exported))
except Exception as e:
process_error(e)

Expand All @@ -43,6 +41,6 @@ def set(self, counter_state: CounterState):


def get_counter_value_store(component_num: int) -> ValueStore[CounterState]:
if compatibility.is_ramdisk_in_use():
return CounterValueStoreRamdisk()
return CounterValueStoreBroker(component_num)
return LoggingValueStore(
CounterValueStoreRamdisk() if compatibility.is_ramdisk_in_use() else CounterValueStoreBroker(component_num)
)
10 changes: 5 additions & 5 deletions packages/modules/common/store/_inverter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from helpermodules import log, compatibility
from helpermodules import compatibility
from modules.common.component_state import InverterState
from modules.common.fault_state import FaultState
from modules.common.store import ValueStore
from modules.common.store._api import LoggingValueStore
from modules.common.store._broker import pub_to_broker
from modules.common.store.ramdisk import files

Expand All @@ -16,7 +17,6 @@ def set(self, inverter_state: InverterState):
self.__pv.energy.write(inverter_state.counter)
self.__pv.energy_k.write(inverter_state.counter / 1000)
self.__pv.currents.write(inverter_state.currents)
log.MainLogger().info('PV Watt: ' + str(inverter_state.power))
except Exception as e:
raise FaultState.from_exception(e)

Expand All @@ -35,6 +35,6 @@ def set(self, inverter_state: InverterState):


def get_inverter_value_store(component_num: int) -> ValueStore[InverterState]:
if compatibility.is_ramdisk_in_use():
return InverterValueStoreRamdisk(component_num)
return InverterValueStoreBroker(component_num)
return LoggingValueStore(
(InverterValueStoreRamdisk if compatibility.is_ramdisk_in_use() else InverterValueStoreBroker)(component_num)
)

0 comments on commit a7e71a2

Please sign in to comment.