فهرست منبع

cleanup: ray-fix add: async get_fix()

micha 2 روز پیش
والد
کامیت
f9b4119c41
1فایلهای تغییر یافته به همراه407 افزوده شده و 145 حذف شده
  1. 407 145
      tkray/fix.py

+ 407 - 145
tkray/fix.py

@@ -1,6 +1,7 @@
 #from pyray import *
 import pyray 
 import sys
+import _thread as thread
 sys.path.insert(0,"/opt/LibreLight/Xdesk/")
 
 
@@ -24,13 +25,24 @@ if store:
     POS[1] += 29  #frame kompensation
     POS=[store[-2],store[-1]]
 
-
+RUN = 1
 
 pyray.set_config_flags(pyray.FLAG_WINDOW_RESIZABLE)
 pyray.init_window(W, H, CAPTION) #"RAY-DMX")#,10,10,10,10)
 pyray.set_window_min_size(200,200)
 pyray.set_window_max_size(1000,1000)
+pyray.set_exit_key(-1)
+#pyray.set_captur_key(-1)
+
+pyray.begin_drawing()
+pyray.clear_background(pyray.BLACK)
+pyray.Color(255,0,0,0)
+pyray.end_drawing()
+pyray.set_target_fps(60)
 
+import json
+import lib.zchat as chat
+cmd_client = chat.Client(port=30003)
 
 win_con = movewin.Control()
 win_con.title = win_title
@@ -127,7 +139,7 @@ class BUTTON():
         pyray.draw_rectangle(pos[1]+2,pos[0]+2,pos[3]-4,pos[2]-4,c)
 
         c=self.btn.btn4.color  # color bar
-        pyray.draw_rectangle(pos[1]+2,pos[0]+2,1+int(110*self.val/255),3,c)
+        pyray.draw_rectangle(pos[1]+2,pos[0]+2,1+int((pos[3]-4)*self.val/255),3,c)
 
         c=self.btn.btn3.get_color()
         #pyray.draw_rectangle(pos[1]+1,pos[0]+1,pos[3]-2,pos[2]-2,c)
@@ -241,15 +253,118 @@ def check_area(pos,event_pos):
     if x and y:
         return 1
 
+
+
+
+def is_hidden_attr(attr_name):
+    if attr_name.endswith("-FINE"):
+        return 1
+    if attr_name == "END":
+        return 1
+    if attr_name.startswith("_"):
+        return 1
+    return 0
+
+def read_dmx():
+    dmx = {} #univ
+    iii = 0
+    y=mc.get("index")
+    if y:
+        key=y.keys()
+        key = list(key)
+        key.sort()
+
+        for k in key:
+            if k.startswith("ltp-out"):
+                #v = y[k]
+                u = k.split(":")[-1]
+                x=mc.get(k)
+                dmx[u] = x
+    return dmx
+            
+def read_fix(dmx):
+    global mc
+    y=mc.get("fix")#cmd)
+    if y is None:
+        print("==== "*10)
+        print("error -- read_fix(dmx) mc.get('fix') return",y)
+        print()
+        mc = memcache.Client(['127.0.0.1:11211'], debug=0)
+        return {}
+    key=y.keys()
+    key = list(key)
+    key.sort()
+    for k in key:#y.items():
+        v = y[k]
+        #print(k,v)
+        x=mc.get(k)
+        dmx_start = 0
+        if "DMX" in v:
+            dmx_start = v["DMX"]
+
+        univ_start = 0
+        if "UNIVERS" in v:
+            univ_start = v["UNIVERS"]
+
+        if "ATTRIBUT" in v: # and 10: 
+            ATTR = v["ATTRIBUT"]
+            for k2 in ATTR:
+                k2_ATTR = ATTR[k2]
+                k2_ATTR["VALUE2"] = -2
+
+                if is_hidden_attr(k2):
+                    #if k2.startswith("_"):
+                    continue
+
+                k3 = k+"-"+k2
+                
+                dmx_nr = 0
+                if "NR" in k2_ATTR:
+                    if k2_ATTR["NR"] >= 1:
+                        dmx_nr = k2_ATTR["NR"]+1
+                
+                val2 = ""
+                if "VALUE" in k2_ATTR:
+                    val2 = k2_ATTR["VALUE"]
+
+                dmx_val=-1
+                dmx_x=-1
+                if dmx_nr > 0 and dmx_start > 0:
+                    try:
+                        dmx_x = dmx_start-1+dmx_nr-1
+                        dmx_val = dmx[str(univ_start)][dmx_x-1]
+                    except:pass
+                if type(dmx_val) in [int,float]:
+                    k2_ATTR["VALUE2"] = dmx_val
+                else:
+                    k2_ATTR["VALUE2"] = 0
+    return y
+
+
+
+
 _load_fix_DATA = {}
