1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- """
- created by caliskan at 19.04.2023
-
- This file contains the main script for the backend server of smart garden project
- It has the task to be a bridge between the frontend and robot.
- It also contains a database with the current plant data
- Used protocol for interaction: mqtt (paho-mqtt module)
- """
-
- # imports
- import paho.mqtt.client as mqtt
- from software.defines import MQTT_BROKER_LOCAL, MQTT_BROKER_GLOBAL, Topics
- from plantdatabase import PlantDataBase
- import data_functions
-
- # inits
- mydatabase = PlantDataBase()
- mydatabase.create_table()
-
-
- 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
- :return:
- """
- 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_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(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")
-
-
- def main():
- client = mqtt.Client()
- client.on_connect = on_connect
- client.connect(MQTT_BROKER_GLOBAL)
- client.loop_forever()
-
-
- if __name__ == "__main__":
- main()
|