Skip to content

Commit

Permalink
improve communication with huawei (#1517)
Browse files Browse the repository at this point in the history
* change communication with Huawei

* reduce line length

* structure

* flake8

---------

Co-authored-by: ndrsnhs <andreas.neuhaus@openwb.de>
  • Loading branch information
LKuemmel and ndrsnhs authored Apr 4, 2024
1 parent dc11e52 commit 7f27cfa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
17 changes: 12 additions & 5 deletions packages/modules/devices/huawei/bat.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/usr/bin/env python3
import time
from typing import Dict, Union

from dataclass_utils import dataclass_from_dict
from modules.common.component_state import BatState
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.modbus import ModbusDataType, ModbusTcpClient_
from modules.common.simcount import SimCounter
from modules.common.store import get_bat_value_store
from modules.devices.huawei.config import HuaweiBatSetup
Expand All @@ -13,19 +15,24 @@
class HuaweiBat:
def __init__(self,
device_id: int,
component_config: Union[Dict, HuaweiBatSetup]) -> None:
component_config: Union[Dict, HuaweiBatSetup],
modbus_id: int) -> None:
self.__device_id = device_id
self.component_config = dataclass_from_dict(HuaweiBatSetup, component_config)
self.modbus_id = modbus_id
self.sim_counter = SimCounter(self.__device_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))

def update(self, bat_power_reg, bat_soc_reg) -> None:
soc = bat_soc_reg / 10
def update(self, client: ModbusTcpClient_) -> None:
time.sleep(1)
power = client.read_holding_registers(37765, ModbusDataType.INT_32, unit=self.modbus_id)
time.sleep(1)
soc = client.read_holding_registers(37760, ModbusDataType.INT_16, unit=self.modbus_id) / 10

imported, exported = self.sim_counter.sim_count(bat_power_reg)
imported, exported = self.sim_counter.sim_count(power)
bat_state = BatState(
power=bat_power_reg,
power=power,
soc=soc,
imported=imported,
exported=exported
Expand Down
15 changes: 11 additions & 4 deletions packages/modules/devices/huawei/counter.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/usr/bin/env python3
import time
from typing import Dict, Union

from dataclass_utils import dataclass_from_dict
from modules.common.component_state import CounterState
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.modbus import ModbusDataType, ModbusTcpClient_
from modules.common.simcount import SimCounter
from modules.common.store import get_counter_value_store
from modules.devices.huawei.config import HuaweiCounterSetup
Expand All @@ -13,16 +15,21 @@
class HuaweiCounter:
def __init__(self,
device_id: int,
component_config: Union[Dict, HuaweiCounterSetup]) -> None:
component_config: Union[Dict, HuaweiCounterSetup],
modbus_id: int) -> None:
self.__device_id = device_id
self.component_config = dataclass_from_dict(HuaweiCounterSetup, component_config)
self.modbus_id = modbus_id
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="bezug")
self.store = get_counter_value_store(self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))

def update(self, counter_currents_reg, counter_power_reg):
power = counter_power_reg * -1
currents = [val / -100 for val in counter_currents_reg]
def update(self, client: ModbusTcpClient_):
time.sleep(1)
currents = client.read_holding_registers(37107, [ModbusDataType.INT_32]*3, unit=self.modbus_id)
currents = [val / -100 for val in currents]
time.sleep(1)
power = client.read_holding_registers(37113, ModbusDataType.INT_32, unit=self.modbus_id) * -1

imported, exported = self.sim_counter.sim_count(power)

Expand Down
28 changes: 5 additions & 23 deletions packages/modules/devices/huawei/device.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#!/usr/bin/env python3
import logging
import time
from typing import Iterable, Union

from modules.common.abstract_device import DeviceDescriptor
from modules.common.component_context import SingleComponentUpdateContext
from modules.common.configurable_device import ComponentFactoryByType, ConfigurableDevice, MultiComponentUpdater
from modules.common.modbus import ModbusDataType, ModbusTcpClient_
from modules.common.modbus import ModbusTcpClient_
from modules.devices.huawei.bat import HuaweiBat
from modules.devices.huawei.config import Huawei, HuaweiBatSetup, HuaweiCounterSetup, HuaweiInverterSetup
from modules.devices.huawei.counter import HuaweiCounter
Expand All @@ -17,38 +16,21 @@

def create_device(device_config: Huawei):
def create_bat_component(component_config: HuaweiBatSetup):
return HuaweiBat(device_config.id, component_config)
return HuaweiBat(device_config.id, component_config, device_config.configuration.modbus_id)

def create_counter_component(component_config: HuaweiCounterSetup):
return HuaweiCounter(device_config.id, component_config)
return HuaweiCounter(device_config.id, component_config, device_config.configuration.modbus_id)

def create_inverter_component(component_config: HuaweiInverterSetup):
return HuaweiInverter(device_config.id, component_config)
return HuaweiInverter(device_config.id, component_config, device_config.configuration.modbus_id)

def update_components(components: Iterable[Union[HuaweiBat, HuaweiCounter, HuaweiInverter]]):
if client.is_socket_open() is False:
client.connect()
try:
modbus_id = device_config.configuration.modbus_id
regs = client.read_holding_registers(32064, [ModbusDataType.INT_32]*5701, unit=modbus_id)
counter_currents_reg = regs[5043:5045] # INT 32, 37107-9
counter_power_reg = regs[5049] # INT32, 37113
bat_power_reg = regs[-1] # INT32, 37765
inverter_power_reg = regs[0] # INT32 32064
# Huawei darf nur mit Regelintervall sehr langsam betrieben werden, daher kann hier eine längere Pause
# eingelegt werden. Ob auch eine kürzere ausreichend ist, ist nicht getestet.
time.sleep(5)
bat_soc_reg = client.read_holding_registers(
37760, ModbusDataType.INT_16, unit=modbus_id) # Int 16 37760

for component in components:
with SingleComponentUpdateContext(component.fault_state):
if isinstance(component, HuaweiBat):
component.update(bat_power_reg, bat_soc_reg)
elif isinstance(component, HuaweiCounter):
component.update(counter_currents_reg, counter_power_reg)
elif isinstance(component, HuaweiInverter):
component.update(inverter_power_reg)
component.update(client)
except Exception as e:
client.close()
raise e
Expand Down
11 changes: 8 additions & 3 deletions packages/modules/devices/huawei/inverter.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/usr/bin/env python3
import time
from typing import Dict, Union

from dataclass_utils import dataclass_from_dict
from modules.common.component_state import InverterState
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.modbus import ModbusDataType, ModbusTcpClient_
from modules.common.simcount import SimCounter
from modules.common.store import get_inverter_value_store
from modules.devices.huawei.config import HuaweiInverterSetup
Expand All @@ -13,15 +15,18 @@
class HuaweiInverter:
def __init__(self,
device_id: int,
component_config: Union[Dict, HuaweiInverterSetup]) -> None:
component_config: Union[Dict, HuaweiInverterSetup],
modbus_id: int) -> None:
self.__device_id = device_id
self.component_config = dataclass_from_dict(HuaweiInverterSetup, component_config)
self.modbus_id = modbus_id
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="pv")
self.store = get_inverter_value_store(self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))

def update(self, inverter_power_reg) -> None:
power = inverter_power_reg * -1
def update(self, client: ModbusTcpClient_) -> None:
time.sleep(1)
power = client.read_holding_registers(32064, ModbusDataType.INT_32, unit=self.modbus_id) * -1

_, exported = self.sim_counter.sim_count(power)
inverter_state = InverterState(
Expand Down

0 comments on commit 7f27cfa

Please sign in to comment.