-_load_fix_host_list = {}
+_load_fix_fix_list = {}
 _load_fix_last = time.time()
 _load_fix_keys = 0
 _load_fix_init = 0
 _load_fix_keys = []
+
+lock = thread.allocate_lock()
+#lock.acquire()
+#lock.release()
+
+def get_fix():
+    lock.acquire()
+    a=_load_fix_keys #= keys.copy()
+    b=_load_fix_fix_list #= fix_list.copy() 
+    #a,b = _load_fix_keys.copy(), _load_fix_fix_list.copy()
+    lock.release()
+    return a,b
+
 def load_fix():
     global _load_fix_DATA 
-    global _load_fix_host_list 
+    global _load_fix_fix_list 
     global _load_fix_keys 
     global _load_fix_init
     global _load_fix_last
@@ -257,7 +372,12 @@ def load_fix():
     if not _load_fix_init: 
         pass
     elif _load_fix_last+1 > time.time():
-        return _load_fix_keys.copy(), _load_fix_host_list.copy()
+        pass
+        #lock.acquire()
+        #a,b = _load_fix_keys.copy(), _load_fix_fix_list.copy()
+        #lock.release()
+        #return a,b
+        #return _load_fix_keys.copy(), _load_fix_fix_list.copy()
 
     print("load_fix update")
     _load_fix_last = time.time()
@@ -265,7 +385,7 @@ def load_fix():
 
     DATA=mc.get("fix")#cmd)
     keys = []
-    host_list = {}
+    fix_list = {}
     if not DATA:
         DATA = {}
         print("mc.get ist NONE")
@@ -273,15 +393,31 @@ def load_fix():
 
     for k in DATA:
         keys.append(k)
-        host_list[k] = DATA[k] #[0]).ljust(" ",18) #,0,"=",0]
+        fix_list[k] = DATA[k] #[0]).ljust(" ",18) #,0,"=",0]
     keys.sort()
 
-    _load_fix_keys = keys
-    _load_fix_host_list = host_list 
+    lock.acquire()
+    _load_fix_keys = keys.copy()
+    _load_fix_fix_list = fix_list.copy() 
+    #a,b = _load_fix_keys.copy(), _load_fix_fix_list.copy()
+    lock.release()
+    #return a,b
 
-    return _load_fix_keys.copy(), _load_fix_host_list.copy()
-
-def check_buttons(buttons,host_list):
+def loop_load_fix():
+    global RUN
+    while RUN:
+        try:
+            load_fix()
+            time.sleep(1)
+        except Exception as e:
+            print("EXC loop_load_fix:",e)
+    
+load_fix()
+thread.start_new_thread(loop_load_fix,())
+
+def check_buttons(buttons,fix_list):
+    #raylib.enable_event_waiting()
+    #pyray.disable_event_waiting() # not working , fast clickinb button or key
     _x=20
     _y=30
 
@@ -290,33 +426,44 @@ def check_buttons(buttons,host_list):
     W2 = W+20
 
     _last_v = {}
-    for i,k in enumerate(host_list): #.items():
+    for i,k in enumerate(fix_list): #.items():
         _x=20
         key = str(i)
+        on_color = [255,255,0,255]
+        on_color = [200,200,50,255] # gold
+
         if key not in buttons: # insert 
-            v=host_list[k]
+            v=fix_list[k]
             _last_v = v
             txt="ID:"+str(v["ID"])
             btn=BUTTON(None,txt,[_y,_x,H,60])
-            btn.btn.btn1.color_on = [255,255,0,255]
+            btn.btn.btn1.color_on = on_color
             buttons[key+".ID"] = btn
             #print("ADD",key,btn)
             _x+=60+2
 
             txt=v["NAME"]
             btn=BUTTON(None,txt,[_y,_x,H,W])
