bdew-api 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bdew_api-0.1.1/LICENSE +201 -0
- bdew_api-0.1.1/PKG-INFO +171 -0
- bdew_api-0.1.1/README.md +149 -0
- bdew_api-0.1.1/pyproject.toml +54 -0
- bdew_api-0.1.1/setup.cfg +4 -0
- bdew_api-0.1.1/src/bdew_api/__init__.py +10 -0
- bdew_api-0.1.1/src/bdew_api/__main__.py +67 -0
- bdew_api-0.1.1/src/bdew_api/answer_state_handler.py +141 -0
- bdew_api-0.1.1/src/bdew_api/answer_state_server.py +95 -0
- bdew_api-0.1.1/src/bdew_api/bdew.py +133 -0
- bdew_api-0.1.1/src/bdew_api/control_demand_handler.py +92 -0
- bdew_api-0.1.1/src/bdew_api/control_demand_server.py +124 -0
- bdew_api-0.1.1/src/bdew_api/id_generator.py +159 -0
- bdew_api-0.1.1/src/bdew_api/tls_config.py +89 -0
- bdew_api-0.1.1/src/bdew_api/utils.py +148 -0
- bdew_api-0.1.1/src/bdew_api.egg-info/PKG-INFO +171 -0
- bdew_api-0.1.1/src/bdew_api.egg-info/SOURCES.txt +20 -0
- bdew_api-0.1.1/src/bdew_api.egg-info/dependency_links.txt +1 -0
- bdew_api-0.1.1/src/bdew_api.egg-info/entry_points.txt +2 -0
- bdew_api-0.1.1/src/bdew_api.egg-info/requires.txt +8 -0
- bdew_api-0.1.1/src/bdew_api.egg-info/top_level.txt +1 -0
- bdew_api-0.1.1/tests/test_bdew_api.py +304 -0
bdew_api-0.1.1/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
bdew_api-0.1.1/PKG-INFO
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: bdew-api
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: BDEW API as part of communication in German energy sytem
|
|
5
|
+
Author-email: Project SimCyberGrid <simcybergrid@haw-hamburg.de>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/SimCyberGrid/bdew_api
|
|
8
|
+
Keywords: energy system,communication,api,bdew,European energy system
|
|
9
|
+
Classifier: Programming Language :: Python
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: flask>=3.1.2
|
|
15
|
+
Requires-Dist: pydantic>=2.13.3
|
|
16
|
+
Requires-Dist: pydantic_core>=2.46.3
|
|
17
|
+
Requires-Dist: requests>=2.25.1
|
|
18
|
+
Provides-Extra: test
|
|
19
|
+
Requires-Dist: pytest>=7; extra == "test"
|
|
20
|
+
Requires-Dist: cryptography>=42; extra == "test"
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
|
|
23
|
+
# bdew_api
|
|
24
|
+
|
|
25
|
+
Python-Paket zur Kommunikation mit der BDEW-Schnittstelle. Es stellt eine `BDEW`-Klasse bereit, mit der Steuerungsanfragen und Statusmeldungen zwischen zwei Netzwerkteilnehmern (AEMT und PEMT) gesendet werden können.
|
|
26
|
+
|
|
27
|
+
## Inhaltsverzeichnis
|
|
28
|
+
|
|
29
|
+
- [Voraussetzungen](#voraussetzungen)
|
|
30
|
+
- [Installation](#installation)
|
|
31
|
+
- [Verwendung](#verwendung)
|
|
32
|
+
- [Paket ausführen](#paket-ausführen)
|
|
33
|
+
- [Konfigurationsparameter](#konfigurationsparameter)
|
|
34
|
+
- [TLS](#tls)
|
|
35
|
+
|
|
36
|
+
## Voraussetzungen
|
|
37
|
+
|
|
38
|
+
Python ≥ 3.10. Die folgenden Abhängigkeiten installiert `pip` automatisch mit:
|
|
39
|
+
|
|
40
|
+
| Paket | Version |
|
|
41
|
+
|-----------------|----------------------|
|
|
42
|
+
| flask | \>= 3.1.2 |
|
|
43
|
+
| pydantic | \>= 2.13.3 |
|
|
44
|
+
| pydantic_core | \>= 2.46.3 |
|
|
45
|
+
| requests | \>= 2.25.1 |
|
|
46
|
+
|
|
47
|
+
Nur zum Bauen des Pakets (Wheel) wird zusätzlich `setuptools` \>= 64 benötigt.
|
|
48
|
+
|
|
49
|
+
## Installation
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
python -m pip install bdew-api
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Optional vorher die Abhängigkeiten mit festen Versionen installieren:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
python -m pip install flask==3.1.2 pydantic==2.13.3 pydantic_core==2.46.3 requests==2.25.1
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Verwendung
|
|
62
|
+
|
|
63
|
+
### Im Terminal prüfen
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
import bdew_api
|
|
67
|
+
|
|
68
|
+
# optional: Version anzeigen
|
|
69
|
+
print(bdew_api.__version__)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Im Projekt
|
|
73
|
+
|
|
74
|
+
Jede Seite betreibt nur ihre eigene Hälfte der Schnittstelle (`role`). Ohne
|
|
75
|
+
`role` laufen beide Seiten in einem Prozess (`role="both"`, für Demo und Tests).
|
|
76
|
+
Für die eigene Seite ist der Host die Bind-Adresse, für die Gegenseite die
|
|
77
|
+
Zieladresse.
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from bdew_api import bdew
|
|
81
|
+
|
|
82
|
+
# AEMT-Seite: nimmt Steuerungsanfragen an (:8301), antwortet dem PEMT (:8202)
|
|
83
|
+
aemt = bdew.BDEW(
|
|
84
|
+
aemt_id="001",
|
|
85
|
+
aemt_host="0.0.0.0", # eigene Seite: Bind-Adresse
|
|
86
|
+
aemt_port=8301,
|
|
87
|
+
pemt_id="002",
|
|
88
|
+
pemt_host="10.0.0.2", # Gegenseite: Zieladresse
|
|
89
|
+
pemt_port=8202,
|
|
90
|
+
role="aemt",
|
|
91
|
+
control_demand_callback=lambda location_id, command, kind, remote_addr: print(location_id, kind),
|
|
92
|
+
answer_to_sender=True, # Antwort an den Absender der Anfrage statt an pemt_host
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
# PEMT-Seite: sendet Steuerungsanfragen an den AEMT, nimmt dessen Antworten an
|
|
96
|
+
pemt = bdew.BDEW(
|
|
97
|
+
aemt_id="001",
|
|
98
|
+
aemt_host="10.0.0.1", # Gegenseite: Zieladresse
|
|
99
|
+
aemt_port=8301,
|
|
100
|
+
pemt_id="002",
|
|
101
|
+
pemt_host="0.0.0.0", # eigene Seite: Bind-Adresse
|
|
102
|
+
pemt_port=8202,
|
|
103
|
+
role="pemt",
|
|
104
|
+
answer_callback=lambda route, body: print(route),
|
|
105
|
+
)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Die Server starten beim Erzeugen in Hintergrund-Threads (abschaltbar mit
|
|
109
|
+
`autostart=False`, dann später `start()` aufrufen).
|
|
110
|
+
|
|
111
|
+
### Verfügbare Funktionen
|
|
112
|
+
|
|
113
|
+
| Funktion | Beschreibung |
|
|
114
|
+
|-----------------------------------------------------------|-----------------------------|
|
|
115
|
+
| `bdew.send_control_request(command_control, location_id)` | Sendet eine Steuerungsanfrage |
|
|
116
|
+
| `bdew.send_control_status(result)` | Sendet einen Steuerungsstatus |
|
|
117
|
+
| `bdew.start()` | Startet die Server der eigenen Seite (nur nötig bei `autostart=False`) |
|
|
118
|
+
|
|
119
|
+
## Paket ausführen
|
|
120
|
+
|
|
121
|
+
Das Paket enthält eine Demo, die beide Seiten in einem Prozess startet, eine
|
|
122
|
+
Steuerungsanfrage und eine positive Antwort (`positive`) sendet und sich dann
|
|
123
|
+
beendet:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
python -m bdew_api <aemt_id> <aemt_host> <aemt_port> <pemt_id> <pemt_host> <pemt_port>
|
|
127
|
+
# gleichwertig, als Konsolenbefehl:
|
|
128
|
+
bdew_api <aemt_id> <aemt_host> <aemt_port> <pemt_id> <pemt_host> <pemt_port>
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Beispiel:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
python -m bdew_api 001 127.0.0.1 8301 002 127.0.0.1 8202
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Die Demo braucht die TLS-Dateien beider Seiten im Arbeitsverzeichnis
|
|
138
|
+
(`aemt-cert.pem`, `aemt-key.pem`, `pemt-cert.pem`, `pemt-key.pem`,
|
|
139
|
+
`wan-ca.pem`, siehe [TLS](#tls)); ohne sie werden nur Warnungen ausgegeben.
|
|
140
|
+
|
|
141
|
+
## Konfigurationsparameter
|
|
142
|
+
|
|
143
|
+
| Parameter | Typ | Beschreibung |
|
|
144
|
+
|-------------|-------|---------------------------|
|
|
145
|
+
| `aemt_id` | `str` | ID des AEMT |
|
|
146
|
+
| `aemt_host` | `str` | Hostname/IP des AEMT (AEMT-Seite: Bind-Adresse, PEMT-Seite: Zieladresse) |
|
|
147
|
+
| `aemt_port` | `int` | Port des AEMT |
|
|
148
|
+
| `pemt_id` | `str` | ID des PEMT |
|
|
149
|
+
| `pemt_host` | `str` | Hostname/IP des PEMT (PEMT-Seite: Bind-Adresse, AEMT-Seite: Zieladresse) |
|
|
150
|
+
| `pemt_port` | `int` | Port des PEMT |
|
|
151
|
+
| `role` | `str` | `"aemt"`, `"pemt"` oder `"both"` (Standard): welche Hälfte der Schnittstelle dieser Prozess betreibt |
|
|
152
|
+
| `control_demand_callback` | Funktion | AEMT-Seite: aufgerufen als `(location_id, command, kind, remote_addr)` für jede gültige Steuerungsanfrage (`kind`: `"control"` oder `"regular"`) |
|
|
153
|
+
| `answer_callback` | Funktion | PEMT-Seite: aufgerufen als `(route, body)` für jede Antwort des AEMT |
|
|
154
|
+
| `answer_to_sender` | `bool` | AEMT-Seite: Antworten an die Adresse der letzten Anfrage statt an `pemt_host` (Standard: `False`) |
|
|
155
|
+
| `autostart` | `bool` | Server beim Erzeugen starten (Standard: `True`) |
|
|
156
|
+
| `aemt_certfile` / `aemt_keyfile` | `str` | Zertifikat + Schlüssel des AEMT (Standard: `aemt-cert.pem` / `aemt-key.pem`) |
|
|
157
|
+
| `pemt_certfile` / `pemt_keyfile` | `str` | Zertifikat + Schlüssel des PEMT (Standard: `pemt-cert.pem` / `pemt-key.pem`) |
|
|
158
|
+
| `cafile` | `str` | WAN-CA, gegen die beide Seiten prüfen (Standard: `wan-ca.pem`) |
|
|
159
|
+
|
|
160
|
+
## TLS
|
|
161
|
+
|
|
162
|
+
Beide Verbindungen (PEMT → AEMT :8301, AEMT → PEMT :8202) nutzen gegenseitig
|
|
163
|
+
authentisiertes TLS 1.2 nach TR-03116-3 (`bdew_api.tls_config`):
|
|
164
|
+
ECDHE_ECDSA-Cipher-Suites, Kurve brainpoolP256r1, Client-Zertifikat Pflicht.
|
|
165
|
+
|
|
166
|
+
Benötigt werden je Seite ein ECDSA-Zertifikat mit Schlüssel auf der Kurve
|
|
167
|
+
brainpoolP256r1 (PEM) und das CA-Zertifikat, mit dem beide Zertifikate
|
|
168
|
+
signiert sind. Die Dateien werden standardmäßig im Arbeitsverzeichnis gesucht.
|
|
169
|
+
Fehlen sie oder sind sie unbrauchbar, wird eine Warnung (mit Dateinamen)
|
|
170
|
+
geloggt und die betroffene Seite bleibt inaktiv: Ihr Server startet nicht,
|
|
171
|
+
ihre Anfragen werden nicht gesendet (jeweils mit Warnung).
|
bdew_api-0.1.1/README.md
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# bdew_api
|
|
2
|
+
|
|
3
|
+
Python-Paket zur Kommunikation mit der BDEW-Schnittstelle. Es stellt eine `BDEW`-Klasse bereit, mit der Steuerungsanfragen und Statusmeldungen zwischen zwei Netzwerkteilnehmern (AEMT und PEMT) gesendet werden können.
|
|
4
|
+
|
|
5
|
+
## Inhaltsverzeichnis
|
|
6
|
+
|
|
7
|
+
- [Voraussetzungen](#voraussetzungen)
|
|
8
|
+
- [Installation](#installation)
|
|
9
|
+
- [Verwendung](#verwendung)
|
|
10
|
+
- [Paket ausführen](#paket-ausführen)
|
|
11
|
+
- [Konfigurationsparameter](#konfigurationsparameter)
|
|
12
|
+
- [TLS](#tls)
|
|
13
|
+
|
|
14
|
+
## Voraussetzungen
|
|
15
|
+
|
|
16
|
+
Python ≥ 3.10. Die folgenden Abhängigkeiten installiert `pip` automatisch mit:
|
|
17
|
+
|
|
18
|
+
| Paket | Version |
|
|
19
|
+
|-----------------|----------------------|
|
|
20
|
+
| flask | \>= 3.1.2 |
|
|
21
|
+
| pydantic | \>= 2.13.3 |
|
|
22
|
+
| pydantic_core | \>= 2.46.3 |
|
|
23
|
+
| requests | \>= 2.25.1 |
|
|
24
|
+
|
|
25
|
+
Nur zum Bauen des Pakets (Wheel) wird zusätzlich `setuptools` \>= 64 benötigt.
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
python -m pip install bdew-api
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Optional vorher die Abhängigkeiten mit festen Versionen installieren:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
python -m pip install flask==3.1.2 pydantic==2.13.3 pydantic_core==2.46.3 requests==2.25.1
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Verwendung
|
|
40
|
+
|
|
41
|
+
### Im Terminal prüfen
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
import bdew_api
|
|
45
|
+
|
|
46
|
+
# optional: Version anzeigen
|
|
47
|
+
print(bdew_api.__version__)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Im Projekt
|
|
51
|
+
|
|
52
|
+
Jede Seite betreibt nur ihre eigene Hälfte der Schnittstelle (`role`). Ohne
|
|
53
|
+
`role` laufen beide Seiten in einem Prozess (`role="both"`, für Demo und Tests).
|
|
54
|
+
Für die eigene Seite ist der Host die Bind-Adresse, für die Gegenseite die
|
|
55
|
+
Zieladresse.
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from bdew_api import bdew
|
|
59
|
+
|
|
60
|
+
# AEMT-Seite: nimmt Steuerungsanfragen an (:8301), antwortet dem PEMT (:8202)
|
|
61
|
+
aemt = bdew.BDEW(
|
|
62
|
+
aemt_id="001",
|
|
63
|
+
aemt_host="0.0.0.0", # eigene Seite: Bind-Adresse
|
|
64
|
+
aemt_port=8301,
|
|
65
|
+
pemt_id="002",
|
|
66
|
+
pemt_host="10.0.0.2", # Gegenseite: Zieladresse
|
|
67
|
+
pemt_port=8202,
|
|
68
|
+
role="aemt",
|
|
69
|
+
control_demand_callback=lambda location_id, command, kind, remote_addr: print(location_id, kind),
|
|
70
|
+
answer_to_sender=True, # Antwort an den Absender der Anfrage statt an pemt_host
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
# PEMT-Seite: sendet Steuerungsanfragen an den AEMT, nimmt dessen Antworten an
|
|
74
|
+
pemt = bdew.BDEW(
|
|
75
|
+
aemt_id="001",
|
|
76
|
+
aemt_host="10.0.0.1", # Gegenseite: Zieladresse
|
|
77
|
+
aemt_port=8301,
|
|
78
|
+
pemt_id="002",
|
|
79
|
+
pemt_host="0.0.0.0", # eigene Seite: Bind-Adresse
|
|
80
|
+
pemt_port=8202,
|
|
81
|
+
role="pemt",
|
|
82
|
+
answer_callback=lambda route, body: print(route),
|
|
83
|
+
)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Die Server starten beim Erzeugen in Hintergrund-Threads (abschaltbar mit
|
|
87
|
+
`autostart=False`, dann später `start()` aufrufen).
|
|
88
|
+
|
|
89
|
+
### Verfügbare Funktionen
|
|
90
|
+
|
|
91
|
+
| Funktion | Beschreibung |
|
|
92
|
+
|-----------------------------------------------------------|-----------------------------|
|
|
93
|
+
| `bdew.send_control_request(command_control, location_id)` | Sendet eine Steuerungsanfrage |
|
|
94
|
+
| `bdew.send_control_status(result)` | Sendet einen Steuerungsstatus |
|
|
95
|
+
| `bdew.start()` | Startet die Server der eigenen Seite (nur nötig bei `autostart=False`) |
|
|
96
|
+
|
|
97
|
+
## Paket ausführen
|
|
98
|
+
|
|
99
|
+
Das Paket enthält eine Demo, die beide Seiten in einem Prozess startet, eine
|
|
100
|
+
Steuerungsanfrage und eine positive Antwort (`positive`) sendet und sich dann
|
|
101
|
+
beendet:
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
python -m bdew_api <aemt_id> <aemt_host> <aemt_port> <pemt_id> <pemt_host> <pemt_port>
|
|
105
|
+
# gleichwertig, als Konsolenbefehl:
|
|
106
|
+
bdew_api <aemt_id> <aemt_host> <aemt_port> <pemt_id> <pemt_host> <pemt_port>
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Beispiel:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
python -m bdew_api 001 127.0.0.1 8301 002 127.0.0.1 8202
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Die Demo braucht die TLS-Dateien beider Seiten im Arbeitsverzeichnis
|
|
116
|
+
(`aemt-cert.pem`, `aemt-key.pem`, `pemt-cert.pem`, `pemt-key.pem`,
|
|
117
|
+
`wan-ca.pem`, siehe [TLS](#tls)); ohne sie werden nur Warnungen ausgegeben.
|
|
118
|
+
|
|
119
|
+
## Konfigurationsparameter
|
|
120
|
+
|
|
121
|
+
| Parameter | Typ | Beschreibung |
|
|
122
|
+
|-------------|-------|---------------------------|
|
|
123
|
+
| `aemt_id` | `str` | ID des AEMT |
|
|
124
|
+
| `aemt_host` | `str` | Hostname/IP des AEMT (AEMT-Seite: Bind-Adresse, PEMT-Seite: Zieladresse) |
|
|
125
|
+
| `aemt_port` | `int` | Port des AEMT |
|
|
126
|
+
| `pemt_id` | `str` | ID des PEMT |
|
|
127
|
+
| `pemt_host` | `str` | Hostname/IP des PEMT (PEMT-Seite: Bind-Adresse, AEMT-Seite: Zieladresse) |
|
|
128
|
+
| `pemt_port` | `int` | Port des PEMT |
|
|
129
|
+
| `role` | `str` | `"aemt"`, `"pemt"` oder `"both"` (Standard): welche Hälfte der Schnittstelle dieser Prozess betreibt |
|
|
130
|
+
| `control_demand_callback` | Funktion | AEMT-Seite: aufgerufen als `(location_id, command, kind, remote_addr)` für jede gültige Steuerungsanfrage (`kind`: `"control"` oder `"regular"`) |
|
|
131
|
+
| `answer_callback` | Funktion | PEMT-Seite: aufgerufen als `(route, body)` für jede Antwort des AEMT |
|
|
132
|
+
| `answer_to_sender` | `bool` | AEMT-Seite: Antworten an die Adresse der letzten Anfrage statt an `pemt_host` (Standard: `False`) |
|
|
133
|
+
| `autostart` | `bool` | Server beim Erzeugen starten (Standard: `True`) |
|
|
134
|
+
| `aemt_certfile` / `aemt_keyfile` | `str` | Zertifikat + Schlüssel des AEMT (Standard: `aemt-cert.pem` / `aemt-key.pem`) |
|
|
135
|
+
| `pemt_certfile` / `pemt_keyfile` | `str` | Zertifikat + Schlüssel des PEMT (Standard: `pemt-cert.pem` / `pemt-key.pem`) |
|
|
136
|
+
| `cafile` | `str` | WAN-CA, gegen die beide Seiten prüfen (Standard: `wan-ca.pem`) |
|
|
137
|
+
|
|
138
|
+
## TLS
|
|
139
|
+
|
|
140
|
+
Beide Verbindungen (PEMT → AEMT :8301, AEMT → PEMT :8202) nutzen gegenseitig
|
|
141
|
+
authentisiertes TLS 1.2 nach TR-03116-3 (`bdew_api.tls_config`):
|
|
142
|
+
ECDHE_ECDSA-Cipher-Suites, Kurve brainpoolP256r1, Client-Zertifikat Pflicht.
|
|
143
|
+
|
|
144
|
+
Benötigt werden je Seite ein ECDSA-Zertifikat mit Schlüssel auf der Kurve
|
|
145
|
+
brainpoolP256r1 (PEM) und das CA-Zertifikat, mit dem beide Zertifikate
|
|
146
|
+
signiert sind. Die Dateien werden standardmäßig im Arbeitsverzeichnis gesucht.
|
|
147
|
+
Fehlen sie oder sind sie unbrauchbar, wird eine Warnung (mit Dateinamen)
|
|
148
|
+
geloggt und die betroffene Seite bleibt inaktiv: Ihr Server startet nicht,
|
|
149
|
+
ihre Anfragen werden nicht gesendet (jeweils mit Warnung).
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=64", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "bdew-api"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
description = "BDEW API as part of communication in German energy sytem"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
authors = [{ name = "Project SimCyberGrid" , email = "simcybergrid@haw-hamburg.de" }]
|
|
11
|
+
license = "Apache-2.0"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Programming Language :: Python",
|
|
15
|
+
"Programming Language :: Python :: 3"]
|
|
16
|
+
keywords = ["energy system", "communication", "api", "bdew", "European energy system"]
|
|
17
|
+
dependencies = [
|
|
18
|
+
"flask>=3.1.2",
|
|
19
|
+
"pydantic>=2.13.3",
|
|
20
|
+
"pydantic_core>=2.46.3",
|
|
21
|
+
"requests>=2.25.1"
|
|
22
|
+
]
|
|
23
|
+
requires-python = ">=3.10"
|
|
24
|
+
|
|
25
|
+
[project.optional-dependencies]
|
|
26
|
+
# pytest + cryptography (the tests generate their own brainpool test PKI)
|
|
27
|
+
test = ["pytest>=7", "cryptography>=42"]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://github.com/SimCyberGrid/bdew_api"
|
|
31
|
+
|
|
32
|
+
[project.scripts]
|
|
33
|
+
bdew_api = "bdew_api.__main__:main"
|
|
34
|
+
|
|
35
|
+
[tool.setuptools.package-dir]
|
|
36
|
+
"" = "src"
|
|
37
|
+
|
|
38
|
+
[tool.black]
|
|
39
|
+
line-length = 120
|
|
40
|
+
|
|
41
|
+
[tool.ruff]
|
|
42
|
+
line-length = 120
|
|
43
|
+
|
|
44
|
+
[tool.ruff.lint]
|
|
45
|
+
ignore = ["E501"]
|
|
46
|
+
|
|
47
|
+
[tool.pytest.ini_options]
|
|
48
|
+
testpaths = ["tests"]
|
|
49
|
+
# some tests deliberately connect with verify=False (to check they are rejected)
|
|
50
|
+
filterwarnings = ["ignore::urllib3.exceptions.InsecureRequestWarning"]
|
|
51
|
+
|
|
52
|
+
[tool.flake8]
|
|
53
|
+
max-line-length = 120
|
|
54
|
+
ignore = "E501"
|
bdew_api-0.1.1/setup.cfg
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|
|
2
|
+
|
|
3
|
+
from bdew_api import bdew
|
|
4
|
+
from bdew_api import answer_state_handler
|
|
5
|
+
from bdew_api import answer_state_server
|
|
6
|
+
from bdew_api import control_demand_handler
|
|
7
|
+
from bdew_api import control_demand_server
|
|
8
|
+
from bdew_api import id_generator
|
|
9
|
+
from bdew_api import tls_config
|
|
10
|
+
from bdew_api import utils
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import argparse
|
|
3
|
+
import socket
|
|
4
|
+
import time
|
|
5
|
+
|
|
6
|
+
from bdew_api.bdew import BDEW
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _wait_until_listening(host: str, port: int, timeout: float = 5.0) -> bool:
|
|
10
|
+
"""Wait until a server accepts TCP connections (servers start in background threads)."""
|
|
11
|
+
deadline = time.monotonic() + timeout
|
|
12
|
+
while time.monotonic() < deadline:
|
|
13
|
+
try:
|
|
14
|
+
with socket.create_connection((host, port), timeout=0.5):
|
|
15
|
+
return True
|
|
16
|
+
except OSError:
|
|
17
|
+
time.sleep(0.1)
|
|
18
|
+
return False
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
#aemt_id: str, aemt_host: str, aemt_port: int, pemt_id:str, pemt_host: str, pemt_port: int
|
|
22
|
+
def main():
|
|
23
|
+
parser = argparse.ArgumentParser(description='establish bdew api communication')
|
|
24
|
+
parser.add_argument('aemt_id',
|
|
25
|
+
metavar='aemt_id',
|
|
26
|
+
type=str,
|
|
27
|
+
help='id of the aemt component')
|
|
28
|
+
parser.add_argument('aemt_host',
|
|
29
|
+
metavar='aemt_host',
|
|
30
|
+
type=str,
|
|
31
|
+
help='host of the aemt component')
|
|
32
|
+
parser.add_argument('aemt_port',
|
|
33
|
+
metavar='aemt_port',
|
|
34
|
+
type=int,
|
|
35
|
+
help='port of the aemt component')
|
|
36
|
+
parser.add_argument('pemt_id',
|
|
37
|
+
metavar='pemt_id',
|
|
38
|
+
type=str,
|
|
39
|
+
help='id of the pemt component')
|
|
40
|
+
parser.add_argument('pemt_host',
|
|
41
|
+
metavar='pemt_host',
|
|
42
|
+
type=str,
|
|
43
|
+
help='host of the pemt component')
|
|
44
|
+
parser.add_argument('pemt_port',
|
|
45
|
+
metavar='pemt_port',
|
|
46
|
+
type=int,
|
|
47
|
+
help='port of the pemt component')
|
|
48
|
+
|
|
49
|
+
argvs = sys.argv
|
|
50
|
+
aemt_id = argvs[1]
|
|
51
|
+
aemt_host = argvs[2]
|
|
52
|
+
aemt_port = int(argvs[3])
|
|
53
|
+
pemt_id = argvs[4]
|
|
54
|
+
pemt_host = argvs[5]
|
|
55
|
+
pemt_port = int(argvs[6])
|
|
56
|
+
bdew = BDEW(aemt_id,aemt_host,aemt_port,pemt_id,pemt_host,pemt_port)
|
|
57
|
+
# Demo of both sides in one process: wait for the servers that did start
|
|
58
|
+
# (a side without TLS material stays inactive and only logs warnings).
|
|
59
|
+
for server, host, port in ((bdew.control_demand_server, aemt_host, aemt_port),
|
|
60
|
+
(bdew.answer_state_server, pemt_host, pemt_port)):
|
|
61
|
+
if server.ssl_context is not None and not _wait_until_listening(host, port):
|
|
62
|
+
print(f"server on {host}:{port} did not come up", file=sys.stderr)
|
|
63
|
+
bdew.send_control_request(None,None)
|
|
64
|
+
bdew.send_control_status("positive")
|
|
65
|
+
|
|
66
|
+
if __name__ == "__main__":
|
|
67
|
+
main()
|