|
|
@@ -23,11 +23,13 @@ def data_sensordata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, my |
|
|
|
payload = json.loads(str_in) |
|
|
|
logging.info("ROBOT_DATA_SENSORDATA Received data: " + json.dumps(payload)) |
|
|
|
drive_data = { |
|
|
|
"PlantID": payload['PlantID'], |
|
|
|
"PlantID": [payload['PlantID']], |
|
|
|
"ActionID": payload['ActionID'] |
|
|
|
} |
|
|
|
|
|
|
|
try: |
|
|
|
print(drive_data) |
|
|
|
print(robot.order_handler) |
|
|
|
robot.delete_order(drive_data) |
|
|
|
mydatabase.insert_measurement_data(plant_id=payload['PlantID'], |
|
|
|
sensordata_temp=payload['AirTemperature'], |
|
|
@@ -35,9 +37,42 @@ def data_sensordata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, my |
|
|
|
sensordata_soil_moisture=payload['SoilMoisture'], |
|
|
|
sensordata_brightness=payload['Brightness']) |
|
|
|
logging.debug("Inserted to data base: " + json.dumps(payload)) |
|
|
|
action_getalldata(client, userdata, message, mydatabase) |
|
|
|
except Exception as e: |
|
|
|
logging.error("Could not delete order: " + str(e)) |
|
|
|
|
|
|
|
def data_sensordataall(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_SENSORDATAALL Received data: " + json.dumps(payload)) |
|
|
|
|
|
|
|
plant_ids = [] |
|
|
|
|
|
|
|
for i in payload['SensorData']: |
|
|
|
plant_ids.append(i["PlantID"]) |
|
|
|
print("Plant Names:", str(plant_ids)) |
|
|
|
drive_data = { |
|
|
|
"PlantID": plant_ids, |
|
|
|
"ActionID": payload['ActionID'] |
|
|
|
} |
|
|
|
|
|
|
|
try: |
|
|
|
print(robot.order_handler) |
|
|
|
print(drive_data) |
|
|
|
robot.delete_order(drive_data) |
|
|
|
for i in payload['SensorData']: |
|
|
|
mydatabase.insert_measurement_data(plant_id=i['PlantID'], |
|
|
|
sensordata_temp=i['AirTemperature'], |
|
|
|
sensordata_humidity=i['AirHumidity'], |
|
|
|
sensordata_soil_moisture=i['SoilMoisture'], |
|
|
|
sensordata_brightness=i['Brightness']) |
|
|
|
logging.debug("Inserted to data base: " + json.dumps(payload)) |
|
|
|
action_getalldata(client, userdata, message, mydatabase) |
|
|
|
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"))) |
|
|
@@ -57,6 +92,7 @@ def data_battery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, robot |
|
|
|
"Timestamp": str(datetime.now()) |
|
|
|
} |
|
|
|
client.publish(Topics['BACKEND_DATA_BATTERY'], json.dumps(battery_data)) |
|
|
|
client.publish(Topics['BACKEND_DATA_ROBOTREADY'], str(robot.get_robot_status())) |
|
|
|
|
|
|
|
|
|
|
|
def data_error(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, robot: Robot): |
|
|
@@ -72,14 +108,15 @@ def data_robotready(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, ro |
|
|
|
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")) |
|
|
|
client.publish(Topics['BACKEND_DATA_ROBOTREADY'], str(robot.get_robot_status())) |
|
|
|
|
|
|
|
|
|
|
|
# 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"]) |
|
|
|
plant_id = mydatabase.get_plant_id(plant_name=json.loads(str(message.payload.decode("UTF-8")))) |
|
|
|
print(str(plant_id)) |
|
|
|
action_id = str(uuid.uuid4()) |
|
|
|
drive_data = { |
|
|
|
"PlantID": plant_id, |
|
|
@@ -87,7 +124,7 @@ def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydat |
|
|
|
} |
|
|
|
|
|
|
|
if robot.get_order_number() < 6 and robot.get_robot_status() is True: |
|
|
|
robot.add_order(drive_data) |
|
|
|
robot.add_order({"PlantID": [plant_id], "ActionID": action_id}) |
|
|
|
client.publish(Topics['ROBOT_ACTION_DRIVE'], json.dumps(drive_data)) |
|
|
|
logging.info("BACKEND_ACTION_DRIVE Drive Command published: " + json.dumps(drive_data)) |
|
|
|
else: |
|
|
@@ -106,7 +143,7 @@ def action_driveall(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, my |
|
|
|
print(plant_names) |
|
|
|
|
|
|
|
for names in plant_names: |
|
|
|
_id = mydatabase.get_plant_id(names) |
|
|
|
_id = mydatabase.get_plant_id(names[0]) |
|
|
|
plant_ids.append(_id) |
|
|
|
|
|
|
|
action_id = str(uuid.uuid4()) |
|
|
@@ -117,7 +154,7 @@ def action_driveall(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, my |
|
|
|
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)) |
|
|
|
client.publish(Topics['ROBOT_ACTION_DRIVEALL'], json.dumps(drive_data)) |
|
|
|
logging.info("BACKEND_ACTION_DRIVEALL Drive Command published: " + json.dumps(drive_data)) |
|
|
|
else: |
|
|
|
if robot.get_order_number() < 6: |
|
|
@@ -146,9 +183,10 @@ def action_getbattery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): |
|
|
|
|
|
|
|
def action_getalldata(client: mqtt.Client, userdata, message: Union[mqtt.MQTTMessage, list], mydatabase: PlantDataBase): |
|
|
|
plant_names = mydatabase.get_plant_names() |
|
|
|
print("SUIII" + str(plant_names)) |
|
|
|
alldata = [] |
|
|
|
for i in plant_names: |
|
|
|
alldata.append(mydatabase.get_latest_data(plant_name=i)) |
|
|
|
alldata.append(mydatabase.get_latest_data(plant_name=i[0])) |
|
|
|
client.publish(Topics['BACKEND_DATA_SENSORDATAALL'], json.dumps(alldata)) |
|
|
|
logging.info("BACKEND_DATA_SENSORDATAALL got data from database:" + str(alldata)) |
|
|
|
|