Browse Source

refactor: libconfig and _exec_fader_loop

micha 8 months ago
parent
commit
ad38adbdf8
4 changed files with 173 additions and 70 deletions
  1. 30 21
      _LibreLightDesk.py
  2. 121 0
      lib/libconfig.py
  3. 5 24
      lib/libtk.py
  4. 17 25
      tksdl/start.py

+ 30 - 21
_LibreLightDesk.py

@@ -30,6 +30,7 @@ import tool.movewin as movewin
 import lib.fixlib as  fixlib
 import lib.libwin as libwin
 import lib.libtk as libtk
+import lib.libconfig as libconfig
 
 rnd_id  = str(random.randint(100,900))
 rnd_id += " beta"
@@ -358,7 +359,6 @@ class MC_FIX():
         self.mc.set("fix", data)
 
 
-
 class MC():
     def __init__(self,server="127.0.0.1",port=11211):
         cprint("MC.init() ----------" ,server,port,color="red")
@@ -416,13 +416,40 @@ class MC():
         if not self.ok():
             return 
 
+    def _exec_fader_loop(self,x):
+        for i, line in enumerate(self.fader_map):
+            try:
+                #print(i,line)
+                dmx = int(line["DMX"])
+                if dmx > 0:
+                    val = x[dmx-1]
+                    #print("mc val",val)
+                    #print("dmx_in change:",[i,val])
+                    change = 0
+                    if i < len(self.last_fader_val):
+                        if self.last_fader_val[i] != val:
+                            self.last_fader_val[i] = val
+                            print("dmx_in change:",[i,val])
+                            change = 1
+                    set_exec_fader(nr=i,val=val,color="#aaa",info="dmx_in",change=change)
+            except Exception as e:
+                cprint("MC exc:",e,color="red")
+                traceback.print_exc()
+                pass
+
     def _loop(self):
+        time.sleep(20)
         cprint("++++++++++ start.memcachd read loop",self )
+        
+        ip = libconfig.load_remote_ip()
+
+        print("IP:",ip)
+        input()
         while 1:
             send = 0
             #print("+")
             try:
-                ip="10.10.10.13:0"
+                #ip="10.10.10.13:0"
                 #ip="ltp-out:0"
                 #print(ip)
                 x=self.mc.get(ip)
@@ -431,25 +458,7 @@ class MC():
                     #print(ip,x)
                     #val = x[501-1]
                     #val = x[141-1]
-                    for i, line in enumerate(self.fader_map):
-                        try:
-                            #print(i,line)
-                            dmx = int(line["DMX"])
-                            if dmx > 0:
-                                val = x[dmx-1]
-                                #print("mc val",val)
-                                #print("dmx_in change:",[i,val])
-                                change = 0
-                                if i < len(self.last_fader_val):
-                                    if self.last_fader_val[i] != val:
-                                        self.last_fader_val[i] = val
-                                        print("dmx_in change:",[i,val])
-                                        change = 1
-                                set_exec_fader(nr=i,val=val,color="#aaa",info="dmx_in",change=change)
-                        except Exception as e:
-                            cprint("MC exc:",e,color="red")
-                            traceback.print_exc()
-                            pass
+                    self._exec_fader_loop(x)
 
                 time.sleep(1/10)
             except Exception as e:

+ 121 - 0
lib/libconfig.py

