""" 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, MAX_PLANT_COUNT 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'], sensordata_brightness=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_driveall(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): # TODO: Implement here pass 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): plant_names = json.loads(message.payload.decode("UTF-8")) print(plant_names) alldata = [] for i in plant_names: alldata.append(mydatabase.get_latest_data(plant_name=i)) client.publish(Topics['BACKEND_DATA_SENSORDATAALL'], json.dumps(alldata)) def action_newplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): plant_data = json.loads(message.payload.decode("UTF-8")) mydatabase.insert_plant(plantname=plant_data["PlantName"], plant_id=plant_data["PlantID"]) mydatabase.insert_measurement_data(plant_id=plant_data["PlantID"], sensordata_temp=plant_data["AirTemperature"], sensordata_humidity=plant_data["AirHumidity"], sensordata_soil_moisture=plant_data["SoilMoisture"], sensordata_brightness=plant_data["Brightness"]) print("BACKEND_ACTION_NEWPLANT RECEIVED DATA: " + str(plant_data)) def action_configureplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): plant_data = json.loads(message.payload.decode("UTF-8")) mydatabase.configure_plant(plant_id=plant_data["plant_ID"], plantname=plant_data["PlantName"]) mydatabase.insert_measurement_data(plant_id=plant_data["PlantID"], sensordata_temp=plant_data["AirTemperature"], sensordata_humidity=plant_data["AirHumidity"], sensordata_soil_moisture=plant_data["SoilMoisture"], sensordata_brightness=plant_data["Brightness"]) print("BACKEND_ACTION_CONFIGUREPLANT RECEIVED DATA: " + str(plant_data)) def action_deleteplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): delete_plant = json.loads(message.payload.decode("UTF-8")) mydatabase.delete_plant(plant_id=delete_plant["PlantID"]) print("BACKEND_ACTION_DELETEPLANT RECEIVED DATA: " + str(delete_plant)) def action_countplants(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): count_payload = { "CurrentCount": mydatabase.plant_count(), "MaxCount": MAX_PLANT_COUNT } print(type(json.dumps(count_payload))) client.publish(Topics["BACKEND_ACTION_PLANTCOUNT"], json.dumps(count_payload))