-            btn.btn.btn1.color_on = [255,255,0,255]
+            btn.btn.btn1.color_on = on_color
             buttons[key] = btn
             #print("ADD",key,btn)
-            
-            _x+=W+2
+            _x+=W+5
+
+            key = str(i)+":dim"
+            txt = ""
+            btn=BUTTON(None,txt,[_y,_x,H,10])
+            btn.btn.btn1.color = [0,0,0,255]
+            btn.btn.btn1.color_on = on_color
+            buttons[key] = btn
+            _x+=10+1
+
             key = str(i)+":rgb"
             txt = ""
             btn=BUTTON(None,txt,[_y,_x,H,20])
             btn.btn.btn1.color = [0,0,0,255]
-            btn.btn.btn1.color_on = [255,255,0,255]
+            btn.btn.btn1.color_on = on_color
             buttons[key] = btn
-            _x+=20+2
+            _x+=20+5
 
             if "ATTRIBUT" in v:
                 #print(txt)
@@ -332,7 +479,7 @@ def check_buttons(buttons,host_list):
                         #print("-",kk,vv)
                         txt = kk+" "+str(VAL)
                         btn=BUTTON(None,txt,[ay2,ax2,H,W2])
-                        btn.btn.btn1.color_on = [255,255,0,255]
+                        btn.btn.btn1.color_on = on_color
 
 
                         #self.btn4.increment = 4.4
@@ -358,9 +505,9 @@ def check_buttons(buttons,host_list):
 
         _y+=23
 
-    for i,k in enumerate(host_list): #.items():
+    for i,k in enumerate(fix_list): #.items():
         key = str(i)
-        v=host_list[k]
+        v=fix_list[k]
 
         txt=v["NAME"]
         btn = buttons[key] 
@@ -404,6 +551,15 @@ def check_buttons(buttons,host_list):
                         if kk == "BLUE":
                             buttons[_key].btn.btn1.color[2] = VAL
 
+                    if kk in ["DIM"]:
+                        _key = str(i)+":dim"
+                        _VAL = VAL
+                        if _VAL > 255:
+                            _VAL=255
+                        if _VAL < 0:
+                            _VAL = 0
+                        buttons[_key].btn.btn1.color = [_VAL,_VAL,_VAL,255] #_VAL]
+
 
                     btn.btn.btn1.name = txt
                     if vv["ACTIVE"]:
@@ -423,158 +579,264 @@ def draw_buttons(buttons):
             continue
         btn.draw()
 
