2023-04-28 11:17:04 +02:00
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
import json
|
|
|
|
import threading
|
|
|
|
import os
|
2023-04-30 12:12:11 +02:00
|
|
|
import raspy_sensors as Sensors
|
2023-04-28 11:17:04 +02:00
|
|
|
from defines import Topics
|
|
|
|
|
|
|
|
def measure_send_data(plantID, actionID, client: mqtt.Client):
|
|
|
|
"""
|
|
|
|
Reads all sensors and publishes data via MQTT
|
|
|
|
|
|
|
|
Args:
|
|
|
|
plantID (_type_): plant to measure
|
|
|
|
actionID (_type_): current ID of driving action
|
|
|
|
client (mqtt.Client): current mqtt client for publishing
|
|
|
|
"""
|
2023-04-30 12:12:11 +02:00
|
|
|
sensor = Sensors.RaspySensors()
|
2023-04-28 11:17:04 +02:00
|
|
|
sensorData = sensor.readSensors()
|
|
|
|
sensorData["PlantID"] = plantID
|
|
|
|
sensorData["ActionID"] = actionID
|
|
|
|
client.publish(Topics["ROBOT_DATA_SENSORDATA"], json.dumps(sensorData, indent=4))
|
|
|
|
|
|
|
|
|
|
|
|
def drive_plant_thread(plantID, actionID, client: mqtt.Client):
|
|
|
|
"""
|
|
|
|
Function to drive to plant according to number from MQTT message in thread
|
|
|
|
Meassure and publish data via MQTT
|
|
|
|
Drive home to starting point
|
|
|
|
|
|
|
|
Args:
|
|
|
|
plantID (_type_): plant to measure
|
|
|
|
actionID (_type_): current ID of driving action
|
|
|
|
client (mqtt.Client): current mqtt client for publishing
|
|
|
|
"""
|
2023-05-05 10:40:33 +02:00
|
|
|
# FIXME Change to color code driving
|
2023-04-28 11:17:04 +02:00
|
|
|
os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/plant_{plantID}.py')
|
2023-05-05 10:40:33 +02:00
|
|
|
# os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_plant.py {plantID}')
|
2023-04-28 11:17:04 +02:00
|
|
|
|
|
|
|
print("Measuring Sensors")
|
|
|
|
|
|
|
|
#FIXME Sensor not working
|
|
|
|
# measure_send_data(plantID, actionID, client)
|
|
|
|
|
|
|
|
print("Robot driving home")
|
2023-05-05 10:40:33 +02:00
|
|
|
os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_back.py {plantID}')
|
2023-04-28 11:17:04 +02:00
|
|
|
print("Robot home")
|
|
|
|
|
|
|
|
#TODO decide about robot occupied message
|
|
|
|
#client.publish("ROBOT/DATA/OCCUPIED", "false")
|
|
|
|
|
|
|
|
|
|
|
|
#region MQTT callbacks
|
|
|
|
|
|
|
|
#Testing
|
|
|
|
def send_data_json(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
|
|
|
|
strIn = str(message.payload.decode("UTF-8"))
|
|
|
|
dataDict = json.loads(strIn)
|
|
|
|
print("Received data: ", json.dumps(dataDict))
|
|
|
|
|
|
|
|
|
|
|
|
def drive_plant(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
|
|
|
|
"""
|
|
|
|
Function to drive to plant according to request
|
|
|
|
Starting Drive in Thread
|
|
|
|
|
|
|
|
Args:
|
|
|
|
clients (mqtt.Client): current mqtt client
|
|
|
|
userdata (_type_): _description_
|
|
|
|
message (mqtt.MQTTMessage): received message
|
|
|
|
"""
|
|
|
|
dictMessage = json.loads(str(message.payload.decode("UTF-8")))
|
|
|
|
plantID = dictMessage["PlantID"]
|
|
|
|
actionID = dictMessage["ActionID"]
|
|
|
|
|
2023-04-30 12:12:11 +02:00
|
|
|
print(f"Received Drive-request to plant {plantID}, ActionID: {actionID}")
|
2023-04-28 11:17:04 +02:00
|
|
|
|
|
|
|
thread = threading.Thread(target= drive_plant_thread, args=(plantID, actionID, clients), daemon=True)
|
|
|
|
thread.start()
|
|
|
|
|
|
|
|
|
|
|
|
def get_position(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
|
|
|
|
"""
|
|
|
|
Callback function for GPS position request
|
|
|
|
Function to send actual GPS position via MQTT
|
|
|
|
|
|
|
|
Args:
|
|
|
|
clients (mqtt.Client): current mqtt client
|
|
|
|
userdata (_type_): _description_
|
|
|
|
message (mqtt.MQTTMessage): received message
|
|
|
|
"""
|
|
|
|
# TODO Write GPS Sensor Function
|
|
|
|
position = {
|
|
|
|
"Position": ""
|
|
|
|
}
|
|
|
|
|
|
|
|
clients.publish(Topics["ROBOT_DATA_POSITION"], json.dumps(position, indent=4))
|
|
|
|
|
|
|
|
|
2023-05-02 14:08:07 +02:00
|
|
|
def get_batteryStatus(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
|
2023-04-28 11:17:04 +02:00
|
|
|
"""
|
|
|
|
Callback function for battery status request
|
|
|
|
Function to read battery status from ev3 and send via MQTT
|
|
|
|
|
|
|
|
Args:
|
|
|
|
clients (mqtt.Client): current mqtt client
|
|
|
|
userdata (_type_): _description_
|
|
|
|
message (mqtt.MQTTMessage): received message
|
|
|
|
"""
|
|
|
|
|
|
|
|
battery = {
|
|
|
|
"Battery": 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
#TODO read Battery
|
|
|
|
clients.publish(Topics["ROBOT_DATA_BATTERY"], json.dumps(battery, indent=4))
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|