|
|
|
|
|
|
|
|
payload = json.loads(str_in) |
|
|
payload = json.loads(str_in) |
|
|
logging.info("ROBOT_DATA_SENSORDATA Received data: " + json.dumps(payload)) |
|
|
logging.info("ROBOT_DATA_SENSORDATA Received data: " + json.dumps(payload)) |
|
|
drive_data = { |
|
|
drive_data = { |
|
|
"PlantID": payload['PlantID'], |
|
|
|
|
|
|
|
|
"PlantID": [payload['PlantID']], |
|
|
"ActionID": payload['ActionID'] |
|
|
"ActionID": payload['ActionID'] |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
|
|
|
print(drive_data) |
|
|
|
|
|
print(robot.order_handler) |
|
|
robot.delete_order(drive_data) |
|
|
robot.delete_order(drive_data) |
|
|
mydatabase.insert_measurement_data(plant_id=payload['PlantID'], |
|
|
mydatabase.insert_measurement_data(plant_id=payload['PlantID'], |
|
|
sensordata_temp=payload['AirTemperature'], |
|
|
sensordata_temp=payload['AirTemperature'], |
|
|
|
|
|
|
|
|
sensordata_soil_moisture=payload['SoilMoisture'], |
|
|
sensordata_soil_moisture=payload['SoilMoisture'], |
|
|
sensordata_brightness=payload['Brightness']) |
|
|
sensordata_brightness=payload['Brightness']) |
|
|
logging.debug("Inserted to data base: " + json.dumps(payload)) |
|
|
logging.debug("Inserted to data base: " + json.dumps(payload)) |
|
|
|
|
|
action_getalldata(client, userdata, message, mydatabase) |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
logging.error("Could not delete order: " + str(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): |
|
|
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"))) |
|
|
logging.info("ROBOT_DATA_POSITION Received data: " + json.dumps(message.payload.decode("UTF-8"))) |
|
|
|
|
|
|
|
|
"Timestamp": str(datetime.now()) |
|
|
"Timestamp": str(datetime.now()) |
|
|
} |
|
|
} |
|
|
client.publish(Topics['BACKEND_DATA_BATTERY'], json.dumps(battery_data)) |
|
|
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): |
|
|
def data_error(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, robot: Robot): |
|
|
|
|
|
|
|
|
logging.info("Waiting Order send to Robot") |
|
|
logging.info("Waiting Order send to Robot") |
|
|
|
|
|
|
|
|
logging.info("ROBOT_DATA_ROBOTREADY status updated: " + str(robot.get_robot_status())) |
|
|
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 |
|
|
# FrontEnd Channel Reactions |
|
|
|
|
|
|
|
|
def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase, |
|
|
def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase, |
|
|
robot: Robot): |
|
|
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()) |
|
|
action_id = str(uuid.uuid4()) |
|
|
drive_data = { |
|
|
drive_data = { |
|
|
"PlantID": plant_id, |
|
|
"PlantID": plant_id, |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if robot.get_order_number() < 6 and robot.get_robot_status() is True: |
|
|
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)) |
|
|
client.publish(Topics['ROBOT_ACTION_DRIVE'], json.dumps(drive_data)) |
|
|
logging.info("BACKEND_ACTION_DRIVE Drive Command published: " + json.dumps(drive_data)) |
|
|
logging.info("BACKEND_ACTION_DRIVE Drive Command published: " + json.dumps(drive_data)) |
|
|
else: |
|
|
else: |
|
|
|
|
|
|
|
|
print(plant_names) |
|
|
print(plant_names) |
|
|
|
|
|
|
|
|
for names in plant_names: |
|
|
for names in plant_names: |
|
|
_id = mydatabase.get_plant_id(names) |
|
|
|
|
|
|
|
|
_id = mydatabase.get_plant_id(names[0]) |
|
|
plant_ids.append(_id) |
|
|
plant_ids.append(_id) |
|
|
|
|
|
|
|
|
action_id = str(uuid.uuid4()) |
|
|
action_id = str(uuid.uuid4()) |
|
|
|
|
|
|
|
|
print(drive_data) |
|
|
print(drive_data) |
|
|
if robot.get_order_number() < 6 and robot.get_robot_status() is True: |
|
|
if robot.get_order_number() < 6 and robot.get_robot_status() is True: |
|
|
robot.add_order(drive_data) |
|
|
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)) |
|
|
logging.info("BACKEND_ACTION_DRIVEALL Drive Command published: " + json.dumps(drive_data)) |
|
|
else: |
|
|
else: |
|
|
if robot.get_order_number() < 6: |
|
|
if robot.get_order_number() < 6: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def action_getalldata(client: mqtt.Client, userdata, message: Union[mqtt.MQTTMessage, list], mydatabase: PlantDataBase): |
|
|
def action_getalldata(client: mqtt.Client, userdata, message: Union[mqtt.MQTTMessage, list], mydatabase: PlantDataBase): |
|
|
plant_names = mydatabase.get_plant_names() |
|
|
plant_names = mydatabase.get_plant_names() |
|
|
|
|
|
print("SUIII" + str(plant_names)) |
|
|
alldata = [] |
|
|
alldata = [] |
|
|
for i in plant_names: |
|
|
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)) |
|
|
client.publish(Topics['BACKEND_DATA_SENSORDATAALL'], json.dumps(alldata)) |
|
|
logging.info("BACKEND_DATA_SENSORDATAALL got data from database:" + str(alldata)) |
|
|
logging.info("BACKEND_DATA_SENSORDATAALL got data from database:" + str(alldata)) |
|
|
|
|
|
|