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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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
  9. import json
  10. import uuid
  11. # Robot Channel Reactions
  12. def data_sensordata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase, order_handler: list):
  13. print("message received")
  14. # TODO: Store data in database
  15. str_in = str(message.payload.decode("UTF-8"))
  16. payload = json.loads(str_in)
  17. print("Received data: ", json.dumps(payload))
  18. order_handler.remove(payload['ActionID'])
  19. mydatabase.insert_measurement_data(plant_id=payload['PlantID'],
  20. sensordata_temp=payload['AirTemperature'],
  21. sensordata_humidity=payload['AirHumidity'],
  22. sensordata_soil_moisture=payload['SoilMoisture'],
  23. pest_infestation=0,
  24. light_intensity=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, order_handler: list):
  33. # TODO: ROBOT READY CHECK
  34. if len(order_handler) < 5:
  35. order_handler.append(uuid.uuid4())
  36. else:
  37. # TODO: What to do when no place in order_list left
  38. pass
  39. client.publish(Topics['ROBOT_ACTION_DRIVE'], message.payload.decode("utf-8"))
  40. def action_getposition(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  41. client.publish(Topics['ROBOT_ACTION_GETPOSITION'])
  42. def action_getbattery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  43. client.publish(Topics['ROBOT_ACTION_GETBATTERY'])
  44. def action_getalldata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
  45. # TODO: get data from database
  46. alldata = []
  47. for i in range(1, 7):
  48. alldata.append(mydatabase.get_latest_data(plant_id=i))
  49. client.publish(Topics['BACKEND_DATA_SENSORDATAALL'], json.dumps(alldata))