-
Notifications
You must be signed in to change notification settings - Fork 120
Add LoadManager #3636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
AlexanderHa98
wants to merge
12
commits into
openWB:master
Choose a base branch
from
AlexanderHa98:feature_LoadManager
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add LoadManager #3636
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0be35bc
Add LoadManager
AlexanderHa98 94a8aa7
Remove unused thread-variable
AlexanderHa98 3e42a9a
Remove unused thread-variable
AlexanderHa98 a44eebe
Potential fix for pull request finding
AlexanderHa98 89c6be7
Add Copilot suggestion
AlexanderHa98 c6e6061
Merge branch 'feature_LoadManager' of https://github.com/AlexanderHa9…
AlexanderHa98 2ef39ed
Add requested changes
AlexanderHa98 498fb9b
Add max_current und Timestamp check
AlexanderHa98 a2eb941
Update Loadmanager
AlexanderHa98 c1cc1b5
Fix timestamp error-handling
AlexanderHa98 e990e5d
Fix row-length
AlexanderHa98 7e32982
Add id to MQTT-msg
AlexanderHa98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/modules/io_actions/controllable_consumers/load_manager/api.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import logging | ||
| from typing import Optional, Tuple | ||
| from control import data | ||
| from control.limiting_value import LimitingValue, LoadmanagementLimit | ||
| from modules.common.abstract_io import AbstractIoAction | ||
| from modules.io_actions.common import check_fault_state_io_device | ||
| from modules.io_actions.controllable_consumers.load_manager.config import LoadManagerSetup | ||
| from modules.io_devices.load_manager.config import AnalogInputMapping | ||
|
|
||
| from modules.common.abstract_device import DeviceDescriptor | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class LoadManager(AbstractIoAction): | ||
| def __init__(self, config: LoadManagerSetup): | ||
| self.config = config | ||
| self.import_power_left = None | ||
| self.import_current_left = None | ||
| super().__init__() | ||
|
|
||
| def setup(self) -> None: | ||
| if check_fault_state_io_device(self.config.configuration.io_device): | ||
| log.warning("Fehler des IO-Geräts: Lastmanager aktiviert für Failsafe-Modus.") | ||
| max_power = self.config.configuration.max_power_on_failure | ||
| max_current = self.config.configuration.max_current_on_failure | ||
| else: | ||
| max_power = data.data.io_states[f"io_states{self.config.configuration.io_device}" | ||
| ].data.get.analog_input[AnalogInputMapping.MAX_POWER.name] | ||
| max_current = sum(data.data.io_states[f"io_states{self.config.configuration.io_device}" | ||
| ].data.get.analog_input[AnalogInputMapping.MAX_CURRENT.name]) | ||
| self.import_power_left = max_power | ||
| self.import_current_left = max_current | ||
|
|
||
| def loadmanager_get_import_power_left(self) -> Tuple[Optional[float], Optional[float], LoadmanagementLimit]: | ||
| if check_fault_state_io_device(self.config.configuration.io_device): | ||
| return (self.import_power_left, self.import_current_left, LoadmanagementLimit( | ||
| LimitingValue.LOADMANAGER_ERROR.value, | ||
| LimitingValue.LOADMANAGER_ERROR)) | ||
| return self.import_power_left, self.import_current_left, LoadmanagementLimit(LimitingValue.LOADMANAGER.value, | ||
| LimitingValue.LOADMANAGER) | ||
|
|
||
| def loadmanager_set_import_power_left(self, used_power: float, used_current: float) -> None: | ||
| self.import_power_left -= used_power | ||
| self.import_current_left -= used_current | ||
| log.debug( | ||
| f"verbleibende Dimm-Leistung: {self.import_power_left}W, verbleibender Strom: {self.import_current_left}A") | ||
|
|
||
|
|
||
| def create_action(config: LoadManagerSetup, parent_device_type: str): | ||
| return LoadManager(config=config) | ||
|
|
||
|
|
||
| device_descriptor = DeviceDescriptor(configuration_factory=LoadManagerSetup) |
29 changes: 29 additions & 0 deletions
29
packages/modules/io_actions/controllable_consumers/load_manager/config.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| from dataclasses import dataclass, field | ||
| from typing import Dict, List, Optional | ||
| from dataclass_utils.factories import empty_io_pattern_boolean_factory, empty_list_factory | ||
| from modules.io_actions.groups import ActionGroup | ||
|
|
||
|
|
||
| @dataclass | ||
| class LoadManagerConfig: | ||
| io_device: Optional[int] = None | ||
| input_pattern: List[Dict] = field(default_factory=empty_io_pattern_boolean_factory) | ||
| devices: List[Dict] = field(default_factory=empty_list_factory) | ||
| # [{"type": "cp", "id": 0}, | ||
| # {"type": "io", "id": 1, "digital_output": "SofortLa"}] | ||
| max_import_power: int = 0 | ||
| max_power_on_failure: float = 0 | ||
| max_current_on_failure: float = 0 | ||
|
|
||
|
|
||
| class LoadManagerSetup: | ||
| def __init__(self, | ||
| name: str = "Begrenzung per Lastmanager", | ||
| type: str = "load_manager", | ||
| id: int = 0, | ||
| configuration: LoadManagerConfig = None): | ||
| self.name = name | ||
| self.id = id | ||
| self.configuration = configuration or LoadManagerConfig() | ||
| self.type = type | ||
| self.group = ActionGroup.CONTROLLABLE_CONSUMERS.value |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| import logging | ||
|
|
||
| from helpermodules import timecheck | ||
| from helpermodules.broker import BrokerClient | ||
| from helpermodules.utils.topic_parser import decode_payload | ||
| from modules.common.abstract_device import DeviceDescriptor | ||
| from modules.common.component_state import IoState | ||
| from modules.common.configurable_io import ConfigurableIo | ||
| from modules.io_devices.load_manager.config import AnalogInputMapping | ||
| from modules.io_devices.load_manager.config import LoadManager | ||
|
|
||
| log = logging.getLogger(__name__) | ||
| control_command_log = logging.getLogger("steuve_control_command") | ||
|
|
||
|
|
||
| def create_io(config: LoadManager): | ||
| received_topics = {} | ||
| broker = None | ||
|
|
||
| def read(): | ||
| broker.start_finite_loop() | ||
| log.debug(f"Empfange MQTT Daten für Lastmanager {config.id}: {received_topics}") | ||
| io_state = IoState() | ||
| io_state.analog_input = getattr(io_state, "analog_input", None) or {} | ||
| io_state.analog_output = getattr(io_state, "analog_output", None) or {} | ||
| io_state.digital_input = getattr(io_state, "digital_input", None) or {} | ||
| io_state.digital_output = getattr(io_state, "digital_output", None) or {} | ||
|
|
||
| if received_topics.get(f"openWB/mqtt/loadmanager/{config.id}/set/loadmanager"): | ||
| payload = received_topics[f"openWB/mqtt/loadmanager/{config.id}/set/loadmanager"] | ||
|
|
||
| timestamp = float(payload["timestamp"]) | ||
| age_s = timecheck.create_timestamp() - timestamp | ||
| # < = deaktiviert | ||
| if age_s > 60: | ||
| raise RuntimeError(f"Lastmanager-Daten sind veraltet: age={age_s:.1f}s, timestamp={timestamp}.") | ||
|
|
||
| io_state.analog_input.update({AnalogInputMapping.MAX_POWER.name: payload["max_power"]}) | ||
| io_state.analog_input.update({AnalogInputMapping.MAX_CURRENT.name: payload["max_current"]}) | ||
| io_state.analog_input.update({AnalogInputMapping.TIMESTAMP.name: timestamp}) | ||
| return io_state | ||
|
|
||
| def initializer(): | ||
| nonlocal broker | ||
| nonlocal received_topics | ||
|
|
||
| def on_connect(client, userdata, flags, rc): | ||
| client.subscribe(f"openWB/mqtt/loadmanager/{config.id}/#") | ||
|
|
||
| def on_message(client, userdata, message): | ||
| received_topics.update({message.topic: decode_payload(message.payload)}) | ||
|
|
||
| received_topics = {} | ||
| broker = BrokerClient("subscribeMqttLoadmanager", | ||
| on_connect, on_message) | ||
|
|
||
| return ConfigurableIo(config=config, component_reader=read, component_writer=lambda: None, initializer=initializer) | ||
|
|
||
|
|
||
| device_descriptor = DeviceDescriptor(configuration_factory=LoadManager) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| from enum import Enum | ||
| from typing import Dict, Union | ||
| from modules.common.io_setup import IoDeviceSetup | ||
|
|
||
|
|
||
| class AnalogInputMapping(Enum): | ||
| MAX_POWER = "max_power" | ||
| MAX_CURRENT = "max_current" | ||
| TIMESTAMP = "timestamp" | ||
|
|
||
|
|
||
| class LoadManagerConfiguration: | ||
| def __init__(self) -> None: | ||
| pass | ||
|
|
||
|
|
||
| def init_input(): | ||
| return {"analog": {pin.name: None for pin in AnalogInputMapping}} | ||
|
|
||
|
|
||
| def init_output(): | ||
| return {"analog": {}} | ||
|
|
||
|
|
||
| class LoadManager(IoDeviceSetup[LoadManagerConfiguration]): | ||
| def __init__(self, | ||
| name: str = "openWB Lastmanager", | ||
| type: str = "load_manager", | ||
| id: Union[int, str] = 0, | ||
| configuration: LoadManagerConfiguration = None, | ||
| input: Dict[str, Dict[int, float]] = None, | ||
| output: Dict[str, Dict[int, float]] = None) -> None: | ||
| self.name = name | ||
| self.type = type | ||
| self.id = id | ||
| self.configuration = configuration or LoadManagerConfiguration() | ||
| if input is None: | ||
| input = init_input() | ||
| if output is None: | ||
| output = init_output() | ||
| super().__init__(name, type, id, configuration or LoadManagerConfiguration(), input=input, output=output) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.