Commit 9a56801f authored by Holger Brand's avatar Holger Brand
Browse files

Merge branch 'release/Release-1-1-1-0' into main

parents 6719f9ae fef7ae26
*.bak
/MQTT-SQLite-Logger.log
/MQTTSQLite.sqlite
.ipynb_checkpoints/*.ipynb
.ipynb_checkpoints/*
.spyproject/*
/MQTT-SQLite.sqlite
.ipynb_checkpoints/*.py
\ No newline at end of file
/MQTT-SQLite-Logger.log
py MQTT-SQLite-Logger.py -d MQTT-SQLite.sqlite -l MQTT-SQLite-Logger.log -t MQTT-Topics.json
\ No newline at end of file
py MQTT-SQLite-Logger.py -b localhost -d MQTT-SQLite.sqlite -l MQTT-SQLite-Logger.log -t MQTT-Topics.json
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
The programm can be used query the MQTT-SQLite-Logger database.
You can use this script with the Spyder IPython console.
Refer to README.md for more details.
Copyright 2022 GSI Helmholtzzentrum für Schwerionenforschung GmbH
Dr. Holger Brand, EEL, Planckstraße 1, 64291 Darmstadt, Germany
eMail: H.Brand@gsi.de
Web: https://www.gsi.de/work/forschung/experimentelektronik/kontrollsysteme
Published under EUPL V. 1.1. Refer to license files on disc.
"""
import datetime
import json#
import paho.mqtt.publish as publish
import matplotlib.pyplot as plt
import paho.mqtt.client as mqtt # import the client
import time
def on_message(client, userdata, message):
"""Handle received MQTT topic."""
try:
"""
print("message topic=", message.topic)
print("message qos=", message.qos)
print("message retain flag=", message.retain)
print("message payload ", str(message.payload.decode("utf-8")))
"""
payload = message.payload.decode("utf-8")
if message.topic.find('SQLStatus') >= 0:
print("SQLStatus received ", payload)
pass
else:
"""
#print("SQLResponse message received ", payload)
#print('Type of payload:', type(payload))
"""
sql_result = json.loads(payload)
"""
#print('SQLQuery:', sql_result['SQLQuery'])
#print('SQLQueryResult:', sql_result['SQLQueryResult'])
"""
data_times = []
data_values = []
ii = 0
for row in sql_result['SQLQueryResult']:
data_times.append(datetime.datetime.strptime(row[0], '%Y-%m-%d %H:%M:%S'))
data_values.append(row[1])
ii += 1
# print('ii=', ii, 'len(t)=', len(data_times), 'len(v)=', len(data_values))
plt.plot(data_times, data_values)
plt.show()
pass
pass
except Exception as e:
print('Exception catched:', e)
pass
broker_address = "localhost"
print("creating new instance")
client = mqtt.Client("P1") # create new instance
client.on_message = on_message # attach function to callback
print("connecting to broker")
client.connect(broker_address) # connect to broker
client.loop_start() # start the loop
print("Subscribing to topic", "mqtt-sqlite-logger/SQLStatus")
client.subscribe("mqtt-sqlite-logger/SQLStatus")
print("Subscribing to topic", "Test/SQLQueryResponse")
client.subscribe("Test/SQLQueryResponse")
sql_query = {
"SQLQuery": "select datetime, data from data where topic_id=(select topic_id from topics where datetime > '2022-05-06 06:00:00' AND topic_name = 'CSPP-Template/SystemMonitor_CPU-Load') ORDER BY datetime ASC",
"SQLResponseTopic": "Test/SQLQueryResponse"
}
publish.single("mqtt-sqlite-logger/SQLQuery", json.dumps(sql_query), hostname=broker_address)
time.sleep(1) # wait
client.loop_stop() # stop the loop
print("Program stopped.")
This diff is collapsed.
......@@ -19,6 +19,8 @@ Query the SQLite database via MQTT, example:
"SQLResponseTopic": "Test/SQLQueryResponse"
}
MQTT-SQLite-Query (.py/.ipynb) can be used to send a query and wait for the query results.
Dependencies
------------
Refer to import statements.
......@@ -44,9 +46,13 @@ Acknowledgements
Revision History
----------------
Revision 1.1.1.0 - 05.05.2022 H.Brand@gsi.de
- Modify code with respect to coding recommendations by spyder
- Combine sending a query and receiving the results into one script.
- Add the broker to the command-line in bat-file.
Revision 1.1.0.0 - 08.04.2022 H.Brand@gsi.de
- Log all or changed values only to SQLite
Revision 1.0.0.1 - 25.03.2022 H.Brand@gsi.de
- Logging to SQLite
- Query SQLite (Topic: SQLQuery)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment