2023-04-27 19:00:09 +02:00

69 lines
2.7 KiB
Python

"""
created by caliskan at 19.04.2023
This file contains all functions, which handle the different cases.
Every function should return json format with the wanted data from the database
"""
import paho.mqtt.client as mqtt
from plantdatabase import PlantDataBase
from software.defines import Topics
import json
import uuid
# Robot Channel Reactions
def data_sensordata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase, order_handler: list):
print("message received")
# TODO: Store data in database
str_in = str(message.payload.decode("UTF-8"))
payload = json.loads(str_in)
print("Received data: ", json.dumps(payload))
order_handler.remove(payload['ActionID'])
mydatabase.insert_measurement_data(plant_id=payload['PlantID'],
sensordata_temp=payload['AirTemperature'],
sensordata_humidity=payload['AirHumidity'],
sensordata_soil_moisture=payload['SoilMoisture'],
pest_infestation=0,
light_intensity=payload['Brightness'])
def data_position(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
# TODO: Forward to frontend in json format
client.publish(Topics['BACKEND_DATA_POSITION'], message.payload.decode("utf-8"))
def data_battery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
# TODO: Forward to frontend in json format
client.publish(Topics['BACKEND_DATA_BATTERY'], message.payload.decode("utf-8"))
# FrontEnd Channel Reactions
def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase, order_handler: list):
# TODO: ROBOT READY CHECK
if len(order_handler) < 5:
order_handler.append(uuid.uuid4())
else:
# TODO: What to do when no place in order_list left
pass
client.publish(Topics['ROBOT_ACTION_DRIVE'], message.payload.decode("utf-8"))
def action_getposition(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
client.publish(Topics['ROBOT_ACTION_GETPOSITION'])
def action_getbattery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
client.publish(Topics['ROBOT_ACTION_GETBATTERY'])
def action_getalldata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
# TODO: get data from database
alldata = []
for i in range(1, 7):
alldata.append(mydatabase.get_latest_data(plant_id=i))
client.publish(Topics['BACKEND_DATA_SENSORDATAALL'], json.dumps(alldata))