+def event_loop():
+    global RUN
+    while RUN:
+        try:
+            _event_loop()
+            time.sleep(0.0001)
+        except Exception as e:
+            print("EXC event:",e)
+#thread.start_new_thread(event_loop,())
+
+old_x = 0
+old_y = 0
+wheel = 0
+class _M():
+    def __init__(self):
+        self.x =0
+        self.y=0
+m = _M() # {"x":0,"y":0}
+MOUSE_BTN = [0,0,0]
+def _event_loop():
+    global RUN
+    global old_x 
+    global old_y 
+    global wheel
+    global mouse_old
+    global m
+    wheel = pyray.get_mouse_wheel_move()
+    if wheel:
+        print("wheel",wheel)
+    m=pyray.get_mouse_position()
+    #print(m)
+    if m.x != old_x or m.y != old_y:
+        old_x = m.x
+        old_y = m.y
+
+    mouse=[None,None,None]
+    mouse_change=0
+    #for i in dir(pyray):
+    #    if "gfwl_" not in i.lower():
+    #        print(i)
+    #print(pyray.exit_window_requested())
+    if pyray.window_should_close():
+        print("sending QUIT ...")
+        pyray.close_window()
+        RUN=0
+        time.sleep(1.3)
+        exit()
+
+    for i in range(3):
+        #v=pyray.is_mouse_button_down(i)
+        for ii in range(10):
+            v=pyray.is_mouse_button_pressed(i)
+            #print(v)
+            if v:
+                if not MOUSE_BTN[i]:
+                    MOUSE_BTN[i] = 1
+                    mouse_change=1
+                    #print("mouse",i,v)
+                    print(time.time(),"mouse",i,MOUSE_BTN)#i,v)
+
+    for i in range(3):
+        v=pyray.is_mouse_button_down(i)
+        if not v:
+            if MOUSE_BTN[i]:
+                MOUSE_BTN[i] = 0
+                mouse_change=1
+                #print("mouse",i,v)
+                print(time.time(),"mouse",i,MOUSE_BTN)#i,v)
+
+    # KEYBOARD INPUT  =========================
+
+    _key_del = [] 
+    for k in KEY_BUF:
+        if not pyray.is_key_down(k):
+            KEY_BUF[k] = [0,1] # release [val,change]
+            _key_del.append(k)
+
+    while RUN:
+        x = pyray.get_key_pressed()
+        if x:
+            if x not in KEY_BUF:
+                KEY_BUF[x] = [1,1] # press  [val,change]
+        else:
+            break
+
+
+    for i,k in enumerate(KEY_BUF):
+        x = k
+        val,change = KEY_BUF[k]
+        KEY_BUF[k][1] = 0
+        if not change:
+            continue 
+
+        if x == 256:
+            if val:
+                msg=json.dumps([{"event":"ESC"}]).encode("utf-8")
+                print("ESC",msg)
+                cmd_client.send(msg)
+        elif x == 269:
+            if val:
+                msg=json.dumps([{"event":"FX-OFF"}]).encode("utf-8")
+                print("FX-OFF",msg)
+                cmd_client.send(msg)
+        elif x >= 290 and x <= 300:
+            btn_nr = 290-x+81
+            btn_nr_raw = 81
+            msg=json.dumps([{"event":"EXEC","EXEC":btn_nr,"VAL":val,"NR-KEY":btn_nr_raw}]).encode("utf-8")
+
+            print("SPCIAL-KEY",msg)
+            cmd_client.send(msg)
+        else:
+            print("key",x)
+
+    for d in _key_del:
+        del KEY_BUF[d] # clear KEY BUFFER
+
+    # ================================================
+
+
+    if mouse != mouse_old and mouse != [None,None,None]:
+        #print(mouse)
+        mouse_old = mouse[:]
+        
+    check_scrollbar()
+
+MMM = 1
+def event_mouse_click():
+    global MMM
+    #{'event': 'FIXTURES', 'TYPE': 'ENCODERS', 'FIX': '13', 'VAL': 'click', 'ATTR': 'GREEN'} 
+    # jsbc.remote-key: {'event': 'FIXTURES', 'TYPE': 'ENCODERS', 'FIX': '12', 'VAL': 'click', 'ATTR': '_ACTIVE'}
+
+    msg = json.dumps( {'event': 'FIXTURES', 'TYPE': 'ENCODERS', 'FIX': '13', 'VAL': 'click', 'ATTR': 'GREEN'} )
+    cmd_client.send(msg)
+
+
 buttons={} #[]
 cur = None
 history = []
 KEY_BUF={}
+MOUSE_BUF={}
 FIRST_RUN = 1
 add=0
 
 core_tick_t = time.time()
 
+dmx_last = time.time()
+
+w = pyray.get_screen_width()
+h = pyray.get_screen_height()
+mw = w
+mh = h
+
+def check_scrollbar():
+    w = pyray.get_screen_width()
+    h = pyray.get_screen_height()
+    mw = w
+    mh = h
+
+    key="scroll-bar"
+    if key not in buttons:
+        txt=""
+        btn=BUTTON(None,txt,[30,mw-35,mh-75,30])
+        #btn.btn.btn1.color_on = [255,255,0,255]
+        buttons[key] = btn
+        btn.btn.btn4.val.set(btn.btn.btn4.val._max*0.4)
+    try:
+        key="scroll-bar"
+        buttons[key].btn.pos[1] = mw-35
+        buttons[key].btn.pos[2] = mh-75
+    except:pass
+
 while not pyray.window_should_close():
-    pyray.begin_drawing()
+    if pyray.is_window_resized():
+        print( "RESIZE WINDOW OK")
+        #check_scrollbar()
 
-    pyray.clear_background(pyray.BLACK)
-    pyray.Color(255,0,0,0)
-    pyray.rl_enable_smooth_lines()
 
 
 
