ArtNetProcessor.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #from __future__ import absolute_import, division, print_function
  4. #from builtins import str, open, range, dict
  5. #from builtins import *
  6. """
  7. This file is part of librelight.
  8. librelight is free software: you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation, either version 2 of the License, or
  11. (at your option) any later version.
  12. librelight is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with librelight. If not, see <http://www.gnu.org/licenses/>.
  18. (c) 2012 micha.rathfelder@gmail.com
  19. """
  20. import sys
  21. sys.stdout.write("\x1b]2;DMX-SHEET 5\x07") # terminal title
  22. import string
  23. import time
  24. import os
  25. import json
  26. from optparse import OptionParser
  27. parser = OptionParser()
  28. parser.add_option("-r", "--recive", dest="recive",
  29. help="set recive ip")
  30. parser.add_option("-s", "--sendto", dest="sendto",
  31. help="set sender ip")
  32. parser.add_option("-t", "--test", dest="testuniv",
  33. help="set test univers ")
  34. #parser.add_option("-q", "--quiet",
  35. # action="store_false", dest="verbose", default=True,
  36. # help="don't print status messages to stdout")
  37. (options, args) = parser.parse_args()
  38. print("option",options)
  39. print(options.sendto)
  40. from collections import OrderedDict
  41. # ============================================================
  42. # Text Grafik Curses =========================================
  43. # ============================================================
  44. import curses
  45. class CursesDummy():
  46. def __init__(self):
  47. self.sel_host=Pager()
  48. self.sel_host.wrap=1
  49. self.sel_univ=Pager()
  50. self.sel_univ.wrap=1
  51. self.sel_mode=Pager()
  52. self.sel_mode.wrap=1
  53. pass
  54. def dir(self):
  55. pass
  56. def test(self):
  57. pass
  58. def init(self):
  59. pass
  60. def addstr(self,x,y,txt):
  61. pass
  62. def draw_lines(self,lines):
  63. pass
  64. def inp(self):
  65. return ""
  66. pass
  67. def read(self):
  68. pass
  69. def clear(self):
  70. pass
  71. def exit(self):
  72. pass
  73. class Manager():
  74. def __init__(self):
  75. self.myscreen = curses.initscr()
  76. print( dir(self.myscreen))
  77. print( self.myscreen.getmaxyx() )
  78. self._inp=""
  79. self.cmd = []
  80. self.mode="ltp"
  81. self.sel_host=Pager()
  82. self.sel_host.wrap=1
  83. self.sel_univ=Pager()
  84. self.sel_univ.wrap=1
  85. self.sel_mode=Pager()
  86. self.sel_mode.wrap=1
  87. self.sel_mode.data = ["ltp","dmx","mtx","main"] # mtx = matrix
  88. self.sel_mode.maxindex = len( self.sel_mode.data )-1
  89. self.ttime = time.time()
  90. self.univ2 = 0
  91. self.host =""
  92. self.ohost = HostBuffer() # as default
  93. def dir(self):
  94. return dir(self.myscreen)
  95. def test(self):
  96. self.init()
  97. #self.loop()
  98. self.draw_lines(["a","b","c"])
  99. try:
  100. time.sleep(10)
  101. finally:
  102. self.exit()
  103. def init(self):
  104. curses.savetty()
  105. curses.noecho()
  106. curses.cbreak()
  107. curses.noqiflush() #?
  108. curses.noraw() #?
  109. self.clear()
  110. curses.beep()
  111. frame = 10
  112. i = 0
  113. def addstr(self,x,y,txt):
  114. self.myscreen.addstr(x, y, txt ) #zeile,spalte,text
  115. def draw_lines(self,lines):
  116. self.clear()
  117. try:
  118. x,y= self.myscreen.getmaxyx()
  119. for i,l in enumerate(lines):
  120. #print(i,l)
  121. if i >= x-2:
  122. break
  123. self.myscreen.addstr(i+1, 1, l ) #zeile,spalte,text
  124. if i >= self.myscreen.getmaxyx()[0]-2:
  125. self.myscreen.addstr(i+1, 1, "..." ) #zeile,spalte,text
  126. self.myscreen.refresh()
  127. self.myscreen.resize(x-1,y-1) # to prevent slowdown..
  128. self.myscreen.resize(x,y)
  129. except KeyboardInterrupt as e:
  130. self.exit()
  131. print("KeyboardInterrupt")
  132. raise e
  133. #except Exception as e:
  134. # self.exit()
  135. # raise e
  136. def inp(self):
  137. x= self._inp
  138. self._inp=""
  139. return x
  140. def read(self):
  141. self.myscreen.nodelay(1)
  142. try:
  143. self._inp=self.myscreen.getkey()
  144. if not self._inp:
  145. self._inp = self.myscreen.getch()
  146. self.myscreen.addstr(0, 1, str(self._inp) ) #zeile,spalte,text
  147. self.myscreen.refresh()
  148. return self._inp
  149. except:
  150. pass#self._inp=""
  151. def clear(self):
  152. self.myscreen.clear()
  153. self.myscreen.border(0)
  154. curses.nocbreak();
  155. self.myscreen.keypad(0);
  156. #self.read()
  157. curses.echo()
  158. curses.resetty()
  159. #self.myscreen.addstr(10, 2, x ) #zeile,spalte,text
  160. def exit(self):
  161. self.clear()
  162. curses.endwin()
  163. print("ENDE",self)
  164. def keyread(self):
  165. #continue
  166. # input command buffer
  167. self.read()
  168. inp2=self.inp()
  169. if "q" == inp2:
  170. inp2=""
  171. self.exit()
  172. sys.exit()
  173. elif "?" == inp2:
  174. self.mode = "?"
  175. elif "," == inp2:
  176. self.sel_mode.next()
  177. inp2=""
  178. elif ";" == inp2:
  179. self.sel_mode.prev()
  180. inp2=""
  181. elif "." == inp2:
  182. self.sel_univ.next()
  183. inp2=""
  184. elif ":" == inp2:
  185. self.sel_univ.prev()
  186. inp2=""
  187. elif "-" == inp2:
  188. self.sel_host.next()
  189. inp2=""
  190. elif "_" == inp2:
  191. self.sel_host.prev()
  192. inp2=""
  193. elif "#" == inp2:
  194. if "main" in self.sel_mode.data:
  195. x = self.sel_mode.data.index( "main")
  196. self.sel_mode.index = x
  197. self.sel_mode.check()
  198. inp2=""
  199. if inp2 == "\n":
  200. cmd2 = "".join( self.cmd).split()
  201. self.cmd=[]
  202. if len(cmd2) < 2:
  203. pass
  204. elif "C^" in cmd2:
  205. screen.exit()
  206. sys.exit()
  207. elif "univ" in cmd2 or "u" == cmd2[0]:
  208. x=""
  209. if cmd2[1] in sel_univ.data:
  210. x = sel_univ.data.index( cmd2[1])
  211. sel_univ.index = x
  212. sel_univ.check()
  213. elif "mode" in cmd2 or "m" == cmd2[0]:
  214. if cmd2[1] in self.sel_mode.data:
  215. x = self.sel_mode.data.index( cmd2[1])
  216. self.sel_mode.index = x
  217. self.sel_mode.check()
  218. elif "host" in cmd2 or "h" == cmd2[0]:
  219. try:
  220. x=int(cmd2[1])
  221. self.sel_host.set(x)
  222. except:
  223. pass
  224. else:
  225. self.cmd.append(inp2)
  226. def loop(self):
  227. self.keyread()
  228. #print( "LOOP")
  229. host = self.sel_host.get()
  230. univ2 = self.sel_univ.get()
  231. self.mode = self.sel_mode.get()
  232. if time.time()-0.12 > self.ttime:
  233. lines = [ ]
  234. #print("cmd:",cmd)
  235. lines.append(" CMD:" + "".join(self.cmd) )
  236. if self.mode=="help" or self.mode=="?":
  237. lines.append("HILFE[h]: " )
  238. lines.append("MODE [m]: inp, in2 in1 " )
  239. lines.append("UNIV [u]: 0-16 " )
  240. lines.append(" " )
  241. lines.append("HILFE " )
  242. elif self.mode=="dmx" or self.mode == "DMX":
  243. self.ttime = time.time()
  244. dmx=self.ohost.get(host,univ=univ2)#univ=head_uni)
  245. info=self.ohost.info()
  246. #lines.append("frame "+str(info.keys()) )
  247. if univ2 in info:
  248. if host in info[univ2] :
  249. lines.append("frame "+str(info[univ2][host]["frame"]))
  250. x=""
  251. for i,v in enumerate(dmx):
  252. if v == 0:
  253. v = "+"
  254. x += str(v).rjust(4," ")
  255. if (i+1) % 20 == 0:# and i:
  256. lines.append(x)
  257. x=""
  258. if x:
  259. lines.append(x)
  260. lines.append(" ")
  261. lines.append(str(self.ttime))
  262. #screen.draw_lines(lines)
  263. elif self.mode=="mtx":
  264. self.ttime = time.time()
  265. dmx=self.ohost.get_mtx(host,univ=univ2)#univ=head_uni)
  266. info=self.ohost.info()
  267. #lines.append("frame "+str(info.keys()) )
  268. if univ2 in info:
  269. if host in info[univ2] :
  270. lines.append("frame "+str(info[univ2][host]["frame"]))
  271. x=""
  272. for i,v in enumerate(dmx):
  273. x += str(v).rjust(4," ")
  274. if (i+1) % 20 == 0:# and i:
  275. lines.append(x)
  276. x=""
  277. if x:
  278. lines.append(x)
  279. lines.append(" ")
  280. lines.append(str(self.ttime))
  281. #screen.draw_lines(lines)
  282. elif self.mode=="ltp" or self.mode=="LTP":
  283. self.ttime = time.time()
  284. dmx=self.ohost.get(univ=univ2)#head_uni)
  285. #univ2=""
  286. host=""
  287. info=self.ohost.info()
  288. lines.append("frame "+str(info.keys()) )
  289. x=""
  290. for i,v in enumerate(dmx):
  291. x += str(v).rjust(4," ")
  292. if (i+1) % 20 == 0:
  293. lines.append(x)
  294. x=""
  295. if x:
  296. lines.append(x)
  297. lines.append(" ")
  298. lines.append(str(self.ttime))
  299. #screen.draw_lines(lines)
  300. else:
  301. self.ttime = time.time()
  302. x=self.ohost.get(univ=univ2)
  303. #lines = []
  304. host=""
  305. univ2=""
  306. info=self.ohost.info()
  307. jinfo = ""
  308. for i in info:
  309. xl = json.dumps(i) + "=======X " # live
  310. lines.append( xl )
  311. for j in info[i]:
  312. lines2=[]
  313. lines.append( " " + json.dumps([j,""]) )
  314. for k in info[i][j]:
  315. if k in ["fpsx","uni","flag"]:
  316. lines2.append( " "+str(k).ljust(5," ")+": " + json.dumps( info[i][j][k]) )
  317. else:
  318. lines.append( " "+str(k).ljust(5," ")+": " + json.dumps( info[i][j][k]) )
  319. lines2 = "".join(lines2)
  320. lines.append(lines2)
  321. lines.append( " " + json.dumps([j,""]) )
  322. lines.append(" ")
  323. lines.append(str(self.ttime))
  324. #screen.draw_lines(lines)
  325. tmp = ""
  326. tmp += " mode:"+(str(self.mode).ljust(10," "))
  327. tmp += " univ:"+str(self.sel_univ.index)+":"+(str(self.sel_univ.get()).ljust(8," "))
  328. tmp += " host:"+str(self.sel_host.index)+":"+(str(self.sel_host.get()).ljust(8," "))
  329. tmp += " --recive:"+str(options.recive)
  330. tmp += " --sendto:"+str(options.sendto)
  331. lines.insert(0,tmp)
  332. tmp = ""
  333. tmp += " univ:"+ (str(self.sel_univ.data))#.ljust(20," "))
  334. tmp += " list:"+ (str(self.sel_host.data))#.ljust(20," "))
  335. lines.insert(0,tmp)
  336. self.draw_lines(lines)
  337. class UniversBuffer():
  338. def __init__(self,univers_nr=0):
  339. """buffer and merge a universe from multiple sender/hosts/ip's
  340. """
  341. self.__hosts = []
  342. self.__universes_dmx = {}
  343. self.__universes_fps = {}
  344. self.__universes_frames = {}
  345. self.__universes_flag = {}
  346. self.__universes_x_frames = {}
  347. self.__universes_x_time = {}
  348. self.__universes_count = {}
  349. self.__universes_timer = {}
  350. self.__universes_matrix = ["."]*512
  351. self.__universes_info = {}
  352. self.__univers_nr = univers_nr
  353. self.__frame = 0
  354. def _add(self,host):
  355. if host not in self.__hosts:
  356. self.__hosts.append(host) #re-order hosts list for LTP
  357. #print( "ADDING HOST:",host,"UNIV:",self.__univers_nr)
  358. self.__universes_dmx[host] = [0]*512
  359. self.__universes_frames[host] = 0
  360. self.__universes_x_frames[host] = 0
  361. self.__universes_fps[host] = [""]*20
  362. self.__universes_flag[host] = [0]*20
  363. self.__universes_x_time[host] = time.time()
  364. self.__universes_timer[host] = [0]*512
  365. self.__universes_info[host] = {}
  366. def _next_frame(self,host):
  367. self.__frame += 1
  368. self.__universes_frames[host] += 1
  369. self.__universes_x_frames[host] += 1
  370. if self.__universes_x_time[host]+10 < time.time():
  371. sec = time.time()-self.__universes_x_time[host]
  372. fps = self.__universes_x_frames[host] /sec
  373. #fps = round(fps,1)
  374. fps = int(fps)
  375. self.__universes_fps[host].pop(0)
  376. self.__universes_fps[host].append(fps)
  377. self.__universes_x_time[host] = time.time()
  378. self.__universes_x_frames[host] = 0
  379. def update(self,host,dmxframe):
  380. if type(dmxframe) != list:
  381. #print( "update ERROR dmxframe is not a list", host )
  382. return
  383. self._add(host)
  384. update_matrix = [0]*512
  385. dmx=[0]*512
  386. update_flag = 0
  387. dmxframe_old = self.__universes_dmx[host]
  388. self._next_frame(host)
  389. if len(dmxframe) <= 512: #len(dmxframe_old):
  390. for i,v in enumerate(dmxframe):
  391. if dmxframe[i] != dmxframe_old[i]:
  392. update_flag += 1
  393. self.__universes_matrix[i] = self.__hosts.index(host)
  394. dmx[i] = v
  395. self.__universes_flag[host].pop(0)
  396. self.__universes_flag[host].append( update_flag )
  397. tmp = {}
  398. tmp["flag"] =update_flag
  399. tmp["flagx"] = self.__universes_flag[host]
  400. tmp["fpsx"] = int(self.__universes_x_frames[host] / (time.time()-self.__universes_x_time[host]))
  401. tmp["frame"] = self.__frame
  402. #tmp["hosts"] = self.__hosts
  403. tmp["uni"] = self.__univers_nr
  404. tmp["fps"] = self.__universes_fps[host]
  405. self.__universes_info[host] = tmp
  406. if update_flag:
  407. #print( "UPDATE HOST:",host, update_flag,"UNIV:",self.__univers_nr)
  408. self.__universes_dmx[host] = dmx # dmxframe
  409. self.__universes_timer[host] = update_matrix
  410. def get(self,host=""):
  411. if host and host in self.__hosts:
  412. return self.__universes_dmx[host]
  413. dmx = [":"]*512
  414. for i,v in enumerate(self.__universes_matrix):
  415. if type(v) is int:
  416. host = self.__hosts[v]
  417. v = self.__universes_dmx[host][i]
  418. dmx[i] = v
  419. return dmx
  420. def get_mtx(self,host=""):
  421. return self.__universes_matrix
  422. def info(self):
  423. return self.__universes_info
  424. def hosts(self):
  425. x = self.__universes_dmx.keys()
  426. x=list(x)
  427. x.sort()
  428. return x
  429. class HostBuffer():
  430. def __init__(self):
  431. """buffer hosts and route data into universes
  432. """
  433. self.__hosts = [] # LTP order
  434. self.__universes = OrderedDict() # {} # 192.168.0.1 = [0]*512
  435. #self.update(host="localhost",univ=0,dmxframe=[6]*512)
  436. dmxframe = [0]*512
  437. dmxframe[15] = 6
  438. #self.update(host="333.333.333.333",univ=8,dmxframe=dmxframe)
  439. def get_mtx(self,host="", univ=""):
  440. return self.__universes[str(univ)].get_mtx(host)
  441. def get(self,host="", univ=""):
  442. if str(univ) in self.__universes:
  443. return self.__universes[str(univ)].get(host)
  444. else:
  445. return [-8]*512
  446. def hosts(self):
  447. hosts = []
  448. for univ in self.__universes:
  449. x=self.__universes[univ].hosts()
  450. for y in x:
  451. #x=univ.hosts()
  452. if y not in hosts:
  453. hosts.append(y)
  454. hosts.sort()
  455. return hosts
  456. def univs(self):
  457. x=self.__universes.keys()
  458. x=list(x)
  459. #x.sort()
  460. return x
  461. def update(self,host,univ, dmxframe):
  462. #print( "update", host )
  463. if str(univ) not in self.__universes:
  464. self.__universes[str(univ)] = UniversBuffer(str(univ))
  465. self.__universes[str(univ)].update(host,dmxframe)
  466. def info(self,univ=0):
  467. out = {}
  468. #print self.__universes.keys()
  469. for univ in self.__universes.keys():
  470. #print("INFO:",univ)
  471. x=self.__universes[univ]
  472. out[univ] = x.info()
  473. return out
  474. # ============================================================
  475. # Network ====================================================
  476. # ============================================================
  477. import socket, struct
  478. import fcntl #socket control
  479. import errno
  480. def toPrintable(nonprintable):
  481. out = ""
  482. for i in str(nonprintable):
  483. printable = string.ascii_letters + string.digits +"/()=?{[]}\;:,.-_ "
  484. if str(i) in printable :
  485. out += str(i)
  486. return out
  487. def unpack_art_dmx(data):
  488. dmx = []
  489. for i in range(len(data[18:]) ):
  490. x=data[18+i]
  491. #print("x",x)
  492. #print( "data",b'!B', data[18+i])
  493. #x=struct.unpack( b'!B',data[18+i])
  494. #print( "data",b'!B', data[18+i],x)
  495. #x=x[0]
  496. dmx += [x]
  497. return dmx
  498. class Socket():
  499. def __init__(self):
  500. self.__poll = 0
  501. self.__data = []
  502. self.__addr = "NONE"
  503. self.open()
  504. def open(self):
  505. try:
  506. print("connecting to ArtNet Port 6454")
  507. self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  508. self.sock.bind(('', 6454))
  509. fcntl.fcntl(self.sock, fcntl.F_SETFL, os.O_NONBLOCK)
  510. except socket.error as e:
  511. print("Socket 6454 ", "ERR: {0} ".format(e.args))
  512. #raw_input()
  513. #sys.exit()
  514. def poll(self):
  515. if not self.__poll:
  516. try:
  517. self.__data, self.__addr = self.sock.recvfrom(6454)
  518. data, addr = (self.__data,self.__addr)
  519. self.host = addr[0]
  520. head = data[:18]
  521. rawdmx = data[18:]
  522. #print([head],addr)
  523. self.univ = -1
  524. try:
  525. self.head = struct.unpack("!8sHBBBBHBB" , head )
  526. except Exception as e:
  527. pass#print( "======E09823" , e)
  528. univ = self.head[6]/255 # /512 # * 512
  529. self.univ = int(univ)
  530. if not options.recive:
  531. self.__poll = 1
  532. return 1
  533. elif self.host.startswith(options.recive):
  534. self.__poll = 1
  535. return 1
  536. else:
  537. self.__poll = 0
  538. except socket.timeout as e:
  539. err = e.args[0]
  540. if err == 'timed out':
  541. sleep(1)
  542. print('recv timed out, retry later')
  543. else:
  544. print(e)
  545. except socket.error as e:
  546. pass
  547. def recive(self):
  548. if self.__poll:
  549. self.__poll = 0
  550. data, addr = (self.__data,self.__addr)
  551. #print( self.univ,self.head)
  552. self.dmx = unpack_art_dmx(data)
  553. return { "host":self.host,"dmx":self.dmx,"univ":self.univ,"head":self.head,"data":data,"addr":addr}
  554. # ============================================================
  555. # miniartnet4.py =============================================
  556. # ============================================================
  557. import time
  558. import socket
  559. import struct
  560. import random
  561. class ArtNetNode():
  562. """simple Object to generate ArtNet Network packages
  563. works in Python2 and Python3 2021-12-05
  564. """
  565. def __init__(self, to="10.10.10.255",univ=7):
  566. try:
  567. univ = int(univ)
  568. except:
  569. print("errror univ",univ ,"is not int ... set to 7")
  570. univ = 7
  571. self.univ=univ
  572. self.sendto = to
  573. self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  574. self.s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
  575. self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  576. self.stamp = time.time()
  577. self.test_stamp = time.time()
  578. self.dmx=[33]*512
  579. self.v=0
  580. self.d=1
  581. def head(self):
  582. self._header = []
  583. self._header.append(b"Art-Net\x00") # Name, 7byte + 0x00
  584. self._header.append(struct.pack('<H', 0x5000)) # OpCode ArtDMX -> 0x5000, Low Byte first
  585. self._header.append(struct.pack('>H', 14)) # Protocol Version 14, High Byte first
  586. self._header.append(b"\x00") # Order -> nope -> 0x00
  587. self._header.append(struct.pack('B',1)) # Eternity Port
  588. # Address
  589. #if 0 <= universe <= 15 and 0 <= net <= 127 and 0 <= subnet <= 15
  590. net, subnet, universe = (0,0,self.univ) #address
  591. self._header.append(struct.pack('<H', net << 8 | subnet << 4 | universe))
  592. self._header = b"".join(self._header)
  593. def send(self,dmx=None):
  594. if dmx is None:
  595. dmx = self.dmx
  596. else:
  597. self.dmx = dmx
  598. self.head()
  599. c=[self._header]
  600. c.append( struct.pack('>H', len(dmx) ) )
  601. #print([c])
  602. dmx_count = 0
  603. for v in dmx:
  604. if type(v) is not int:
  605. v=0
  606. elif v > 255: # max dmx value 255
  607. v = 255
  608. elif v < 0: # min dmx value 0
  609. v = 0
  610. dmx_count += 1
  611. c.append(struct.pack("B",v))
  612. c = b"".join(c)
  613. self.s.sendto(c, (self.sendto, 6454))
  614. return c
  615. def _test_frame(self):
  616. if self.test_stamp+0.1 > time.time():
  617. return 0
  618. self.test_stamp = time.time()
  619. dmx = [0]*512
  620. dmx[420] = self.v
  621. self.dmx = dmx
  622. self.next()
  623. #self.send(dmx)
  624. #print( [x] )
  625. if self.v >= 255:
  626. self.d=0
  627. elif self.v <=0:
  628. self.d=1
  629. if self.d:
  630. self.v+=1
  631. else:
  632. self.v-=1
  633. #time.sleep(1/30.)
  634. def next(self):
  635. if self.stamp + (1/60) <= time.time():
  636. self.send()
  637. def artnet_test():
  638. artnet = ArtNetNode()
  639. artnet._tes_frame()
  640. # ============================================================
  641. # helper =====================================================
  642. # ============================================================
  643. class Pager(): #scroll thru list
  644. def __init__(self):
  645. self.data = []
  646. self.index = 0
  647. self.wrap = 0
  648. self.maxindex = 0
  649. def append(self,val):
  650. self.data.append(val)
  651. self.check()
  652. def set(self,nr):
  653. self.index = nr
  654. self.check()
  655. def get(self):
  656. self.check()
  657. if self.data:
  658. return self.data[self.index]
  659. def next(self):
  660. self.index += 1
  661. self.check(flag=1)
  662. def prev(self):
  663. self.index -= 1
  664. self.check(flag=1)
  665. def check(self,flag=0):
  666. if flag:
  667. if self.maxindex and self.maxindex <= len(self.data):
  668. max = self.maxindex
  669. else:
  670. max = len(self.data)
  671. else:
  672. max = len(self.data)
  673. if self.wrap:
  674. if self.index >= max:
  675. self.index = 0
  676. elif self.index < 0:
  677. self.index = max-1
  678. else:
  679. if self.index >= max:
  680. self.index = max-1
  681. elif self.index < 0:
  682. self.index = 0
  683. # ============================================================
  684. # main =======================================================
  685. # ============================================================
  686. class Main():
  687. def __init__(self):
  688. pass
  689. def loop(self):
  690. ohost = HostBuffer()
  691. screen=Manager()
  692. screen.exit()
  693. screen.ohost = ohost
  694. #artnet_out = ArtNetNode(to="10.0.25.255")
  695. artnet_out = ArtNetNode(to=options.sendto)
  696. #artnet_out._test_frame()
  697. if options.testuniv:
  698. artnet = ArtNetNode(univ=options.testuniv)
  699. artnet._test_frame()
  700. xsocket = Socket()
  701. send_time = time.time()
  702. try:
  703. while 1:
  704. if options.testuniv:
  705. artnet._test_frame()
  706. #artnet_out._test_frame()
  707. if xsocket.poll():
  708. x = xsocket.recive()
  709. ohost.update(x["host"],x["univ"],x["dmx"])
  710. screen.sel_univ.data = ohost.univs()
  711. screen.sel_host.data = ohost.hosts()
  712. if send_time +(1/30.) < time.time() and options.sendto:
  713. send_time = time.time()
  714. #x=ohost.get(univ=univ2)
  715. info=ohost.info()
  716. jinfo = ""
  717. for i in info:
  718. univ = i
  719. #print( [ univ])
  720. if str(univ) == "54":
  721. break
  722. xl = json.dumps(univ) + "======= " #XX
  723. ltp=ohost.get(univ=i)
  724. #print( xl )
  725. #print( len(ltp) ,ltp[:20])
  726. ltp[511] = int(univ)
  727. artnet_out.univ=int(univ)
  728. artnet_out.send(ltp)
  729. #for j in info[i]:
  730. # print( str(univ)+" " + json.dumps([j,""]) )
  731. # for k in info[i][j]:
  732. # print( str(univ)+ " "+str(k).ljust(5," ")+": " + json.dumps( info[i][j][k]) )
  733. screen.loop()
  734. time.sleep(.001)
  735. finally:
  736. screen.exit()
  737. #print(dir(curses))
  738. if __name__ == "__main__":
  739. main = Main()
  740. main.loop()