Skip to content

Commit

Permalink
Use create_device for byd, huawei smartlogger, carlo gavazzi, good_we…
Browse files Browse the repository at this point in the history
…, kostal piko, janitza (openWB#1742)

* BYD, CarloGavazzi, GoodWe

* sample

* huawei smartlogger

* kostal piko

* janitza
  • Loading branch information
LKuemmel authored Jul 10, 2024
1 parent 6405abe commit 212a673
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 530 deletions.
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

0 comments on commit 212a673

Please sign in to comment.