|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import logging |
| 3 | +from typing import Iterable, Union |
| 4 | + |
| 5 | +from modules.common.configurable_device import ComponentFactoryByType, ConfigurableDevice, MultiComponentUpdater |
| 6 | +from modules.devices.elgris.elgris import bat, counter, inverter |
| 7 | +from modules.devices.elgris.elgris.config import Elgris, ElgrisBatSetup, ElgrisCounterSetup |
| 8 | +from modules.common import modbus |
| 9 | +from modules.common.abstract_device import DeviceDescriptor |
| 10 | +from modules.common.component_context import SingleComponentUpdateContext |
| 11 | +from modules.devices.elgris.elgris.config import ElgrisInverterSetup |
| 12 | + |
| 13 | +log = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +def create_device(device_config: Elgris): |
| 17 | + client = None |
| 18 | + |
| 19 | + def create_bat_component(component_config: ElgrisBatSetup): |
| 20 | + nonlocal client |
| 21 | + return bat.ElgrisBat(component_config=component_config, tcp_client=client, |
| 22 | + modbus_id=device_config.configuration.modbus_id) |
| 23 | + |
| 24 | + def create_counter_component(component_config: ElgrisCounterSetup): |
| 25 | + nonlocal client |
| 26 | + return counter.ElgrisCounter(component_config=component_config, tcp_client=client, |
| 27 | + modbus_id=device_config.configuration.modbus_id) |
| 28 | + |
| 29 | + def create_inverter_component(component_config: ElgrisInverterSetup): |
| 30 | + nonlocal client |
| 31 | + return inverter.ElgrisInverter(component_config=component_config, tcp_client=client, |
| 32 | + modbus_id=device_config.configuration.modbus_id) |
| 33 | + |
| 34 | + def update_components(components: Iterable[Union[bat.ElgrisBat, counter.ElgrisCounter, inverter.ElgrisInverter]]): |
| 35 | + with client: |
| 36 | + for component in components: |
| 37 | + with SingleComponentUpdateContext(component.fault_state): |
| 38 | + component.update() |
| 39 | + |
| 40 | + def initializer(): |
| 41 | + nonlocal client |
| 42 | + client = modbus.ModbusTcpClient_(device_config.configuration.ip_address, device_config.configuration.port) |
| 43 | + |
| 44 | + return ConfigurableDevice( |
| 45 | + device_config=device_config, |
| 46 | + initializer=initializer, |
| 47 | + component_factory=ComponentFactoryByType( |
| 48 | + bat=create_bat_component, |
| 49 | + counter=create_counter_component, |
| 50 | + inverter=create_inverter_component |
| 51 | + ), |
| 52 | + component_updater=MultiComponentUpdater(update_components) |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +device_descriptor = DeviceDescriptor(configuration_factory=Elgris) |
0 commit comments