fix.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. table={}
  107. table_grid={}
  108. btn1_press = [] #["10.10.10.13:0"]
  109. y=[]
  110. bx_font0 = pygame.font.SysFont("freesans-bold",20)
  111. while 1:
  112. fps +=1
  113. t = time.time()
  114. if t-fps_t >= 1:
  115. #print("FPS:",fps)
  116. fps_old = fps
  117. fps=0
  118. fps_t =t
  119. pygame.display.flip()
  120. pos = [160,10,70,60]
  121. rgb = (0xdd,0xdd,0xdd,0)
  122. rgb = (0xaa,0xaa,0xaa,0)
  123. window.fill((5,5,5))
  124. pygame.draw.rect(window,(0,0,0),[0,0,main_size[0],main_size[1]])
  125. fr = font22.render("FIXTURE DATA (READONLY!)" ,1, (200,200,200))
  126. window.blit(fr,(20,10 ))
  127. fr = font22.render("DEMO / TEST - MODE ! " ,1, (200,200,200))
  128. #window.blit(fr,(10,30 ))
  129. pos = [160,110,70+80,20]
  130. #pygame.draw.rect(window,rgb,pos)
  131. t=(time.time()-start)
  132. if t > 15:
  133. start = time.time()
  134. b= 80-int(t*10)
  135. pos = [160,110,70+(b),20]
  136. rgb = (0x00,0xff,0xff,0)
  137. dmx = read_dmx()
  138. rgb = (0xff,0,0xaa,0)
  139. i = 0
  140. r=40
  141. if 1:
  142. ch = 141
  143. send = 0
  144. #cmd="stats items"
  145. #if not y:
  146. y=mc.get("fix")#cmd)
  147. if y:
  148. iii = 0
  149. key=y.keys()
  150. key = list(key)
  151. key.sort()
  152. if len(btn1_press) == 0:
  153. btn1_press = [key[0]]
  154. rgb = (0x00,0,0xff,0)
  155. k2 = btn1_press[-1]
  156. fr = font22.render("SRC:"+str(k2) ,1, rgb) #(200,200,200))
  157. window.blit(fr,(400,1))
  158. fr = font22.render("FPS:"+str(fps_old) ,1, rgb) #(200,200,200))
  159. window.blit(fr,(600,1))
  160. for k in key:#y.items():
  161. v = y[k]
  162. #print(k,v)
  163. x=mc.get(k)
  164. cccount = 0
  165. txt = str([k,v,ch,"=",cccount]) #x[ch-1]])
  166. rgb = (0xaa,0xaa,0xaa,0)
  167. fr = font22.render(str(txt) ,1, rgb) #(200,200,200))
  168. #window.blit(fr,(30,40+iii))
  169. i += 1
  170. if k not in table:
  171. bx = sdl_elm.Button(window,pos=[20,r,50,20])
  172. bx.btn1.color_on = [255,255,0]
  173. table[k] = bx
  174. bxc = sdl_elm.Button(window,pos=[-11,r,5,20])
  175. bxc.btn1.color_on = [255,255,0]
  176. table[k+"_color"] = bxc
  177. bx = table[k]
  178. bx.data = v
  179. bxc.data = {}
  180. dmx_start = 0
  181. if "DMX" in v:
  182. dmx_start = v["DMX"]
  183. univ_start = 0
  184. if "UNIVERS" in v:
  185. univ_start = v["UNIVERS"]
  186. #dmx_start += univ_start*512
  187. active = 0
  188. if "ATTRIBUT" in v:
  189. if "_ACTIVE" in v["ATTRIBUT"]:
  190. if "ACTIVE" in v["ATTRIBUT"]["_ACTIVE"]:
  191. if v["ATTRIBUT"]["_ACTIVE"]["ACTIVE"] >=1:
  192. active = 1
  193. bx.btn1.val.set(active)
  194. bx.text = "ID:"+ k #str(txt) #+"\n<val>\n" #.format(i+1)
  195. #bx.font0 = pygame.font.SysFont("freesans-bold",20)
  196. bx.font0 = bx_font0 #pygame.font.SysFont("freesans-bold",20)
  197. bx.btn1.bg_on = [0,255,255]
  198. #bx.dbg = 1
  199. bx.btn1.type = "toggle"
  200. bx.pos = [10,r,70,20]
  201. if "ATTRIBUT" in v:
  202. bcv_r = 0
  203. bcv_g = 0
  204. bcv_b = 0
  205. if "RED" in v["ATTRIBUT"]:
  206. bcv_r = v["ATTRIBUT"]["RED"]["VALUE"]
  207. if "GREEN" in v["ATTRIBUT"]:
  208. bcv_g = v["ATTRIBUT"]["GREEN"]["VALUE"]
  209. if "BLUE" in v["ATTRIBUT"]:
  210. bcv_b = v["ATTRIBUT"]["BLUE"]["VALUE"]
  211. if bcv_r > 255:
  212. bcv_r=255
  213. if bcv_g > 255:
  214. bcv_g=255
  215. if bcv_b > 255:
  216. bcv_b=255
  217. #print([bcv_r,bcv_g,bcv_b])
  218. #bxc.btn1.color = [255,0,0] #[bcv_r,bcv_g,bcv_b]
  219. bxc.btn1.color = [bcv_r,bcv_g,bcv_b]
  220. bxc.btn1.color_on = [bcv_r,bcv_g,bcv_b]
  221. #bxc.btn4.color_on = [bcv_r,bcv_g,bcv_b]#[0,0,255]
  222. bxc.pos = [85,r,20,20]
  223. bxc.text = ""
  224. bx.draw()
  225. bxc.draw()
  226. r_buf=bx.get_rect()[3]+2
  227. iii += 35
  228. rr = 0
  229. if "ATTRIBUT" in v: # and 10:
  230. ATTR = v["ATTRIBUT"]
  231. for k2 in ATTR:
  232. k2_ATTR = ATTR[k2]
  233. #if k2.endswith("_color"):
  234. # continue
  235. if k2.startswith("_"):
  236. continue
  237. k3 = k+"-"+k2
  238. dmx_nr = 0
  239. if "NR" in k2_ATTR:
  240. if k2_ATTR["NR"] >= 1:
  241. dmx_nr = k2_ATTR["NR"]+1
  242. val2 = ""
  243. if "VALUE" in k2_ATTR:
  244. val2 = k2_ATTR["VALUE"]
  245. dmx_val=0
  246. if dmx_nr > 0 and dmx_start > 0:
  247. dmx_x = dmx_start+dmx_nr
  248. dmx_val = dmx[str(univ_start)][dmx_x]
  249. #val2 += " :"+str(dmx_val)
  250. #bx.data["rDMX"] = dmx_val
  251. virt = 1
  252. if "NR" in k2_ATTR:
  253. if k2_ATTR["NR"] > 0:
  254. virt = 0
  255. if k3 not in table_grid:
  256. bx = sdl_elm.Button(window,pos=[300,rr,60,20])
  257. bx.btn1.color_on = [255,255,0]
  258. table_grid[k3] = bx
  259. if "ACTIVE" in k2_ATTR:
  260. if k2_ATTR["ACTIVE"] >=1:
  261. table_grid[k3].btn1.val.set(1)
  262. else:
  263. table_grid[k3].btn1.val.set(0)
  264. bx = table_grid[k3]
  265. bx.data = k2_ATTR
  266. try:val = v
  267. except:pass
  268. #print(k,k2,val2)
  269. #bx.text = k3 +" "+str(val2) #str(txt) #+"\n<val>\n" #.format(i+1)
  270. bx.text = k2 +" "+str(val2)+" "+str(dmx_val) #str(txt) #+"\n<val>\n" #.format(i+1)
  271. bx.font0 = bx_font0 #pygame.font.SysFont("freesans-bold",20)
  272. if k2 == "RED":
  273. bx.btn4.color_on = [255,0,0]
  274. elif k2 == "GREEN":
  275. bx.btn4.color_on = [0,255,0]
  276. elif k2 == "BLUE":
  277. bx.btn4.color_on = [0,0,255]
  278. #else:
  279. # bx.btn4.color_on = [0,255,255]
  280. if virt == 1:
  281. #bx.btn2.color= [125,120,122]
  282. #bx.btn3.color= [125,120,122]
  283. bx.btn3.color= [0,0,0]
  284. #bx.btn3.color_on= [0,120,122]
  285. #bx.dbg = 1
  286. bx.btn1.type = "toggle"
  287. #bx.btn4.val.set(val2) # "toggle"
  288. if type(dmx_val) == int:
  289. bx.btn4.val.set(dmx_val) # "toggle"
  290. bx.pos = [100+rr,r,120,20]
  291. bx.draw()
  292. rr+=bx.get_rect()[2]+2
  293. #rr += 25
  294. if rr > 1000:
  295. break
  296. r += r_buf
  297. #if r > 800:
  298. # break
  299. #print(k,v)
  300. if 0:#kill:
  301. #time.sleep(.13)
  302. last_k = ""
  303. for k in table:
  304. t = table[k]
  305. if t.btn1.val.get():
  306. if k not in btn1_press:
  307. btn1_press.append(k)
  308. #print(btn1_press)
  309. if len(btn1_press) > 1:
  310. for k in table:
  311. table[k].btn1.val.set(0)
  312. k = btn1_press[-1]
  313. if k in table:
  314. btn1_press = [k]
  315. try:
  316. k = btn1_press[-1]
  317. table[k].btn1.val.set(1)
  318. except:pass
  319. resize_changed = 0
  320. for event in pygame.event.get():
  321. if "scancode" in event.dict:
  322. if event.scancode == 9:
  323. for k in table:
  324. t = table[k]
  325. #t.btn2.clean()
  326. t.btn1.clean()
  327. for k in table_grid:
  328. t = table_grid[k]
  329. #t.btn2.clean()
  330. t.btn1.clean()
  331. print("event",event)
  332. if event.type == pygame.QUIT:
  333. pygame.quit()
  334. sys.exit(0)
  335. elif event.type == pygame.VIDEORESIZE:
  336. scrsize = event.size
  337. width = event.w
  338. hight = event.h
  339. resize_changed = True
  340. for t in table:
  341. #print(t)
  342. table[t].event(event)
  343. if table[t].btn3.get():
  344. data = table[t].data
  345. print("FIX:",data)
  346. for t in table_grid:
  347. #print(t)
  348. table_grid[t].event(event)
  349. if table_grid[t].btn3.get():
  350. data = table_grid[t].data
  351. print(" ATTR:",data)
  352. if "pos" in event.dict:
  353. if "button" in event.dict:
  354. if event.type == 5:#press
  355. mouse_down = 1
  356. mouse_pos1 = [event.pos[0],event.pos[1]]
  357. if event.type == 6:#release
  358. mouse_down = 0
  359. for btn in mouse_grab:
  360. #btn.btn2.val.set(1)
  361. btn.btn1.val.set(1)
  362. mouse_grab = []
  363. mouse_pos2 = [event.pos[0],event.pos[1]]
  364. if mouse_down:
  365. d1 = mouse_pos1[0]-mouse_pos2[0]
  366. d2 = mouse_pos1[1]-mouse_pos2[1]
  367. pix = 23
  368. #print(d1,d2)
  369. if ( d1 > pix or d1 < -pix) or ( d2 >pix or d2 < -pix):
  370. sdl_elm.draw_mouse_box(window,mouse_pos1,mouse_pos2)
  371. for k in table:
  372. t = table[k]
  373. pos = t.get_rect()
  374. mpos = [mouse_pos1[0],mouse_pos1[1],mouse_pos2[0],mouse_pos2[1]]
  375. if sdl_elm.check_area2(pos,mpos):
  376. t._set_mouse_focus(1)
  377. mouse_grab.append(t)
  378. else:
  379. t._set_mouse_focus(0)
  380. for k3 in table_grid:
  381. t = table_grid[k3]
  382. pos = t.get_rect()
  383. mpos = [mouse_pos1[0],mouse_pos1[1],mouse_pos2[0],mouse_pos2[1]]
  384. if sdl_elm.check_area2(pos,mpos):
  385. t._set_mouse_focus(1)
  386. mouse_grab.append(t)
  387. else:
  388. t._set_mouse_focus(0)
  389. if resize_changed:# = True
  390. screen = pygame.display.set_mode(scrsize,pg.RESIZABLE)
  391. clock.tick(10)