Skip to content
Closed
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
32 changes: 20 additions & 12 deletions packages/modules/devices/algodue/algodue/counter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python3
from typing import Dict, Union
from typing import Any, TypedDict

from dataclass_utils import dataclass_from_dict
from modules.devices.algodue.algodue.config import AlgodueCounterSetup
from modules.common import modbus
from modules.common.abstract_device import AbstractCounter
Expand All @@ -13,16 +12,22 @@
from modules.common.store import get_counter_value_store


class KwargsDict(TypedDict):
device_id: int
tcp_client: modbus.ModbusTcpClient_
modbus_id: int


class AlgodueCounter(AbstractCounter):
def __init__(self,
device_id: int,
component_config: Union[Dict, AlgodueCounterSetup],
tcp_client: modbus.ModbusTcpClient_,
modbus_id: int) -> None:
self.__device_id = device_id
self.component_config = dataclass_from_dict(AlgodueCounterSetup, component_config)
self.__tcp_client = tcp_client
self.__modbus_id = modbus_id
def __init__(self, component_config: AlgodueCounterSetup, **kwargs: Any) -> None:
self.component_config = component_config
self.kwargs: KwargsDict = kwargs

def initialize(self) -> None:
# return
self.__device_id: int = self.kwargs['device_id']
self.__tcp_client: modbus.ModbusTcpClient_ = self.kwargs['tcp_client']
self.__modbus_id: int = self.kwargs['modbus_id']
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="bezug")
self.store = get_counter_value_store(self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
Expand All @@ -37,6 +42,8 @@ def update(self):
power = sum(powers)
voltages = self.__tcp_client.read_input_registers(
0x1000, [ModbusDataType.FLOAT_32]*3, unit=self.__modbus_id)
power_factors = self.__tcp_client.read_input_registers(
0x1018, [ModbusDataType.FLOAT_32]*3, unit=self.__modbus_id)

imported, exported = self.sim_counter.sim_count(power)

Expand All @@ -47,7 +54,8 @@ def update(self):
imported=imported,
exported=exported,
power=power,
frequency=frequency
frequency=frequency,
power_factors=power_factors
)
self.store.set(counter_state)

Expand Down
7 changes: 5 additions & 2 deletions packages/modules/devices/algodue/algodue/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@


def create_device(device_config: Algodue):
client = None

def create_counter_component(component_config: AlgodueCounterSetup):
return counter.AlgodueCounter(device_config.id, component_config, client,
device_config.configuration.modbus_id)
nonlocal client
return counter.AlgodueCounter(component_config=component_config, device_id=device_config.id,
tcp_client=client, modbus_id=device_config.configuration.modbus_id)

Check failure on line 21 in packages/modules/devices/algodue/algodue/device.py

View workflow job for this annotation

GitHub Actions / build

continuation line over-indented for visual indent

def update_components(components: Iterable[counter.AlgodueCounter]):
with client:
Expand Down
Loading