|
1 | 1 | import logging |
2 | | -from control import data |
3 | 2 | from typing import Optional |
| 3 | +from control import data |
4 | 4 | from helpermodules.logger import ModifyLoglevelContext |
| 5 | +from helpermodules.pub import Pub |
| 6 | +from helpermodules.timecheck import create_timestamp |
5 | 7 | from modules.common.abstract_device import DeviceDescriptor |
6 | 8 | from modules.common.abstract_io import AbstractIoAction |
7 | | -from modules.common.utils.component_parser import get_component_name_by_id |
8 | 9 | from modules.io_actions.generator_systems.stepwise_control.config import StepwiseControlSetup |
9 | 10 |
|
10 | 11 | control_command_log = logging.getLogger("steuve_control_command") |
|
13 | 14 | class StepwiseControl(AbstractIoAction): |
14 | 15 | def __init__(self, config: StepwiseControlSetup): |
15 | 16 | self.config = config |
16 | | - control_command_log.info(f"Stufenweise Steuerung einer EZA: Eingang {self.config.configuration.s1} für S1, " |
17 | | - f"Eingang {self.config.configuration.s2} für S2, und Eingang " |
18 | | - f"{self.config.configuration.w3} für W3 wird überwacht. Die Beschränkung muss in " |
19 | | - "der EZA vorgenommen werden.") |
| 17 | + self.__unique_inputs = [] |
| 18 | + for pattern in self.config.configuration.input_pattern: |
| 19 | + for key in pattern["input_matrix"].keys(): |
| 20 | + if key not in self.__unique_inputs: |
| 21 | + self.__unique_inputs.append(key) |
| 22 | + assigned_devices = [io_device["id"] for io_device in self.config.configuration.devices] |
| 23 | + control_command_log.info( |
| 24 | + f"Stufenweise Steuerung von EZA: I/O-Gerät: {self.config.configuration.io_device}, " |
| 25 | + f"Überwachte digitale Eingänge: {self.__unique_inputs}, " |
| 26 | + f"zugeordnete Erzeugungsanlagen: {assigned_devices} " |
| 27 | + "Die Begrenzung muss in den EZA vorgenommen werden!" |
| 28 | + ) |
20 | 29 | super().__init__() |
21 | 30 |
|
22 | 31 | def setup(self) -> None: |
23 | | - pass |
24 | | - |
25 | | - def control_stepwise(self) -> Optional[str]: |
26 | | - text = (f"Die Einspeiseleistung von {get_component_name_by_id(self.config.configuration.pv_id)} ist auf " |
27 | | - "{} % beschränkt. Die Beschränkung muss in der EZA vorgenommen werden.") |
28 | | - msg = None |
29 | | - digital_input = data.data.io_states[f"io_states{self.config.configuration.io_device}"].data.get.digital_input |
30 | | - digital_input_prev = data.data.io_states[ |
31 | | - f"io_states{self.config.configuration.io_device}"].data.get.digital_input_prev |
| 32 | + with ModifyLoglevelContext(control_command_log, logging.DEBUG): |
| 33 | + digital_input = ( |
| 34 | + data.data.io_states[ |
| 35 | + f"io_states{self.config.configuration.io_device}" |
| 36 | + ].data.get.digital_input |
| 37 | + ) |
| 38 | + digital_input_prev = data.data.io_states[ |
| 39 | + f"io_states{self.config.configuration.io_device}"].data.get.digital_input_prev |
| 40 | + changed = len([ |
| 41 | + input_name for input_name in self.__unique_inputs |
| 42 | + if digital_input[input_name] != digital_input_prev[input_name] |
| 43 | + ]) > 0 |
32 | 44 |
|
33 | | - active_inputs = [ |
34 | | - digital_input[self.config.configuration.s1], |
35 | | - digital_input[self.config.configuration.s2], |
36 | | - digital_input[self.config.configuration.w3] |
37 | | - ] |
38 | | - num_active = sum(1 for v in active_inputs if v) |
| 45 | + for pattern in self.config.configuration.input_pattern: |
| 46 | + for action_input, value in pattern["input_matrix"].items(): |
| 47 | + if digital_input[action_input] != value: |
| 48 | + break |
| 49 | + else: |
| 50 | + # Alle digitalen Eingänge entsprechen dem Pattern |
| 51 | + if pattern["value"] != 1: |
| 52 | + if changed: |
| 53 | + Pub().pub(f"openWB/set/io/action/{self.config.id}/timestamp", create_timestamp()) |
| 54 | + control_command_log.info(f"EZA-Begrenzung mit Wert {int(pattern['value']*100)}% aktiviert.") |
| 55 | + break |
| 56 | + else: |
| 57 | + if changed: |
| 58 | + Pub().pub(f"openWB/set/io/action/{self.config.id}/timestamp", None) |
| 59 | + control_command_log.info("EZA-Begrenzung aufgehoben.") |
39 | 60 |
|
40 | | - if num_active > 1: |
41 | | - error_msg = (f"Fehler: Mehr als ein Eingang ist aktiv für die stufenweise Steuerung der EZA! " |
42 | | - f"S1: {digital_input[self.config.configuration.s1]}, " |
43 | | - f"S2: {digital_input[self.config.configuration.s2]}, " |
44 | | - f"W3: {digital_input[self.config.configuration.w3]}") |
45 | | - with ModifyLoglevelContext(control_command_log, logging.ERROR): |
46 | | - control_command_log.error(error_msg) |
47 | | - raise ValueError(error_msg) |
48 | | - |
49 | | - if digital_input[self.config.configuration.s1]: |
50 | | - msg = text.format(60) |
51 | | - elif digital_input[self.config.configuration.s2]: |
52 | | - msg = text.format(30) |
53 | | - elif digital_input[self.config.configuration.w3]: |
54 | | - msg = text.format(0) |
| 61 | + def control_stepwise(self) -> Optional[float]: |
| 62 | + for pattern in self.config.configuration.input_pattern: |
| 63 | + for digital_input, value in pattern["input_matrix"].items(): |
| 64 | + if data.data.io_states[f"io_states{self.config.configuration.io_device}" |
| 65 | + ].data.get.digital_input[digital_input] != value: |
| 66 | + break |
| 67 | + else: |
| 68 | + # Alle digitalen Eingänge entsprechen dem Pattern |
| 69 | + return pattern['value'] |
55 | 70 | else: |
56 | | - # Keine Beschränkung soll nicht dauerhaft im WR angezeigt werden. |
57 | | - msg = (f"Die Einspeiseleistung von {get_component_name_by_id(self.config.configuration.pv_id)} ist " |
58 | | - "nicht beschränkt. Die Beschränkung muss in der EZA vorgenommen werden.") |
59 | | - |
60 | | - if not (digital_input[self.config.configuration.s1] == digital_input_prev[self.config.configuration.s1] and |
61 | | - digital_input[self.config.configuration.s2] == digital_input_prev[self.config.configuration.s2] and |
62 | | - digital_input[self.config.configuration.w3] == digital_input_prev[self.config.configuration.w3]): |
63 | | - # Wenn sich was geändert hat, loggen |
64 | | - with ModifyLoglevelContext(control_command_log, logging.DEBUG): |
65 | | - control_command_log.info(msg) |
66 | | - return msg |
| 71 | + # Zustand entspricht keinem Pattern, Leistungsbegrenzung aufheben |
| 72 | + return 1 |
67 | 73 |
|
68 | 74 |
|
69 | 75 | def create_action(config: StepwiseControlSetup): |
|
0 commit comments