+    if dmx_last+1 < time.time():
+        #print("dmx")
+        dmx_last = time.time()
+        dmx = read_dmx()
+        read_fix(dmx)
+
     font_size = font12.baseSize
     try:
-        mw = pyray.get_screen_width()
-        mh = pyray.get_screen_height()
 
-        keys,host_list = load_fix()
+        #keys,fix_list = load_fix()
+        keys,fix_list = get_fix()
+        check_buttons(buttons,fix_list)
 
-        GREY = [122,122,122,255]
-        
-        k=keys[0]
-
-        if cur is None:
-            cur = k #k[3] #"xx123" # k
-
-        check_buttons(buttons,host_list)
-
-
-        pyray.draw_text_ex(font_l_22,"FIXTURE LIST",[20,2], 22, 0,  pyray.GRAY)
-
-        pyray.draw_text_ex(font12,"ACTIVE:{:0.1f} {:0.1f}".format(0,0),[220,5], 12, 0,  pyray.YELLOW)
-        pyray.draw_text_ex(font12,"ENCODER:{:0.2f}".format(5),[315,5], 12, 0,  pyray.YELLOW)
-
-        pyray.draw_text_ex(font12,"FPS:{}".format(fps_count)     ,[420,5], 12, 0,  pyray.BLUE)
-        pyray.draw_text_ex(font12,"BTN:{}".format(len(buttons))  ,[460,5], 12, 0,  pyray.BLUE)
-        pyray.draw_text_ex(font12,"FIX:{}".format(len(host_list)),[520,5], 12, 0,  pyray.BLUE)
-
-        wheel = pyray.get_mouse_wheel_move()
-        if wheel:
-            print(wheel)
-        m=pyray.get_mouse_position()
-        #print(m)
-        if m.x != old_x or m.y != old_y:
-            old_x = m.x
-            old_y = m.y
-
-        mouse=[None,None,None]
-        mouse_change=0
-        for i in range(3):
-            ev = ""
-            if pyray.is_mouse_button_released(i):
-                ev = "release"
-            if pyray.is_mouse_button_pressed(i):
-                ev = "click" #"press"
-            if ev and ev != mouse[i]:
-                mouse[i] = ev
-                mouse_change=1
+        for i in range(3): # hack fast event polling button and key
 
-        if mouse_change:
-            print("MOUSE:",mouse,old_x,old_y) #=[None,None,None]
+            pyray.begin_drawing()
 
+            pyray.clear_background(pyray.BLACK)
+            pyray.Color(255,0,0,0)
+            pyray.rl_enable_smooth_lines()
 
-        for i in range(1,12+1):
-            _KEY = "KEY_F{}".format(i)
-            x = pyray.KeyboardKey[_KEY]
-            #k_val=pyray.is_key_pressed(x)
-            k_val=pyray.is_key_down(x)
-            if _KEY not in KEY_BUF:
-                KEY_BUF[_KEY] = k_val
-            if KEY_BUF[_KEY] != k_val:
-                print("KEY_CHANGE",_KEY,k_val)
-                KEY_BUF[_KEY] = k_val
+            GREY = [122,122,122,255]
+            
+            k=keys[0]
+
+            if cur is None:
+                cur = k #k[3] #"xx123" # k
+
+            #check_buttons(buttons,fix_list)
 
-        x=pyray.get_key_pressed()
-        if x:
-            print("key",x)
-        x=pyray.is_key_pressed(pyray.KEY_F1)
-        if x:
-            print("key",x)
 
-        if mouse != mouse_old and mouse != [None,None,None]:
-            #print(mouse)
-            mouse_old = mouse[:]
+            pyray.draw_text_ex(font_l_22,"FIXTURE LIST",[20,2], 22, 0,  pyray.GRAY)
+
+            pyray.draw_text_ex(font12,"ACTIVE:{:0.1f} {:0.1f}".format(0,0),[220,5], 12, 0,  pyray.YELLOW)
+            pyray.draw_text_ex(font12,"ENCODER:{:0.2f}".format(5),[315,5], 12, 0,  pyray.YELLOW)
+
+            pyray.draw_text_ex(font12,"FPS:{}".format(fps_count)     ,[420,5], 12, 0,  pyray.BLUE)
+            pyray.draw_text_ex(font12,"BTN:{}".format(len(buttons))  ,[460,5], 12, 0,  pyray.BLUE)
+            pyray.draw_text_ex(font12,"FIX:{}".format(len(fix_list)),[520,5], 12, 0,  pyray.BLUE)
+
             
