Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/modules/devices/saxpower/saxpower/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ def __init__(self,
self.configuration = configuration or SaxpowerConfiguration()


class SaxpowerCounterConfiguration:
def __init__(self):
pass


class SaxpowerCounterSetup(ComponentSetup[SaxpowerCounterConfiguration]):
def __init__(self,
name: str = "Saxpower Zähler",
type: str = "counter",
id: int = 0,
configuration: SaxpowerCounterConfiguration = None) -> None:
super().__init__(name, type, id, configuration or SaxpowerCounterConfiguration())


class SaxpowerBatConfiguration:
def __init__(self):
pass
Expand Down
47 changes: 47 additions & 0 deletions packages/modules/devices/saxpower/saxpower/counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3
from typing import TypedDict, Any

from modules.common.abstract_device import AbstractCounter
from modules.common.component_state import CounterState
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.modbus import ModbusDataType, ModbusTcpClient_
from modules.common.simcount import SimCounter
from modules.common.store import get_counter_value_store
from modules.devices.saxpower.saxpower.config import SaxpowerCounterSetup


class KwargsDict(TypedDict):
device_id: int
client: ModbusTcpClient_
modbus_id: int


class SaxpowerCounter(AbstractCounter):
def __init__(self, component_config: SaxpowerCounterSetup, **kwargs: Any) -> None:
self.component_config = component_config
self.kwargs: KwargsDict = kwargs

def initialize(self) -> None:
self.__device_id: int = self.kwargs['device_id']
self.client: ModbusTcpClient_ = self.kwargs['client']
self.__modbus_id: int = self.kwargs['modbus_id']
self.store = get_counter_value_store(self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="bezug")

def update(self) -> None:
with self.__tcp_client:
power = self.__tcp_client.read_holding_registers(48, [ModbusDataType.INT_16]*2, unit=self.__modbus_id)
power = power * -1 + 16384
imported, exported = self.sim_counter.sim_count(power)

counter_state = CounterState(
imported=imported,
exported=exported,
power=power
)
self.store.set(counter_state)


component_descriptor = ComponentDescriptor(configuration_factory=SaxpowerCounterSetup)
12 changes: 10 additions & 2 deletions packages/modules/devices/saxpower/saxpower/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from modules.common.component_context import SingleComponentUpdateContext
from modules.common.configurable_device import ComponentFactoryByType, ConfigurableDevice, MultiComponentUpdater
from modules.devices.saxpower.saxpower.bat import SaxpowerBat
from modules.devices.saxpower.saxpower.config import Saxpower, SaxpowerBatSetup
from modules.devices.saxpower.saxpower.counter import SaxpowerCounter
from modules.devices.saxpower.saxpower.config import Saxpower, SaxpowerBatSetup, SaxpowerCounterSetup

log = logging.getLogger(__name__)

Expand All @@ -22,7 +23,13 @@ def create_bat_component(component_config: SaxpowerBatSetup):
client=client,
modbus_id=device_config.configuration.modbus_id)

def update_components(components: Iterable[SaxpowerBat]):
def create_counter_component(component_config: SaxpowerCounterSetup):
return SaxpowerCounter(component_config,
device_id=device_config.id,
client=client,
modbus_id=device_config.configuration.modbus_id)

def update_components(components: Iterable[SaxpowerBat, SaxpowerCounter]):
nonlocal client
with client:
for component in components:
Expand All @@ -38,6 +45,7 @@ def initializer():
initializer=initializer,
component_factory=ComponentFactoryByType(
bat=create_bat_component,
counter=create_counter_component
),
component_updater=MultiComponentUpdater(update_components)
)
Expand Down