diff --git a/.idea/projektarbeit_duelger_waldhauser_caliskan.iml b/.idea/projektarbeit_duelger_waldhauser_caliskan.iml index 68f2462..74d515a 100644 --- a/.idea/projektarbeit_duelger_waldhauser_caliskan.iml +++ b/.idea/projektarbeit_duelger_waldhauser_caliskan.iml @@ -4,7 +4,7 @@ - + \ No newline at end of file diff --git a/software/backend/data_functions.py b/software/backend/data_functions.py index 8ff1650..13b3ea1 100644 --- a/software/backend/data_functions.py +++ b/software/backend/data_functions.py @@ -7,41 +7,62 @@ Every function should return json format with the wanted data from the database import paho.mqtt.client as mqtt from plantdatabase import PlantDataBase from software.defines import Topics +import json +import uuid # Robot Channel Reactions -def data_sensordata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): +def data_sensordata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase, order_handler: list): print("message received") # TODO: Store data in database - pass + str_in = str(message.payload.decode("UTF-8")) + payload = json.loads(str_in) + print("Received data: ", json.dumps(payload)) + + order_handler.remove(payload['ActionID']) + + mydatabase.insert_measurement_data(plant_id=payload['PlantID'], + sensordata_temp=payload['AirTemperature'], + sensordata_humidity=payload['AirHumidity'], + sensordata_soil_moisture=payload['SoilMoisture'], + pest_infestation=0, + light_intensity=payload['Brightness']) -def data_position(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): +def data_position(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): # TODO: Forward to frontend in json format - pass + client.publish(Topics['BACKEND_DATA_POSITION'], message.payload.decode("utf-8")) -def data_battery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): +def data_battery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): # TODO: Forward to frontend in json format - pass + client.publish(Topics['BACKEND_DATA_BATTERY'], message.payload.decode("utf-8")) # FrontEnd Channel Reactions -def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): +def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase, order_handler: list): # TODO: ROBOT READY CHECK - client.publish(Topics['ROBOT_ACTION_DRIVE'], message.payload).decode("utf-8") + if len(order_handler) < 5: + order_handler.append(uuid.uuid4()) + else: + # TODO: What to do when no place in order_list left + pass + client.publish(Topics['ROBOT_ACTION_DRIVE'], message.payload.decode("utf-8")) -def action_getposition(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): +def action_getposition(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): client.publish(Topics['ROBOT_ACTION_GETPOSITION']) -def action_getbattery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): +def action_getbattery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): client.publish(Topics['ROBOT_ACTION_GETBATTERY']) -def action_getalldata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): +def action_getalldata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): # TODO: get data from database - pass + alldata = [] + for i in range(1, 7): + alldata.append(mydatabase.get_latest_data(plant_id=i)) + client.publish(Topics['BACKEND_DATA_SENSORDATAALL'], json.dumps(alldata)) diff --git a/software/backend/main.py b/software/backend/main.py index cbd978e..41e7d02 100644 --- a/software/backend/main.py +++ b/software/backend/main.py @@ -16,47 +16,51 @@ import data_functions # inits mydatabase = PlantDataBase() mydatabase.create_table() +order_handler = [] # will contain UUIDS with Order IDs -def on_connect(client: mqtt.Client, userdata, flags, rc): +def on_connect(_client: mqtt.Client, _userdata, _flags, _rc): """ This method gets called, when it connects to a mqtt broker. It is used to subscribe to the specific topics - :param client: mqtt client object - :param userdata: - :param flags: - :param rc: connection flag + :param _client: mqtt client object + :param _userdata: + :param _flags: + :param _rc: connection flag :return: """ - if rc == 0: + if _rc == 0: print("connected") # TOPIC SUBSCRIPTIONS # From Robot: - client.subscribe(Topics['ROBOT_DATA_SENSORDATA']) - client.message_callback_add(Topics['ROBOT_DATA_SENSORDATA'], data_functions.data_sensordata) + _client.subscribe(Topics['ROBOT_DATA_SENSORDATA']) + _client.message_callback_add(Topics['ROBOT_DATA_SENSORDATA'], lambda client, userdata, message: data_functions. + data_sensordata(client, userdata, message, mydatabase, order_handler)) - client.subscribe(Topics['ROBOT_DATA_POSITION']) - client.message_callback_add(Topics['ROBOT_DATA_POSITION'], data_functions.data_position) + _client.subscribe(Topics['ROBOT_DATA_POSITION']) + _client.message_callback_add(Topics['ROBOT_DATA_POSITION'], data_functions.data_position) - client.subscribe(Topics['ROBOT_DATA_BATTERY']) - client.message_callback_add(Topics['ROBOT_DATA_BATTERY'], data_functions.data_sensordata) + _client.subscribe(Topics['ROBOT_DATA_BATTERY']) + _client.message_callback_add(Topics['ROBOT_DATA_BATTERY'], lambda client, userdata, message: data_functions. + data_battery(client, userdata, message, mydatabase)) # client.subscribe('Robot/Data/Picture') # From FrontEnd: - client.subscribe(Topics['BACKEND_ACTION_DRIVE']) - client.message_callback_add(Topics['BACKEND_ACTION_DRIVE'], data_functions.action_drive) + _client.subscribe(Topics['BACKEND_ACTION_DRIVE']) + _client.message_callback_add(Topics['BACKEND_ACTION_DRIVE'], lambda client, userdata, message: data_functions. + action_drive(client, userdata, message, mydatabase, order_handler)) - client.subscribe(Topics['BACKEND_ACTION_GETPOSITION']) - client.message_callback_add(Topics['BACKEND_ACTION_GETPOSITION'], data_functions.action_getposition) + _client.subscribe(Topics['BACKEND_ACTION_GETPOSITION']) + _client.message_callback_add(Topics['BACKEND_ACTION_GETPOSITION'], data_functions.action_getposition) - client.subscribe(Topics['BACKEND_ACTION_GETBATTERY']) - client.message_callback_add(Topics['BACKEND_ACTION_GETBATTERY'], data_functions.action_getbattery) + _client.subscribe(Topics['BACKEND_ACTION_GETBATTERY']) + _client.message_callback_add(Topics['BACKEND_ACTION_GETBATTERY'], data_functions.action_getbattery) - client.subscribe(Topics['BACKEND_ACTION_GETALLDATA']) - client.message_callback_add(Topics['BACKEND_ACTION_GETALLDATA'], data_functions.action_getalldata) + _client.subscribe(Topics['BACKEND_ACTION_GETALLDATA']) + _client.message_callback_add(Topics['BACKEND_ACTION_GETALLDATA'], data_functions.action_getalldata) # END TOPIC SUBSCRIPTIONS else: diff --git a/software/defines.py b/software/defines.py index face226..9eef7ae 100644 --- a/software/defines.py +++ b/software/defines.py @@ -33,15 +33,14 @@ Topics = { } - # MQTT Messages: # region Robot -> Backend SENSORDATA = { "AirTemperature": 0.0, - "AirHumidity" : 0.0, - "SoilMoisture" : 0.0, - "Brightness" : 0, + "AirHumidity": 0.0, + "SoilMoisture": 0.0, + "Brightness": 0, "PlantID": 0, "ActionID": 0 } @@ -74,9 +73,9 @@ DRIVE = { PLANTDATA = { "AirTemperature": 0.0, - "AirHumidity" : 0.0, - "SoilMoisture" : 0.0, - "Brightness" : 0, + "AirHumidity": 0.0, + "SoilMoisture": 0.0, + "Brightness": 0, "PlantID": 0, "Timestamp": "", "MeasurementID": 0 @@ -116,4 +115,4 @@ DRIVE = { # GETALLDATA -> no message needed -# endregion \ No newline at end of file +# endregion diff --git a/software/roboter/raspy/receive_json.py b/software/roboter/raspy/receive_json.py index 545c12c..b09dd81 100644 --- a/software/roboter/raspy/receive_json.py +++ b/software/roboter/raspy/receive_json.py @@ -1,11 +1,12 @@ import paho.mqtt.client as mqtt import json + def on_message_json(client, userdata, message): strIn = str(message.payload.decode("UTF-8")) dataDict = json.loads(strIn) - print("Received data: ", json.dumps(dataDict)) - + print("Received data: ", type(dataDict)) + mqttBroker = "mqtt.eclipseprojects.io" client = mqtt.Client("Smartphone_temp") @@ -17,4 +18,4 @@ client.message_callback_add("Robot/Data", on_message_json) client.connect(mqttBroker) client.subscribe("Robot/Data") -client.loop_forever() \ No newline at end of file +client.loop_forever()