Browse Source

add: index-artpoll -> memcachd and artnet-pkg-count

micha 3 months ago
parent
commit
7037120a81
1 changed files with 62 additions and 35 deletions
  1. 62 35
      ArtNetProcessor.py

+ 62 - 35
ArtNetProcessor.py

@@ -111,6 +111,26 @@ def update_memcachd_index(key,val="",active=-1):
     except Exception as e:
         print("memcach exception",e)
 
+if mc:
+    INDEX="index-artpoll"
+    mc.set(INDEX ,{})
+def update_mc_artpoll_index(key,val=""):
+    try:
+        INDEX="index-artpoll"
+        _index =  mc.get(INDEX)
+        #print("A",_index)
+        if type(_index) is type(None):
+            _index = {}
+        #print("A",_index)
+
+        if key not in _index:
+            _index[key] = [0,""]
+        _index[key][0] += 1
+        _index[key][1] = val
+
+        mc.set(INDEX ,_index)
+    except Exception as e:
+        print("memcach exception",e)
 
 def count_active_dmx_ch(dmx):
     active = 0
@@ -713,8 +733,18 @@ def unpack_art_dmx(data):
         dmx += [x]
     return dmx
 
+def ArtPollCheck(data,addr):
+    opcode=artnet_get_opcode(data)
+    if "ArtPoll" in opcode or  "ArtPollReplay" in opcode:
+         #print("PKG3",addr, opcode,len(data))
+         try:
+               k = "{}:{}".format(addr[0],opcode[0])
+               mc.set(k, data)
+               update_mc_artpoll_index(k,val="")
+         except Exception as e:
+               print("ArtPollCheck err:",e)
         
-class Socket():
+class IN_Socket():
     def __init__(self,bind='',port=6454):
         self.__port =port
         self.__bind =bind
@@ -725,8 +755,10 @@ class Socket():
         self.__hosts = {}
         self.hosts = self.__hosts
         self.open()
-        self._poll_clean_time = time.time()
-        self._poll_clean_count = 0
+        self.pkg_time = time.time()
+        self.start = time.time()
+        self.pkg_fps = 0
+        self.pkg_fps_count = 0
     def open(self):
         try:
             print("connecting to ArtNet bind:",self.__bind,"Port",self.__port)
@@ -740,36 +772,29 @@ class Socket():
             print("Socket ",self.__bind,self.__port, "ERR: {0} ".format(e.args))
             #raw_input()
             #sys.exit()
-    def poll_clean(self):
-        if self._poll_clean_time+(1/25.) <= time.time():
-            self._poll_clean_time = time.time()
-            self._poll_clean()
-            x = self._poll_clean_count 
-            self._poll_clean_count = 0
-            return x
-    def _poll_clean(self):
-        while 1:
-            try:
-                self.__data, self.__addr = self.sock.recvfrom(self.__port)
-                self._poll_clean_count += 1
-                #return 1
-            except socket.timeout as e:
-                err = e.args[0]
-                if err == 'timed out':
-                    time.sleep(1)
-                    print('recv timed out, retry later')
-                else:
-                    print(e)
-                break
-            except socket.error as e:
-                break
+
+
     def poll(self):
         if not self.__poll:
+            if mc:
+                if time.time()-self.pkg_time > 1:
+                    self.pkg_time = time.time()
+                    self.pkg_fps = self.pkg_fps_count/1
+                    self.pkg_fps_count = 0
+                    run_time=int((time.time()-self.start)/60)
+                    mc.set("artnet-pkg-count" , {"ArtDMX-fps":self.pkg_fps,"min":run_time})
+                    
             try:
                 self.__data, self.__addr = self.sock.recvfrom(self.__port)
-                
-
                 data, addr = (self.__data,self.__addr)
+                self.pkg_fps_count += 1
+
+                ArtPollCheck(data,addr)
+
+                opcode=artnet_get_opcode(data)
+                if opcode[0] != "ArtDMX":
+                    return
+
                 self.host = addr[0]
                 head    = data[:18]
                 rawdmx  = data[18:]
@@ -779,9 +804,11 @@ class Socket():
                     self.head = struct.unpack("!8sHBBBBHBB" , head )
                 except Exception as e:
                     pass#print( "======E09823" , e)
+
+
                 univ = self.head[6]/255 # /512  # * 512
                 self.univ = int(univ)
-                opcode=artnet_get_opcode(head)
+
                 #if "2.0.0.11" not in addr:
                 #    print("PKG3",addr,"univ:",self.univ, opcode,len(rawdmx))
 
@@ -946,6 +973,7 @@ def artnet_get_opcode(head):
         opcode=hex(struct.unpack('<h', head[8:10])[0])
     except:
         print("opcode error",[head])
+
     if opcode == '0x5000':
         name = "ArtDMX"
     elif opcode == '0x2000':
@@ -1050,7 +1078,7 @@ class Main():
             artnet._test_frame()
 
         #ysocket = Socket(bind='127.0.0.1' ,port=6555)
-        xsocket = Socket()
+        in_socket = IN_Socket()
 
         xt = time.time()
         ohost_buf = {}
@@ -1063,11 +1091,11 @@ class Main():
                 if options.testuniv:
                     artnet._test_frame()
                 #artnet_out._test_frame()
-                #if xsocket.poll():
-                while xsocket.poll():
+                #if in_socket.poll():
+                while in_socket.poll():
                     xt = time.time()
                     poll_flag = 1
-                    x = xsocket.recive()
+                    x = in_socket.recive()
                     if x["host"] == options.recive:
                         try:
                             x["univ"] = int(options.inmap )
@@ -1112,7 +1140,6 @@ class Main():
                     ohost_buf = {} # clear package buffer
 
                 if send_timer.check() and options.sendto:
-                    #x= xsocket.poll_clean()
 
                     #x=ohost.get(univ=univ2)
                     info=ohost.info()
@@ -1138,7 +1165,7 @@ class Main():
 
                 if not poll_flag: 
                     time.sleep(.001)
-                screen.loop()
+                #screen.loop()
         finally:
             pass
             #screen.exit()