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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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, DATABASE_NAME
  11. from plantdatabase import PlantDataBase
  12. import data_functions
  13. import logging
  14. import sys
  15. from robot import Robot
  16. def on_connect(_client: mqtt.Client, _userdata, _flags, _rc, _mydatabase, _robot):
  17. """
  18. This method gets called, when it connects to a mqtt broker.
  19. It is used to subscribe to the specific topics
  20. :param _robot:
  21. :param _mydatabase:
  22. :param _client: mqtt client object
  23. :param _userdata:
  24. :param _flags:
  25. :param _rc: connection flag
  26. :return:
  27. """
  28. if _rc == 0:
  29. print("connected")
  30. # TOPIC SUBSCRIPTIONS
  31. # From Robot:
  32. _client.subscribe(Topics['ROBOT_DATA_SENSORDATA'])
  33. _client.message_callback_add(Topics['ROBOT_DATA_SENSORDATA'], lambda client, userdata, message: data_functions.
  34. data_sensordata(client, userdata, message, _mydatabase, _robot))
  35. _client.subscribe(Topics['ROBOT_DATA_POSITION'])
  36. _client.message_callback_add(Topics['ROBOT_DATA_POSITION'], data_functions.data_position)
  37. _client.subscribe(Topics['ROBOT_DATA_BATTERY'])
  38. _client.message_callback_add(Topics['ROBOT_DATA_BATTERY'], lambda client, userdata, message: data_functions.
  39. data_battery(client, userdata, message))
  40. # client.subscribe('Robot/Data/Picture')
  41. # From FrontEnd:
  42. _client.subscribe(Topics['BACKEND_ACTION_DRIVE'])
  43. _client.message_callback_add(Topics['BACKEND_ACTION_DRIVE'], lambda client, userdata, message: data_functions.
  44. action_drive(client, userdata, message, _mydatabase, _robot))
  45. _client.subscribe(Topics['BACKEND_ACTION_DRIVEALL'])
  46. _client.message_callback_add(Topics['BACKEND_ACTION_DRIVEALL'], lambda client, userdata, message: data_functions.
  47. action_driveall(client, userdata, message, _mydatabase, _robot))
  48. _client.subscribe(Topics['BACKEND_ACTION_GETPOSITION'])
  49. _client.message_callback_add(Topics['BACKEND_ACTION_GETPOSITION'], data_functions.action_getposition)
  50. _client.subscribe(Topics['BACKEND_ACTION_GETBATTERY'])
  51. _client.message_callback_add(Topics['BACKEND_ACTION_GETBATTERY'], data_functions.action_getbattery)
  52. _client.subscribe(Topics['BACKEND_ACTION_GETALLDATA'])
  53. _client.message_callback_add(Topics['BACKEND_ACTION_GETALLDATA'],
  54. lambda client, userdata, message: data_functions.
  55. action_getalldata(client, userdata, message, _mydatabase))
  56. _client.subscribe(Topics['BACKEND_ACTION_NEWPLANT'])
  57. _client.message_callback_add(Topics['BACKEND_ACTION_NEWPLANT'], lambda client, userdata, message: data_functions.
  58. action_newplant(client, userdata, message, _mydatabase))
  59. _client.subscribe(Topics['BACKEND_ACTION_CONFIGUREPLANT'])
  60. _client.message_callback_add(Topics['BACKEND_ACTION_CONFIGUREPLANT'], lambda client, userdata, message: data_functions.
  61. action_configureplant(client, userdata, message, _mydatabase))
  62. _client.subscribe(Topics['BACKEND_ACTION_DELETEPLANT'])
  63. _client.message_callback_add(Topics['BACKEND_ACTION_DELETEPLANT'],
  64. lambda client, userdata, message: data_functions.
  65. action_deleteplant(client, userdata, message, _mydatabase))
  66. _client.subscribe(Topics['BACKEND_ACTION_PLANTCOUNT'])
  67. _client.message_callback_add(Topics['BACKEND_ACTION_PLANTCOUNT'], lambda client, userdata, message: data_functions.
  68. action_countplants(client, userdata, message, _mydatabase))
  69. _client.subscribe(Topics['ROBOT_DATA_ERROR'])
  70. _client.message_callback_add(Topics['ROBOT_DATA_ERROR'], lambda client, userdata, message: data_functions.
  71. data_error(client, userdata, message, _robot))
  72. _client.subscribe(Topics['ROBOT_DATA_ROBOTREADY'])
  73. _client.message_callback_add(Topics['ROBOT_DATA_ROBOTREADY'], lambda client, userdata, message: data_functions.
  74. data_robotready(client, userdata, message, _robot))
  75. # END TOPIC SUBSCRIPTIONS
  76. else:
  77. print("connection failed")
  78. def main():
  79. robot = Robot()
  80. my_database = PlantDataBase(database_name=DATABASE_NAME)
  81. my_database.create_tables()
  82. mqttclient = mqtt.Client(BACKEND_CLIENT_ID)
  83. mqttclient.on_connect = lambda client, userdata, flags, rc: on_connect(_client=client,
  84. _userdata=userdata,
  85. _flags=flags,
  86. _rc=rc,
  87. _mydatabase=my_database,
  88. _robot=robot)
  89. mqttclient.connect(MQTT_BROKER_GLOBAL)
  90. logging.basicConfig(filename="server.log", filemode="a", encoding="utf-8", level=logging.DEBUG,
  91. format='%(asctime)s %(name)s %(levelname)s %(message)s',
  92. datefmt="%d-%m-%Y %H:%M:%S")
  93. logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
  94. mqttclient.loop_forever()
  95. if __name__ == "__main__":
  96. main()