import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sequence_number=1360

UDP_IP = "192.168.0.100"
UDP_PORT = 52381
VV =  "10 " # Pan speed setting between 01 and 18 in normal speed, up to 0x7E in slow move mode.
WW = "10 " # Tilt speed 
POS_1=["0A00","FB00"]
POS_2=["F700","FB00"]
payload = ""
header=""
message=""
commande = ""

while commande != "reset":
    command = input('Indique ta commande : pos1, pos2, movetuyau, manualfocus, \n focustuyau, autofocus, zoom0, zoom2 \n')
    if command in ["pos1", "pos2", "reset"]:
        
        if(command == "pos2"):
            payload = "81 01 06 02 " #Movetoabsoluteposition in POS
            payload = payload + VV + WW #add move speed
            for i in POS_1: #add position
                for j in i:
                    payload = payload + "0" + j +" "
            payload = payload + "FF " #end byte
            header = "01 00 " # Payload type : 0100 for a command, 0110 for and inquiry, 0200 for a control command and 0120 for a device setting command
            header = header + "00 "
            #header = header + ('0'*(4-len(hex(len(payload.replace(' ',''))//2))) +hex(len(payload.replace(' ',''))//2))
            header=header + '0F '
            header = header + ('0'*(8-len(str(sequence_number)))+str(sequence_number)) 
            message = header + payload
            sock.sendto(bytes.fromhex(message.replace(' ','')),(UDP_IP,UDP_PORT))
            print("salle2" + message)
            sequence_number = sequence_number+1
        if(command=="pos1"):
            payload = "81 01 06 02 " #Movetoabsoluteposition in POS
            payload = payload + VV + WW #add move speed
            for i in POS_2: #add position
                for j in i:
                    payload = payload + "0" + j +" "
            payload = payload + "FF " #end byte
            header = "01 00 " # Payload type : 0100 for a command, 0110 for and inquiry, 0200 for a control command and 0120 for a device setting command
            header = header + "00 "
            #header = header + ('0'*(4-len(hex(len(payload.replace(' ',''))//2))) +hex(len(payload.replace(' ',''))//2))
            header=header + '0F '
            header = header + ('0'*(8-len(str(sequence_number)))+str(sequence_number)) 
            message = header + payload
            sock.sendto(bytes.fromhex(message.replace(' ','')),(UDP_IP,UDP_PORT))
            print("salle1 " + message)
            sequence_number=sequence_number+1
        # if(command=="reset"):
        #     payload = "01 " #Movetoabsoluteposition in POS
        #     payload = payload + "FF " #end byte
        #     
        #     header = "02 00 " # Payload type : 0100 for a command, 0110 for and inquiry, 0200 for a control command and 0120 for a device setting command
        #     
        #     #header = header + ('0'*(4-len(hex(len(payload.replace(' ',''))//2))) +hex(len(payload.replace(' ',''))//2))
        #     header=header + '01 '
        #     header = header + "00 00 00 00"
        #     message = header + payload
        #     
        #     sock.sendto(bytes.fromhex(message.replace(' ','')),(UDP_IP,UDP_PORT))
        #     print("reset " + message)
        #     sequence_number = sequence_number+1
    else:
        if(command=='moveleft_c'):
            payload = "81 01 06 01 18 17 01 03 FF"
            header = "10 00 00 09"
        if(command =='moveright_c'):
            payload = "81 01 06 01 18 17 02 03 FF"
            header = "10 00 00 09"
        if(command == 'stop'):
            payload = "81 01 06 01 00 00 03 03 FF"
            header = "10 00 00 09"
        if(command=='d'):
            # payload = "81 01 06 03 VV WW 0Y 0Y 0Y 0Y 00 00 00 00 FF     YYYY de DE00 à 2200 center 0000
            payload = "81 01 06 03 09 09 00 01 02 02 00 00 00 00 FF"     
            header = "01 00 00 0F"
        if(command =='q'):
            payload = "81 01 06 03 09 09 0F 0E 0D 0E 00 00 00 00 FF"
            header = "01 00 00 0F"
        if(command =='z'):
            payload = "81 01 06 03 09 09 0F 0E 0D 0E 0 00 00 00 FF"
            header = "01 00 0q
            q
            dezo 0 0F"
        if(command =='s'):
            payload = "81 01 06 03 09 09 0F 0E 0D 0E 00 00 00 00 FF"
            header = "01 00 00 0F"
        if(command=="zoom0"):
            payload="81 01 04 47 00 00 00 00 FF"
            header = "01 00 00 09"
        if(command=="zoom2"):
            payload="81 01 04 47 01 06 0A 01 FF"
            header = "01 00 00 09"
        if(command=="focustuyau"): #6m
            payload="81 01 04 48 04 00 00 00 FF"
            header = "01 00 00 09"
        if(command=="manualfocus"):
            payload="81 01 04 38 03 FF"
            header = "01 00 00 06"
        if(command=="autofocus"):
            payload="81 01 04 38 02 FF"
            header = "01 00 00 06"
        if(command=="movetuyau"):
            payload="81 01 06 02 10 10 01 02 00 00 0F 0E 0C 00 FF"
            header = "01 00 00 0F"
        
        header = header + ('0'*(8-len(str(sequence_number)))+str(sequence_number)) 
        message = header + payload
        sock.sendto(bytes.fromhex(message.replace(' ','')),(UDP_IP,UDP_PORT))
        print("fait "+ command)
        sequence_number = sequence_number + 1
    print(sequence_number)