oszi_grid.py 8.5 KB

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