oszi_grid.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. # -*- coding: UTF-8 -*-
  2. import os
  3. import fcntl
  4. import time
  5. import socket
  6. import struct
  7. import random
  8. import json
  9. from optparse import OptionParser
  10. parser = OptionParser()
  11. parser.add_option("-r", "--recive", dest="recive",
  12. help="set recive ip like --recive 10.")
  13. parser.add_option("-s", "--sendto", dest="sendto",
  14. help="set sender ip like --sendto 2.255.255.255")
  15. parser.add_option("-t", "--test", dest="testuniv",
  16. help="set test univers like --test [0-16]")
  17. parser.add_option("", "--inmap", dest="inmap",
  18. help="set test univers like --test [0-16]")
  19. #parser.add_option("-q", "--quiet",
  20. # action="store_false", dest="verbose", default=True,
  21. # help="don't print status messages to stdout")
  22. (options, args) = parser.parse_args()
  23. print("option",options)
  24. print(options.sendto)
  25. x=0
  26. y=0
  27. running = True
  28. import pygame
  29. import pygame.gfxdraw
  30. pygame.init()
  31. #screen = pygame.display.set_mode((780, 610))
  32. screen = pygame.display.set_mode((560, 610))
  33. pygame.display.set_caption("pygame: DMX OSZI 2024-05")
  34. pygame.mouse.set_visible(1)
  35. pygame.key.set_repeat(1, 30)
  36. clock = pygame.time.Clock()
  37. pygame.init()
  38. import copy
  39. import _thread as thread
  40. import sys
  41. font = pygame.font.SysFont("FreeSans", 12) #,color=(255,0,0))
  42. def list_pygame_vars():
  43. for i in dir(pygame):
  44. if i == i.upper(): # and "MOUSE" in i:
  45. v=getattr(pygame,i)
  46. print(":",i,v)
  47. if v == 3:
  48. print(":"*20 , i)
  49. NR = 0
  50. NR2 =0
  51. def read_event():
  52. global NR,NR2
  53. running = True
  54. #list_pygame_vars()
  55. for event in pygame.event.get():
  56. if event.type == pygame.QUIT:
  57. running = False
  58. if event.type == pygame.KEYDOWN:
  59. print(event.type)
  60. if event.key == pygame.K_ESCAPE:
  61. pygame.event.post(pygame.event.Event(pygame.QUIT))
  62. try:
  63. print("read_event:",event.dict,event.type ) #event.button)
  64. if event.type in [5,pygame.MOUSEBUTTONDOWN]:
  65. if "button" in event.dict and event.dict["button"] == 3: #event.button)
  66. NR += 1
  67. if NR > 8:
  68. NR = 0
  69. if "button" in event.dict and event.dict["button"] == 1: #event.button)
  70. NR -= 1
  71. if NR < 0:
  72. NR = 8
  73. if "button" in event.dict and event.dict["button"] == 4: #event.button)
  74. NR2 += 1
  75. if NR2 > 512-1:
  76. NR2 = 0
  77. if "button" in event.dict and event.dict["button"] == 5: #event.button)
  78. NR2 -= 1
  79. if NR2 < 0:
  80. NR2 = 512-1
  81. except Exception as e:
  82. print(e)
  83. return running
  84. class Trans():
  85. def __init__(self,y=150):
  86. self.y = 150
  87. self.s = 0.25
  88. def get_y(self,y):
  89. return int((y*self.s)+(self.y*self.s))*-1
  90. T = Trans()
  91. class OSZI():
  92. def __init__(self):
  93. self._x=0
  94. self.sdata={}
  95. self.lz = 0
  96. self.c=0
  97. def draw_graph(self,screen,oszi_data,univ=0,NR2=0,label="ABCDEFGHIJK"):
  98. x=int(self._x)
  99. if time.time() > self.lz:
  100. self.lz = time.time()+7.5
  101. self._x=0
  102. x=0
  103. #rec = pygame.Rect(x+1,T.get_y(10),30,245) # clear balken
  104. #pygame.draw.rect(screen,(10,10,0),rec)
  105. T.y=-260
  106. c=0
  107. i = 0
  108. for d in oszi_data:
  109. y=d
  110. pygame.gfxdraw.pixel(screen,x,T.get_y(255),(255,0,0))
  111. pygame.gfxdraw.pixel(screen,x,T.get_y(0),(10,10,25))
  112. #rec = pygame.Rect(x+4,T.get_y(0),20,-80) # clear balken
  113. #pygame.draw.rect(screen,(c,210,110),rec)
  114. rec = pygame.Rect(x+2,T.get_y(250),20,80) # clear balken
  115. pygame.draw.rect(screen,(10,10,10),rec)
  116. rec = pygame.Rect(x+4,T.get_y(235),20,70) # clear balken
  117. pygame.draw.rect(screen,(110,110,110),rec)
  118. #text = font.render( str(y), True, (0,0,0))
  119. pygame.draw.circle(screen,(255,155,0),(x,T.get_y(y)),1)
  120. try:
  121. rec = pygame.Rect(0,T.get_y(-20) ,50,12)
  122. pygame.draw.rect(screen,(20,20,20),rec)
  123. text = font.render( "DMX:{}".format(int(NR2+i)+1), True, (255,255,255))
  124. screen.blit(text, ( 0,T.get_y(-20) ) )
  125. except:
  126. pass
  127. c+=50
  128. if c >255:
  129. c=255
  130. T.y-=275
  131. i+=1
  132. self._x+=1.5 #*2
  133. class GRID():
  134. def __init__(self):
  135. self.grid_timer = time.time()
  136. def draw_hosts(self,screen,hosts,x=0,y=0):
  137. rx = x
  138. ry = y
  139. grid_timer = self.grid_timer
  140. if grid_timer > time.time():
  141. return
  142. x2 = 350
  143. y2 = 5
  144. rec = pygame.Rect(x2,y2,200,450) # clear balken
  145. pygame.draw.rect(screen,(20,40,20),rec)
  146. for h,d in hosts.items():
  147. text = font.render( "HOST: {}".format(h), True, (255,255,255))
  148. screen.blit(text, ( x2+10, y2+10 ) )
  149. text = font.render( ": {}".format(d["uni"]), True, (255,255,255))
  150. screen.blit(text, ( x2+165, y2+10 ) )
  151. y2 += 14
  152. def draw_dmx(self,screen,dmx,highlight=0,title="title",x=0,y=250):
  153. rx = x
  154. ry = y
  155. grid_timer = self.grid_timer
  156. if grid_timer > time.time():
  157. return
  158. x2 = 310
  159. y2 = 10
  160. rec = pygame.Rect(rx,ry,600,600) # clear balken
  161. pygame.draw.rect(screen,(20,20,20),rec)
  162. text = font.render( "{}".format(title), True, (255,255,255))
  163. screen.blit(text, ( rx+20, ry+10 ) )
  164. ry+=22
  165. grid_timer=time.time()+.0215
  166. rec = pygame.Rect(rx,ry,600,600) # clear balken
  167. pygame.draw.rect(screen,(20,20,20),rec)
  168. for i,v in enumerate(dmx):
  169. fg = (255,255,255)
  170. if v > 127:
  171. fg = (0,0,0)
  172. bg = (v,v,v)
  173. pygame.draw.rect(screen,bg, (rx+9,ry+10,23,11))
  174. if i >= highlight and i <= highlight+2:
  175. pygame.draw.rect(screen,(255,0,0), (rx+9,ry+9,23,13))
  176. text = font.render( str(v).rjust(3," "), True, fg) # (255,255,255))
  177. screen.blit(text, ( rx+10, ry+10 ) )
  178. rx+=29
  179. if (i+1) % 20 == 0:
  180. rx=x
  181. ry+=12
  182. _ips = {}
  183. def print_ips(sdata):
  184. if int(time.time()*10) % 20 == 0:
  185. for k in sdata:
  186. if k in _ips:
  187. _ips[k] += 1
  188. else:
  189. _ips[k] = 0
  190. print(k.ljust(15," "),_ips[k])
  191. print()
  192. if __name__ == "__main__":
  193. grid_a = GRID()
  194. oszi_a = OSZI()
  195. import artnet_read
  196. mc = None
  197. try:
  198. import memcache
  199. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  200. except Exception as e:
  201. print("err",e)
  202. ARead = artnet_read.ArtNetRead()
  203. thread.start_new_thread(ARead.loop,())
  204. while running:
  205. sdata = ARead.get()
  206. print_ips(sdata)
  207. source="ARTNET:netns"
  208. xsdata = copy.deepcopy(sdata)
  209. for h,d in xsdata.items():
  210. xsdata[h]["uni"] = d["head"][6] // 256
  211. sdata = None
  212. if mc is not None:
  213. keys=mc.get("index")
  214. if keys:
  215. source="memcached"
  216. xsdata = {}
  217. for k in keys:
  218. tdata ={"host":k,"dmx":[2]*20,"uni":-1}
  219. uni = int(k.split(":")[-1])
  220. tdata["uni"] = uni
  221. dmx = mc.get(k)
  222. tdata["dmx"] = dmx
  223. xsdata[k] = tdata
  224. running = read_event()
  225. dmx = []
  226. oszi_data = []
  227. hosts = {}
  228. for h,xx in xsdata.items():
  229. uni = xx["uni"] #["head"][6] // 256
  230. tmp_h = {}
  231. tmp_h["uni"] = uni
  232. tmp_h["host"] = h
  233. hosts[h] = tmp_h
  234. hosts2 = {}
  235. h2 = list(hosts.keys())
  236. h2.sort()
  237. for h in h2[::-1]:
  238. hosts2[h] = hosts[h]
  239. hosts = hosts2 #[::-1]
  240. for h,xx in xsdata.items():
  241. #print(h,xx)
  242. uni = xx["uni"]
  243. if NR != uni:
  244. continue
  245. if h.startswith('2.0.0.') or h.startswith('10.10.10.'):
  246. pass
  247. else:
  248. continue
  249. dmx = xx["dmx"]
  250. if len(dmx) > NR2+2:
  251. oszi_data = [dmx[NR2],dmx[NR2+1],dmx[NR2+2]]
  252. break
  253. oszi_a.draw_graph(screen, oszi_data,NR2=NR2) #,label=_filter)
  254. grid_a.draw_hosts(screen,hosts,x=0,y=0)
  255. info ="----- HELP:CLICK MOUSE R or L, or SCROLL >> source:"+source
  256. grid_a.draw_dmx(screen,dmx,title="UNI:{:03} CH:{:03} {}".format(NR,NR2,info),highlight=NR2,y=230)
  257. pygame.display.flip()
  258. clock.tick(35)