diff --git a/documentation/~$finitions.docx b/documentation/~$finitions.docx new file mode 100644 index 0000000..5b37f4c Binary files /dev/null and b/documentation/~$finitions.docx differ diff --git a/software/backend/data_functions.py b/software/backend/data_functions.py new file mode 100644 index 0000000..ef4360f --- /dev/null +++ b/software/backend/data_functions.py @@ -0,0 +1,46 @@ +""" +created by caliskan at 19.04.2023 + +This file contains all functions, which handle the different cases. +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 + + +# Robot Channel Reactions + +def data_sensordata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): + print("message received") + # TODO: Store data in database + pass + + +def data_position(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): + # TODO: Publish as json + pass + + +def data_battery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): + # TODO: Publish as json + pass + + +# FrontEnd Channel Reactions + +def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): + client.publish('Robot/Action/Drive', message.payload).decode("utf-8") + + +def action_getposition(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): + client.publish('Robot/Action/GetPosition') + + +def action_getbattery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): + client.publish(Topics.drive) + + +def action_getalldata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): + # TODO: get data from database + pass diff --git a/software/backend/data_handling_functions.py b/software/backend/data_handling_functions.py deleted file mode 100644 index 9b3ba23..0000000 --- a/software/backend/data_handling_functions.py +++ /dev/null @@ -1,70 +0,0 @@ -""" -created by caliskan at 19.04.2023 - -This file contains all functions, which handle the different cases. -Every function should return json format with the wanted data from the database -""" -import paho.mqtt.client as mqtt -from plantdatabase import PlantDataBase - - -def data_handler(client: mqtt.Client, message: mqtt.MQTTMessage, mydatabase: PlantDataBase) -> None: - """ - main entrypoint for a message handling method - :param mydatabase: database with plant data - :param client: mqtt client - :param message: received message - :return: None - """ - if message.topic.startswith('Robot'): - if message.topic == 'Robot/Action/SensorData': - data_sensordata(client, message) - elif message.topic == 'Robot/Action/Position': - data_position(client, message) - elif message.topic == 'Robot/Action/Battery': - data_battery(client, message) - elif message.topic.startswith('BackEnd'): - if message.topic == 'BackEnd/Action/Drive': - action_drive(client, message) - elif message.topic == 'BackEnd/Action/GetPosition': - action_getposition(client) - elif message.topic == 'BackEnd/Action/GetBattery': - action_getbattery(client) - elif message.topic == 'BackEnd/Action/GetAllData': - action_getalldata(client) - - -# Robot Channel Reactions - -def data_sensordata(client: mqtt.Client, message: mqtt.MQTTMessage): - # TODO: Store data in database - pass - - -def data_position(client: mqtt.Client, message: mqtt.MQTTMessage): - # TODO: Publish as json - pass - - -def data_battery(client: mqtt.Client, message: mqtt.MQTTMessage): - # TODO: Publish as json - pass - - -# FrontEnd Channel Reactions - -def action_drive(client: mqtt.Client, message: mqtt.MQTTMessage): - client.publish('Robot/Action/Drive', message.payload).decode("utf-8") - - -def action_getposition(client: mqtt.Client): - client.publish('Robot/Action/GetPosition') - - -def action_getbattery(client: mqtt.Client): - client.publish('Robot/Action/GetBattery') - - -def action_getalldata(client: mqtt.Client): - # TODO: get data from database - pass diff --git a/software/backend/defines.py b/software/backend/defines.py deleted file mode 100644 index 947b4d6..0000000 --- a/software/backend/defines.py +++ /dev/null @@ -1,10 +0,0 @@ -""" -created by caliskan at 19.04.2023 - -contains all constants for the backend architecture of the smart garden project -""" - -MQTT_BROKER_LOCAL = "lorem ipsum" -MQTT_BROKER_GLOBAL = "mqtt.eclipseprojects.io" -RASPI_CLIENT_ID = "lorem ipsum" -BACKEND_CLIENT_ID = "lorem ipsum" diff --git a/software/backend/dev_test_examples/mqtt_publisher.py b/software/backend/dev_test_examples/mqtt_publisher.py index 4e65bc1..47f00da 100644 --- a/software/backend/dev_test_examples/mqtt_publisher.py +++ b/software/backend/dev_test_examples/mqtt_publisher.py @@ -9,8 +9,7 @@ client = mqtt.Client("Temperature_Inside") client.connect(mqttBroker) while True: - randNumber = uniform(20.0, 21.0) - client.publish("Robot/Data/Battery") - print("Just published " + str(randNumber) + " to topic TEMPERATURE") + client.publish("ROBOT/ACTION/DRIVE") + print("Just published to topic TEMPERATURE") time.sleep(0.5) diff --git a/software/backend/main.py b/software/backend/main.py index dd8ed26..8164850 100644 --- a/software/backend/main.py +++ b/software/backend/main.py @@ -9,29 +9,15 @@ Used protocol for interaction: mqtt (paho-mqtt module) # imports import paho.mqtt.client as mqtt -from defines import MQTT_BROKER_LOCAL, MQTT_BROKER_GLOBAL +from software.defines import MQTT_BROKER_LOCAL, MQTT_BROKER_GLOBAL, Topics from plantdatabase import PlantDataBase -from data_handling_functions import data_handler +import data_functions # inits mydatabase = PlantDataBase() mydatabase.create_table() -def on_message(client: mqtt.Client, userdata, message): - """ - This method gets called, if a subscribed channel gets a new message. - Message gets forwarded to the data handler - :param client: mqtt client object - :param userdata: - :param message: received message object - :return: None - """ - print(type(message)) - print(f'message received! {message.topic}') - data_handler(client, message, mydatabase) - - def on_connect(client: mqtt.Client, userdata, flags, rc): """ This method gets called, when it connects to a mqtt broker. @@ -48,16 +34,30 @@ def on_connect(client: mqtt.Client, userdata, flags, rc): # TOPIC SUBSCRIPTIONS # From Robot: - client.subscribe('Robot/Data/SensorData') - client.subscribe('Robot/Data/Position') - client.subscribe('Robot/Data/Battery') + client.subscribe(Topics['robot_data_sensordata']) + client.message_callback_add(Topics['robot_data_sensordata'], data_functions.data_sensordata) + + 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('Robot/Data/Picture') # From FrontEnd: - client.subscribe('BackEnd/Action/Drive') - client.subscribe('BackEnd/Action/GetPosition') - client.subscribe('BackEnd/Action/GetBattery') - client.subscribe('BackEnd/Action/GetAllData') + client.subscribe(Topics['backend_action_drive']) + client.message_callback_add(Topics['backend_action_drive'], data_functions.action_drive) + + 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_getalldata']) + client.message_callback_add(Topics['backend_action_getalldata'], data_functions.action_getalldata) + # END TOPIC SUBSCRIPTIONS else: print("connection failed") @@ -66,7 +66,6 @@ def on_connect(client: mqtt.Client, userdata, flags, rc): def main(): client = mqtt.Client() client.on_connect = on_connect - client.on_message = on_message client.connect(MQTT_BROKER_GLOBAL) client.loop_forever() diff --git a/software/defines.py b/software/defines.py new file mode 100644 index 0000000..211542a --- /dev/null +++ b/software/defines.py @@ -0,0 +1,34 @@ +""" +created by caliskan at 19.04.2023 + +contains all constants for the backend architecture of the smart garden project +""" + +MQTT_BROKER_LOCAL = "lorem ipsum" +MQTT_BROKER_GLOBAL = "mqtt.eclipseprojects.io" +RASPI_CLIENT_ID = "lorem ipsum" +BACKEND_CLIENT_ID = "lorem ipsum" + +# Topics: +Topics = { + "robot_action_drive": "ROBOT/ACTION/DRIVE", + "robot_action_getposition": "ROBOT/ACTION/GETPOSITION", + "robot_action_getbattery": "ROBOT/ACTION/GETBATTERY", + + "robot_data_sensordata": "ROBOT/DATA/SENSORDATA", + "robot_data_battery": "ROBOT/DATA/BATTERY", + "robot_data_position": "ROBOT/DATA/POSITION", + "robot_data_picture": "ROBOT/DATA/PICTURE", + + "backend_action_drive": "BACKEND/ACTION/DRIVE", + "backend_action_getposition": "BACKEND/ACTION/GETPOSITION", + "backend_action_getbattery": "BACKEND/ACTION/GETBATTERY", + "backend_action_getalldata": "BACKEND/ACTION/GETALLDATA", + + "backend_data_sensordata": "BACKEND/DATA/SENSORDATA", + "backend_data_sensordataall": "BACKEND/DATA/SENSORDATA_ALL", + "backend_data_position": "BACKEND/DATA/POSITION", + "backend_data_battery": "BACKEND/DATA/BATTERY", + "backend_data_picture": "BACKEND/DATA/PICTURE" + +}