diff --git a/software/roboter/raspy/functions.py b/software/roboter/raspy/functions.py index 59c20fa..338340e 100644 --- a/software/roboter/raspy/functions.py +++ b/software/roboter/raspy/functions.py @@ -2,6 +2,7 @@ import paho.mqtt.client as mqtt import json import threading import os +import logging import raspy_sensors as Sensors from defines import Topics, SENSORDATA @@ -44,23 +45,23 @@ def drive_plant_thread(plantID, actionID, client: mqtt.Client): # TODO Error Message if errorCode != 0: - print(f"Robot Error {errorCode} occurred") - print("Drive Plant aborted, Robot at starting position") + logging.error(f"Robot Error {errorCode} occurred") + logging.error("Drive Plant aborted, Robot at starting position") return - print("Measuring Sensors") + logging.info("Measuring Sensors") measure_send_data(plantID, actionID, client) - print("Robot driving home") + logging.info("Robot driving home") errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_back.py {plantID}') # TODO Error Message if errorCode != 0: - print(f"Robot Error {errorCode} occurred") - print(f"Drive Home aborted, Robot at plant {plantID}") + logging.error(f"Robot Error {errorCode} occurred") + logging.error(f"Drive Home aborted, Robot at plant {plantID}") return - print("Robot home") + logging.info("Robot home") #TODO decide about robot occupied message #client.publish("ROBOT/DATA/OCCUPIED", "false") @@ -82,7 +83,7 @@ def drive_plant(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage): plantID = dictMessage["PlantID"] actionID = dictMessage["ActionID"] - print(f"Received Drive-request to plant {plantID}, ActionID: {actionID}") + logging.info(f"Received Drive-request to plant {plantID}, ActionID: {actionID}") thread = threading.Thread(target= drive_plant_thread, args=(plantID, actionID, clients), daemon=True) thread.start() @@ -118,12 +119,12 @@ def get_BatteryStatus(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage) userdata (_type_): _description_ message (mqtt.MQTTMessage): received message """ - print("Received battery status request") + logging.info("Received battery status request") try: batteryLevel = int(os.popen('sshpass -p maker ssh robot@ev3dev.local cat /sys/devices/platform/battery/power_supply/lego-ev3-battery/voltage_now').read()) except: - print("Robot not connected") + logging.error("Robot not connected") #TODO Error Message return diff --git a/software/roboter/raspy/mainProg.py b/software/roboter/raspy/mainProg.py index 07d2ebd..fdc1a2b 100644 --- a/software/roboter/raspy/mainProg.py +++ b/software/roboter/raspy/mainProg.py @@ -10,6 +10,8 @@ Interaction with the EV3 via SSH import paho.mqtt.client as mqtt import functions +import logging +import sys from defines import Topics, RASPI_CLIENT_ID, MQTT_BROKER_GLOBAL @@ -35,7 +37,7 @@ def on_connect(client: mqtt.Client, userdata, flags, rc): client.subscribe(Topics["ROBOT_ACTION_GETPOSITION"]) client.subscribe(Topics["ROBOT_ACTION_GETBATTERY"]) - print("MQTT initialized") + logging.info("MQTT initialized") @@ -44,6 +46,10 @@ def main(): Initialises MQTT and Sensors Runs forever and controlls all robot functions """ + logging.basicConfig(filename="robot.log", filemode="a", encoding="utf-8", level=logging.DEBUG, format='%(asctime)s %(name)s %(levelname)s %(message)s', + datefmt="%d-%m-%Y %H:%M:%S") + logging.getLogger().addHandler(logging.StreamHandler(sys.stdout)) + client = mqtt.Client(RASPI_CLIENT_ID) client.on_connect = on_connect client.connect(MQTT_BROKER_GLOBAL) @@ -51,7 +57,7 @@ def main(): # CHECK forever or start client.loop_start() - print("Starting Loop") + logging.info("Starting Loop") while True: pass