|
1 | 1 | import inspect |
| 2 | +import logging |
2 | 3 | from typing import TypeVar, Generic, Dict, Any, Callable, Iterable, List |
3 | 4 |
|
4 | 5 | from dataclass_utils import dataclass_from_dict |
5 | 6 | from modules.common.abstract_device import AbstractDevice |
6 | 7 | from modules.common.component_context import SingleComponentUpdateContext, MultiComponentUpdateContext |
7 | | -from modules.common.fault_state import FaultState |
| 8 | +from modules.common.fault_state import ComponentInfo, FaultState |
8 | 9 |
|
9 | 10 | T_DEVICE_CONFIG = TypeVar("T_DEVICE_CONFIG") |
10 | 11 | T_COMPONENT = TypeVar("T_COMPONENT") |
|
13 | 14 | ComponentUpdater = Callable[[Iterable[T_COMPONENT]], None] |
14 | 15 | ComponentFactory = Callable[[T_COMPONENT_CONFIG], T_COMPONENT] |
15 | 16 |
|
| 17 | +log = logging.getLogger(__name__) |
| 18 | + |
16 | 19 |
|
17 | 20 | class IndependentComponentUpdater(Generic[T_COMPONENT]): |
18 | 21 | def __init__(self, updater: Callable[[T_COMPONENT], None]): |
19 | 22 | self.__updater = updater |
20 | 23 |
|
21 | | - def __call__(self, components: Iterable[T_COMPONENT]) -> None: |
| 24 | + def __call__(self, components: Iterable[T_COMPONENT], error_handler: Callable) -> None: |
22 | 25 | for component in components: |
23 | | - with SingleComponentUpdateContext(component.fault_state): |
| 26 | + with SingleComponentUpdateContext(component.fault_state, error_handler): |
24 | 27 | self.__updater(component) |
25 | 28 |
|
26 | 29 |
|
27 | 30 | class MultiComponentUpdater: |
28 | 31 | def __init__(self, updater: Callable[[List[T_COMPONENT]], None]): |
29 | 32 | self.__updater = updater |
30 | 33 |
|
31 | | - def __call__(self, components: Iterable[T_COMPONENT]) -> None: |
| 34 | + def __call__(self, components: Iterable[T_COMPONENT], error_handler: Callable) -> None: |
32 | 35 | components_list = list(components) |
33 | | - with MultiComponentUpdateContext(components_list): |
| 36 | + with MultiComponentUpdateContext(components_list, error_handler): |
34 | 37 | if not components: |
35 | 38 | raise FaultState.warning("Keine Komponenten konfiguriert") |
36 | 39 | self.__updater(components_list) |
@@ -62,14 +65,23 @@ class ConfigurableDevice(Generic[T_COMPONENT, T_DEVICE_CONFIG, T_COMPONENT_CONFI |
62 | 65 | def __init__(self, |
63 | 66 | device_config: T_DEVICE_CONFIG, |
64 | 67 | component_factory: ComponentFactory[Any, T_COMPONENT], |
65 | | - component_updater: ComponentUpdater[T_COMPONENT]) -> None: |
| 68 | + component_updater: ComponentUpdater[T_COMPONENT], |
| 69 | + initializer: Callable = lambda: None) -> None: |
| 70 | + self.__initializer = initializer |
66 | 71 | self.__component_factory = component_factory |
67 | 72 | self.__component_updater = component_updater |
68 | 73 | self.device_config = device_config |
69 | 74 | self.components: Dict[str, T_COMPONENT] = {} |
70 | 75 |
|
| 76 | + try: |
| 77 | + self.__initializer() |
| 78 | + except Exception: |
| 79 | + log.exception(f"Initialisierung von Gerät {self.device_config.name} fehlgeschlagen") |
| 80 | + |
71 | 81 | def add_component(self, component_config: T_COMPONENT_CONFIG) -> None: |
72 | | - self.components["component" + str(component_config.id)] = self.__component_factory(component_config) |
| 82 | + with SingleComponentUpdateContext(FaultState(ComponentInfo.from_component_config(component_config)), |
| 83 | + self.__initializer): |
| 84 | + self.components["component" + str(component_config.id)] = self.__component_factory(component_config) |
73 | 85 |
|
74 | 86 | def update(self): |
75 | | - self.__component_updater(self.components.values()) |
| 87 | + self.__component_updater(self.components.values(), self.__initializer) |
0 commit comments