Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/modules/common/store/_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from modules.common.store import ValueStore
from modules.common.store._api import LoggingValueStore
from modules.common.store._broker import pub_to_broker
from modules.common.utils.component_parser import get_hierarchy_obj_by_id
from modules.common.utils.component_parser import get_consumer_name_by_id, get_hierarchy_obj_by_id


class ConsumerValueStoreBroker(ValueStore[ConsumerState]):
Expand Down Expand Up @@ -54,6 +54,10 @@ def update(self) -> None:
currents=consumer_state.currents,
powers=consumer_state.powers,
))
except ValueError:
raise Exception(
f"Zähler {extra_meter_id} für die separate Leistungsmessung von Verbraucher "
f"{get_consumer_name_by_id(self.delegate.delegate.num)} ist im Fehlerzustand oder gelöscht worden.")
Comment on lines +57 to +60
except Exception:
raise Exception(f"Fehler beim Auslesen des Verbrauchszählers {extra_meter_id} "
f"für Verbraucher {self.delegate.delegate.num}")
Expand Down
12 changes: 0 additions & 12 deletions packages/modules/common/store/_counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,7 @@ def _add_values(self, element, calc_imported_exported: bool):
self.exported += element.exported
self.power += element.power

def _get_assigned_extra_meter_ids(self) -> set:
assigned_extra_meter_ids = set()
for consumer in data.data.consumer_data.values():
extra_meter_id = consumer.data.extra_meter
if extra_meter_id is not None:
assigned_extra_meter_ids.add(extra_meter_id)
return assigned_extra_meter_ids

def calc_consumers(self, elements: Dict, calc_imported_exported: bool = False) -> CounterState:
assigned_extra_meter_ids = self._get_assigned_extra_meter_ids()
for element in elements:
try:
if element["type"] == ComponentType.CHARGEPOINT.value:
Expand All @@ -120,9 +111,6 @@ def calc_consumers(self, elements: Dict, calc_imported_exported: bool = False) -
consumer = get_hierarchy_obj_by_id(element["id"], element["type"])
consumer_state = consumer.module.store.delegate.delegate.state
self._add_values(consumer_state, calc_imported_exported)
elif element["type"] == ComponentType.COUNTER.value and element["id"] in assigned_extra_meter_ids:
log.debug(f"Zähler counter{element['id']} wird übersprungen, da er als separater Zähler "
"einem Verbraucher zugeordnet ist.")
else:
component = get_hierarchy_obj_by_id(element["id"], element["type"])
self._add_values(component.store.delegate.delegate.state, calc_imported_exported)
Comment thread
LKuemmel marked this conversation as resolved.
Resolved
Expand Down
77 changes: 0 additions & 77 deletions packages/modules/common/store/_counter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,80 +270,3 @@ def mock_get_hierarchy_obj_by_id(component_id, component_type):
)

assert vars(result_state) == vars(expected_state)


def test_calc_consumers_skips_assigned_extra_meter_counter(monkeypatch):
# setup
data.data.counter_all_data.data.get.hierarchy = [
{
"id": 0,
"type": "counter",
"children": [
{"id": 1, "type": "consumer", "children": []},
{"id": 2, "type": "counter", "children": []},
],
}
]
elements = [
{"id": 1, "type": "consumer", "children": []},
{"id": 2, "type": "counter", "children": []},
]

consumer_state = Mock(
power=1000,
currents=[1.0, 1.0, 1.0],
imported=10,
exported=0,
)
consumer_obj = Mock(
data=Mock(extra_meter=2),
module=Mock(
store=Mock(
delegate=Mock(
delegate=Mock(state=consumer_state)
)
)
)
)
data.data.consumer_data["consumer1"] = consumer_obj

counter_state = CounterState(
power=2000,
currents=[2.0, 2.0, 2.0],
imported=20,
exported=0,
)
counter_component = Mock(
store=Mock(
delegate=Mock(
delegate=Mock(state=counter_state)
)
)
)

def mock_get_hierarchy_obj_by_id(component_id, component_type):
if component_id == 1 and component_type == ComponentType.CONSUMER.value:
return consumer_obj
if component_id == 2 and component_type == ComponentType.COUNTER.value:
return counter_component
raise ValueError(f"unexpected lookup: {component_type}{component_id}")

monkeypatch.setattr(_counter, "get_hierarchy_obj_by_id", mock_get_hierarchy_obj_by_id)

purge = PurgeCounterState(delegate=Mock(delegate=Mock(num=0)),
add_child_values=True,
simcounter=SimCounter(0, 0, ComponentType.COUNTER))
purge.currents = [0.0, 0.0, 0.0]
purge.power = 0
purge.imported = 0
purge.exported = 0
purge.incomplete_currents = False

# execution
result_state = purge.calc_consumers(elements, calc_imported_exported=True)

# evaluation
assert result_state.power == 1000
assert result_state.currents == [1.0, 1.0, 1.0]
assert result_state.imported == 10
assert result_state.exported == 0
12 changes: 10 additions & 2 deletions packages/modules/common/utils/component_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ def get_io_name_by_id(id: int):
raise ValueError(f"Element {id} konnte keinem Gerät zugeordnet werden.")


def get_consumer_name_by_id(id: int):
for consumer in data.data.consumer_data.values():
if consumer.module.config.id == id:
return consumer.module.config.name
else:
raise ValueError(f"Element {id} konnte keinem Verbraucher zugeordnet werden.")


def get_finished_component_obj_by_id(id: int, not_finished_threads: List[str]) -> Optional[Any]:
for item in data.data.system_data.values():
if isinstance(item, AbstractDevice):
Expand Down Expand Up @@ -73,7 +81,7 @@ def get_hierarchy_obj_by_id(id: int, element_type: str) -> Any:
for comp in item.components.values():
if comp.component_config.id == id:
obj = comp
else:
if obj is None:
raise ValueError(f"Element {id} vom Typ {element_type} konnte nicht aufgelöst werden.")
if obj is not None:
else:
return obj
5 changes: 5 additions & 0 deletions packages/modules/loadvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ def _set_values(self) -> List[str]:
if consumer.module is None:
# Verbraucher, dessen Modul nicht erstellt werden konnte (z.B. defekte Konfiguration).
continue
if consumer.data.extra_meter is not None:
# Verbraucher hat ein zusätzliches Messgerät, wird übersprungen.
consumer.module.fault_state.no_error()
consumer.module.fault_state.store_error()
continue
modules_threads.append(Thread(target=consumer.module.update,
args=(),
name=f"set values consumer{consumer.data.module.id}"))
Expand Down
Loading