Skip to content

Commit

Permalink
update modbus sample: add power limit (#2053)
Browse files Browse the repository at this point in the history
* update modbus sample: add power limit

* comment

* ammend
  • Loading branch information
LKuemmel authored Dec 17, 2024
1 parent 3c8156b commit 8289b83
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
12 changes: 9 additions & 3 deletions docs/samples/sample_modbus/bat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
from typing import Optional
from dataclass_utils import dataclass_from_dict
from modules.common.abstract_device import AbstractBat
from modules.common.component_state import BatState
Expand All @@ -11,15 +12,16 @@


class SampleBat(AbstractBat):
def __init__(self, device_id: int, component_config: SampleBatSetup) -> None:
def __init__(self, device_id: int, component_config: SampleBatSetup, client: ModbusTcpClient_) -> None:
self.__device_id = device_id
self.component_config = dataclass_from_dict(SampleBatSetup, component_config)
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))
self.client = client

def update(self, client: ModbusTcpClient_) -> None:
power = client.read_holding_registers(reg, ModbusDataType.INT_32, unit=unit)
def update(self) -> None:
power = self.client.read_holding_registers(reg, ModbusDataType.INT_32, unit=unit)
imported, exported = self.sim_counter.sim_count(power)

bat_state = BatState(
Expand All @@ -30,5 +32,9 @@ def update(self, client: ModbusTcpClient_) -> None:
)
self.store.set(bat_state)

def set_power_limit(self, power_limit: Optional[int]) -> None:
# Methode entfernen, falls die Batterie keine Steuerung der Ladeleistung unterstützt
self.client.write_registers(reg, power_limit)


component_descriptor = ComponentDescriptor(configuration_factory=SampleBatSetup)
7 changes: 4 additions & 3 deletions docs/samples/sample_modbus/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@


class SampleCounter(AbstractCounter):
def __init__(self, device_id: int, component_config: SampleCounterSetup) -> None:
def __init__(self, device_id: int, component_config: SampleCounterSetup, client: ModbusTcpClient_) -> None:
self.__device_id = device_id
self.component_config = dataclass_from_dict(SampleCounterSetup, component_config)
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))
self.client = client

def update(self, client: ModbusTcpClient_):
power = client.read_holding_registers(reg, ModbusDataType.INT_32, unit=unit)
def update(self):
power = self.client.read_holding_registers(reg, ModbusDataType.INT_32, unit=unit)
imported, exported = self.sim_counter.sim_count(power)

counter_state = CounterState(
Expand Down
10 changes: 5 additions & 5 deletions docs/samples/sample_modbus/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@

def create_device(device_config: Sample):
def create_bat_component(component_config: SampleBatSetup):
return SampleBat(device_config.id, component_config, device_config.configuration.ip_address)
return SampleBat(device_config.id, component_config, device_config.configuration.ip_address, client)

def create_counter_component(component_config: SampleCounterSetup):
return SampleCounter(device_config.id, component_config, device_config.configuration.ip_address)
return SampleCounter(device_config.id, component_config, device_config.configuration.ip_address, client)

def create_inverter_component(component_config: SampleInverterSetup):
return SampleInverter(device_config.id, component_config, device_config.configuration.ip_address)
return SampleInverter(device_config.id, component_config, device_config.configuration.ip_address, client)

def update_components(components: Iterable[Union[SampleBat, SampleCounter, SampleInverter]]):
with client as c:
with client:
for component in components:
with SingleComponentUpdateContext(component.fault_state):
component.update(c)
component.update()

try:
client = ModbusTcpClient_(device_config.configuration.ip_address, device_config.configuration.port)
Expand Down
7 changes: 4 additions & 3 deletions docs/samples/sample_modbus/inverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@


class SampleInverter(AbstractInverter):
def __init__(self, device_id: int, component_config: SampleInverterSetup) -> None:
def __init__(self, device_id: int, component_config: SampleInverterSetup, client: ModbusTcpClient_) -> None:
self.__device_id = device_id
self.component_config = dataclass_from_dict(SampleInverterSetup, component_config)
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))
self.client = client

def update(self, client: ModbusTcpClient_) -> None:
power = client.read_holding_registers(reg, ModbusDataType.INT_32, unit=unit)
def update(self) -> None:
power = self.client.read_holding_registers(reg, ModbusDataType.INT_32, unit=unit)
exported = self.sim_counter.sim_count(power)[1]

inverter_state = InverterState(
Expand Down

0 comments on commit 8289b83

Please sign in to comment.