From 1ded4b96aa2a0fafeb708c40aa94cc3ddffa604e Mon Sep 17 00:00:00 2001 From: LKuemmel Date: Thu, 12 Oct 2023 14:16:19 +0200 Subject: [PATCH] fix loadmanagement for sub counter --- .../control/algorithm/additional_current.py | 11 +++------ packages/control/algorithm/algorithm.py | 2 +- packages/control/algorithm/common.py | 24 ++++++++++++------- packages/control/algorithm/common_test.py | 6 ++--- packages/control/algorithm/min_current.py | 11 +++++---- packages/control/algorithm/no_current.py | 6 +++++ .../control/algorithm/surplus_controlled.py | 9 ++----- 7 files changed, 38 insertions(+), 31 deletions(-) diff --git a/packages/control/algorithm/additional_current.py b/packages/control/algorithm/additional_current.py index 9f89ba75c7..e677948697 100644 --- a/packages/control/algorithm/additional_current.py +++ b/packages/control/algorithm/additional_current.py @@ -5,7 +5,7 @@ from control.loadmanagement import LimitingValue, Loadmanagement from control.counter import Counter from control.chargepoint.chargepoint import Chargepoint -from control.algorithm.filter_chargepoints import (get_chargepoints_by_mode, get_chargepoints_by_mode_and_counter, +from control.algorithm.filter_chargepoints import (get_chargepoints_by_mode_and_counter, get_preferenced_chargepoint_charging) from modules.common.utils.component_parser import get_component_name_by_id @@ -17,7 +17,7 @@ def __init__(self) -> None: pass def set_additional_current(self, mode_range: List[int]) -> None: - self._reset_current() + common.reset_current_by_chargemode(common.CHARGEMODES[0:8]) for mode_tuple, counter in common.mode_and_counter_generator(mode_range): preferenced_chargepoints, preferenced_cps_without_set_current = get_preferenced_chargepoint_charging( get_chargepoints_by_mode_and_counter(mode_tuple, f"counter{counter.num}")) @@ -48,14 +48,9 @@ def _set_loadmangement_message(self, chargepoint: Chargepoint, counter: Counter) -> None: # Strom muss an diesem Zähler geändert werden - if (current != max(chargepoint.data.set.target_current, chargepoint.data.set.current) and + if (current != max(chargepoint.data.set.target_current, chargepoint.data.set.current or 0) and # Strom erreicht nicht die vorgegebene Stromstärke current != max( chargepoint.data.set.charging_ev_data.data.control_parameter.required_currents)): chargepoint.set_state_and_log(f"Es kann nicht mit der vorgegebenen Stromstärke geladen werden" f"{limit.value.format(get_component_name_by_id(counter.num))}") - - def _reset_current(self) -> None: - for mode in common.CHARGEMODES[0:8]: - for cp in get_chargepoints_by_mode(mode): - cp.data.set.current = 0 diff --git a/packages/control/algorithm/algorithm.py b/packages/control/algorithm/algorithm.py index 08de281913..be2923c3fb 100644 --- a/packages/control/algorithm/algorithm.py +++ b/packages/control/algorithm/algorithm.py @@ -27,7 +27,6 @@ def calc_current(self) -> None: self._check_auto_phase_switch_delay() self.surplus_controlled.check_submode_pv_charging() common.reset_current() - common.reset_current_to_target_current() log.info("**Mindestrom setzen**") self.min_current.set_min_current() log.info("**Sollstrom setzen**") @@ -43,6 +42,7 @@ def calc_current(self) -> None: else: log.info("**Keine Leistung für PV-geführtes Laden übrig.**") self.no_current.set_no_current() + self.no_current.set_none_current() except Exception: log.exception("Fehler im Algorithmus-Modul") diff --git a/packages/control/algorithm/common.py b/packages/control/algorithm/common.py index 329407fcc4..f1f690c02e 100644 --- a/packages/control/algorithm/common.py +++ b/packages/control/algorithm/common.py @@ -2,6 +2,7 @@ from typing import Iterable, List, Optional, Tuple from control import data +from control.algorithm.filter_chargepoints import get_chargepoints_by_mode from control.chargemode import Chargemode from control.chargepoint.chargepoint import Chargepoint from control.counter import Counter @@ -34,11 +35,18 @@ def reset_current(): for cp in data.data.cp_data.values(): try: - cp.data.set.current = 0 + cp.data.set.current = None + cp.data.set.target_current = 0 except Exception: log.exception(f"Fehler im Algorithmus-Modul für Ladepunkt{cp.num}") +def reset_current_by_chargemode(mode_tuple: Tuple[Optional[str], str, bool]) -> None: + for mode in mode_tuple: + for cp in get_chargepoints_by_mode(mode): + cp.data.set.current = None + + def mode_range_list_factory() -> List[int]: return [0, -1] @@ -94,12 +102,12 @@ def get_current_to_set(set_current: float, diff: float, prev_current: float) -> Zähler nicht zu untergraben. Der Vergleich muss positiv sein, wenn zum ersten Mal auf dieser Stufe ein Strom gesetzt wird.""" new_current = prev_current + diff - if new_current > set_current and set_current != 0: - log.debug("Neuer Sollstrom darf nicht höher als bisher gesetzter sein: " - f"bisher {set_current}A, neuer {new_current}") - return set_current - else: - return new_current + if set_current is not None: + if new_current > set_current: + log.debug("Neuer Sollstrom darf nicht höher als bisher gesetzter sein: " + f"bisher {set_current}A, neuer {new_current}") + return set_current + return new_current # tested @@ -130,7 +138,7 @@ def update_raw_data(preferenced_chargepoints: List[Chargepoint], continue charging_ev_data = chargepoint.data.set.charging_ev_data required_currents = charging_ev_data.data.control_parameter.required_currents - max_target_set_current = max(chargepoint.data.set.target_current, chargepoint.data.set.current) + max_target_set_current = max(chargepoint.data.set.target_current, chargepoint.data.set.current or 0) if diff_to_zero is False: if charging_ev_data.ev_template.data.min_current < max_target_set_current: diff --git a/packages/control/algorithm/common_test.py b/packages/control/algorithm/common_test.py index 76dc8d9f44..f187860c90 100644 --- a/packages/control/algorithm/common_test.py +++ b/packages/control/algorithm/common_test.py @@ -18,8 +18,8 @@ def cp() -> None: @pytest.mark.parametrize("set_current, expected_current", - [pytest.param(6, 0), - pytest.param(0, 0)]) + [pytest.param(6, None), + pytest.param(0, None)]) def test_reset_current(set_current: int, expected_current: int): # setup data.data.cp_data["cp0"].data.set.current = set_current @@ -87,7 +87,7 @@ def test_get_min_current(required_currents: List[float], expected_mins_counts: T @pytest.mark.parametrize( "set_current, diff, expected_current", [ - pytest.param(0, 2, 8, id="min current is set, no current has been set on this iteration"), + pytest.param(None, 2, 8, id="min current is set, no current has been set on this iteration"), pytest.param(6, 2, 6, id="min current is set, current has been set on this iteration"), pytest.param(7, 2, 7, id="new current is higher, current has been set on this iteration"), pytest.param(9, 2, 8, id="new current is lower, current has been set on this iteration"), diff --git a/packages/control/algorithm/min_current.py b/packages/control/algorithm/min_current.py index 74bc68aaf1..6f52173925 100644 --- a/packages/control/algorithm/min_current.py +++ b/packages/control/algorithm/min_current.py @@ -24,6 +24,8 @@ def set_min_current(self) -> None: available_currents, limit = Loadmanagement().get_available_currents(missing_currents, counter) available_for_cp = common.available_current_for_cp( cp, counts, available_currents, missing_currents) + current = common.get_current_to_set( + cp.data.set.current, available_for_cp, cp.data.set.target_current) if common.consider_not_charging_chargepoint_in_loadmanagement(cp): cp.data.set.current = cp.data.set.charging_ev_data.ev_template.data.min_current log.debug( @@ -31,10 +33,11 @@ def set_min_current(self) -> None: "A. Zuteilung ohne Berücksichtigung im Lastmanagement, da kein Ladestart zu erwarten " "ist und Reserve für nicht-ladende inaktiv.") else: - if available_for_cp < cp.data.set.charging_ev_data.ev_template.data.min_current: - common.set_current_counterdiff(-cp.data.set.current, 0, cp) - cp.set_state_and_log( - f"Ladung kann nicht gestartet werden{limit.value.format(counter.num)}") + if current < cp.data.set.charging_ev_data.ev_template.data.min_current: + common.set_current_counterdiff(-(cp.data.set.current or 0), 0, cp) + if limit: + cp.set_state_and_log( + f"Ladung kann nicht gestartet werden{limit.value.format(counter.num)}") else: common.set_current_counterdiff( (cp.data.set.charging_ev_data.ev_template.data.min_current diff --git a/packages/control/algorithm/no_current.py b/packages/control/algorithm/no_current.py index 344cb9840e..3c17a27aba 100644 --- a/packages/control/algorithm/no_current.py +++ b/packages/control/algorithm/no_current.py @@ -1,5 +1,6 @@ import logging from typing import List +from control import data from control.algorithm.common import CHARGEMODES from control.chargepoint.chargepoint import Chargepoint @@ -18,3 +19,8 @@ def set_no_current(self) -> None: chargepoints.extend(get_chargepoints_by_mode(mode)) for cp in chargepoints: cp.data.set.current = 0 + + def set_none_current(self) -> None: + for cp in data.data.cp_data.values(): + if cp.data.set.current is None: + cp.data.set.current = 0 diff --git a/packages/control/algorithm/surplus_controlled.py b/packages/control/algorithm/surplus_controlled.py index 99f41d63c3..f66828454d 100644 --- a/packages/control/algorithm/surplus_controlled.py +++ b/packages/control/algorithm/surplus_controlled.py @@ -6,7 +6,7 @@ from control.loadmanagement import LimitingValue, Loadmanagement from control.counter import Counter from control.chargepoint.chargepoint import Chargepoint -from control.algorithm.filter_chargepoints import (get_chargepoints_by_mode, get_chargepoints_by_mode_and_counter, +from control.algorithm.filter_chargepoints import (get_chargepoints_by_mode_and_counter, get_preferenced_chargepoint_charging, get_chargepoints_pv_charging, get_chargepoints_surplus_controlled) from control.chargepoint.chargepoint_state import ChargepointState, CHARGING_STATES @@ -20,7 +20,7 @@ def __init__(self) -> None: pass def set_surplus_current(self, mode_range) -> None: - self._reset_current() + common.reset_current_by_chargemode(common.CHARGEMODES[6:12]) for mode_tuple, counter in common.mode_and_counter_generator(mode_range): preferenced_chargepoints, preferenced_cps_without_set_current = get_preferenced_chargepoint_charging( get_chargepoints_by_mode_and_counter(mode_tuple, f"counter{counter.num}")) @@ -58,11 +58,6 @@ def _set(self, surplus=True) chargepoints.pop(0) - def _reset_current(self) -> None: - for mode in common.CHARGEMODES[6:12]: - for cp in get_chargepoints_by_mode(mode): - cp.data.set.current = 0 - def _set_loadmangement_message(self, current: float, limit: LimitingValue,