Skip to content

Commit e4e0874

Browse files
committed
API trial tool added
1 parent 09e180d commit e4e0874

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ A template can be found under
130130
| InfluxBucket | Bucket to be used for storage of EMS data |
131131
| csvOutput | Specifies whether EMS data shall be written to a csv file (Default: false) |
132132
| csvDir | Path to the directory where csv files shall be located |
133-
| **emsData** | List of configuration sets for Influx measurements to be created.<br>For explanation of the different elements, see [Mapping of OpenEMS to InfluxDB Data Points](#mapping-of-openems-to-influxdb-data-points), below.<br>For an example, see [monitorEMS_tpl.json](./config/monitorEMS_tpl.json).|
133+
| **emsData** | List of configuration sets for Influx measurements to be created.<br>For explanation of the different elements, see [Mapping of OpenEMS to InfluxDB Data Points](#mapping-of-openems-to-influxdb-data-points), below.<br>For an example, see [monitorEMS_tpl.json](./config/monitorEMS_tpl.json).<br>See also [How to find Configurations for Data Points Of Interest](#finding-configurations-for-data-points-of-interest).|
134134
|- measurement | Influx **Measurement** to be used for data points of this configuration |
135135
|- component | Pattern to be applied to the OpenEMS **Component-ID** to identify Influx **Tag Key** and **Tag Value**<br>e.g. pattern<br>```battery?```, applied to the Component-ID<br>```battery0```,<br>identifies "battery" as Tag Key and "0" as Tag Value.|
136136
|- channel_root | Pattern to be applied to the OpenEMS **Channel-ID** to identify Influx **Tag Keys**, **Tag Values** and **Field Keys**<br> e.g. pattern<br>```/Tower?Module?Cell???```, applied to<br>```/Tower0Module3Cell012Voltage```<br> identifues the Influx **Tags** {"Tower": "0", "Module": "3", "Cell": "012" } and "Voltage" as Influx **Field Key**. |
@@ -283,3 +283,19 @@ usage: monitorEMS.py [-h] [-t] [-s] [-l] [-L] [-F] [-p LOGFILE] [-f FILE] [-v] [
283283
-c CONFIG, --config CONFIG
284284
Path to config file to be used
285285
```
286+
287+
## Finding Configurations for Data Points Of Interest
288+
289+
This package includes a small [tryEmsApi.py](./tryEmsApi/tryEmsApi.py) Python program which can be used to evaluate the different OpenEMS REST-API endpoints and the data they disclose.
290+
291+
Before usage, copy this file to $PARENTDIR/tests and name it according to your needs.
292+
($PARENTDIR/tests is ignored by git)
293+
294+
1. Set 'emsURL' to the URL of your system
295+
2. Set 'component' to one of the components shown in your OpenEMS configuration
296+
3. Adjust the name of the 'csvFile' to be produced
297+
4. Run the program in the [virtual environment for monitorEMS](#running-monitorems-as-python-program).
298+
It will create the CSV file under an 'emsOutput' subdirectory.
299+
5. From the CSV file, select the data points to be monitored
300+
6. Adjust the regular expression in 'channel' to a more restrictive variant
301+
7. Use the resulting output to set up an 'emsData' entry in the 'monitorEMS.json' [configuration file](#configuration)

tryEmsApi/tryEmsApi.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
""" Program to try OpenEMS REST-API calls
2+
3+
Before usage, copy this file to $PARENTDIR/tests and name it according to your needs
4+
5+
1. Set 'emsURL' to the URL of your system
6+
2. Set 'component' to one of the components shown in your OpenEMS configuration
7+
3. Adjust the name of the 'csvFile' to be produced
8+
4. Run the program in the virtual environment for monitorEMS.
9+
It will create the CSV file under an 'emsOutput' subdirectory.
10+
5. From the CSV file, select the data points to be monitored
11+
6. Adjust the regular expression in 'channel' to a more restrictive variant
12+
7. Use the resulting output to set up an 'emsData' entry in 'monitorEMS.json'
13+
"""
14+
15+
import requests
16+
import json
17+
import os
18+
19+
# === Adjust here ==============
20+
emsUrl = "http://<emsName|IP>:80"
21+
component = "_sum"
22+
channel = ".*"
23+
csvFile = "ems__sum.csv"
24+
# ==============================
25+
26+
fd = "emsOutput"
27+
fp = fd + "/" + csvFile
28+
29+
url = emsUrl + "/rest/channel/" + component + "/" + channel
30+
31+
user = "x"
32+
password = "user"
33+
34+
session = requests.Session()
35+
session.auth = (user, password)
36+
37+
response = session.get(url)
38+
response.raise_for_status()
39+
40+
resp = json.loads(response.text)
41+
42+
f = None
43+
os.makedirs(fd, exist_ok=True)
44+
f = open(fp, "w", encoding="utf-8")
45+
title = "address;type;accessMode;text;unit;value\n"
46+
f.write(title)
47+
for el in resp:
48+
data = f'{el["address"]};{el["type"]};{el["accessMode"]};{el["text"]};{el["unit"]};{el["value"]}\n'
49+
f.write(data)
50+
f.close()

0 commit comments

Comments
 (0)