2023-04-24 11:21:01 +02:00
|
|
|
"""
|
|
|
|
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
|
2023-04-27 19:00:09 +02:00
|
|
|
import json
|
|
|
|
import uuid
|
2023-04-24 11:21:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Robot Channel Reactions
|
|
|
|
|
2023-04-27 19:00:09 +02:00
|
|
|
def data_sensordata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase, order_handler: list):
|
2023-04-24 11:21:01 +02:00
|
|
|
print("message received")
|
|
|
|
# TODO: Store data in database
|
2023-04-27 19:00:09 +02:00
|
|
|
str_in = str(message.payload.decode("UTF-8"))
|
|
|
|
payload = json.loads(str_in)
|
|
|
|
print("Received data: ", json.dumps(payload))
|
2023-04-24 11:21:01 +02:00
|
|
|
|
2023-04-27 19:00:09 +02:00
|
|
|
order_handler.remove(payload['ActionID'])
|
2023-04-24 11:21:01 +02:00
|
|
|
|
2023-04-27 19:00:09 +02:00
|
|
|
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):
|
2023-04-24 11:55:10 +02:00
|
|
|
# TODO: Forward to frontend in json format
|
2023-04-27 19:00:09 +02:00
|
|
|
client.publish(Topics['BACKEND_DATA_POSITION'], message.payload.decode("utf-8"))
|
2023-04-24 11:21:01 +02:00
|
|
|
|
|
|
|
|
2023-04-27 19:00:09 +02:00
|
|
|
def data_battery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
|
2023-04-24 11:55:10 +02:00
|
|
|
# TODO: Forward to frontend in json format
|
2023-04-27 19:00:09 +02:00
|
|
|
client.publish(Topics['BACKEND_DATA_BATTERY'], message.payload.decode("utf-8"))
|
2023-04-24 11:21:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
# FrontEnd Channel Reactions
|
|
|
|
|
2023-04-27 19:00:09 +02:00
|
|
|
def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase, order_handler: list):
|
2023-04-24 11:55:10 +02:00
|
|
|
# TODO: ROBOT READY CHECK
|
2023-04-27 19:00:09 +02:00
|
|
|
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"))
|
2023-04-24 11:21:01 +02:00
|
|
|
|
|
|
|
|
2023-04-27 19:00:09 +02:00
|
|
|
def action_getposition(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
|
2023-04-24 11:55:10 +02:00
|
|
|
client.publish(Topics['ROBOT_ACTION_GETPOSITION'])
|
2023-04-24 11:21:01 +02:00
|
|
|
|
|
|
|
|
2023-04-27 19:00:09 +02:00
|
|
|
def action_getbattery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
|
2023-04-24 11:55:10 +02:00
|
|
|
client.publish(Topics['ROBOT_ACTION_GETBATTERY'])
|
2023-04-24 11:21:01 +02:00
|
|
|
|
|
|
|
|
2023-04-27 19:00:09 +02:00
|
|
|
def action_getalldata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
|
2023-04-24 11:21:01 +02:00
|
|
|
# TODO: get data from database
|
2023-04-27 19:00:09 +02:00
|
|
|
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))
|