123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- """
- 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 defines import Topics, MAX_PLANT_COUNT
- import json
- import uuid
- from typing import Union
- from datetime import datetime
- import logging
- from robot import Robot
-
-
- # Robot Channel Reactions
-
- def data_sensordata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase,
- robot: Robot):
- str_in = str(message.payload.decode("UTF-8"))
- payload = json.loads(str_in)
- logging.info("ROBOT_DATA_SENSORDATA Received data: " + json.dumps(payload))
- drive_data = {
- "PlantID": payload['PlantID'],
- "ActionID": payload['ActionID']
- }
-
- try:
- robot.delete_order(drive_data)
- 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'])
- logging.debug("Inserted to data base: " + json.dumps(payload))
- except Exception as e:
- logging.error("Could not delete order: " + str(e))
-
-
- def data_position(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, robot: Robot):
- logging.info("ROBOT_DATA_POSITION Received data: " + json.dumps(message.payload.decode("UTF-8")))
- robot.store_position(json.loads(message.payload.decode("UTF-8"))["Position"])
- position_data = {
- "Position": robot.get_position(),
- "Timestamp": str(datetime.now())
- }
- client.publish(Topics['BACKEND_DATA_POSITION'], json.dumps(position_data))
-
-
- def data_battery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, robot: Robot):
- logging.info("ROBOT_DATA_BATTERY Received data: " + str(json.dumps(message.payload.decode("UTF-8"))))
- robot.store_battery(json.loads(message.payload.decode("UTF-8"))["Battery"])
- battery_data = {
- "Battery": robot.get_battery(),
- "Timestamp": str(datetime.now())
- }
- client.publish(Topics['BACKEND_DATA_BATTERY'], json.dumps(battery_data))
-
-
- def data_error(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, robot: Robot):
- robot.store_last_error(message.payload.decode("UTF-8"))
- logging.error("ROBOT_DATA_ERROR new error received from Robot: " + robot.get_last_error())
- client.publish(Topics['BACKEND_DATA_ERROR'], message.payload.decode("UTF-8"))
-
-
- def data_robotready(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, robot: Robot):
- robot.change_robot_status(message.payload.decode("UTF-8") == 'True')
- if robot.get_robot_status() is True and robot.get_order_number() >= 1:
- client.publish(Topics['ROBOT_ACTION_DRIVE'], json.dumps(robot.get_next_order()))
- logging.info("Waiting Order send to Robot")
-
- logging.info("ROBOT_DATA_ROBOTREADY status updated: " + str(robot.get_robot_status()))
- client.publish(Topics['BACKEND_DATA_ROBOTREADY'], message.payload.decode("UTF-8"))
-
-
- # FrontEnd Channel Reactions
-
- def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase,
- robot: Robot):
- plant_id = mydatabase.get_plant_id(plant_name=json.loads(message.payload.decode("UTF-8"))["PlantName"])
- action_id = str(uuid.uuid4())
- drive_data = {
- "PlantID": plant_id,
- "ActionID": action_id
- }
-
- if robot.get_order_number() < 6 and robot.get_robot_status() is True:
- robot.add_order(drive_data)
- client.publish(Topics['ROBOT_ACTION_DRIVE'], json.dumps(drive_data))
- logging.info("BACKEND_ACTION_DRIVE Drive Command published: " + json.dumps(drive_data))
- else:
- if robot.get_order_number() < 6:
- robot.add_order(drive_data)
- logging.info("BACKEND_ACTION_DRIVE New data added to order list: " + str(drive_data))
- elif robot.get_order_number() >= 6:
- logging.error("Could not add Order to list. Order discarded")
- client.publish(Topics['BACKEND_DATA_ERROR'], "Could not add Order to list. Order discarded")
-
-
- def action_driveall(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase,
- robot: Robot):
- plant_names = mydatabase.get_plant_names()
- plant_ids = []
- print(plant_names)
-
- for names in plant_names:
- _id = mydatabase.get_plant_id(names)
- plant_ids.append(_id)
-
- action_id = str(uuid.uuid4())
- drive_data = {
- "PlantID": plant_ids,
- "ActionID": action_id
- }
- print(drive_data)
- if robot.get_order_number() < 6 and robot.get_robot_status() is True:
- robot.add_order(drive_data)
- client.publish(Topics['ROBOT_ACTION_DRIVEALL'], json.dumps(plant_ids))
- logging.info("BACKEND_ACTION_DRIVEALL Drive Command published: " + json.dumps(drive_data))
- else:
- if robot.get_order_number() < 6:
- robot.add_order(drive_data)
- logging.info("BACKEND_ACTION_DRIVEALL New data added to order list: " + str(drive_data))
- elif robot.get_order_number() >= 6:
- client.publish(Topics['BACKEND_DATA_ERROR'], "Could not add Order to list. Order discarded")
- logging.error("Could not add Order to list. Order discarded")
-
-
- def action_getposition(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
- client.publish(Topics['ROBOT_ACTION_GETPOSITION'])
- logging.info("BACKEND_ACTION_GETPOSITION message forwarded to Robot")
-
-
- def action_getbattery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage):
- client.publish(Topics['ROBOT_ACTION_GETBATTERY'])
- logging.info("BACKEND_ACTION_GETBATTERY message forwarded to Robot")
- battery_data = {
- "Battery": 66,
- "Timestamp": str(datetime.now())
- }
- print(battery_data)
- client.publish(Topics['BACKEND_DATA_BATTERY'], json.dumps(battery_data))
-
-
- def action_getalldata(client: mqtt.Client, userdata, message: Union[mqtt.MQTTMessage, list], mydatabase: PlantDataBase):
- plant_names = mydatabase.get_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))
- logging.info("BACKEND_DATA_SENSORDATAALL got data from database:" + 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"])
- logging.info("BACKEND_ACTION_NEWPLANT new plant data received and inserted: " + str(plant_data))
- 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"])
- logging.info("BACKEND_ACTION_CONFIGUREPLANT configure plant data received and inserted: " + 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)
- logging.info("BACKEND_ACTION_DELETEPLANT delete plant data received and deleted: " + 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))
- logging.info("BACKEND_DATA_PLANTCOUNT forwarded plant count to FrontEnd: " + str(count_payload))
|