execlib.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. #/usr/bin/python3
  2. import copy
  3. import __main__ as MAIN
  4. from lib.cprint import cprint
  5. from collections import OrderedDict
  6. import json
  7. try:
  8. import memcache
  9. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  10. except:
  11. mc = None
  12. def reshape_exec(data ,value=None,xfade=0,flash=0,ptfade=0,DELAY=None):
  13. f=0 #fade
  14. out = []
  15. delay=0
  16. for row in data:
  17. #cprint("reshape_exec in:",row)
  18. line = {}
  19. line["DELAY"]=delay
  20. if type(value) is float:
  21. line["VALUE"] = value #round(value,3)
  22. else:
  23. line["VALUE"] = value
  24. if "FX" not in row:
  25. cprint("698 FX not in row...",row,color="red")
  26. row["FX"] = ""
  27. else:
  28. if type(row["FX"]) is not str:
  29. cprint("702 FX is not str...",row,color="red")
  30. row["FX"] = ""
  31. if value is not None:
  32. line["FX"] = row["FX"].split(":",1)[-1]
  33. else:
  34. line["FX"] = row["FX"]
  35. if row["FX2"]:
  36. line["FX2"] = row["FX2"]
  37. if row["FIX"]:
  38. line["FIX"] = row["FIX"]
  39. if row["ATTR"]:
  40. line["ATTR"] = row["ATTR"]
  41. if row["VALUE"] is not None:
  42. if value is None:
  43. v=row["VALUE"]
  44. if type(v) is float:
  45. line["VALUE"] = v #round(v,3)
  46. else:
  47. line["VALUE"] = v
  48. if row["ATTR"] in ["PAN","TILT"]:
  49. f = ptfade
  50. for a in ["DIM","ZOOM","FOCUS","RED","GREEN","BLUE","WHITE","AMBER","IRIS","BLADE"]:
  51. #FADE ATTRIBUTES
  52. if a in row["ATTR"]:
  53. f = xfade
  54. break
  55. if flash:
  56. xfade = 0
  57. if type( f ) is float:
  58. line["FADE"] = round(f,4)
  59. else:
  60. line["FADE"] = f
  61. if 0:
  62. cprint("reshape_exec j",line,color="red")
  63. #cprint("reshape_exec out:",line)
  64. out.append(line)
  65. if DELAY:
  66. if DELAY._is():
  67. delay+=DELAY.val()/100 #0.02
  68. return out
  69. import lib.showlib as showlib
  70. def EXEC_CFG_CHECKER(sdata):
  71. "repair CFG "
  72. ok = 0
  73. if "CFG" not in sdata:
  74. sdata["CFG"] = OrderedDict()
  75. ok += 1
  76. if "FADE" not in sdata["CFG"]:
  77. sdata["CFG"]["FADE"] = 4
  78. ok += 1
  79. if "HAVE-FX" not in sdata["CFG"]:
  80. sdata["CFG"]["HAVE-FX"] = 0
  81. if "HAVE-VAL" not in sdata["CFG"]:
  82. sdata["CFG"]["HAVE-VAL"] = 0
  83. if "FIX-COUNT" not in sdata["CFG"]:
  84. sdata["CFG"]["FIX-COUNT"] = 0
  85. if "DEALY" in sdata["CFG"]: # REMOVE TYPO !
  86. del sdata["CFG"]["DEALY"]
  87. if "DELAY" not in sdata["CFG"]:
  88. sdata["CFG"]["DELAY"] = 0
  89. ok += 1
  90. if "BUTTON" not in sdata["CFG"]:
  91. sdata["CFG"]["BUTTON"] = "GO"
  92. ok += 1
  93. if "HTP-MASTER" not in sdata["CFG"]:
  94. sdata["CFG"]["HTP-MASTER"] = 100 #%
  95. ok += 1
  96. if "SIZE-MASTER" not in sdata["CFG"]:
  97. sdata["CFG"]["SIZE-MASTER"] = 100 #%
  98. ok += 1
  99. if "SPEED-MASTER" not in sdata["CFG"]:
  100. sdata["CFG"]["SPEED-MASTER"] = 100 #%
  101. ok += 1
  102. if "OFFSET-MASTER" not in sdata["CFG"]:
  103. sdata["CFG"]["OFFSET-MASTER"] = 100 #%
  104. ok += 1
  105. if "HAVE-FX" not in sdata["CFG"]:
  106. sdata["CFG"]["HAVE-FX"] = 0 # yes/no
  107. ok += 1
  108. if "HAVE-VAL" not in sdata["CFG"]:
  109. sdata["CFG"]["HAVE-VAL"] = 0 # yes/no
  110. ok += 1
  111. if "FIX-COUNT" not in sdata["CFG"]:
  112. sdata["CFG"]["FIX-COUNT"] = 0 # yes/no
  113. ok += 1
  114. #try:del sdata["CFG"]["SPEED-MASTER"] #= 100 #%
  115. #except:pass
  116. return ok
  117. import time
  118. def exec_set_mc(excec_labels,exec_data):
  119. l = excec_labels
  120. d = exec_data
  121. if not mc:
  122. #cprint("MC not connected !")
  123. return
  124. index=[]
  125. for i,k in enumerate(l):
  126. nr = i
  127. data = d[k]
  128. label=l[nr]
  129. exec_set_mc_single(nr,label,data)
  130. #print("--------------exec_set_mc-------EXEC-META---------------------")
  131. def exec_set_mc_single(nr,label,data):
  132. #data = d[v]
  133. key="EXEC-"+str(nr)
  134. mc.set(key,json.dumps(data))
  135. key2="EXEC-META-"+str(nr)
  136. cfg = {'FADE': 2.0, 'DELAY': 0, 'BUTTON': 'GO', 'HTP-MASTER': 100
  137. ,'SIZE-MASTER': 100, 'SPEED-MASTER': 100, 'OFFSET-MASTER': 100, 'OUT-FADE': 10.0
  138. ,'HAVE-FX': 0, 'HAVE-VAL': 0, 'FIX-COUNT': 11
  139. }
  140. if "CFG" in data:
  141. cfg.update(data["CFG"])
  142. mc.set(key2,json.dumps({"LABEL":label,"CFG":cfg}) )
  143. class EXEC(): #Presets():
  144. def __init__(self):
  145. self.base = showlib.Base()
  146. self._last_copy = None
  147. self._last_move = None
  148. self.fx_buffer = {}
  149. def load_exec(self):
  150. filename="exec"
  151. filename="presets" # preset.sav
  152. d,l = self.base._load(filename)
  153. for i in d:
  154. sdata = d[i]
  155. ok = EXEC_CFG_CHECKER(sdata)
  156. start = time.time()
  157. self.val_exec = d
  158. self.label_exec = l
  159. exec_set_mc(self.label_exec,self.val_exec)
  160. def check_cfg(self,nr=None):
  161. #cprint("EXEC.check_cfg()",nr)#,color="red")
  162. ok = 0
  163. if nr is not None:
  164. if nr in self.val_exec:
  165. sdata = self.val_exec[nr]
  166. ok += self._check_cfg(sdata)
  167. else:
  168. cprint("nr not in data ",nr,color="red")
  169. else:
  170. for nr in self.val_exec:
  171. sdata = self.val_exec[nr]
  172. ok += self._check_cfg(sdata)
  173. return ok
  174. def _check_cfg(self,sdata):
  175. #cprint("EXEC._check_cfg()")#,color="red")
  176. ok = EXEC_CFG_CHECKER(sdata)
  177. if ok:
  178. pass#cprint("REPAIR CFG's",ok,"count:",sdata["CFG"],color="red")
  179. return ok
  180. def backup_exec(self,save_as="",new=0):
  181. filename = "exec" # new
  182. filename = "presets" # preset.sav
  183. data = self.val_exec
  184. labels = self.label_exec
  185. if new:
  186. data = [] #*512
  187. labls = [""]*512
  188. r=self.base._backup(filename,data,labels,save_as)
  189. return r
  190. def get_cfg(self,nr):
  191. #cprint("EXEC.get_cfg()",nr)
  192. self.check_cfg(nr)
  193. if nr not in self.val_exec:
  194. cprint("get_cfg",self,"error get_cfg no nr:",nr,color="red")
  195. return {}
  196. if "CFG" in self.val_exec[nr]:
  197. return self.val_exec[nr]["CFG"]
  198. def clean(self,nr):
  199. if nr not in self.val_exec:
  200. self.val_exec[nr] = OrderedDict()
  201. #self.val_exec[nr]["VALUE"] = 0
  202. #self.val_exec[nr]["FX"] = ""
  203. sdata = self.val_exec[nr]
  204. for fix in sdata:
  205. #print("exec.clear()",nr,fix,sdata[fix])
  206. for attr in sdata[fix]:
  207. row = sdata[fix][attr]
  208. if fix == "CFG":
  209. continue
  210. if "VALUE" not in row:
  211. row["VALUE"] = None
  212. if "FX" not in row:
  213. row["FX"] = ""
  214. if "FX2" not in row:
  215. row["FX2"] = OrderedDict()
  216. elif row["FX2"]:
  217. for k in ["SIZE","SPEED","START","OFFSET"]:
  218. row["FX2"][k] = int( row["FX2"][k] )
  219. row["FX"] = ""
  220. if "FX" in row and row["FX"] and not row["FX2"]: # rebuild old FX to Dict-FX2
  221. #"off:0:0:0:16909:-:"
  222. x = row["FX"].split(":")
  223. cprint("-fx",x,len(x))
  224. #'FX2': {'TYPE': 'sinus', 'SIZE': 200, 'SPEED': 30, 'START': 0, 'OFFSET': 2805, 'BASE': '-'}}
  225. if len(x) >= 6:
  226. row["FX2"]["TYPE"] = x[0]
  227. row["FX2"]["SIZE"] = int(x[1])
  228. row["FX2"]["SPEED"] = int(x[2])
  229. row["FX2"]["START"] = int(x[3])
  230. row["FX2"]["OFFSET"] = int(x[4])
  231. row["FX2"]["BASE"] = x[5]
  232. row["FXOLD"] = row["FX"]
  233. row["FX"] = ""
  234. #cprint("exec.clear()",nr,fix,row)
  235. def get_raw_map(self,nr):
  236. self.clean(nr)
  237. #cprint("EXEC.get_raw_map",nr)
  238. sdata = self.val_exec[nr]
  239. cmd = ""
  240. out = []
  241. dmx=-1
  242. for fix in sdata:
  243. if fix == "CFG":
  244. #print("CFG",nr,sdata[fix])
  245. continue
  246. for attr in sdata[fix]:
  247. x = {}
  248. #print("RAW",attr)
  249. x["FIX"] = fix
  250. x["ATTR"] = attr
  251. x["VALUE"] = sdata[fix][attr]["VALUE"]
  252. x["FX"] = sdata[fix][attr]["FX"]
  253. x["FX2"] = sdata[fix][attr]["FX2"]
  254. #x["DMX"] = sdata[fix][attr]["NR"]
  255. out.append(x)
  256. return out
  257. def get_btn_txt(self,nr):
  258. sdata=self.val_exec[nr]
  259. BTN="go"
  260. if "CFG" in sdata:
  261. if "BUTTON" in sdata["CFG"]:
  262. BTN = sdata["CFG"]["BUTTON"]
  263. _label = self.label_exec[nr] # = label
  264. #txt=str(nr+1)+":"+str(BTN)+":"+str(len(sdata)-1)+"\n"+_label
  265. #txt=str(nr+1)+" "+str(BTN)+" "+str(len(sdata)-1)+"\n"+_label
  266. txt="{} {} {}\n{}".format(nr+1,BTN,len(sdata)-1,_label)
  267. cprint("get_btn_txt",nr,[txt])
  268. return txt
  269. def _btn_cfg(self,nr,txt=None):
  270. if nr not in self.val_exec:
  271. return ""
  272. if "CFG" not in self.val_exec[nr]:
  273. self.val_exec[nr]["CFG"] = OrderedDict()
  274. return self.val_exec[nr]["CFG"]
  275. def btn_cfg(self,nr,txt=None):
  276. if nr not in self.val_exec:
  277. return ""
  278. if "CFG" not in self.val_exec[nr]:
  279. self.val_exec[nr]["CFG"] = OrderedDict()
  280. if "BUTTON" not in self.val_exec[nr]["CFG"]:
  281. self.val_exec[nr]["CFG"]["BUTTON"] = ""
  282. if type(txt) is str:
  283. self.val_exec[nr]["CFG"]["BUTTON"] = txt
  284. if self.val_exec[nr]["CFG"]["BUTTON"] is None:
  285. self.val_exec[nr]["CFG"]["BUTTON"] = ""
  286. MAIN.master._refresh_exec(nr=nr)
  287. cprint("EEE", self.val_exec[nr]["CFG"]["BUTTON"] )
  288. return self.val_exec[nr]["CFG"]["BUTTON"]
  289. def label(self,nr,txt=None):
  290. if nr not in self.label_exec:
  291. return ""
  292. if type(txt) is str:
  293. self.label_exec[nr] = txt
  294. cprint("set label",nr,[txt])
  295. cprint("??? ?? set label",nr,[txt])
  296. return self.label_exec[nr]
  297. def clear_move(self):
  298. cprint("EXEC.clear_move()",end=" ")
  299. self.clear_copy()
  300. def clear_copy(self):
  301. cprint("EXEC.clear_copy()",end=" ")
  302. if self._last_copy is not None:
  303. cprint("=OK=",color="red")
  304. self._last_copy = None
  305. else:
  306. cprint("=NONE=",color="green")
  307. def copy(self,nr,overwrite=1):
  308. cprint("EXEC._copy",nr,"last",self._last_copy)
  309. if nr >= 0:
  310. if self._last_copy is not None:
  311. if MAIN.modes.val("COPY"):
  312. MAIN.modes.val("COPY",3)
  313. ok = self._copy(self._last_copy,nr,overwrite=overwrite)
  314. return ok #ok
  315. else:
  316. if MAIN.modes.val("COPY"):
  317. MAIN.modes.val("COPY",2)
  318. self._last_copy = nr
  319. cprint("EXEC.copy START ",color="red")
  320. return 0
  321. return 1 # on error reset move
  322. def _copy(self,nr_from,nr_to,overwrite=1):
  323. cprint("EXEC._copy",nr_from,"to",nr_to)
  324. self.check_cfg(nr_from)
  325. if type(self._last_copy) is None:
  326. cprint("EXEC._copy last nr is None",color="red")
  327. return 0
  328. cprint("------ EXEC._copy", nr_from in self.val_exec , nr_to in self.val_exec)
  329. if nr_from in self.val_exec and nr_to in self.val_exec:
  330. fdata = self.val_exec[nr_from]
  331. tdata = self.val_exec[nr_to]
  332. #cprint(fdata)
  333. flabel = self.label_exec[nr_from]
  334. tlabel = self.label_exec[nr_to]
  335. self.val_exec[nr_to] = copy.deepcopy(fdata)
  336. self.label_exec[nr_to] = flabel
  337. if not overwrite: #default
  338. cprint("overwrite",overwrite)
  339. self.val_exec[nr_from] = copy.deepcopy(tdata)
  340. self.label_exec[nr_from] = tlabel
  341. #self.label_exec[nr_from] = "MOVE"
  342. self.clear_copy()
  343. cprint("EXEC.copy OK",color="green")
  344. return 1
  345. def move(self,nr):
  346. cprint("EXEC.move",self._last_copy,"to",nr)
  347. if nr >= 0:
  348. last = self._last_copy
  349. if MAIN.modes.val("MOVE"):
  350. MAIN.modes.val("MOVE",2)
  351. ok= self.copy(nr,overwrite=0)
  352. if ok and last >= 0:
  353. if MAIN.modes.val("MOVE"):
  354. MAIN.modes.val("MOVE",3)
  355. cprint("EXEC.move OK",color="red")
  356. #self.delete(last)
  357. return ok,nr,last #ok
  358. return 0,nr,last # on error reset move
  359. def delete(self,nr):
  360. cprint("EXEC.delete",nr)
  361. ok=0
  362. if nr in self.val_exec:
  363. self.val_exec[nr] = OrderedDict()
  364. self.label_exec[nr] = "-"
  365. ok = 1
  366. self.check_cfg(nr)
  367. return ok
  368. def rec(self,nr,data,arg=""):
  369. cprint("rec",self,"rec()",len(data),arg,color="red")
  370. self.check_cfg(nr)
  371. self._check_cfg(data) #by ref
  372. odata=self.val_exec[nr]
  373. #print("odata",odata)
  374. if "CFG" in odata:
  375. if "BUTTON" in odata["CFG"]:
  376. data["CFG"]["BUTTON"] = odata["CFG"]["BUTTON"]
  377. self.val_exec[nr] = data
  378. return 1