forked from openWB/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use create_device for byd, huawei smartlogger, carlo gavazzi, good_we…
…, kostal piko, janitza (openWB#1742) * BYD, CarloGavazzi, GoodWe * sample * huawei smartlogger * kostal piko * janitza
- Loading branch information
Showing
10 changed files
with
154 additions
and
530 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.