From 9cffde5d2b788d4d5798b33be1af42dbc7809cf1 Mon Sep 17 00:00:00 2001 From: d-parkin Date: Sun, 25 Jun 2023 20:26:28 +0200 Subject: [PATCH] testing json decoding --- webApp/Backend/sensor/mqtt.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/webApp/Backend/sensor/mqtt.py b/webApp/Backend/sensor/mqtt.py index 554f26c..daa844a 100644 --- a/webApp/Backend/sensor/mqtt.py +++ b/webApp/Backend/sensor/mqtt.py @@ -83,16 +83,27 @@ def on_message(client, userdata, msg): # Parsing the payload message for the data we want def process_message(msg): + msg = [{-2: "gps", 6: 6, 3: "1234.5678,1234.5678"}, {-2: "crash", 6: 1, 4: True}, {-2: "hum", 6: 2, 2: 61}] # Decode the SenML message - # Might change to SenmlRecord # Convert SenML data to JSON format # payload = json.dumps(msg) doc = SenmlPack('Payload') doc.add(msg) - json_data = doc.to_json() + # Extract values from the first SenML data object + # Extracts GPS + first_senml_data = json_data[0] + value = first_senml_data[3] + sensor = first_senml_data[-2] + current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + + print("Current Time:", current_time) + print("GPS Value:", value) + print("Sensor Type:", sensor) + # Print the decoded SenML message + print("=============================================") print(doc.to_json()) @@ -117,15 +128,15 @@ def process_message(msg): # client.on_message = on_message -# client.username_pw_set(USERNAME, API_KEY) -# client.connect(TTN_MQTT_SERVER, 80, 60) +client.username_pw_set(USERNAME, API_KEY) +client.connect(TTN_MQTT_SERVER, 80, 60) # Blocking call that processes network traffic, dispatches callbacks and # handles reconnecting. # Other loop*() functions are available that give a threaded interface and a # manual interface. -# client.loop_forever() +client.loop_forever() # if __name__ == "__main__": # print("Test") # postToDatabase("time", "data", "dataType", "owner")