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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 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: robot object for
  21. :param _mydatabase: object, that contains the custom plant database and methods for its usage
  22. :param _client: mqtt client object
  23. :param _userdata:
  24. :param _flags:
  25. :param _rc: connection flag
  26. :return: none
  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_ALL'])
  36. _client.message_callback_add(Topics['ROBOT_DATA_ALL'], lambda client, userdata, message: data_functions.
  37. data_sensordataall(client, userdata, message, _mydatabase, _robot))
  38. _client.subscribe(Topics['ROBOT_DATA_POSITION'])
  39. _client.message_callback_add(Topics['ROBOT_DATA_POSITION'], data_functions.data_position)
  40. _client.subscribe(Topics['ROBOT_DATA_BATTERY'])
  41. _client.message_callback_add(Topics['ROBOT_DATA_BATTERY'], lambda client, userdata, message: data_functions.
  42. data_battery(client, userdata, message, _robot))
  43. _client.subscribe(Topics['ROBOT_DATA_ERROR'])
  44. _client.message_callback_add(Topics['ROBOT_DATA_ERROR'], lambda client, userdata, message: data_functions.
  45. data_error(client, userdata, message, _robot))
  46. _client.subscribe(Topics['ROBOT_DATA_ROBOTREADY'])
  47. _client.message_callback_add(Topics['ROBOT_DATA_ROBOTREADY'], lambda client, userdata, message: data_functions.
  48. data_robotready(client, userdata, message, _robot))
  49. # From FrontEnd:
  50. _client.subscribe(Topics['BACKEND_ACTION_DRIVE'])
  51. _client.message_callback_add(Topics['BACKEND_ACTION_DRIVE'], lambda client, userdata, message: data_functions.
  52. action_drive(client, userdata, message, _mydatabase, _robot))
  53. _client.subscribe(Topics['BACKEND_ACTION_DRIVEALL'])
  54. _client.message_callback_add(Topics['BACKEND_ACTION_DRIVEALL'], lambda client, userdata, message: data_functions.
  55. action_driveall(client, userdata, message, _mydatabase, _robot))
  56. _client.subscribe(Topics['BACKEND_ACTION_GETPOSITION'])
  57. _client.message_callback_add(Topics['BACKEND_ACTION_GETPOSITION'], data_functions.action_getposition)
  58. _client.subscribe(Topics['BACKEND_ACTION_GETBATTERY'])
  59. _client.message_callback_add(Topics['BACKEND_ACTION_GETBATTERY'], data_functions.action_getbattery)
  60. _client.subscribe(Topics['BACKEND_ACTION_GETALLDATA'])
  61. _client.message_callback_add(Topics['BACKEND_ACTION_GETALLDATA'],
  62. lambda client, userdata, message: data_functions.
  63. action_getalldata(client, userdata, message, _mydatabase))
  64. _client.subscribe(Topics['BACKEND_ACTION_NEWPLANT'])
  65. _client.message_callback_add(Topics['BACKEND_ACTION_NEWPLANT'], lambda client, userdata, message: data_functions.
  66. action_newplant(client, userdata, message, _mydatabase))
  67. _client.subscribe(Topics['BACKEND_ACTION_CONFIGUREPLANT'])
  68. _client.message_callback_add(Topics['BACKEND_ACTION_CONFIGUREPLANT'], lambda client, userdata, message: data_functions.
  69. action_configureplant(client, userdata, message, _mydatabase))
  70. _client.subscribe(Topics['BACKEND_ACTION_DELETEPLANT'])
  71. _client.message_callback_add(Topics['BACKEND_ACTION_DELETEPLANT'],
  72. lambda client, userdata, message: data_functions.
  73. action_deleteplant(client, userdata, message, _mydatabase))
  74. _client.subscribe(Topics['BACKEND_ACTION_PLANTCOUNT'])
  75. _client.message_callback_add(Topics['BACKEND_ACTION_PLANTCOUNT'], lambda client, userdata, message: data_functions.
  76. action_countplants(client, userdata, message, _mydatabase))
  77. # END TOPIC SUBSCRIPTIONS
  78. else:
  79. print("connection failed")
  80. def main():
  81. # Create Robot Object
  82. robot = Robot()
  83. # Connect to Plant_Database and create tables if database did not exist
  84. my_database = PlantDataBase(database_name=DATABASE_NAME)
  85. my_database.create_tables()
  86. # Create MQTT Client and connect to local broker
  87. mqttclient = mqtt.Client(BACKEND_CLIENT_ID, """transport="websockets""""") # transport websockets required for local broker
  88. mqttclient.on_connect = lambda client, userdata, flags, rc: on_connect(_client=client,
  89. _userdata=userdata,
  90. _flags=flags,
  91. _rc=rc,
  92. _mydatabase=my_database,
  93. _robot=robot)
  94. mqttclient.connect(MQTT_BROKER_GLOBAL)
  95. # Initialize logger and save in server.log file
  96. logging.basicConfig(filename="server.log", filemode="a", encoding="utf-8", level=logging.DEBUG,
  97. format='%(asctime)s %(name)s %(levelname)s %(message)s',
  98. datefmt="%d-%m-%Y %H:%M:%S")
  99. logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
  100. # Starting mqttclient infinite loop
  101. mqttclient.loop_forever()
  102. if __name__ == "__main__":
  103. main()