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.

data_functions.py 5.0KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """
  2. created by caliskan at 19.04.2023
  3. This file contains all functions, which handle the different cases.
  4. Every function should return json format with the wanted data from the database
  5. """
  6. import paho.mqtt.client as mqtt
  7. from plantdatabase import PlantDataBase
  8. from software.defines import Topics, MAX_PLANT_COUNT
  9. import json
  10. import uuid
  11. # Robot Channel Reactions
  12. def data_sensordata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase,
  13. order_handler: list):
  14. print("message received")
  15. # TODO: Store data in database
  16. str_in = str(message.payload.decode("UTF-8"))
  17. payload = json.loads(str_in)
  18. print("Received data: ", json.dumps(payload))
  19. order_handler.remove(payload['ActionID'])
  20. mydatabase.insert_measurement_data(plant_id=payload['PlantID'],
  21. sensordata_temp=payload['AirTemperature'],
  22. sensordata_humidity=payload['AirHumidity'],
  23. sensordata_soil_moisture=payload['SoilMoisture'],
  24. sensordata_brightness=payload['Brightness'])
  25. def data_position(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  26. # TODO: Forward to frontend in json format
  27. client.publish(Topics['BACKEND_DATA_POSITION'], message.payload.decode("utf-8"))
  28. def data_battery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  29. # TODO: Forward to frontend in json format
  30. client.publish(Topics['BACKEND_DATA_BATTERY'], message.payload.decode("utf-8"))
  31. # FrontEnd Channel Reactions
  32. def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase,
  33. order_handler: list):
  34. # TODO: ROBOT READY CHECK
  35. if len(order_handler) < 5:
  36. order_handler.append(uuid.uuid4())
  37. else:
  38. # TODO: What to do when no place in order_list left
  39. pass
  40. client.publish(Topics['ROBOT_ACTION_DRIVE'], message.payload.decode("utf-8"))
  41. def action_driveall(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  42. # TODO: Implement here
  43. pass
  44. def action_getposition(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  45. client.publish(Topics['ROBOT_ACTION_GETPOSITION'])
  46. def action_getbattery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  47. client.publish(Topics['ROBOT_ACTION_GETBATTERY'])
  48. def action_getalldata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  49. plant_names = json.loads(message.payload.decode("UTF-8"))
  50. print(plant_names)
  51. alldata = []
  52. for i in plant_names:
  53. alldata.append(mydatabase.get_latest_data(plant_name=i))
  54. client.publish(Topics['BACKEND_DATA_SENSORDATAALL'], json.dumps(alldata))
  55. def action_newplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  56. plant_data = json.loads(message.payload.decode("UTF-8"))
  57. mydatabase.insert_plant(plantname=plant_data["PlantName"], plant_id=plant_data["PlantID"])
  58. mydatabase.insert_measurement_data(plant_id=plant_data["PlantID"],
  59. sensordata_temp=plant_data["AirTemperature"],
  60. sensordata_humidity=plant_data["AirHumidity"],
  61. sensordata_soil_moisture=plant_data["SoilMoisture"],
  62. sensordata_brightness=plant_data["Brightness"])
  63. print("BACKEND_ACTION_NEWPLANT RECEIVED DATA: " + str(plant_data))
  64. def action_configureplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  65. plant_data = json.loads(message.payload.decode("UTF-8"))
  66. mydatabase.configure_plant(plant_id=plant_data["plant_ID"], plantname=plant_data["PlantName"])
  67. mydatabase.insert_measurement_data(plant_id=plant_data["PlantID"],
  68. sensordata_temp=plant_data["AirTemperature"],
  69. sensordata_humidity=plant_data["AirHumidity"],
  70. sensordata_soil_moisture=plant_data["SoilMoisture"],
  71. sensordata_brightness=plant_data["Brightness"])
  72. print("BACKEND_ACTION_CONFIGUREPLANT RECEIVED DATA: " + str(plant_data))
  73. def action_deleteplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  74. delete_plant = json.loads(message.payload.decode("UTF-8"))
  75. mydatabase.delete_plant(plant_id=delete_plant["PlantID"])
  76. print("BACKEND_ACTION_DELETEPLANT RECEIVED DATA: " + str(delete_plant))
  77. def action_countplants(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  78. count_payload = {
  79. "CurrentCount": mydatabase.plant_count(),
  80. "MaxCount": MAX_PLANT_COUNT
  81. }
  82. print(type(json.dumps(count_payload)))
  83. client.publish(Topics["BACKEND_ACTION_PLANTCOUNT"], json.dumps(count_payload))