execlib.py 13 KB

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