Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
LKuemmel committed Nov 24, 2022
1 parent a998506 commit 2d4dee3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 deletions.
2 changes: 1 addition & 1 deletion modules/bezug_kostalplenticoreem300haus/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ else
MYLOGFILE="${RAMDISKDIR}/evu.log"
fi

bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.kostal_plenticore.device" "counter" "${kostalplenticorehaus}" >> "$MYLOGFILE" 2>&1
bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.kostal_plenticore.device" "counter" "${kostalplenticoreip}" "" "0" "" "${kostalplenticorehaus}" >> "$MYLOGFILE" 2>&1

cat "${RAMDISKDIR}/wattbezug"
2 changes: 1 addition & 1 deletion modules/speicher_kostalplenticore/main.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash

# wr_plenticore will fetch data for both inverter and counter and there is nothing left for us to do.
# wr_plenticore will fetch data for both PV and battery and there is nothing left for us to do.
#
# The usage of speicher_kostalplenticore without wr_plenticore is not intended and thus not handled.
6 changes: 3 additions & 3 deletions packages/modules/kostal_plenticore/bat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def __init__(self,
self.sim_counter = SimCounter(device_id, self.component_config.id, prefix="speicher")
self.component_info = ComponentInfo.from_component_config(self.component_config)

def update(self, reader: Callable[[int, ModbusDataType], Any]) -> BatState:
power = reader(582, ModbusDataType.INT_16)
def read_state(self, reader: Callable[[int, ModbusDataType], Any]) -> BatState:
power = reader(582, ModbusDataType.INT_16) * -1
soc = reader(514, ModbusDataType.INT_16)
imported, exported = self.sim_counter.sim_count(power)

Expand All @@ -30,7 +30,7 @@ def update(self, reader: Callable[[int, ModbusDataType], Any]) -> BatState:
exported=exported,
)

def set(self, state):
def update(self, state):
self.store.set(state)


Expand Down
4 changes: 2 additions & 2 deletions packages/modules/kostal_plenticore/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ def get_values(self, reader: Callable[[int, ModbusDataType], Any]) -> CounterSta
frequency=frequency
)

def get_imported_exported(self, state):
def update_imported_exported(self, state):
state.imported, state.exported = self.sim_counter.sim_count(state.power)
return state

def update(self, reader: Callable[[int, ModbusDataType], Any]):
self.store.set(self.get_imported_exported(self.get_values(reader)))
self.store.set(self.update_imported_exported(self.get_values(reader)))


component_descriptor = ComponentDescriptor(configuration_factory=KostalPlenticoreCounterSetup)
49 changes: 25 additions & 24 deletions packages/modules/kostal_plenticore/device.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# !/usr/bin/env python3
from enum import IntEnum
from ipparser import ipparser
from itertools import chain
from typing import Any, Callable, Iterable, List, Union
from pymodbus.constants import Endian
from ipparser import ipparser
import logging
import functools
from modules.common.component_state import BatState, InverterState
import logging

from helpermodules.cli import run_using_positional_cli_args
from modules.common import modbus
from modules.common.abstract_device import DeviceDescriptor
from modules.common.component_state import BatState, InverterState
from modules.common.configurable_device import ConfigurableDevice, ComponentFactoryByType, MultiComponentUpdater
from modules.common.store import get_counter_value_store
from modules.kostal_plenticore.bat import KostalPlenticoreBat
from modules.kostal_plenticore.inverter import KostalPlenticoreInverter
from modules.kostal_plenticore.counter import KostalPlenticoreCounter
from modules.kostal_plenticore.config import (KostalPlenticore, KostalPlenticoreBatSetup, KostalPlenticoreCounterSetup,
KostalPlenticoreInverterSetup)
from modules.kostal_plenticore.bat import KostalPlenticoreBat
from modules.common.store import get_counter_value_store
from modules.common.configurable_device import ConfigurableDevice, ComponentFactoryByType, MultiComponentUpdater
from modules.common.abstract_device import DeviceDescriptor
from modules.common import modbus
from helpermodules.cli import run_using_positional_cli_args
from modules.kostal_plenticore.counter import KostalPlenticoreCounter


