Commit 2741fb4e authored by Gezer's avatar Gezer
Browse files

Es tut, aber am besten noch verschönern etc.

parent 3ce51a0e
Showing with 38 additions and 30 deletions
+38 -30
import json
file_name = "mac_to_room.json"
file_name = "src/mqtt_influx_backend/mac_to_room.json"
def load_json():
with open(file_name) as f:
......
import json
from datetime import datetime
import paho.mqtt.client as mqtt
from src.mqtt_influx_backend import jsonhandler
from src.mqtt_influx_backend import influxDBWriter
class MQTTClientHandler:
MEASUREMENT_NAME = "sensor_data"
TAG_ROOM = "room"
TAG_MAC = "mac"
FIELD_CO2 = "co2"
FIELD_TEMP = "temperature"
FIELD_HUMIDITY = "humidity"
# Konstruktor
def __init__(
self, broker_url: str, topic: str, influx_writer: influxDBWriter
):
def __init__(self, broker_url: str, topic: str, influx_writer: influxDBWriter):
# logger sollte hier zugeweist werden
self.mac_to_room = jsonhandler.load_json()
self.broker_url = broker_url
self.topic = topic
self.influx_writer = influx_writer
self.client = mqtt.Client()
# Events werden hier methoden
# Methoden werden hier Events zugeteilt
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
......@@ -23,46 +31,44 @@ class MQTTClientHandler:
print("Connected with result code " + str(rc))
client.subscribe(self.topic)
# def save_mapping(self):
# with open(self.mapping_file, "w") as f:
# json.dump(self.mac_to_room, f, indent=4)
# eventuell refactorn und die Aufgaben in Methoden aufteilen
def on_message(self, client, userdata, msg):
"""
Wenn das Topic eine Nachricht bekommt wird diese Methode ausgeführt
self: ist die MQTTClientHandler instanz, die wird gebraucht
self: ist die MQTTClientHandler instanz, die wird gebraucht um die Einträge in
die InfluxDB zu schreiben
"""
# log
msg = json.loads(msg.payload)
metadate = msg["metadata"]
# key: mac : value : room
# hier prüfen, ob die Mac-Adresse einen Raum hat,
# wenn nicht trage es in mac_to_room leer ein
# "aa:bb:cc:dd:ee:ff" : ""
mac = metadate["mac"]
mac = metadate["mac-address"]
# ToImplement
"""
if mac not in self.mac_to_room:
# log
print(f"Neue MAC-Adresse gefunden: {mac}. Mapping wird ergänzt.")
self.mac_to_room[mac] = "" # leerer Platzhalter
self.save_mapping()
jsonhandler.write_json(self.mac_to_room)
self.mac_to_room = jsonhandler.load_json()
return
room = self.mac_to_room[mac]
"""
self.write_to_influxDB(msg,metadate)
def write_to_influxDB(self, msg, metadate):
try:
self.influx_writer.write_point(
measurement="sensor_data",
measurement=self.MEASUREMENT_NAME,
tags={
# ToDo "room": metadate["todo"],
"mac": metadate["mac-address"]
self.TAG_ROOM : self.mac_to_room[metadate["mac-address"]],
self.TAG_MAC: metadate["mac-address"]
},
fields={
"co2": msg["co2"],
"temperature": msg["temp"],
"humidity": msg["rh"],
self.FIELD_CO2: msg["co2"],
self.FIELD_TEMP: msg["temp"],
self.FIELD_HUMIDITY: msg["rh"],
},
timestamp=metadate["time"],
)
......@@ -71,7 +77,9 @@ class MQTTClientHandler:
) # muss später rausgeschmiessen werden
except Exception as e:
# log
print("Error processing message:", e)
print("Failed writing to InfluxDb: ",e)
def start(self):
self.client.connect(self.broker_url)
......
{
"AA:BB:CC:DD:EE:FF": "Wohnzimmer",
"11:22:33:44:55:66": "Küche",
"77:88:99:AA:BB:CC": "Schlafzimmer"
"11:22:33:44:55:66": "K\u00fcche",
"77:88:99:AA:BB:CC": "Schlafzimmer",
"DE:AD:BE:EF:12:34": ""
}
\ No newline at end of file
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