Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use create_device for byd, huawei smartlogger, carlo gavazzi, good_we, kostal piko, janitza #1742

Merged
merged 5 commits into from
Jul 10, 2024
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
2 changes: 1 addition & 1 deletion docs/samples/sample_modbus/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def update_components(components: Iterable[Union[SampleBat, SampleCounter, Sampl
component.update(c)

try:
client = ModbusTcpClient_(device_config.configuration.ip_address, port)
client = ModbusTcpClient_(device_config.configuration.ip_address, device_config.configuration.port)
except Exception:
log.exception("Fehler in create_device")
return ConfigurableDevice(
Expand Down
3 changes: 1 addition & 2 deletions packages/modules/devices/byd/bat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from html.parser import HTMLParser
from typing import Dict, List, Union, Tuple

from dataclass_utils import dataclass_from_dict
from modules.devices.byd.config import BYDBatSetup
from modules.common import req
from modules.common.component_state import BatState
Expand All @@ -20,7 +19,7 @@ def __init__(self,
component_config: Union[Dict, BYDBatSetup],
device_config) -> None:
self.__device_config = device_config
self.component_config = dataclass_from_dict(BYDBatSetup, component_config)
self.component_config = component_config
self.sim_counter = SimCounter(self.__device_config.id, self.component_config.id, prefix="speicher")
self.store = get_bat_value_store(self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
Expand Down
91 changes: 13 additions & 78 deletions packages/modules/devices/byd/device.py
Original file line number Diff line number Diff line change
@@ -1,90 +1,25 @@
#!/usr/bin/env python3
import logging
from typing import Dict, Optional, List, Union

from dataclass_utils import dataclass_from_dict
from helpermodules.cli import run_using_positional_cli_args
from modules.devices.byd.config import BYD, BYDBatSetup, BYDConfiguration
from modules.common.configurable_device import ComponentFactoryByType, ConfigurableDevice, IndependentComponentUpdater
from modules.devices.byd.config import BYD, BYDBatSetup
from modules.devices.byd import bat
from modules.common.abstract_device import AbstractDevice, DeviceDescriptor
from modules.common.component_context import SingleComponentUpdateContext
from modules.common.abstract_device import DeviceDescriptor

log = logging.getLogger(__name__)


class Device(AbstractDevice):
COMPONENT_TYPE_TO_CLASS = {
"bat": bat.BYDBat
}
def create_device(device_config: BYD):
def create_bat_component(component_config: BYDBatSetup):
return bat.BYDBat(component_config, device_config)

def __init__(self, device_config: Union[Dict, BYD]) -> None:
self.components = {} # type: Dict[str, bat.BYDBat]
try:
self.device_config = dataclass_from_dict(BYD, device_config)
except Exception:
log.exception("Fehler im Modul "+self.device_config.name)

def add_component(self, component_config: Union[Dict, BYDBatSetup]) -> None:
if isinstance(component_config, Dict):
component_type = component_config["type"]
else:
component_type = component_config.type
component_config = dataclass_from_dict(COMPONENT_TYPE_TO_MODULE[
component_type].component_descriptor.configuration_factory, component_config)
if component_type in self.COMPONENT_TYPE_TO_CLASS:
self.components["component"+str(component_config.id)] = (self.COMPONENT_TYPE_TO_CLASS[component_type](
component_config,
self.device_config))
else:
raise Exception(
"illegal component type " + component_type + ". Allowed values: " +
','.join(self.COMPONENT_TYPE_TO_CLASS.keys())
)

def update(self) -> None:
log.debug("Start device reading " + str(self.components))
if self.components:
for component in self.components:
# Auch wenn bei einer Komponente ein Fehler auftritt, sollen alle anderen noch ausgelesen werden.
with SingleComponentUpdateContext(self.components[component].fault_state):
self.components[component].update()
else:
log.warning(
self.device_config.name +
": Es konnten keine Werte gelesen werden, da noch keine Komponenten konfiguriert wurden."
)


COMPONENT_TYPE_TO_MODULE = {
"bat": bat
}


def read_legacy(component_type: str,
ip_address: str,
username: str,
password: str,
num: Optional[int] = None) -> None:
dev = Device(BYD(configuration=BYDConfiguration(user=username, password=password, ip_address=ip_address)))
if component_type in COMPONENT_TYPE_TO_MODULE:
component_config = COMPONENT_TYPE_TO_MODULE[component_type].component_descriptor.configuration_factory()
else:
raise Exception(
"illegal component type " + component_type + ". Allowed values: " +
','.join(COMPONENT_TYPE_TO_MODULE.keys())
)
component_config.id = num
dev.add_component(component_config)

log.debug('byd IP-Adresse: ' + ip_address)
log.debug('byd Benutzer: ' + username)
log.debug('byd Passwort: ' + password)

dev.update()


def main(argv: List[str]):
run_using_positional_cli_args(read_legacy, argv)
return ConfigurableDevice(
device_config=device_config,
component_factory=ComponentFactoryByType(
bat=create_bat_component
),
component_updater=IndependentComponentUpdater(lambda component: component.update())
)


device_descriptor = DeviceDescriptor(configuration_factory=BYD)
3 changes: 1 addition & 2 deletions packages/modules/devices/carlo_gavazzi/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from pymodbus.constants import Endian

from dataclass_utils import dataclass_from_dict
from modules.devices.carlo_gavazzi.config import CarloGavazziCounterSetup
from modules.common import modbus
from modules.common.component_state import CounterState
Expand All @@ -21,7 +20,7 @@ def __init__(self,
tcp_client: modbus.ModbusTcpClient_,
modbus_id: int) -> None:
self.__device_id = device_id
self.component_config = dataclass_from_dict(CarloGavazziCounterSetup, component_config)
self.component_config = component_config
self.__tcp_client = tcp_client
self.__modbus_id = modbus_id
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="bezug")
Expand Down
100 changes: 25 additions & 75 deletions packages/modules/devices/carlo_gavazzi/device.py
Original file line number Diff line number Diff line change
@@ -1,89 +1,39 @@
#!/usr/bin/env python3
import logging
from typing import Dict, Optional, List, Union
from typing import Iterable

from dataclass_utils import dataclass_from_dict
from helpermodules.cli import run_using_positional_cli_args
from modules.common.configurable_device import ComponentFactoryByType, ConfigurableDevice, MultiComponentUpdater
from modules.devices.carlo_gavazzi import counter
from modules.devices.carlo_gavazzi.config import CarloGavazzi, CarloGavazziCounterSetup
from modules.common import modbus
from modules.common.abstract_device import AbstractDevice, DeviceDescriptor
from modules.common.abstract_device import DeviceDescriptor
from modules.common.component_context import SingleComponentUpdateContext

log = logging.getLogger(__name__)


class Device(AbstractDevice):
COMPONENT_TYPE_TO_CLASS = {
"counter": counter.CarloGavazziCounter,
}

def __init__(self, device_config: Union[Dict, CarloGavazzi]) -> None:
self.components = {} # type: Dict[str, counter.CarloGavazziCounter]
try:
self.device_config = dataclass_from_dict(CarloGavazzi, device_config)
self.client = modbus.ModbusTcpClient_(
self.device_config.configuration.ip_address, self.device_config.configuration.port)
except Exception:
log.exception("Fehler im Modul "+self.device_config.name)

def add_component(self, component_config: Union[Dict, CarloGavazziCounterSetup]) -> None:
if isinstance(component_config, Dict):
component_type = component_config["type"]
else:
component_type = component_config.type
component_config = dataclass_from_dict(COMPONENT_TYPE_TO_MODULE[
component_type].component_descriptor.configuration_factory, component_config)
if component_type in self.COMPONENT_TYPE_TO_CLASS:
self.components["component"+str(component_config.id)] = self.COMPONENT_TYPE_TO_CLASS[component_type](
self.device_config.id, component_config, self.client,
self.device_config.configuration.modbus_id)
else:
raise Exception(
"illegal component type " + component_type + ". Allowed values: " +
','.join(self.COMPONENT_TYPE_TO_CLASS.keys())
)

def update(self) -> None:
log.debug("Start device reading " + str(self.components))
if self.components:
for component in self.components:
# Auch wenn bei einer Komponente ein Fehler auftritt, sollen alle anderen noch ausgelesen werden.
with SingleComponentUpdateContext(self.components[component].fault_state):
self.components[component].update()
else:
log.warning(
self.device_config.name +
": Es konnten keine Werte gelesen werden, da noch keine Komponenten konfiguriert wurden."
)


COMPONENT_TYPE_TO_MODULE = {
"counter": counter
}


def read_legacy(component_type: str, ip_address: str, num: Optional[int] = None) -> None:
device_config = CarloGavazzi()
device_config.configuration.ip_address = ip_address
dev = Device(device_config)
if component_type in COMPONENT_TYPE_TO_MODULE:
component_config = COMPONENT_TYPE_TO_MODULE[component_type].component_descriptor.configuration_factory()
else:
raise Exception(
"illegal component type " + component_type + ". Allowed values: " +
','.join(COMPONENT_TYPE_TO_MODULE.keys())
)
component_config.id = num
dev.add_component(component_config)

log.debug('carlo gavazzi IP-Adresse: ' + str(ip_address))

dev.update()


def main(argv: List[str]):
run_using_positional_cli_args(read_legacy, argv)
def create_device(device_config: CarloGavazzi):
def create_counter_component(component_config: CarloGavazziCounterSetup):
return counter.CarloGavazziCounter(device_config.id, component_config, client,
device_config.configuration.modbus_id)

def update_components(components: Iterable[counter.CarloGavazziCounter]):
with client:
for component in components:
with SingleComponentUpdateContext(component.fault_state):
component.update()

try:
client = modbus.ModbusTcpClient_(device_config.configuration.ip_address, device_config.configuration.port)
except Exception:
log.exception("Fehler in create_device")
return ConfigurableDevice(
device_config=device_config,
component_factory=ComponentFactoryByType(
counter=create_counter_component
),
component_updater=MultiComponentUpdater(update_components)
)


device_descriptor = DeviceDescriptor(configuration_factory=CarloGavazzi)
1 change: 0 additions & 1 deletion packages/modules/devices/good_we/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def __init__(self,
self.__modbus_id = modbus_id
self.version = version
self.firmware = firmware
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="bezug")
self.component_config = dataclass_from_dict(GoodWeCounterSetup, component_config)
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="bezug")
self.__tcp_client = tcp_client
Expand Down
Loading