fix.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. #!/usr/bin/python3
  2. import time
  3. boot = time.time()
  4. import random
  5. import os
  6. import sys
  7. #sys.path.insert(0,os.path.realpath(os.getcwd() + '/..'))
  8. sys.path.insert(0,"/opt/LibreLight/Xdesk/")
  9. print(sys.path)
  10. print()
  11. import pathlib
  12. _file_path=pathlib.Path(__file__)
  13. print("file:",_file_path)
  14. import tool.movewin as movewin
  15. pids = movewin.search_process(_file_path)
  16. CAPTION = 'LibreLight SDL-FIXTURE '
  17. if len(pids) >= 2:
  18. search = CAPTION[:]
  19. _ids = movewin.winfo(search)
  20. for _id in _ids:
  21. c3 = movewin.activate(_id)
  22. os.system(c3)
  23. sys.exit()
  24. # ===== GUI =========
  25. import pygame
  26. import pygame.gfxdraw
  27. import pygame.font
  28. pg = pygame
  29. main_size=(850,460)
  30. window = pygame.display.set_mode(main_size,pg.RESIZABLE,32)
  31. pg = pygame
  32. pygame.init()
  33. pygame.mixer.quit()
  34. clock = pygame.time.Clock()
  35. icon = pygame.image.load('icon/scribble.png')
  36. pygame.display.set_icon(icon)
  37. import tool.movewin as movewin
  38. import tool.sdl_elm as sdl_elm
  39. #CAPTION = 'LibreLight DMX '
  40. CAPTION += ':{}'.format(random.randint(100,999))
  41. import tool.git as git
  42. CAPTION += git.get_all()
  43. _id = movewin.winfo(CAPTION)
  44. c1 = movewin.movewin(_id,main_size[0],main_size[1]) #800,500)
  45. os.system(c1)
  46. c1 = movewin.activate(_id)
  47. os.system(c1)
  48. pg.display.set_caption(CAPTION)
  49. font0 = pygame.font.SysFont("freesans",10)
  50. font0b = pygame.font.SysFont("freesansbold",10)
  51. font = pygame.font.SysFont("freemonobold",22)
  52. font10 = pygame.font.SysFont("freemonobold",10)
  53. font12 = pygame.font.SysFont("freemonobold",12)
  54. font15 = pygame.font.SysFont("freemonobold",15)
  55. font16 = pygame.font.SysFont("freemonobold",16)
  56. font22 = pygame.font.SysFont("FreeSans",22)
  57. #font = pygame.font.SysFont(None,30)
  58. fr = font.render("hallo" ,1, (200,0,255))
  59. mouse_down = 0
  60. mouse_pos1 = [0,0]
  61. mouse_pos2 = [0,0]
  62. mouse_grab = []
  63. print(int((time.time()-boot)*10),"loop...")
  64. fps_t = time.time()
  65. fps = 0
  66. fps_old = 0
  67. import memcache
  68. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  69. mc.set("some_key", "Some value")
  70. value = mc.get("some_key")
  71. mc.set("another_key", 3)
  72. mc.delete("another_key")
  73. import time
  74. import json
  75. data = {}
  76. start = time.time()
  77. delta = start
  78. #for i in dir(mc):
  79. # print(i)#,[i.__doc__])
  80. # print()
  81. #for i in mc.get_stats():
  82. # print("keys",i)
  83. #fps_btn = []
  84. #fps_btn_press = []
  85. #i += 1
  86. #bx = sdl_elm.Button(window,pos=[30,r,190,60])
  87. #bx.text = "FIX:{}\n<val>\nx".format(i+1)
  88. #bx.font0 = pygame.font.SysFont("freesans-bold",20)
  89. #bx.btn1.type = "flash"
  90. #fps_btn.append(bx)
  91. #r+=bx.get_rect()[3]
  92. def read_dmx():
  93. dmx = {} #univ
  94. iii = 0
  95. y=mc.get("index")
  96. key=y.keys()
  97. key = list(key)
  98. key.sort()
  99. for k in key:
  100. if k.startswith("ltp-out"):
  101. #v = y[k]
  102. u = k.split(":")[-1]
  103. x=mc.get(k)
  104. dmx[u] = x
  105. return dmx
  106. def read_fix(dmx):
  107. y=mc.get("fix")#cmd)
  108. key=y.keys()
  109. key = list(key)
  110. key.sort()
  111. for k in key:#y.items():
  112. v = y[k]
  113. #print(k,v)
  114. x=mc.get(k)
  115. dmx_start = 0
  116. if "DMX" in v:
  117. dmx_start = v["DMX"]
  118. univ_start = 0
  119. if "UNIVERS" in v:
  120. univ_start = v["UNIVERS"]
  121. if "ATTRIBUT" in v: # and 10:
  122. ATTR = v["ATTRIBUT"]
  123. for k2 in ATTR:
  124. k2_ATTR = ATTR[k2]
  125. #print(ATTR) #[k2_ATTR]) #["VALUE2"] = -2
  126. k2_ATTR["VALUE2"] = -2
  127. #if k2.endswith("-FINE"):
  128. # continue
  129. if k2.startswith("_"):
  130. continue
  131. k3 = k+"-"+k2
  132. dmx_nr = 0
  133. if "NR" in k2_ATTR:
  134. if k2_ATTR["NR"] >= 1:
  135. dmx_nr = k2_ATTR["NR"]+1
  136. val2 = ""
  137. if "VALUE" in k2_ATTR:
  138. val2 = k2_ATTR["VALUE"]
  139. dmx_val=-1
  140. dmx_x=-1
  141. if dmx_nr > 0 and dmx_start > 0:
  142. try:
  143. dmx_x = dmx_start-1+dmx_nr-1
  144. dmx_val = dmx[str(univ_start)][dmx_x-1]
  145. except:pass
  146. if type(dmx_val) in [int,float]:
  147. k2_ATTR["VALUE2"] = dmx_val
  148. else:
  149. k2_ATTR["VALUE2"] = 0
  150. return y
  151. def add_dmx(data,dmx):
  152. pass
  153. table={}
  154. table_grid={}
  155. btn1_press = [] #["10.10.10.13:0"]
  156. y=[]
  157. bx_font0 = pygame.font.SysFont("freesans-bold",20)
  158. import lib.zchat as chat
  159. cmd_client = chat.Client(port=30003)
  160. while 1:
  161. fps +=1
  162. t = time.time()
  163. if t-fps_t >= 1:
  164. #print("FPS:",fps)
  165. fps_old = fps
  166. fps=0
  167. fps_t =t
  168. pygame.display.flip()
  169. pos = [160,10,70,60]
  170. rgb = (0xdd,0xdd,0xdd,0)
  171. rgb = (0xaa,0xaa,0xaa,0)
  172. window.fill((5,5,5))
  173. pygame.draw.rect(window,(0,0,0),[0,0,main_size[0],main_size[1]])
  174. fr = font22.render("FIXTURE DATA (READONLY!)" ,1, (200,200,200))
  175. window.blit(fr,(20,10 ))
  176. fr = font22.render("DEMO / TEST - MODE ! " ,1, (200,200,200))
  177. #window.blit(fr,(10,30 ))
  178. pos = [160,110,70+80,20]
  179. #pygame.draw.rect(window,rgb,pos)
  180. t=(time.time()-start)
  181. if t > 15:
  182. start = time.time()
  183. b= 80-int(t*10)
  184. pos = [160,110,70+(b),20]
  185. rgb = (0x00,0xff,0xff,0)
  186. dmx = read_dmx()
  187. rgb = (0xff,0,0xaa,0)
  188. data = read_fix(dmx)
  189. #data = add_dmx(data,dmx)
  190. i = 0
  191. r=40
  192. if 1:
  193. ch = 141
  194. send = 0
  195. #cmd="stats items"
  196. #if not y:
  197. y=data #mc.get("fix")#cmd)
  198. if y:
  199. iii = 0
  200. key=y.keys()
  201. key = list(key)
  202. key2 = []
  203. for k in key:
  204. try:
  205. key2.append(int(k))
  206. except:
  207. pass
  208. key2.sort()
  209. key = key2 #.sort()
  210. if len(btn1_press) == 0:
  211. btn1_press = [key[0]]
  212. rgb = (0x00,0,0xff,0)
  213. k2 = btn1_press[-1]
  214. fr = font22.render("SRC:"+str(k2) ,1, rgb) #(200,200,200))
  215. window.blit(fr,(400,1))
  216. fr = font22.render("FPS:"+str(fps_old) ,1, rgb) #(200,200,200))
  217. window.blit(fr,(600,1))
  218. for k in key:#y.items():
  219. k = str(k)
  220. v = y[k]
  221. attr_count = 0
  222. if "ATTRIBUT" in v:
  223. for ATTR in v["ATTRIBUT"]:
  224. if ATTR.startswith("_"):
  225. continue
  226. if ATTR.endswith("-FINE"):
  227. continue
  228. if ATTR == "DIM":
  229. continue
  230. attr_count += 1
  231. if attr_count <= 0:
  232. continue
  233. #print(k,v)
  234. x=mc.get(k)
  235. cccount = 0
  236. txt = str([k,v,ch,"=",cccount]) #x[ch-1]])
  237. rgb = (0xaa,0xaa,0xaa,0)
  238. i += 1
  239. if k not in table:
  240. bx = sdl_elm.Button(window,pos=[20,r,50,20])
  241. bx.btn1.color_on = [255,255,0]
  242. table[k] = bx
  243. bxc = sdl_elm.Button(window,pos=[-11,r,5,20])
  244. bxc.btn1.color_on = [255,255,0]
  245. table[k+"_color"] = bxc
  246. bx = table[k]
  247. bx.data = v
  248. bxc.data = {}
  249. active = 0
  250. if "ATTRIBUT" in v:
  251. if "_ACTIVE" in v["ATTRIBUT"]:
  252. if "ACTIVE" in v["ATTRIBUT"]["_ACTIVE"]:
  253. if v["ATTRIBUT"]["_ACTIVE"]["ACTIVE"] >=1:
  254. active = 1
  255. bx.btn1.val.set(active)
  256. bx.text = "ID:"+ k #str(txt) #+"\n<val>\n" #.format(i+1)
  257. bx.font0 = bx_font0 #pygame.font.SysFont("freesans-bold",20)
  258. bx.btn1.bg_on = [0,255,255]
  259. bx.btn1.type = "toggle"
  260. bx.pos = [10,r,70,20]
  261. if "ATTRIBUT" in v:
  262. bcv_r = 0
  263. bcv_g = 0
  264. bcv_b = 0
  265. if "RED" in v["ATTRIBUT"]:
  266. bcv_r = v["ATTRIBUT"]["RED"]["VALUE2"]
  267. if "GREEN" in v["ATTRIBUT"]:
  268. bcv_g = v["ATTRIBUT"]["GREEN"]["VALUE2"]
  269. if "BLUE" in v["ATTRIBUT"]:
  270. bcv_b = v["ATTRIBUT"]["BLUE"]["VALUE2"]
  271. #print("bvc_rgb" [bcv_r,bcv_g,bcv_b])
  272. if bcv_r > 255:
  273. bcv_r=255
  274. if bcv_g > 255:
  275. bcv_g=255
  276. if bcv_b > 255:
  277. bcv_b=255
  278. bxc.btn1.color = [bcv_r,bcv_g,bcv_b]
  279. bxc.btn1.color_on = [bcv_r,bcv_g,bcv_b]
  280. bxc.pos = [85,r,20,20]
  281. bxc.text = ""
  282. bx.draw()
  283. bxc.draw()
  284. r_buf=bx.get_rect()[3]+2
  285. iii += 35
  286. rr = 0
  287. if "ATTRIBUT" in v: # and 10:
  288. ATTR = v["ATTRIBUT"]
  289. for k2 in ATTR:
  290. k2_ATTR = ATTR[k2]
  291. if k2.endswith("-FINE"):
  292. continue
  293. if k2.startswith("_"):
  294. continue
  295. k3 = k+"-"+k2
  296. val2 = ""
  297. if "VALUE" in k2_ATTR:
  298. val2 = k2_ATTR["VALUE"]
  299. virt = 1
  300. if "NR" in k2_ATTR:
  301. if k2_ATTR["NR"] > 0:
  302. virt = 0
  303. dmx_val = -3
  304. if "VALUE2"in k2_ATTR:
  305. dmx_val = k2_ATTR["VALUE2"]
  306. if k3 not in table_grid:
  307. bx = sdl_elm.Button(window,pos=[300,rr,60,20])
  308. bx.btn1.color_on = [255,255,0]
  309. bx.ID = 0
  310. if "ID" in v:
  311. bx.ID = v["ID"]
  312. bx.ATTR = k2
  313. table_grid[k3] = bx
  314. if "ACTIVE" in k2_ATTR:
  315. if k2_ATTR["ACTIVE"] >=1:
  316. table_grid[k3].btn1.val.set(1)
  317. else:
  318. table_grid[k3].btn1.val.set(0)
  319. bx = table_grid[k3]
  320. bx.data = k2_ATTR
  321. try:val = v
  322. except:pass
  323. bx.text = k2 +" "+str(val2)+" "+str(dmx_val)
  324. bx.font0 = bx_font0
  325. if k2 == "RED":
  326. bx.btn4.color_on = [255,0,0]
  327. elif k2 == "GREEN":
  328. bx.btn4.color_on = [0,255,0]
  329. elif k2 == "BLUE":
  330. bx.btn4.color_on = [0,0,255]
  331. if virt == 1:
  332. bx.btn3.color= [0,0,0]
  333. bx.btn1.type = "toggle"
  334. if type(dmx_val) == int:
  335. bx.btn4.val.set(dmx_val) # "toggle"
  336. bx.pos = [100+rr,r,120,20]
  337. bx.draw()
  338. rr+=bx.get_rect()[2]+2
  339. if rr > 1000:
  340. break
  341. r += r_buf
  342. if r > 800:
  343. break
  344. if 0:#kill:
  345. #time.sleep(.13)
  346. last_k = ""
  347. for k in table:
  348. t = table[k]
  349. if t.btn1.val.get():
  350. if k not in btn1_press:
  351. btn1_press.append(k)
  352. #print(btn1_press)
  353. if len(btn1_press) > 1:
  354. for k in table:
  355. table[k].btn1.val.set(0)
  356. k = btn1_press[-1]
  357. if k in table:
  358. btn1_press = [k]
  359. try:
  360. k = btn1_press[-1]
  361. table[k].btn1.val.set(1)
  362. except:pass
  363. resize_changed = 0
  364. for event in pygame.event.get():
  365. if "scancode" in event.dict:
  366. if event.scancode == 9:
  367. for k in table:
  368. t = table[k]
  369. #t.btn2.clean()
  370. t.btn1.clean()
  371. for k in table_grid:
  372. t = table_grid[k]
  373. #t.btn2.clean()
  374. t.btn1.clean()
  375. msg=json.dumps([{"event":"CLEAR"}]).encode("utf-8")
  376. print("ESC",msg)
  377. cmd_client.send(msg)
  378. #print("event",event)
  379. if event.type == pygame.QUIT:
  380. pygame.quit()
  381. sys.exit(0)
  382. elif event.type == pygame.VIDEORESIZE:
  383. scrsize = event.size
  384. width = event.w
  385. hight = event.h
  386. resize_changed = True
  387. for t in table:
  388. #print(t)
  389. table[t].event(event)
  390. if table[t].btn3.get():
  391. data = table[t].data
  392. print("FIX:",data)
  393. for t in table_grid:
  394. #print(t)
  395. change = table_grid[t].event(event)
  396. if table_grid[t].btn3.get():
  397. data = table_grid[t].data
  398. FIX = table_grid[t].ID
  399. ATTR = table_grid[t].ATTR
  400. print("change",change)
  401. key = "MOUSE ENCODER"
  402. if key in change:
  403. if "press" in change[key]:
  404. msg = json.dumps([{"event":"FIXTURES","TYPE":"ENCODERS","FIX":str(FIX),"VAL":"++","ATTR":ATTR}]).encode("utf-8")
  405. if "release" in change[key]:
  406. msg = json.dumps([{"event":"FIXTURES","TYPE":"ENCODERS","FIX":str(FIX),"VAL":"--","ATTR":ATTR}]).encode("utf-8")
  407. print(" ",msg)
  408. cmd_client.send(msg)
  409. key = "BUTTON"
  410. if key in change:
  411. if "press" in change[key]:
  412. #print(" ATTR:",FIX,ATTR,data)
  413. #print(" CHANGE",change)
  414. msg = json.dumps([{"event":"FIXTURES","TYPE":"ENCODERS","FIX":str(FIX),"VAL":"click","ATTR":ATTR}]).encode("utf-8")
  415. print(" ",msg)
  416. cmd_client.send(msg)
  417. if "release" in change[key]:
  418. for mg in mouse_grab:
  419. FIX = mg.ID
  420. ATTR = mg.ATTR
  421. #ATTR = "ALL"
  422. msg = json.dumps([{"event":"FIXTURES","TYPE":"ENCODERS","FIX":str(FIX),"VAL":"click","ATTR":ATTR}]).encode("utf-8")
  423. print(" mouse_grab ",msg)
  424. cmd_client.send(msg)
  425. mouse_grab = []
  426. if "pos" in event.dict:
  427. if "button" in event.dict:
  428. if event.type == 5:#press
  429. mouse_down = 1
  430. mouse_pos1 = [event.pos[0],event.pos[1]]
  431. if event.type == 6:#release
  432. mouse_down = 0
  433. for btn in mouse_grab:
  434. #btn.btn2.val.set(1)
  435. btn.btn1.val.set(1)
  436. mouse_grab = []
  437. mouse_pos2 = [event.pos[0],event.pos[1]]
  438. if mouse_down:
  439. d1 = mouse_pos1[0]-mouse_pos2[0]
  440. d2 = mouse_pos1[1]-mouse_pos2[1]
  441. pix = 23
  442. #print(d1,d2)
  443. if ( d1 > pix or d1 < -pix) or ( d2 >pix or d2 < -pix):
  444. sdl_elm.draw_mouse_box(window,mouse_pos1,mouse_pos2)
  445. for k in table:
  446. t = table[k]
  447. pos = t.get_rect()
  448. mpos = [mouse_pos1[0],mouse_pos1[1],mouse_pos2[0],mouse_pos2[1]]
  449. if sdl_elm.check_area2(pos,mpos):
  450. t._set_mouse_focus(1)
  451. mouse_grab.append(t)
  452. else:
  453. t._set_mouse_focus(0)
  454. for k3 in table_grid:
  455. t = table_grid[k3]
  456. pos = t.get_rect()
  457. mpos = [mouse_pos1[0],mouse_pos1[1],mouse_pos2[0],mouse_pos2[1]]
  458. if sdl_elm.check_area2(pos,mpos):
  459. t._set_mouse_focus(1)
  460. mouse_grab.append(t)
  461. else:
  462. t._set_mouse_focus(0)
  463. if resize_changed:# = True
  464. screen = pygame.display.set_mode(scrsize,pg.RESIZABLE)
  465. clock.tick(10)