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 4.1KB

1 year ago
1 year ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_DRIVEALL'])
  45. _client.message_callback_add(Topics['BACKEND_ACTION_DRIVE'], lambda client, userdata, message: data_functions.
  46. action_driveall(client, userdata, message, mydatabase))
  47. _client.subscribe(Topics['BACKEND_ACTION_GETPOSITION'])
  48. _client.message_callback_add(Topics['BACKEND_ACTION_GETPOSITION'], data_functions.action_getposition)
  49. _client.subscribe(Topics['BACKEND_ACTION_GETBATTERY'])
  50. _client.message_callback_add(Topics['BACKEND_ACTION_GETBATTERY'], data_functions.action_getbattery)
  51. _client.subscribe(Topics['BACKEND_ACTION_GETALLDATA'])
  52. _client.message_callback_add(Topics['BACKEND_ACTION_GETALLDATA'],
  53. lambda client, userdata, message: data_functions.
  54. action_getalldata(client, userdata, message, mydatabase))
  55. _client.subscribe(Topics['BACKEND_ACTION_NEWPLANT'])
  56. _client.message_callback_add(Topics['BACKEND_ACTION_NEWPLANT'], lambda client, userdata, message: data_functions.
  57. action_newplant(client, userdata, message, mydatabase))
  58. _client.subscribe(Topics['BACKEND_ACTION_CONFIGUREPLANT'])
  59. _client.message_callback_add(Topics['BACKEND_ACTION_CONFIGUREPLANT'], lambda client, userdata, message: data_functions.
  60. action_configureplant(client, userdata, message, mydatabase))
  61. _client.subscribe(Topics['BACKEND_ACTION_DELETEPLANT'])
  62. _client.message_callback_add(Topics['BACKEND_ACTION_DELETEPLANT'],
  63. lambda client, userdata, message: data_functions.
  64. action_deleteplant(client, userdata, message, mydatabase))
  65. # END TOPIC SUBSCRIPTIONS
  66. else:
  67. print("connection failed")
  68. def main():
  69. client = mqtt.Client(BACKEND_CLIENT_ID)
  70. client.on_connect = on_connect
  71. client.connect(MQTT_BROKER_GLOBAL)
  72. client.loop_forever()
  73. if __name__ == "__main__":
  74. main()