execlib.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 set_cfg(self,nr,sdata):
  199. cprint("EXEC.set_cfg()",nr,color="yellow")
  200. cprint(" ",sdata,color="yellow")
  201. #self.check_cfg(nr)
  202. if nr < 0:
  203. return {}
  204. if nr not in self.val_exec:
  205. cprint(" get_cfg",self,"error get_cfg no nr:",nr,color="red")
  206. return {}
  207. if "CFG" in self.val_exec[nr]:
  208. print(" ",self.val_exec[nr]["CFG"] )
  209. print(" ",sdata)
  210. # OrderedDict([('FADE', 4), ('HAVE-FX', 0), ('HAVE-VAL', 0), ('FIX-COUNT', 0), ('DELAY', 0),
  211. #('BUTTON', 'GO'), ('HTP-MASTER', 100), ('SIZE-MASTER', 100), ('SPEED-MASTER', 100), ('OFFSET-MASTER', 100)])
  212. r=EXEC_CFG_CHECKER(sdata)
  213. print(" ",sdata)
  214. self.val_exec[nr]["CFG"] = sdata
  215. if "Label" in sdata:
  216. self.label_exec[nr] = sdata["Label"]
  217. def clean(self,nr):
  218. if nr not in self.val_exec:
  219. self.val_exec[nr] = OrderedDict()
  220. #self.val_exec[nr]["VALUE"] = 0
  221. #self.val_exec[nr]["FX"] = ""
  222. sdata = self.val_exec[nr]
  223. for fix in sdata:
  224. #print("exec.clear()",nr,fix,sdata[fix])
  225. for attr in sdata[fix]:
  226. row = sdata[fix][attr]
  227. if fix == "CFG":
  228. continue
  229. if "VALUE" not in row:
  230. row["VALUE"] = None
  231. if "FX" not in row:
  232. row["FX"] = ""
  233. if "FX2" not in row:
  234. row["FX2"] = OrderedDict()
  235. elif row["FX2"]:
  236. for k in ["SIZE","SPEED","START","OFFSET"]:
  237. row["FX2"][k] = int( row["FX2"][k] )
  238. row["FX"] = ""
  239. if "FX" in row and row["FX"] and not row["FX2"]: # rebuild old FX to Dict-FX2
  240. #"off:0:0:0:16909:-:"
  241. x = row["FX"].split(":")
  242. cprint("-fx",x,len(x))
  243. #'FX2': {'TYPE': 'sinus', 'SIZE': 200, 'SPEED': 30, 'START': 0, 'OFFSET': 2805, 'BASE': '-'}}
  244. if len(x) >= 6:
  245. row["FX2"]["TYPE"] = x[0]
  246. row["FX2"]["SIZE"] = int(x[1])
  247. row["FX2"]["SPEED"] = int(x[2])
  248. row["FX2"]["START"] = int(x[3])
  249. row["FX2"]["OFFSET"] = int(x[4])
  250. row["FX2"]["BASE"] = x[5]
  251. row["FXOLD"] = row["FX"]
  252. row["FX"] = ""
  253. #cprint("exec.clear()",nr,fix,row)
  254. def get_raw_map(self,nr):
  255. self.clean(nr)
  256. #cprint("EXEC.get_raw_map",nr)
  257. sdata = self.val_exec[nr]
  258. cmd = ""
  259. out = []
  260. dmx=-1
  261. for fix in sdata:
  262. if fix == "CFG":
  263. #print("CFG",nr,sdata[fix])
  264. continue
  265. for attr in sdata[fix]:
  266. x = {}
  267. #print("RAW",attr)
  268. x["FIX"] = fix
  269. x["ATTR"] = attr
  270. x["VALUE"] = sdata[fix][attr]["VALUE"]
  271. x["FX"] = sdata[fix][attr]["FX"]
  272. x["FX2"] = sdata[fix][attr]["FX2"]
  273. #x["DMX"] = sdata[fix][attr]["NR"]
  274. out.append(x)
  275. return out
  276. def get_btn_txt(self,nr):
  277. sdata=self.val_exec[nr]
  278. BTN="go"
  279. if "CFG" in sdata:
  280. if "BUTTON" in sdata["CFG"]:
  281. BTN = sdata["CFG"]["BUTTON"]
  282. _label = self.label_exec[nr] # = label
  283. #txt=str(nr+1)+":"+str(BTN)+":"+str(len(sdata)-1)+"\n"+_label
  284. #txt=str(nr+1)+" "+str(BTN)+" "+str(len(sdata)-1)+"\n"+_label
  285. txt="{} {} {}\n{}".format(nr+1,BTN,len(sdata)-1,_label)
  286. cprint("get_btn_txt",nr,[txt])
  287. return txt
  288. def _btn_cfg(self,nr,txt=None):
  289. if nr not in self.val_exec:
  290. return ""
  291. if "CFG" not in self.val_exec[nr]:
  292. self.val_exec[nr]["CFG"] = OrderedDict()
  293. return self.val_exec[nr]["CFG"]
  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. if "BUTTON" not in self.val_exec[nr]["CFG"]:
  300. self.val_exec[nr]["CFG"]["BUTTON"] = ""
  301. if type(txt) is str:
  302. self.val_exec[nr]["CFG"]["BUTTON"] = txt
  303. if self.val_exec[nr]["CFG"]["BUTTON"] is None:
  304. self.val_exec[nr]["CFG"]["BUTTON"] = ""
  305. MAIN.master._refresh_exec(nr=nr)
  306. cprint("EEE", self.val_exec[nr]["CFG"]["BUTTON"] )
  307. return self.val_exec[nr]["CFG"]["BUTTON"]
  308. def label(self,nr,txt=None):
  309. if nr not in self.label_exec:
  310. return ""
  311. if type(txt) is str:
  312. self.label_exec[nr] = txt
  313. cprint("set label",nr,[txt])
  314. cprint("??? ?? set label",nr,[txt])
  315. return self.label_exec[nr]
  316. def clear_move(self):
  317. cprint("EXEC.clear_move()",end=" ")
  318. self.clear_copy()
  319. def clear_copy(self):
  320. cprint("EXEC.clear_copy()",end=" ")
  321. if self._last_copy is not None:
  322. cprint("=OK=",color="red")
  323. self._last_copy = None
  324. else:
  325. cprint("=NONE=",color="green")
  326. def copy(self,nr,overwrite=1):
  327. cprint("EXEC._copy",nr,"last",self._last_copy)
  328. if nr >= 0:
  329. if self._last_copy is not None:
  330. if MAIN.modes.val("COPY"):
  331. MAIN.modes.val("COPY",3)
  332. ok = self._copy(self._last_copy,nr,overwrite=overwrite)
  333. return ok #ok
  334. else:
  335. if MAIN.modes.val("COPY"):
  336. MAIN.modes.val("COPY",2)
  337. self._last_copy = nr
  338. cprint("EXEC.copy START ",color="red")
  339. return 0
  340. return 1 # on error reset move
  341. def _copy(self,nr_from,nr_to,overwrite=1):
  342. cprint("EXEC._copy",nr_from,"to",nr_to)
  343. self.check_cfg(nr_from)
  344. if type(self._last_copy) is None:
  345. cprint("EXEC._copy last nr is None",color="red")
  346. return 0
  347. cprint("------ EXEC._copy", nr_from in self.val_exec , nr_to in self.val_exec)
  348. if nr_from in self.val_exec and nr_to in self.val_exec:
  349. fdata = self.val_exec[nr_from]
  350. tdata = self.val_exec[nr_to]
  351. #cprint(fdata)
  352. flabel = self.label_exec[nr_from]
  353. tlabel = self.label_exec[nr_to]
  354. self.val_exec[nr_to] = copy.deepcopy(fdata)
  355. self.label_exec[nr_to] = flabel
  356. if not overwrite: #default
  357. cprint("overwrite",overwrite)
  358. self.val_exec[nr_from] = copy.deepcopy(tdata)
  359. self.label_exec[nr_from] = tlabel
  360. #self.label_exec[nr_from] = "MOVE"
  361. self.clear_copy()
  362. cprint("EXEC.copy OK",color="green")
  363. return 1
  364. def move(self,nr):
  365. cprint("EXEC.move",self._last_copy,"to",nr)
  366. if nr >= 0:
  367. last = self._last_copy
  368. if MAIN.modes.val("MOVE"):
  369. MAIN.modes.val("MOVE",2)
  370. ok= self.copy(nr,overwrite=0)
  371. if ok and last >= 0:
  372. if MAIN.modes.val("MOVE"):
  373. MAIN.modes.val("MOVE",3)
  374. cprint("EXEC.move OK",color="red")
  375. #self.delete(last)
  376. return ok,nr,last #ok
  377. return 0,nr,last # on error reset move
  378. def delete(self,nr):
  379. cprint("EXEC.delete",nr)
  380. ok=0
  381. if nr in self.val_exec:
  382. self.val_exec[nr] = OrderedDict()
  383. self.label_exec[nr] = "-"
  384. ok = 1
  385. self.check_cfg(nr)
  386. return ok
  387. def rec(self,nr,data,arg=""):
  388. cprint("rec",self,"rec()",len(data),arg,color="red")
  389. self.check_cfg(nr)
  390. self._check_cfg(data) #by ref
  391. odata=self.val_exec[nr]
  392. #print("odata",odata)
  393. if "CFG" in odata:
  394. if "BUTTON" in odata["CFG"]:
  395. data["CFG"]["BUTTON"] = odata["CFG"]["BUTTON"]
  396. self.val_exec[nr] = data
  397. return 1