From a3015e7a25ae4036e2dd5eccd6dd2741fdc7bb32 Mon Sep 17 00:00:00 2001
From: minssen_pie <pierre.minssen@supelec.fr>
Date: Fri, 21 Dec 2018 19:02:59 +0100
Subject: [PATCH] Centralisation dans camera du listener, de la gestion (pas
 encore implemente) des reponses

---
 camera_main.py                             |  2 +
 viscaoveriplib/Test camera file.py         |  0
 viscaoveriplib/__init__.py                 |  7 ---
 viscaoveriplib/advanced_controls.py        |  0
 viscaoveriplib/camera.py                   | 54 ++++++++++++++++++++--
 viscaoveriplib/commands_library.py         |  4 +-
 viscaoveriplib/controleur.py               | 44 ------------------
 viscaoveriplib/debut.py                    | 14 ++++++
 viscaoveriplib/inquiry_commands_library.py |  3 +-
 viscaoveriplib/listener.py                 | 10 ++--
 10 files changed, 73 insertions(+), 65 deletions(-)
 delete mode 100644 viscaoveriplib/Test camera file.py
 delete mode 100644 viscaoveriplib/__init__.py
 delete mode 100644 viscaoveriplib/advanced_controls.py
 delete mode 100644 viscaoveriplib/controleur.py
 create mode 100644 viscaoveriplib/debut.py

diff --git a/camera_main.py b/camera_main.py
index dd0cbb1..3b1a32e 100644
--- a/camera_main.py
+++ b/camera_main.py
@@ -1,3 +1,4 @@
+"""
 from tkinter import *
 import tkinter as Tk
 import socket
@@ -45,3 +46,4 @@ quit_menu=Tk.Button(top, text='QUIT', bg='red', fg='white', command=quit)
 quit_menu.pack(anchor='e')
 
 root.mainloop()
+"""
diff --git a/viscaoveriplib/Test camera file.py b/viscaoveriplib/Test camera file.py
deleted file mode 100644
index e69de29..0000000
diff --git a/viscaoveriplib/__init__.py b/viscaoveriplib/__init__.py
deleted file mode 100644
index ebb1cba..0000000
--- a/viscaoveriplib/__init__.py
+++ /dev/null
@@ -1,7 +0,0 @@
-import viscaoveriplib.camera as cam
-import viscaoveriplib.listener as listener
-
-c = cam.Camera()
-l = listener.Listener()
-l.start()
-
diff --git a/viscaoveriplib/advanced_controls.py b/viscaoveriplib/advanced_controls.py
deleted file mode 100644
index e69de29..0000000
diff --git a/viscaoveriplib/camera.py b/viscaoveriplib/camera.py
index 80f4227..8c74d78 100644
--- a/viscaoveriplib/camera.py
+++ b/viscaoveriplib/camera.py
@@ -1,11 +1,16 @@
 import socket
-# Both protocols (input and outpout are UDP
+import viscaoveriplib.commands_library as command_lib
+import viscaoveriplib.inquiry_commands_library as inq_command_lib
+import viscaoveriplib.listener as listener
+import viscaoveriplib.inquiry_responds_library as inq_resp
+import viscaoveriplib.responds_messages as default_resp
+# Both protocols (input and outpout are UDP)
 "192.168.0.100"
 UDP_PORT = 52381
 
 
-class Camera:
-    def __init__(self, UDP_IP = "192.168.0.100", UDP_PORT = 52381, UDP_IP_OUT = "192.168.0.57", VV= "09", WW= "09", seq_num: hex ="0",debug=False, virtualcam=False):
+class Camera(command_lib.Command, inq_command_lib.Inquiry):
+    def __init__(self, UDP_IP = "192.168.0.100", UDP_PORT = 52381, UDP_IP_OUT = "192.168.0.57", VV= "09", WW= "09", seq_num: int = 0,debug=False, virtualcam=False):
         """
         Création d'une caméra avec
         :param UDP_IP: IP de la caméra (192.168.0.100 par défaut)
@@ -24,6 +29,8 @@ class Camera:
         self.CAMERA_IP_OUT = UDP_IP_OUT
         self.debug = debug
         self.virtualcam = virtualcam
+        self.listener = listener.Listener(self.CAMERA_IP_OUT, self.CAMERA_PORT)
+        self.listener.start()
 
     def send(self, message):
         """
@@ -31,15 +38,18 @@ class Camera:
         :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, self.UDP_PORT))
+            self.camera_sock.sendto(bytes.fromhex(message.replace(' ', '')), (self.CAMERA_IP, 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):
         message=payloadtype +' ' + self.payload2header(payload) + payload
         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, self.UDP_PORT))
+
+            self.camera_sock.sendto(bytes.fromhex(message.replace(' ', '')), (self.CAMERA_IP, 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))
@@ -59,6 +69,40 @@ class Camera:
         header = header + ('0' * (8 - len(str(self.seq_num))) + str(self.seq_num))
         return header
 