-        key="scroll-bar"
-        if key not in buttons:
-            txt=""
-            btn=BUTTON(None,txt,[30,mw-35,mh-75,30])
-            #btn.btn.btn1.color_on = [255,255,0,255]
+            txt="HELP"
+            key="HELP"
+            btn=BUTTON(None,txt,[4,160,16,40])
+            btn.font = font10
             buttons[key] = btn
-            btn.btn.btn4.val.set(btn.btn.btn4.val._max*0.4)
 
-        txt="HELP"
-        key="HELP"
-        btn=BUTTON(None,txt,[4,160,16,40])
-        btn.font = font10
-        buttons[key] = btn
+            draw_buttons(buttons)
+            
+            pyray.draw_text_ex(font12,"X:{}".format(int(m.x)),[590,5], 12, 0,  pyray.VIOLET)
+            pyray.draw_text_ex(font12,"Y:{}".format(int(m.y)),[620,5], 12, 0,  pyray.VIOLET)
 
-        draw_buttons(buttons)
-        
-        pyray.draw_text_ex(font12,"X:{}".format(int(m.x)),[590,5], 12, 0,  pyray.VIOLET)
-        pyray.draw_text_ex(font12,"Y:{}".format(int(m.y)),[620,5], 12, 0,  pyray.VIOLET)
+            pyray.draw_rectangle(int(old_x-10),int(old_y-1),20,2,[255,0,255,255])
+            pyray.draw_rectangle(int(old_x-1),int(old_y-10),2,20,[255,0,255,255])
 
-        pyray.draw_rectangle(int(old_x-10),int(old_y-1),20,2,[255,0,255,255])
-        pyray.draw_rectangle(int(old_x-1),int(old_y-10),2,20,[255,0,255,255])
+            aapos = draw_mouse_box(None,[old_x,old_y],mouse_old,color=[128,128,128],text=1)
 
-        aapos = draw_mouse_box(None,[old_x,old_y],mouse_old,color=[128,128,128],text=1)
+            pyray.end_drawing()
+            
+            # frame tick speed, CPU LOAD. ------
+            delta = 1/5
+            if delta > (time.time()-core_tick_t):
+                delta -= (time.time()-core_tick_t)
+            #print(delta)
+            for t in range(8):
+                time.sleep(delta/8)
+                _event_loop()
+                pyray.begin_drawing()
+                pyray.end_drawing()
+
+            core_tick_t = time.time()
+            # --------------------------------
+
+            frame_count += 1
+            if time.time()-start > 1:
+                start = time.time()
+                fps_count = frame_count
+                frame_count = 0
 
-        pyray.end_drawing()
-        
-        # frame tick speed, CPU LOAD. ------
-        delta = 1/5
-        if delta > (time.time()-core_tick_t):
-            delta -= (time.time()-core_tick_t)
-        #print(delta)
-        time.sleep(delta)
-        core_tick_t = time.time()
-        # --------------------------------
-
-
-
-        for i,v in enumerate(mouse_old):
-            if v == "release":
-                mouse_old[i] = None
-            if v == "click":
-                mouse_old[i] = "press"
-        #print(mouse_old)
-        frame_count += 1
-        if time.time()-start > 1:
-            start = time.time()
-            fps_count = frame_count
-            frame_count = 0
-
-        #pyray.set_window_size(POS[0],POS[1])
-        #POS[0]+=1
-        if pyray.is_window_resized():
-            print( "RESIZE WINDOW OK")
-            #for i in dir(pyray):
-            #    if "height" in i.lower(): 
-            #        print(i)
-            w = pyray.get_screen_width()
-            h = pyray.get_screen_height()
-            print(h,w)
-        #print(pyray.FLAG_WINDOW_RESIZABLE  )
-        #print("E",pyray.is_window_focused() )
         FIRST_RUN = 0
     except KeyboardInterrupt as e:
         raise e