repository to manage all files related to the makeathon farm bot project (Software + Documentation).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.py 2.8KB

1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """
  2. created by caliskan at 19.04.2023
  3. This file contains the main script for the backend server of smart garden project
  4. It has the task to be a bridge between the frontend and robot.
  5. It also contains a database with the current plant data
  6. Used protocol for interaction: mqtt (paho-mqtt module)
  7. """
  8. # imports
  9. import paho.mqtt.client as mqtt
  10. from software.defines import MQTT_BROKER_LOCAL, MQTT_BROKER_GLOBAL, Topics, BACKEND_CLIENT_ID
  11. from plantdatabase import PlantDataBase
  12. import data_functions
  13. # inits
  14. mydatabase = PlantDataBase()
  15. mydatabase.create_table()
  16. order_handler = [] # will contain UUIDS with Order IDs
  17. def on_connect(_client: mqtt.Client, _userdata, _flags, _rc):
  18. """
  19. This method gets called, when it connects to a mqtt broker.
  20. It is used to subscribe to the specific topics
  21. :param _client: mqtt client object
  22. :param _userdata:
  23. :param _flags:
  24. :param _rc: connection flag
  25. :return:
  26. """
  27. if _rc == 0:
  28. print("connected")
  29. # TOPIC SUBSCRIPTIONS
  30. # From Robot:
  31. _client.subscribe(Topics['ROBOT_DATA_SENSORDATA'])
  32. _client.message_callback_add(Topics['ROBOT_DATA_SENSORDATA'], lambda client, userdata, message: data_functions.
  33. data_sensordata(client, userdata, message, mydatabase, order_handler))
  34. _client.subscribe(Topics['ROBOT_DATA_POSITION'])
  35. _client.message_callback_add(Topics['ROBOT_DATA_POSITION'], data_functions.data_position)
  36. _client.subscribe(Topics['ROBOT_DATA_BATTERY'])
  37. _client.message_callback_add(Topics['ROBOT_DATA_BATTERY'], lambda client, userdata, message: data_functions.
  38. data_battery(client, userdata, message, mydatabase))
  39. # client.subscribe('Robot/Data/Picture')
  40. # From FrontEnd:
  41. _client.subscribe(Topics['BACKEND_ACTION_DRIVE'])
  42. _client.message_callback_add(Topics['BACKEND_ACTION_DRIVE'], lambda client, userdata, message: data_functions.
  43. action_drive(client, userdata, message, mydatabase, order_handler))
  44. _client.subscribe(Topics['BACKEND_ACTION_GETPOSITION'])
  45. _client.message_callback_add(Topics['BACKEND_ACTION_GETPOSITION'], data_functions.action_getposition)
  46. _client.subscribe(Topics['BACKEND_ACTION_GETBATTERY'])
  47. _client.message_callback_add(Topics['BACKEND_ACTION_GETBATTERY'], data_functions.action_getbattery)
  48. _client.subscribe(Topics['BACKEND_ACTION_GETALLDATA'])
  49. _client.message_callback_add(Topics['BACKEND_ACTION_GETALLDATA'], data_functions.action_getalldata)
  50. # END TOPIC SUBSCRIPTIONS
  51. else:
  52. print("connection failed")
  53. def main():
  54. client = mqtt.Client(BACKEND_CLIENT_ID)
  55. client.on_connect = on_connect
  56. client.connect(MQTT_BROKER_GLOBAL)
  57. client.loop_forever()
  58. if __name__ == "__main__":
  59. main()