+    # cam's respond
+    @staticmethod
+    def decompose_reception(reception: str):
+        """
+        decomposes the hex received messaged if it's not a error respond
+
+        :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]
+
+    @staticmethod
+    def payload2meaning(payload: str, command_type="default"):
+        """
+        Reveal the meaning of the visca respond
+        :param payload: payload recieved in str hex eg '9051ff'
+        :param command_type: type of command, the respond's meaning depends on the inquiry/command
+        :return: the meaning in str
+        """
+        meaning = "Not known"
+        if command_type in inq_resp.inq_list:
+            # if payload is not in the dictionary meaning = None
+            meaning = inq_resp.inq_list.get(command_type).get(payload)
+        else:
+            meaning = default_resp.liste_respond.get(payload)
+        if meaning is None:
+            meaning = "Respond not understood"
+        return meaning
+
+
 
 
 
diff --git a/viscaoveriplib/commands_library.py b/viscaoveriplib/commands_library.py
index 93c9d2b..f5f25fe 100644
--- a/viscaoveriplib/commands_library.py
+++ b/viscaoveriplib/commands_library.py
@@ -1,7 +1,7 @@
-from camera_main import *
+#from camera_main import *
 
 
-class Camera:
+class Command:
 
     ####
     ####### FONCTIONS COMMANDES D'EXECUTIONS
diff --git a/viscaoveriplib/controleur.py b/viscaoveriplib/controleur.py
deleted file mode 100644
index 31b66f1..0000000
--- a/viscaoveriplib/controleur.py
+++ /dev/null
@@ -1,44 +0,0 @@
-from viscaoveriplib.camera import Camera
-from viscaoveriplib.listener import Listener
-import viscaoveriplib.inquiry_responds_library as inq_resp
-import viscaoveriplib.responds_messages as default_resp
-
-
-class Controller:
-
-    def __init__(self, c: Camera, l: Listener):
-        self.camera = c
-        self.listener = l
-
-    @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]
-
-    @staticmethod
-    def payload2meaning(payload: str, command_type="default"):
-        """
-        Reveal the meaning of the visca respond
-        :param payload: payload recieved in str hex eg '9051ff'
-        :param command_type: type of command, the respond's meaning depends on the inquiry/command
-        :return: the meaning in str
-        """
-        meaning = "Not known"
-        if command_type in inq_resp.inq_list:
-            # if payload is not in the dictionary meaning = None
-            meaning = inq_resp.inq_list.get(command_type).get(payload)
-        else:
-            meaning = default_resp.liste_respond.get(payload)
-        if meaning is None:
-            meaning = "Respond not understood"
-        return meaning
-
diff --git a/viscaoveriplib/debut.py b/viscaoveriplib/debut.py
new file mode 100644
index 0000000..dc40ba8
--- /dev/null
+++ b/viscaoveriplib/debut.py
@@ -0,0 +1,14 @@
+
+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/inquiry_commands_library.py b/viscaoveriplib/inquiry_commands_library.py
index 962ee2f..52045b7 100644
--- a/viscaoveriplib/inquiry_commands_library.py
+++ b/viscaoveriplib/inquiry_commands_library.py
@@ -1,7 +1,7 @@
 import camera_main
 
 
-class Camera:
+class Inquiry:
 
     ####
     ####### FONCTIONS COMMANDES D'EXECUTIONS
@@ -15,7 +15,6 @@ class Camera:
     def power(self):
         payload = '81 09 04 00 FF'
         self.send_inquiry(payload)
-        self.camera.recieve()
 
     def zoom_pos(self):
         payload = '81 09 04 47 FF'
diff --git a/viscaoveriplib/listener.py b/viscaoveriplib/listener.py
index 123f039..55d6662 100644
--- a/viscaoveriplib/listener.py
+++ b/viscaoveriplib/listener.py
@@ -9,17 +9,17 @@ class Listener(Thread):
     def __init__(self, IP_OUT = "192.168.0.57", PORT = 52381):
 
         Thread.__init__(self)
-        self.message = "FF"
+        self.message = {}
         self.IP_OUT = IP_OUT
         self.PORT = PORT
 
     def run(self):
-        sock = socket.socket(socket.AF_INET,  # Internet
+        sock_recp = socket.socket(socket.AF_INET,  # Internet
                              socket.SOCK_DGRAM)  # UDP
-        sock.bind((self.IP_OUT, self.PORT))
+        sock_recp.bind((self.IP_OUT, self.PORT))
         while True:
-            data, addr = sock.recvfrom(1024)  # buffer size is 1024 bytes
-            self.message = data.hex()
+            data, addr = sock_recp.recvfrom(1024)  # buffer size is 1024 bytes
+            self.message.append(data.hex())
 
 
 
-- 
GitLab