From 3be6b31cbf3cb3932beda433dfe328566ee203d4 Mon Sep 17 00:00:00 2001 From: minssen_pie <pierre.minssen@supelec.fr> Date: Fri, 28 Dec 2018 17:47:08 +0100 Subject: [PATCH] ajout de commentaire et command_type partiel pour inq_command --- viscaoveriplib/camera.py | 6 +- viscaoveriplib/commands_library.py | 8 +-- viscaoveriplib/debut.py | 14 ---- viscaoveriplib/fake_camera.py | 74 +++++++++------------- viscaoveriplib/fake_listener.py | 6 +- viscaoveriplib/inquiry_commands_library.py | 20 +++--- viscaoveriplib/listener.py | 24 +++++-- viscaoveriplib/responds_library.py | 4 +- 8 files changed, 71 insertions(+), 85 deletions(-) delete mode 100644 viscaoveriplib/debut.py diff --git a/viscaoveriplib/camera.py b/viscaoveriplib/camera.py index 6f6c8bb..b1d0bc1 100644 --- a/viscaoveriplib/camera.py +++ b/viscaoveriplib/camera.py @@ -19,6 +19,7 @@ class Camera(command_lib.Command, inq_command_lib.Inquiry): :param VV: Vitesse de déplacement horizontale (entre 01 et 18) en hex :param WW: Vitesse de déplacement veritcale (entre 01 et 17) en hex :param seq_num: sequence number de départ + :param virtualcam: si True change les ip/port en local, lancer fake_camera pour avoir l'interactivité """ self.seq_num = seq_num self.VV = VV @@ -47,12 +48,13 @@ class Camera(command_lib.Command, inq_command_lib.Inquiry): """ #if(not(self.virtualcam)): #Si la cam n'est pas virtuel, on envoie le paquet self.camera_sock.sendto(bytes.fromhex(message.replace(' ', '')), (self.CAMERA_IP_SEND, self.CAMERA_PORT)) - self.seq_num += 1 # ballec de l'hexa pour l'instant if(self.debug or self.virtualcam): #Si la cam est virtuelle ou on debug, on envoie print("Commande n°{} envoyée : {} \n".format(self.seq_num,message)) - def send_payload(self, payloadtype, payload, command_type="default"): + self.increment_seq_number() + + def send_payload(self, payloadtype, payload, command_type="defaul"): message = payloadtype + ' ' + self.payload2header(payload) + ' ' + payload #if(not(self.virtualcam)): #Si la cam n'est pas virtuel, on envoie le paquet self.listener.message[self.seq_num] = [command_type] diff --git a/viscaoveriplib/commands_library.py b/viscaoveriplib/commands_library.py index 4c58841..3b14909 100644 --- a/viscaoveriplib/commands_library.py +++ b/viscaoveriplib/commands_library.py @@ -1,6 +1,3 @@ -#from camera_main import * - - class Command: #### @@ -18,9 +15,12 @@ class Command: message.pop() return message + def send_payload(self, payloadtype, payload, *args): + pass + def send_command(self, payload: hex): payloadtype = "01 00" - self.send_payload(payloadtype, payload, "command") + self.send_payload(payloadtype, payload, "default") #CAM_POWER : Power ON/OFF def power_on(self): diff --git a/viscaoveriplib/debut.py b/viscaoveriplib/debut.py deleted file mode 100644 index dc40ba8..0000000 --- a/viscaoveriplib/debut.py +++ /dev/null @@ -1,14 +0,0 @@ - -import viscaoveriplib.camera as cam -import viscaoveriplib.listener as listener -c = cam.Camera() -print("ok") -print(c.listener.isAlive()) -c.pan_tiltDrive_down() -c.pan_tiltDrive_down() -c.pan_tiltDrive_down() -c.pan_tiltDrive_down() - - - - diff --git a/viscaoveriplib/fake_camera.py b/viscaoveriplib/fake_camera.py index 2efef52..fe3ad56 100644 --- a/viscaoveriplib/fake_camera.py +++ b/viscaoveriplib/fake_camera.py @@ -7,13 +7,16 @@ import viscaoveriplib.responds_messages as default_resp class FakeCamera(command_lib.Command, inq_command_lib.Inquiry): - def __init__(self, UDP_IP_SEND ="127.0.0.2", UDP_PORT_SEND = 5007, UDP_IP_RECEIVE ="127.0.0.1", seq_num: int = 0, debug=False, virtualcam=False): + def __init__(self, UDP_IP_SEND ="127.0.0.2", UDP_PORT_SEND = 5007, UDP_IP_RECEIVE = "127.0.0.1", + seq_num: hex = '00000000', debug=False): """ - Simulation of a camera for testing purpuses + Simulation of a camera for testing purposes in one python console run camera.py with the virtual cam True -> cam = Camera() in another one run fake_camera.py -> fake = FakeCamera() then test command on cam eg cam.ir_receive() - the respond message is choosen in fake_listener.py + the respond message is chosen in fake_listener.py + + NB cam send to IP 127.0.0.1 Port 5006; fake_cam send to IP 127.0.0.2, Port 5007 :param UDP_IP_SEND: IP de la caméra (192.168.0.100 par défaut) :param UDP_PORT_SEND: Port de la caméra (52381 par défaut) @@ -26,9 +29,7 @@ class FakeCamera(command_lib.Command, inq_command_lib.Inquiry): self.CAMERA_PORT = UDP_PORT_SEND self.CAMERA_IP_RECEIVE = UDP_IP_RECEIVE self.debug = debug - self.virtualcam = virtualcam self.listener = listener.FakeListener(self, self.CAMERA_IP_RECEIVE, 5006) - #self.listener = listener.FakeListener(self.CAMERA_IP_RECEIVE, self.CAMERA_PORT) self.listener.start() def send(self, message): @@ -36,56 +37,41 @@ class FakeCamera(command_lib.Command, inq_command_lib.Inquiry): Envoie un message à la cam (supporte les ' ') :param message: message str['hex'] """ - if not self.virtualcam: # Si la cam n'est pas virtuel, on envoie le paquet - self.camera_sock.sendto(bytes.fromhex(message.replace(' ', '')), (self.CAMERA_IP_SEND, self.CAMERA_PORT)) - self.seq_num += 1 # ballec de l'hexa pour l'instant - - if self.debug or self.virtualcam: #Si la cam est virtuelle ou on debug, on envoie - print("Commande n°{} envoyée : {} \n".format(self.seq_num,message)) + # if(not(self.virtualcam)): #Si la cam n'est pas virtuel, on envoie le paquet + self.camera_sock.sendto(bytes.fromhex(message.replace(' ', '')), (self.CAMERA_IP_SEND, self.CAMERA_PORT)) - def send_payload(self, payloadtype, payload): - message=payloadtype + ' ' + self.payload2header(payload) + payload - if not self.virtualcam: # Si la cam n'est pas virtuel, on envoie le paquet + if (self.debug or self.virtualcam): # Si la cam est virtuelle ou on debug, on envoie + print("Commande n°{} envoyée : {} \n".format(self.seq_num, message)) - # self.camera_sock.sendto(bytes.fromhex(message.replace(' ', '')), (self.CAMERA_IP_SEND, self.CAMERA_PORT)) - self.camera_sock.sendto(bytes.fromhex(message), (self.CAMERA_IP_SEND, self.CAMERA_PORT)) + self.increment_seq_number() - self.seq_num += 1 # ballec de l'hexa pour l'instant - if self.debug or self.virtualcam: #Si la cam est virtuelle ou on debug, on envoie - print("Commande n°{} envoyée : {} \n".format(self.seq_num,message)) + def send_payload(self, payloadtype, payload, command_type="defaul"): + message = payloadtype + ' ' + self.payload2header(payload) + ' ' + payload + # if(not(self.virtualcam)): #Si la cam n'est pas virtuel, on envoie le paquet + self.listener.message[self.seq_num] = [command_type] + self.camera_sock.sendto(bytes.fromhex(message.replace(' ', '')), (self.CAMERA_IP_SEND, self.CAMERA_PORT)) + if (self.debug or self.virtualcam): # Si la cam est virtuelle ou on debug, on envoie + print("Commande n°{} envoyée : {} \n".format(self.seq_num, message)) + self.increment_seq_number() def payload2header(self, payload): """ take the payload, calculate the payload length and add the sequence number :param payload: - :return: header without prefix = payloadlength + seq_num + :return: header without prefix = payload_length + seq_num """ - length = hex(len(payload.replace(' ', ''))) - payloadlength = length[2:] - if len(payloadlength) == 1: - payloadlength = '0' + payloadlength - - header = payloadlength - header = header + ('0' * (8 - len(str(self.seq_num))) + str(self.seq_num)) + length = hex(len(payload.replace(' ', '')) // 2) + payload_length = length[2:] + diff = 4 - len(payload_length) + payload_length = '0' * diff + payload_length + header = payload_length + header = header + ' ' + self.seq_num return header - # cam's respond - @staticmethod - def decompose_reception(reception: str): - """ - decomposes the hex received messaged - - :param reception: str hex eg '01110003000085029051ff' - :return: list of str hex [payload_type, payload_length, sequence_number, payload] eg ['0111', '0003', '00008502', '9051ff'] - """ - payload_type = reception[0:4] - payload_length = reception[4:8] - sequence_number = reception[8:16] - payload = reception[16:] - return [payload_type, payload_length, sequence_number, payload] - - - + def increment_seq_number(self): + seq_num = hex(int(self.seq_num, 16) + 1)[2:] + diff = 8 - len(seq_num) + self.seq_num = '0' * diff + seq_num diff --git a/viscaoveriplib/fake_listener.py b/viscaoveriplib/fake_listener.py index d6753c9..a5d47d6 100644 --- a/viscaoveriplib/fake_listener.py +++ b/viscaoveriplib/fake_listener.py @@ -8,19 +8,19 @@ class FakeListener(Thread): """Thread chargé simplement d'afficher un mot dans la console.""" - def __init__(self, camera, IP_OUT = "127.0.0.2", PORT = 5006): + def __init__(self, camera, IP_RECEIVE = "127.0.0.2", PORT = 5006): Thread.__init__(self) self.daemon = True self.message = [] - self.IP_OUT = IP_OUT + self.IP_RECEIVE = IP_RECEIVE self.PORT = PORT self.camera = camera self.liste_respond = ['9041ff', '9051ff', "006002ff", "906003ff", "906104ff", "906105ff", "906141ff"] def run(self): sock_recp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - sock_recp.bind((self.IP_OUT, self.PORT)) + sock_recp.bind((self.IP_RECEIVE, self.PORT)) while True: data, addr = sock_recp.recvfrom(1024) # buffer size is 1024 bytes self.message.append(data.hex()) diff --git a/viscaoveriplib/inquiry_commands_library.py b/viscaoveriplib/inquiry_commands_library.py index 933f036..6e6f216 100644 --- a/viscaoveriplib/inquiry_commands_library.py +++ b/viscaoveriplib/inquiry_commands_library.py @@ -4,10 +4,12 @@ class Inquiry: #### ####### FONCTIONS COMMANDES D'EXECUTIONS #### + def send_payload(self, *args, **kwargs): + pass - def send_inquiry(self, payload: hex): + def send_inquiry(self, payload: hex, command_type="basic_inq_respond"): payloadtype = "01 10" - self.send_payload(payloadtype, payload) + self.send_payload(payloadtype, payload, command_type) #CAM_POWER : Power ON/OFF def power(self): @@ -52,7 +54,7 @@ class Inquiry: def wb_mode(self): payload = '81 09 04 35 FF' - self.send_inquiry(payload) + self.send_inquiry(payload, "wb_mode_respond") def r_gain(self): payload = '81 09 04 43 FF' @@ -64,7 +66,7 @@ class Inquiry: def ae_mode(self): payload = '81 09 04 39 FF' - self.send_inquiry(payload) + self.send_inquiry(payload, "ae_mode_respond") def slow_shutter_mode(self): payload = '81 09 04 5A FF' @@ -100,7 +102,7 @@ class Inquiry: def wd_mode(self): payload = '81 09 7E 04 00 FF' - self.send_inquiry(payload) + self.send_inquiry(payload, "wd_mode_respond") def defog(self): payload = '81 09 04 37 FF' @@ -128,7 +130,7 @@ class Inquiry: def picture_effect_mode(self): payload = '81 09 04 63 FF' - self.send_inquiry(payload) + self.send_inquiry(payload, "picture_mode_respond") def icr_mode(self): payload = '81 09 04 01 FF' @@ -180,11 +182,11 @@ class Inquiry: def video_system(self): payload = '81 09 06 23 FF' - self.send_inquiry(payload) + self.send_inquiry(payload, "video_system_respond") def color_system(self): payload = '81 09 7E 01 03 FF' - self.send_inquiry(payload) + self.send_inquiry(payload, "olor_system_respond") def ir_receive(self): payload = '81 09 7E 01 03 FF' @@ -192,7 +194,7 @@ class Inquiry: def ir_condition(self): payload = '81 09 06 34 FF' - self.send_inquiry(payload) + self.send_inquiry(payload, "ir_condition") def slow_pantilt(self): payload = '81 09 06 44 FF' diff --git a/viscaoveriplib/listener.py b/viscaoveriplib/listener.py index 11b4fbd..9b23022 100644 --- a/viscaoveriplib/listener.py +++ b/viscaoveriplib/listener.py @@ -6,25 +6,35 @@ from viscaoveriplib.responds_library import respond_list class Listener(Thread): - def __init__(self, IP_OUT = "192.168.0.57", PORT = 52381): + def __init__(self, IP_RECEIVE = "192.168.0.57", PORT = 52381): + """ + + :param IP_RECEIVE: IP d'écoute + :param PORT: port d'écoute + self.message dico de clef sequence number et liste les message recus eg + {'00000001' : [command_type, 'Acknowledge', 'Completion']} + """ Thread.__init__(self) self.daemon = True - self.IP_OUT = IP_OUT + self.IP_RECEIVE = IP_RECEIVE self.PORT = PORT self.message = {} def run(self): sock_recp = socket.socket(socket.AF_INET, # Internet socket.SOCK_DGRAM) # UDP - sock_recp.bind((self.IP_OUT, self.PORT)) + sock_recp.bind((self.IP_RECEIVE, self.PORT)) while True: data, addr = sock_recp.recvfrom(1024) # buffer size is 1024 bytes [payload_type, payload_length, sequence_number, payload] = self.decompose_reception(data.hex()) - type_command = self.message[sequence_number][0] - if type_command in respond_list: - if payload in respond_list[type_command]: - print("Respond : " + respond_list[type_command][payload]) + try: + type_command = self.message[sequence_number][0] + respond = respond_list[type_command][payload] + self.message[sequence_number].append(respond) + print("Respond : " + respond) + except Exception as e: + print(e) diff --git a/viscaoveriplib/responds_library.py b/viscaoveriplib/responds_library.py index 332b344..8dcf801 100644 --- a/viscaoveriplib/responds_library.py +++ b/viscaoveriplib/responds_library.py @@ -2,7 +2,7 @@ Dictionary of responds for command and inquiry """ -basic_respond = {'905002FF': "On", 'y05003FF': "Off", "905000FF": "auto/standard"} +basic_inq_respond = {'905002FF': "On", 'y05003FF': "Off", "905000FF": "auto/standard"} wb_mode_respond = {"905000FF": "auto", "905001FF": "In Door", "905002FF": "Out Door", "905003FF": "One Push WB", "905004FF": "ATW", "905005FF": "Manual"} @@ -31,7 +31,7 @@ default = {'9041ff': "Acknowledge", '9051ff': "Completion", "006002ff": "Syntax "906003ff": "Command Buffer Full", "906104ff": "Command Canceled", "906105ff": "No Socket", "906141ff": "Command Not Executable"} -respond_list = {"default": default, "basic_respond": basic_respond, "wb_mode_respond": wb_mode_respond, +respond_list = {"default": default, "basic_inq_respond": basic_inq_respond, "wb_mode_respond": wb_mode_respond, "picture_mode_respond": picture_mode_respond, "video_system_respond": video_system_respond, "color_system_respond": color_system_respond, "ir_condition": ir_condition} -- GitLab