""" 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 from typing import Union # 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: Union[mqtt.MQTTMessage, list], mydatabase: PlantDataBase): plant_names = mydatabase.get_plant_names() print(type(plant_names)) alldata = [] for i in plant_names: print("I Type: " + str(type(i))) print("I: " + i[0]) alldata.append(mydatabase.get_latest_data(plant_name=i[0])) client.publish(Topics['BACKEND_DATA_SENSORDATAALL'], json.dumps(alldata)) print("BACKEND_DATA_SENSORDATAALL SEND DATA:" + str(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)) print(mydatabase.get_plant_names()) action_getalldata(client, userdata, message, mydatabase) 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["PlantID"], 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)) action_getalldata(client, userdata, message, mydatabase) 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) print("BACKEND_ACTION_DELETEPLANT RECEIVED DATA: " + str(delete_plant)) action_getalldata(client, userdata, message, mydatabase) def action_countplants(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): count = mydatabase.plant_count() count_payload = { "CurrentCount": count, "MaxCount": MAX_PLANT_COUNT } client.publish(Topics["BACKEND_DATA_PLANTCOUNT"], json.dumps(count_payload)) print("BACKEND_DATA_PLANTCOUNT SEND DATA: " + str(count_payload))