execlib.py 14 KB

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