import socket
import time

HOST = "192.168.0.10"
# The remote host whcih in this case is UR
DASHBOARD_PORT = 29999
# Port for Dashboard

#Open the Dashboard "Channel" to start the program on Robot
def send_dashboard_command(ip, port, command):
    """
    Hilfsfunktion zum Senden von Befehlen an den Dashboard Server (Port 29999).
    """
    s_dash = None
    try:
        s_dash = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s_dash.connect((ip, port))
        
        full_command = f"{command}\n"
        print(f"[Dashboard] Sende Befehl: '{command.strip()}'")
        s_dash.sendall(full_command.encode('utf8')) # Dashboard Server verwendet typischerweise UTF-8

        # Lese Antwort vom Dashboard Server (optional, aber gut für Debugging)
        response = s_dash.recv(1024).decode('utf8').strip()
        print(f"[Dashboard] Antwort: {response}")
        return response

    except Exception as e:
        print(f"[Dashboard] Fehler: {e}")
        return f"Error: {e}"
    finally:
        if s_dash:
            s_dash.close()

#### Start of Program
##Start Program to move gripper
send_dashboard_command(HOST, DASHBOARD_PORT, "stop")
time.sleep(0.5)
send_dashboard_command(HOST, DASHBOARD_PORT, "play")
time.sleep(3)
send_dashboard_command(HOST, DASHBOARD_PORT, "stop")