execlib.py 15 KB

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