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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. from typing import Union
  12. # Robot Channel Reactions
  13. def data_sensordata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase,
  14. order_handler: list):
  15. print("message received")
  16. # TODO: Store data in database
  17. str_in = str(message.payload.decode("UTF-8"))
  18. payload = json.loads(str_in)
  19. print("Received data: ", json.dumps(payload))
  20. order_handler.remove(payload['ActionID'])
  21. mydatabase.insert_measurement_data(plant_id=payload['PlantID'],
  22. sensordata_temp=payload['AirTemperature'],
  23. sensordata_humidity=payload['AirHumidity'],
  24. sensordata_soil_moisture=payload['SoilMoisture'],
  25. sensordata_brightness=payload['Brightness'])
  26. def data_position(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  27. # TODO: Forward to frontend in json format
  28. client.publish(Topics['BACKEND_DATA_POSITION'], message.payload.decode("utf-8"))
  29. def data_battery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  30. # TODO: Forward to frontend in json format
  31. client.publish(Topics['BACKEND_DATA_BATTERY'], message.payload.decode("utf-8"))
  32. # FrontEnd Channel Reactions
  33. def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase,
  34. order_handler: list):
  35. # TODO: ROBOT READY CHECK
  36. if len(order_handler) < 5:
  37. order_handler.append(uuid.uuid4())
  38. else:
  39. # TODO: What to do when no place in order_list left
  40. pass
  41. client.publish(Topics['ROBOT_ACTION_DRIVE'], message.payload.decode("utf-8"))
  42. def action_driveall(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  43. # TODO: Implement here
  44. pass
  45. def action_getposition(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  46. client.publish(Topics['ROBOT_ACTION_GETPOSITION'])
  47. def action_getbattery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  48. client.publish(Topics['ROBOT_ACTION_GETBATTERY'])
  49. def action_getalldata(client: mqtt.Client, userdata, message: Union[mqtt.MQTTMessage, list], mydatabase: PlantDataBase):
  50. plant_names = mydatabase.get_plant_names()
  51. print(type(plant_names))
  52. alldata = []
  53. for i in plant_names:
  54. print("I Type: " + str(type(i)))
  55. print("I: " + i[0])
  56. alldata.append(mydatabase.get_latest_data(plant_name=i[0]))
  57. client.publish(Topics['BACKEND_DATA_SENSORDATAALL'], json.dumps(alldata))
  58. print("BACKEND_DATA_SENSORDATAALL SEND DATA:" + str(alldata))
  59. def action_newplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  60. plant_data = json.loads(message.payload.decode("UTF-8"))
  61. mydatabase.insert_plant(plantname=plant_data["PlantName"], plant_id=plant_data["PlantID"])
  62. mydatabase.insert_measurement_data(plant_id=plant_data["PlantID"],
  63. sensordata_temp=plant_data["AirTemperature"],
  64. sensordata_humidity=plant_data["AirHumidity"],
  65. sensordata_soil_moisture=plant_data["SoilMoisture"],
  66. sensordata_brightness=plant_data["Brightness"])
  67. print("BACKEND_ACTION_NEWPLANT RECEIVED DATA: " + str(plant_data))
  68. print(mydatabase.get_plant_names())
  69. action_getalldata(client, userdata, message, mydatabase)
  70. def action_configureplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  71. plant_data = json.loads(message.payload.decode("UTF-8"))
  72. mydatabase.configure_plant(plant_id=plant_data["PlantID"], plantname=plant_data["PlantName"])
  73. mydatabase.insert_measurement_data(plant_id=plant_data["PlantID"],
  74. sensordata_temp=plant_data["AirTemperature"],
  75. sensordata_humidity=plant_data["AirHumidity"],
  76. sensordata_soil_moisture=plant_data["SoilMoisture"],
  77. sensordata_brightness=plant_data["Brightness"])
  78. print("BACKEND_ACTION_CONFIGUREPLANT RECEIVED DATA: " + str(plant_data))
  79. action_getalldata(client, userdata, message, mydatabase)
  80. def action_deleteplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  81. delete_plant = json.loads(message.payload.decode("UTF-8"))
  82. mydatabase.delete_plant(plant_id=delete_plant)
  83. print("BACKEND_ACTION_DELETEPLANT RECEIVED DATA: " + str(delete_plant))
  84. action_getalldata(client, userdata, message, mydatabase)
  85. def action_countplants(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  86. count = mydatabase.plant_count()
  87. count_payload = {
  88. "CurrentCount": count,
  89. "MaxCount": MAX_PLANT_COUNT
  90. }
  91. client.publish(Topics["BACKEND_DATA_PLANTCOUNT"], json.dumps(count_payload))
  92. print("BACKEND_DATA_PLANTCOUNT SEND DATA: " + str(count_payload))