|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import logging |
| 3 | +from pathlib import Path |
| 4 | +from typing import Iterable, Union |
| 5 | + |
| 6 | +from helpermodules.utils.run_command import run_command |
| 7 | +from modules.common.abstract_device import DeviceDescriptor |
| 8 | +from modules.common.component_context import SingleComponentUpdateContext |
| 9 | +from modules.common.configurable_device import ComponentFactoryByType, ConfigurableDevice, MultiComponentUpdater |
| 10 | +from modules.common.modbus import ModbusTcpClient_ |
| 11 | +from modules.devices.huawei.huawei_emma.bat import Huawei_EmmaBat |
| 12 | +from modules.devices.huawei.huawei_emma.config import Huawei_Emma, Huawei_EmmaBatSetup |
| 13 | +from modules.devices.huawei.huawei_emma.config import Huawei_EmmaCounterSetup, Huawei_EmmaInverterSetup |
| 14 | +from modules.devices.huawei.huawei_emma.counter import Huawei_EmmaCounter |
| 15 | +from modules.devices.huawei.huawei_emma.inverter import Huawei_EmmaInverter |
| 16 | + |
| 17 | +log = logging.getLogger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +def create_device(device_config: Huawei_Emma): |
| 21 | + client = None |
| 22 | + |
| 23 | + def create_bat_component(component_config: Huawei_EmmaBatSetup): |
| 24 | + nonlocal client |
| 25 | + return Huawei_EmmaBat(component_config, |
| 26 | + device_id=device_config.id, |
| 27 | + modbus_id=device_config.configuration.modbus_id, |
| 28 | + client=client) |
| 29 | + |
| 30 | + def create_counter_component(component_config: Huawei_EmmaCounterSetup): |
| 31 | + nonlocal client |
| 32 | + return Huawei_EmmaCounter(component_config, |
| 33 | + device_id=device_config.id, |
| 34 | + modbus_id=device_config.configuration.modbus_id, |
| 35 | + client=client) |
| 36 | + |
| 37 | + def create_inverter_component(component_config: Huawei_EmmaInverterSetup): |
| 38 | + nonlocal client |
| 39 | + return Huawei_EmmaInverter(component_config, |
| 40 | + device_id=device_config.id, |
| 41 | + modbus_id=device_config.configuration.modbus_id, |
| 42 | + client=client) |
| 43 | + |
| 44 | + def update_components(components: Iterable[Union[Huawei_EmmaBat, Huawei_EmmaCounter, Huawei_EmmaInverter]]): |
| 45 | + nonlocal client |
| 46 | + with client: |
| 47 | + for component in components: |
| 48 | + with SingleComponentUpdateContext(component.fault_state): |
| 49 | + component.update() |
| 50 | + |
| 51 | + def initializer(): |
| 52 | + nonlocal client |
| 53 | + client = ModbusTcpClient_(device_config.configuration.ip_address, |
| 54 | + device_config.configuration.port) |
| 55 | + |
| 56 | + def error_handler(): |
| 57 | + run_command(f"{Path(__file__).resolve().parents[4]}/modules/common/restart_protoss_admin") |
| 58 | + |
| 59 | + return ConfigurableDevice( |
| 60 | + device_config=device_config, |
| 61 | + initializer=initializer, |
| 62 | + error_handler=error_handler, |
| 63 | + component_factory=ComponentFactoryByType( |
| 64 | + bat=create_bat_component, |
| 65 | + counter=create_counter_component, |
| 66 | + inverter=create_inverter_component, |
| 67 | + ), |
| 68 | + component_updater=MultiComponentUpdater(update_components) |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +device_descriptor = DeviceDescriptor(configuration_factory=Huawei_Emma) |
0 commit comments