log = logging.getLogger(__name__)
Expand All @@ -33,21 +33,21 @@ def update(
reader: Callable[[int, modbus.ModbusDataType], Any],
set_inverter_state: bool = True):
battery = next((component for component in components if isinstance(component, KostalPlenticoreBat)), None)
bat_state_net = battery.update(reader) if battery else None
bat_state_net = battery.read_state(reader) if battery else None
for component in components:
if isinstance(component, KostalPlenticoreInverter):
inverter_state = component.update(reader)
inverter_state = component.read_state(reader)
if bat_state_net:
dc_in = component.dc_in_string_1_2(reader)
home_consumption = component.home_consumption(reader)
if dc_in >= 0:
# Wird PV-DC-Leistung erzeugt, müssen die Wandlungsverluste betrachtet werden.
# Kostal liefert nur DC-seitige Werte.
if bat_state_net.power > 0:
if bat_state_net.power < 0:
# Wird die Batterie entladen, werden die Wandlungsverluste anteilig an der DC-Leistung auf PV
# und Batterie verteilt. Dazu muss der Divisor Total_DC_power != 0 sein.
power_gross = bat_state_net.power + dc_in
pv_state = InverterState(power=bat_state_net.power / power_gross * inverter_state.power,
power_gross = dc_in - bat_state_net.power
pv_state = InverterState(power=dc_in / power_gross * inverter_state.power,
exported=inverter_state.exported)
# Speicherladung muss durch Wandlungsverluste und internen Verbrauch korrigiert werden, sonst
# wird ein falscher Hausverbrauch berechnet. Die Verluste fallen hier unter den Tisch.
Expand All @@ -59,24 +59,25 @@ def update(
# Ladeleistung der Batterie. Die PV-Leistung ist die Summe aus verlustbehafteter
# AC-Leistungsabgabe des WR und der DC-Ladeleistung. Die Wandlungsverluste werden also nur
# in der PV-Leistung ersichtlich.
pv_state = InverterState(power=inverter_state.power + bat_state_net.power,
pv_state = InverterState(power=inverter_state.power - bat_state_net.power,
exported=inverter_state.exported)
bat_state_gross = bat_state_net
# https://github.com/snaptec/openWB/pull/2440#discussion_r996275286
# power_gross = bat_state.power + dc_in
# bat_state_gross = BatteryState(power=bat_state_net.power / power_gross * inverter_state.power)
# pv_state = InverterState(power=inverter_state.power - bat_state_gross.power)
else:
inverter_state.power = 0
pv_state = inverter_state
bat_state_gross = bat_state_net
else:
pv_state = inverter_state
if set_inverter_state:
component.set(pv_state)
component.update(pv_state)
elif isinstance(component, KostalPlenticoreCounter):
component.update(reader)
if bat_state_net:
battery.set(bat_state_gross)
battery.update(bat_state_gross)
if set_inverter_state is False:
return pv_state

Expand Down Expand Up @@ -118,14 +119,14 @@ def read_legacy_inverter(ip1: str, ip2: str, battery: int, ip3: str) -> Inverter

def get_hybrid_inverter_state(ip: str) -> InverterState:
battery_component = KostalPlenticoreBat(1, KostalPlenticoreBatSetup())
with modbus.ModbusTcpClient_(ip) as client:
with modbus.ModbusTcpClient_(ip, 1502) as client:
return update(
[inverter_component, battery_component], _create_reader(client), set_inverter_state=False
)

def get_standard_inverter_state(ip: str) -> InverterState:
with modbus.ModbusTcpClient_(ip) as client:
return inverter_component.update(_create_reader(client))
with modbus.ModbusTcpClient_(ip, 1502) as client:
return inverter_component.read_state(_create_reader(client))

def inverter_state_sum(a: InverterState, b: InverterState) -> InverterState:
return InverterState(exported=a.exported + b.exported, power=a.power + b.power)
Expand All @@ -135,7 +136,7 @@ def inverter_state_sum(a: InverterState, b: InverterState) -> InverterState:
map(get_standard_inverter_state, filter("none".__ne__, chain([ip2], ipparser(ip3)))),
get_hybrid_inverter_state(ip1) if battery else get_standard_inverter_state(ip1)
)
inverter_component.set(inverter_state)
inverter_component.update(inverter_state)
return inverter_state


Expand All @@ -150,10 +151,10 @@ def read_legacy_counter(ip1: str, ip2: str, battery: int, ip3: str, position: in
else:
with client:
counter_state = counter_component.get_values(reader)
bat_power = KostalPlenticoreBat(None, KostalPlenticoreBatSetup()).update(reader).power
bat_power = KostalPlenticoreBat(None, KostalPlenticoreBatSetup()).read_state(reader).power
inverter_power = read_legacy_inverter(ip1, ip2, battery, ip3).power
counter_state.power += inverter_power + bat_power
counter_state = counter_component.get_imported_exported(counter_state)
counter_state = counter_component.update_imported_exported(counter_state)
get_counter_value_store(None).set(counter_state)


Expand Down
6 changes: 3 additions & 3 deletions packages/modules/kostal_plenticore/inverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def __init__(self,
self.store = get_inverter_value_store(self.component_config.id)
self.component_info = ComponentInfo.from_component_config(self.component_config)

def update(self, reader: Callable[[int, ModbusDataType], Any]) -> InverterState:
def read_state(self, reader: Callable[[int, ModbusDataType], Any]) -> InverterState:
# PV-Anlage kann nichts verbrauchen, also ggf. Register-/Rundungsfehler korrigieren.
power = min(0, reader(575, ModbusDataType.INT_16)) * -1
power = reader(575, ModbusDataType.INT_16) * -1
exported = reader(320, ModbusDataType.FLOAT_32)

return InverterState(
Expand All @@ -31,7 +31,7 @@ def dc_in_string_1_2(self, reader: Callable[[int, ModbusDataType], Any]):
def home_consumption(self, reader: Callable[[int, ModbusDataType], Any]):
return reader(106, ModbusDataType.FLOAT_32)

def set(self, state):
def update(self, state):
self.store.set(state)


Expand Down

0 comments on commit 2d4dee3

Please sign in to comment.