@@ -0,0 +1,121 @@
+#!/usr/bin/python3
+
+import os
+import json
+
+h = os.environ["HOME"]
+
+def cprint(txt,*args):
+    print(txt)
+
+def _load_remote_ip(cfg_file):
+    f = open(cfg_file)
+    lines = f.readlines()
+    f.close()
+    ip = ""
+    for line in lines:
+        # take the last IP in config file
+        if "DMX-REMOTE-IP" in line:
+            try:
+                jd=json.loads(line)
+                jip = jd["DMX-REMOTE-IP"]
+                if ":" in jip and jip.count(".") :
+                    ip = jip
+            except:pass
+    return ip
+
+def _write_default_remote_ip(cfg_file):
+    try:
+        f = open(cfg_file ,"a")
+        f.seek(99999999999)
+        txt ='\n'
+        txt+=json.dumps({"DMX-REMOTE-IP":"10.10.10.13:0"})
+        f.write(txt)
+        f.flush()
+        f.close()
+    except Exception as e:
+        print("EXCEPT",e)
+
+def _create_default_config():
+    txt=''
+    txt+='{"POS_TOP":10}'
+    txt+="\n"
+    txt+='{"POS_LEFT":10}'
+    txt+="\n"
+
+    for i in range(10):
+        txt+='{"DMX-FADER-'+str(i+1)+'":500}'
+        txt+="\n"
+
+    txt+='{"START_MODE":"PROx"}'
+    txt+="\n"
+    txt+='{"START_MODE":"EASYx"}'
+    txt+="\n"
+    txt+=json.dumps({"DMX-REMOTE-IP":"10.10.10.13:0"})
+    txt+="\n"
+
+    f = open(h +"/LibreLight/config.json","w")
+    f.write(txt)
+    f.close()
+
+def _load_config():
+    _config = []
+
+    lines = [{}]
+    try:
+        f = open(h +"/LibreLight/config.json")
+        lines = f.readlines()
+    except FileNotFoundError as e: #Exception as e:
+        _create_default_config()
+        cprint("Exception:",e)
+
+    try:
+        cprint("config read")
+        for line in lines:
+            line=line.strip()
+            print("   config:",line)
+            row = json.loads(line)
+            _config.append(row)
+
+    except Exception as e:
+        cprint("Exception:",e)
+
+    return _config
+
+
+
+def load_remote_ip():
+    cfg_file = "/home/user/LibreLight/config.json"
+    
+    ip = _load_remote_ip(cfg_file)
+    if not ip:
+        _write_default_remote_ip(cfg_file)
+        ip = _load_remote_ip(cfg_file)
+
+    if not ip:
+        ip = "0.0.0.0:err"
+
+    return ip
+
+
+
+def check_pro_easy():
+    try:
+        f = open("/home/user/LibreLight/config.json")
+        lines = f.readlines()
+        f.close()
+        for line in lines:
+            if '{"START_MODE":"PRO"}' in line:
+                print(" PRO")
+                return "PRO"
+            elif '{"START_MODE":"EASY"}' in line:
+                print(" EASY")
+                return "EASY"
+            print(line)
+
+    except Exception as e:
+        print("Exception",e)
+
+    return ""
+
+

+ 5 - 24
lib/libtk.py

@@ -14,36 +14,17 @@ import __main__ as MAIN
 from lib.cprint import cprint
 import lib.libwin as libwin
 import lib.baselib as baselib
+import lib.libconfig as libconfig
 
 import tkgui.dialog  as dialoglib
 dialog = dialoglib.Dialog()
 
 
-_config = []
-try: 
-    h = os.environ["HOME"]
-    lines = [{}]
-    try: 
-        f = open(h +"/LibreLight/config.json")
-        lines = f.readlines()
-
-    except FileNotFoundError as e: #Exception as e:
-        f = open(h +"/LibreLight/config.json","w")
-        f.write('{"POS_TOP":0}\n{"POS_LEFT":0}')
-        f.close()
-        cprint("Exception:",e)
-
-    cprint("config read")
-    for line in lines:
-        line=line.strip()
-        print("   config:",line)
-        row = json.loads(line) 
-        _config.append(row)
-
-except Exception as e:
-    cprint("Exception:",e)
-
+for i in dir(MAIN):
+    print(i)
 
+#_config = MAIN._load_config()
+_config = libconfig._load_config()
 
 _POS_LEFT = 0
 _POS_TOP  = 15

+ 17 - 25
tksdl/start.py

@@ -13,6 +13,18 @@ import pathlib
 _file_path=pathlib.Path(__file__)
 print("__file__ =",_file_path)
 
+
+import lib.restart as restart
+import lib.libconfig as libconfig
+
+r = libconfig.check_pro_easy()
+if r == "PRO":
+    restart.pro()
+if r == "EASY":
+    restart.easy()
+
+
+
 import tool.movewin as movewin
 
 CAPTION = 'LibreLight Start XX'
@@ -79,34 +91,13 @@ import lib.baselib as baselib
 SHOW_NAME = baselib.current_show_name()
 print([SHOW_NAME])
                 
-import lib.restart as restart
-
 def exit(args=None):
     pygame.quit()
     sys.exit()
 
-def check_default():
-    try:
-        f = open("/home/user/LibreLight/config.json")
-        lines = f.readlines()
-        f.close()
-        for line in lines:
-            if '{"START_MODE":"PRO"}' in line:
-                restart.pro()
-                print(" PRO")
-                #pygame.quit()
-                return 0
-            elif '{"START_MODE":"EASY"}' in line:
-                restart.easy()
-                print(" EASY")
-                #pygame.quit()
-                return 0
-            print(line)
-
-    except Exception as e:
-        print("Exception",e)
-
-    return 1
+
+
+
 
 bx = sdl_elm.Button(window,pos=[x,y,400,60])
 bx.text = "       EASY" 
@@ -154,7 +145,8 @@ fps_t = time.time()
 fps = 0
 fps_old = 0
 
-run = check_default()
+
+run = 1
 while run:
     fps +=1
     t = time.time()