_console.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. #! /usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. This file is part of LibreLight.
  5. LibreLight is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, version 2 of the License.
  8. LibreLight is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with LibreLight. If not, see <http://www.gnu.org/licenses/>.
  14. (c) 2012 micha@uxsrv.de
  15. """
  16. import sys
  17. import time
  18. import json
  19. import zlib
  20. rnd_id = ""
  21. rnd_id += " Beta 22.02 "
  22. import subprocess
  23. rnd_id += subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('ascii').strip()
  24. if "__file__" in dir():
  25. sys.stdout.write("\x1b]2;"+str(__file__)+" "+rnd_id+"\x07") # terminal title
  26. else:
  27. sys.stdout.write("\x1b]2;"+str("__file__")+" "+rnd_if+"\x07") # terminal title
  28. __run_main = 0
  29. if __name__ == "__main__":
  30. __run_main = 1
  31. else:
  32. import __main__
  33. print(dir())
  34. if "unittest" not in dir(__main__):
  35. __run_main = 1
  36. import time
  37. import socket
  38. import struct
  39. import sys
  40. import random
  41. import math
  42. from collections import OrderedDict
  43. import lib.zchat as chat
  44. import lib.ArtNetNode as ANN
  45. import _thread as thread
  46. #thread.start_new_thread
  47. import lib.motion as motion
  48. #idmx = [0]*512 # incremental dmx
  49. dmx = [0]*512 # absolute dmx data
  50. gcolor = 1
  51. def cprint(*text,color="blue",space=" ",end="\n"):
  52. #return 0 #disable print dbg
  53. if not gcolor:
  54. print(text)
  55. return 0
  56. if color == "green":
  57. txt = '\033[92m'
  58. elif color == "red":
  59. txt = '\033[0;31m\033[1m'
  60. elif color == "yellow":
  61. txt = '\033[93m\033[1m'
  62. elif color == "cyan":
  63. txt = '\033[96m'
  64. else:
  65. txt = '\033[94m'
  66. for t in text:
  67. txt += str(t ) +" "
  68. #HEADER = '\033[95m'
  69. #OKBLUE = '\033[94m'
  70. #OKCYAN = '\033[96m'
  71. #OKGREEN = '\033[92m'
  72. #WARNING = '\033[93m'
  73. #FAIL = '\033[91m'
  74. #ENDC = '\033[0m'
  75. #BOLD = '\033[1m'
  76. #UNDERLINE = '\033[4m'
  77. txt += '\033[0m'
  78. print(txt,end=end)
  79. #return txt
  80. def artnet_loop():
  81. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=12)
  82. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=0)
  83. artnet = ANN.ArtNetNode(to="10.10.10.255",univ=0)
  84. #artnet = ANN.ArtNetNode(to="2.0.0.255",univ=0)
  85. #artnet = ANN.ArtNetNode(to="10.10.10.255",univ=1)
  86. #dmx[205] = 255 #205 BLUE
  87. artnet.dmx= dmx #[0]*512
  88. artnet.send()
  89. while 1:
  90. #artnet._test_frame()
  91. artnet.next()
  92. time.sleep(0.01)
  93. class CLOCK():
  94. def __init__(self):
  95. self.__time = 0
  96. self.__start = time.time() # only for debugging
  97. self.__tick = 0.01 # incremental timer drift's on highe cpu load ?
  98. def time(self):
  99. return self.__time
  100. def get_drift(self):
  101. run_time = time.time() - self.__start
  102. tick_time = self.__time # * self.__tick
  103. print( "runtime:{:0.2f} tick_timer:{:0.2f} drift:{:0.2f}".format(run_time,tick_time,run_time-tick_time))
  104. def loop(self):
  105. while 1:
  106. self.__time +=self.__tick
  107. #if int(self.__time*100)/10. % 10 == 0:# self.__time % 2 == 0:
  108. # print( self.get_drift())
  109. #print(self.__time)
  110. #for i in range(10):
  111. time.sleep(self.__tick)
  112. class CLOCK_REAL():
  113. def __init__(self):
  114. self.__time = 0
  115. self.__start = time.time() # only for debugging
  116. self.__tick = 0.01 # incremental timer drift's on highe cpu load ?
  117. def time(self):
  118. self.__time = time.time()
  119. return self.__time
  120. def get_drift(self):
  121. run_time = time.time() - self.__start
  122. tick_time = self.__time # * self.__tick
  123. print( "runtime:{:0.2f} tick_timer:{:0.2f} drift:{:0.2f}".format(run_time,tick_time,run_time-tick_time))
  124. def loop(self):
  125. pass
  126. #clock = CLOCK()
  127. clock = CLOCK_REAL()
  128. if __run_main:
  129. thread.start_new_thread(clock.loop,())
  130. class Fade():
  131. def __init__(self,start,target,ftime,clock,delay=0):
  132. #print("init Fade ",start,target,ftime,clock)
  133. if delay < 0:
  134. delay = 0.0001
  135. if ftime <= 0:
  136. ftime = 0.0001
  137. clock += delay
  138. self.__delay = delay
  139. self.__clock = clock
  140. self.__clock_curr = clock
  141. self.__ftime = ftime
  142. self.__start = start
  143. self.__last = start
  144. self.__target = target
  145. self.run = 1
  146. #print("INIT", str(self) )
  147. def __str__(self):
  148. return self.__repr__()
  149. def __repr__(self):
  150. return "<Fade Next:{:0.2f} Start:{:0.2f} Target:{:0.2f} T{:0.2f} Clock:{:0.2f} run:{} delay:{:0.2f}>".format(
  151. self.__last, self.__start,self.__target,self.__ftime,self.__clock_curr,self.run,self.__delay )
  152. def next(self,clock=None):
  153. if self.__ftime <= 0 and self.__delay <= 0:
  154. self.__last = self.__target
  155. self.run = 0
  156. if type(clock) is float or type(clock) is int:#not None:
  157. self.__clock_curr = clock
  158. if self.__target > self.__start:
  159. if self.__last >= self.__target:
  160. self.run = 0
  161. return self.__target
  162. else:
  163. if self.__last <= self.__target:
  164. self.run = 0
  165. return self.__target
  166. current = (self.__clock - self.__clock_curr) / self.__ftime
  167. length = self.__start - self.__target
  168. self.__last = self.__start+ length*current
  169. #if self.__last < 0:
  170. # self.__last = 0
  171. #if self.__last > 255:
  172. # self.__last = 255
  173. self.run = 1
  174. return self.__last
  175. def ctl(self,cmd="",value=None): # if x-fade cmd="%" value=50
  176. # start,stop,fwd,bwd,revers
  177. pass
  178. class _MASTER():
  179. def __init__(self,name="None"):
  180. self.__data = {}
  181. self.name = name
  182. def val(self,name,value=None):
  183. _value = 100 #%
  184. name = str(name)
  185. if name not in self.__data:
  186. self.__data[name] = 100
  187. _value = self.__data[name]
  188. if value is not None:
  189. if _value != value:
  190. print(self.name,"CHANGE MASTER",name,_value)
  191. self.__data[name] = value
  192. _value = self.__data[name]
  193. return _value /100.
  194. size_master = _MASTER("SIZE")
  195. speed_master = _MASTER("SPEED")
  196. class HTP_MASTER():
  197. """functional implementation as class for namespace encapsulation
  198. """
  199. def __init__(self):
  200. self.data = OrderedDict()
  201. #self.data[1] = {"DMX":[22,23,24],"VALUE":80}
  202. #self.data[2] = {"DMX":[42,43,44],"VALUE":70}
  203. #self.data[3] = {"DMX":[22,23,24],"VALUE":99}
  204. def _list_by_dmx(self,_dmx=0):
  205. data = OrderedDict()
  206. for i,link in self.data.items(): # enumerate(self.data):
  207. if _dmx in link["DMX"]:
  208. #print( "_list_by_dmx",i,link)
  209. data[i] = link
  210. return data
  211. def dmx_by_id(self,_id=0):
  212. #print("dmx by master-id:",_id)
  213. if _id in self.data:
  214. for i,link in self.data[_id].items():
  215. #print("dmx_by_id", i,link)
  216. return (i,link)
  217. return 0,{}
  218. def master_by_dmx(self,dmx=0):
  219. #print("master of dmx:",dmx)
  220. val=0
  221. flag = 0
  222. data = self._list_by_dmx(dmx)
  223. for i,link in data.items():
  224. #print("master_by_dmx", i,link)
  225. if link["VALUE"] > val:
  226. #print("master_by_dmx", i,link)
  227. val = link["VALUE"]
  228. flag=1
  229. if flag:
  230. return val
  231. else:
  232. return 100. # default
  233. exe_master = []
  234. exe_master.append({"SIZE":100,"SPEED":100,"id":12,"link-ids":[2]})
  235. class MASTER_FX():
  236. def __init__(self):
  237. #cprint(self,"MASTER_FX INIT !",color="green")
  238. self.__data = []
  239. self.__ok = []
  240. self.i=0
  241. self.old_offsets = []
  242. self.offsets = []
  243. self.count = -1
  244. self.init = 10
  245. def add(self,fx):
  246. if fx not in self.__data:
  247. #cprint(self,"ADD TO MASTER !",color="green")
  248. self.__data.append(fx)
  249. info = fx._get_info()
  250. #cprint(self,"ADD" ,info,color="green")
  251. offset = 0
  252. if "offset" in info:
  253. offset = info["offset"]
  254. self.old_offsets.append(offset)
  255. self.offsets.append(offset)
  256. if "xtype" in info:
  257. if info["xtype"] == "rnd":
  258. self._shuffle()
  259. #self.init += 1
  260. def _shuffle(self):
  261. #cprint(self,"REORDER RANDOM !",color="green")
  262. #self.init = 0
  263. #cprint(self.old_offsets)
  264. random.shuffle(self.old_offsets)
  265. #cprint(self.old_offsets)
  266. def _init(self):
  267. self._shuffle()
  268. #self.offsets = []
  269. for i,v in enumerate(self.old_offsets):
  270. offset = self.old_offsets[i]
  271. self.offsets[i] = offset
  272. self.init = 0
  273. def next(self,child):
  274. i = self.__data.index(child)
  275. offset = self.old_offsets[i]
  276. self.offsets[i] = offset
  277. return offset
  278. #for i,v in enumerate(self.old_offsets):
  279. # offset = self.old_offsets[i]
  280. # self.offsets[i] = offset
  281. def get(self,child,count):
  282. offset = 0
  283. if child not in self.__data:
  284. return offset
  285. if self.init:
  286. self._init()
  287. idx = self.__data.index(child)
  288. if (self.count != count and idx == 0 ) or self.init == 0:
  289. self.init = 1
  290. self._shuffle()
  291. #print( count)
  292. self.count=count
  293. idx = self.__data.index(child)
  294. offset = self.offsets[idx]
  295. return offset
  296. class FX():
  297. def __init__(self,xtype="sinus",size=10,speed=10,invert=0,width=100,start=0,offset=0,base="",clock=0,master=None,master_id=0):
  298. self.__xtype=xtype
  299. self.__size = size
  300. self.__start = start
  301. self.__master_id = master_id
  302. if width > 200:
  303. width = 200
  304. if width <= 0:
  305. width = 1
  306. self.__fade_in_master = 0
  307. self.__width = width
  308. self.__invert = invert
  309. self.__base = base
  310. self.__speed = speed
  311. self.__offset = offset
  312. self.__clock = clock
  313. self.__clock_curr = clock
  314. self.__clock_delta = 0
  315. self.__clock_old = self.__clock_curr
  316. self.out = 0
  317. self.old_v = -1
  318. self.run = 1
  319. self.count = -1
  320. self.__angel = self.__clock_curr*360%360
  321. if master is None:
  322. cprint(master, "MASTER_FX ERR",master,color="red")
  323. self.__master = MASTER_FX()
  324. self.__master.add(self)
  325. else:
  326. #cprint( "MASTER_FX OK",master,color="red")
  327. self.__master = master
  328. self.__master.add(self)
  329. if self.__xtype == "rnd":
  330. self.__offset = self.__master.get(self,-2)
  331. self.__offset = self.__master.next(self)#,count)
  332. self.next()
  333. #print("init FX",self)
  334. def _get_info(self):
  335. print(self.__offset)
  336. return {"offset":self.__offset,"xtype":self.__xtype}
  337. #return self.next(),self.__xtype, self.__size,self.__speed,self.__angel, self.__base,self.__clock_curr,self.run
  338. def __str__(self):
  339. return self.__repr__()
  340. def __repr__(self):
  341. return "<FX Next:{:0.2f} xtype:{} Size:{:0.2f} Speed:{:0.2f} ang:{:0.2f} base:{} Clock:{:0.2f} run:{}>".format(
  342. self.next(),self.__xtype, self.__size,self.__speed,self.__angel, self.__base,self.__clock_curr,self.run )
  343. def next(self,clock=None):
  344. if type(clock) is float or type(clock) is int:#not None:
  345. self.__clock_curr = clock
  346. self.__clock_delta += (self.__clock_curr - self.__clock_old) * ( speed_master.val(self.__master_id)-1)
  347. #print(self.__clock_delta )
  348. self.__clock_old = self.__clock_curr
  349. t = self.__clock_curr
  350. t += self.__clock_delta
  351. t *= self.__speed / 60
  352. t += self.__offset / 100 #255 #1024 #255
  353. t += self.__start / 1024 #255
  354. #t = t*speed_master.val(self.__master_id)
  355. tw = t%1
  356. count = t//1
  357. t = t * (100/self.__width)
  358. if tw > self.__width/100:
  359. t = 1
  360. self.__angel = t%1*360
  361. t = t%1
  362. rad = math.radians(self.__angel)
  363. v=0
  364. out = 0
  365. base = 0
  366. size = self.__size
  367. if self.__base == "+": # add
  368. base = size/2
  369. elif self.__base == "-": # sub
  370. base = size/2*-1
  371. if self.__xtype == "sinus":
  372. v = math.sin( rad )
  373. v/=2
  374. elif self.__xtype == "cosinus":
  375. v = math.cos( rad )
  376. if self.__base == "+": # add
  377. size *= -1
  378. v/=2
  379. elif self.__xtype == "rnd":
  380. #base = 0
  381. if self.__angel > 90 and self.__angel <=270:
  382. v=1
  383. else:
  384. v=0
  385. #if count != self.count and v: # % 2 == 0:#!= self.count:
  386. # #self.__offset = random.randint(0,1024)# /1024
  387. # self.__master._shuffle()
  388. if count != self.count and v == 0: # and v: # % 2 == 0:#!= self.count:
  389. self.__master.next(self)#,count)
  390. #self.__master.next(self)#,count)
  391. self.__offset = self.__master.get(self,count)
  392. base = 0
  393. if self.__base == "-": # sub
  394. if self.__invert:
  395. v = 1-v
  396. #base = -size
  397. size *=-1
  398. v *=-1
  399. elif self.__base == "+": # sub
  400. if self.__invert:
  401. v = v-1
  402. else:
  403. v = (t%1-0.5)
  404. elif self.__xtype == "on":
  405. #base = 0
  406. if self.__angel > 90 and self.__angel <=270:
  407. v=1
  408. else:
  409. v=0
  410. base = 0
  411. if self.__base == "-": # sub
  412. if self.__invert:
  413. v = 1-v
  414. #base = -size
  415. size *=-1
  416. v *=-1
  417. elif self.__base == "+": # sub
  418. if self.__invert:
  419. v = v-1
  420. else:
  421. v = (t%1-0.5)
  422. elif self.__xtype == "ramp" or self.__xtype == "ramp":
  423. v = (t%1)
  424. base = 0
  425. if self.__base == "-": # sub
  426. if self.__invert:
  427. v = 1-v
  428. #base = -size
  429. size *=-1
  430. v *=-1
  431. elif self.__base == "+": # sub
  432. if self.__invert:
  433. v = v-1
  434. else:
  435. v = (t%1-0.5)
  436. elif self.__xtype == "ramp2" or self.__xtype == "bump2":
  437. v = (t%1)
  438. v = 1-v
  439. if v == 1:
  440. v=0
  441. base = 0
  442. if self.__base == "-": # sub
  443. if self.__invert:
  444. v = 1-v
  445. #base = -size
  446. size *=-1
  447. v *=-1
  448. elif self.__base == "+": # sub
  449. if self.__invert:
  450. v = v-1
  451. else:
  452. v = (t%1-0.5)
  453. elif self.__xtype == "fade":
  454. x = t * 2
  455. if x > 1:
  456. x = 2-x
  457. x -= 0.5
  458. v = x*2
  459. #base /= 2
  460. #base *=2
  461. if self.__base == "+": # add
  462. pass#base /= 2
  463. else:
  464. v *= -1
  465. v/=2
  466. if self.__invert:
  467. v *=-1
  468. #if self.__fade_in_master < 255:
  469. # self.__fade_in_master += v*size
  470. out = v *size +base
  471. self.out = out
  472. self.count = count
  473. return out * size_master.val(self.__master_id) #* (self.__fade_in_master /255.)
  474. #= master_id
  475. class DMXCH(object):
  476. def __init__(self):
  477. self._base_value = 0
  478. self._fade = None
  479. self._fx = [None,None] # None
  480. self._fx_value = 0
  481. self._flash = None
  482. self._flash_fx = None
  483. self._flash_fx_value = 0
  484. self._last_val = None
  485. def fade(self,target,ftime=0,clock=0,delay=0):
  486. if target != self._base_value:
  487. try:
  488. target = float(target)
  489. self._fade = Fade(self._base_value,target,ftime=ftime,clock=clock,delay=delay)
  490. #self._fade.next()
  491. #self._fade.next()
  492. except Exception as e:
  493. print( "Except:fade",e,target,ftime,clock)
  494. def fx(self,xtype="sinus",size=40,speed=40,invert=0,width=100,start=0,offset=0,base="", clock=0,master=None):
  495. print([self,xtype,size,speed,start,offset,base, clock])
  496. self._fx[0] = self._fx[1]
  497. if str(xtype).lower() == "off":
  498. fx_value = self._fx_value
  499. if fx_value != 0:
  500. cprint("???????______ FX OFF AS FADE",fx_value,0,255)
  501. self._fx[1] = Fade(fx_value,0,ftime=0.5,clock=clock)
  502. else:
  503. self._fx[1] = None
  504. self._fx_value = 0
  505. else:
  506. self._fx[1] = FX(xtype=xtype,size=size,speed=speed,invert=invert,width=width,start=start,offset=offset,base=base,clock=clock,master=master,master_id=1)
  507. def flash(self,target,ftime=0,clock=0,delay=0):
  508. if str(target).lower() == "off":
  509. self._flash = None
  510. else:#elif target != self._base_value:
  511. try:
  512. target = float(target)
  513. self._flash = Fade(self._last_val,target,ftime=ftime,clock=clock,delay=delay)
  514. except Exception as e:
  515. print( "Except:flash",target,ftime,clock,__name__,e,)
  516. def flash_fx(self,xtype="sinus",size=40,speed=40,invert=0,width=100,start=0,offset=0,base="",clock=0,master=None):
  517. #if self._flash_fx is not None :
  518. # cprint("flash_fx",xtype)
  519. if str(xtype).lower() == "off":
  520. fx_value = self._fx_value
  521. #if fx_value != 0:
  522. # cprint("???????______ FX OFF AS FADE",fx_value,0,255)
  523. # self._flash_fx = Fade(fx_value,0,ftime=0.5,clock=clock)#,delay=delay)
  524. # self._flash_fx = None
  525. #else:
  526. # self._flash_fx = None
  527. # self._flash_fx_value = 0
  528. self._flash_fx = None
  529. self._flash_fx_value = 0
  530. else:
  531. self._flash_fx = FX(xtype=xtype,size=size,speed=speed,invert=invert,width=width,start=start,offset=offset,base=base,clock=clock,master=master,master_id=0)
  532. def fx_ctl(self,cmd=""):#start,stop,off
  533. pass
  534. def __str__(self):
  535. return self.__repr__()
  536. def __repr__(self):
  537. return "< DMXCH {:0.2f} > {} {}".format( self._last_val,self._fx,self._fade)
  538. def fade_ctl(self,cmd=""):#start,stop,backw,fwd,bounce
  539. pass
  540. def next(self,clock=0):
  541. value = self._base_value
  542. if self._last_val is None:
  543. self._last_val = value
  544. fx_value = self._fx_value
  545. if self._flash is not None:
  546. value = self._flash.next(clock)
  547. #flicker bug ?!
  548. value = self._flash.next(clock)
  549. fx_value = 0
  550. elif self._fade is not None:#is Fade:# is Fade:
  551. self._base_value = self._fade.next(clock)
  552. #flicker bug ?!
  553. self._base_value = self._fade.next(clock)
  554. value = self._base_value
  555. if self._flash_fx is not None:# is FX:
  556. fx_value = self._flash_fx.next(clock)
  557. else:
  558. self._fx_value = 0
  559. if self._fx[-1] is not None and self._flash is None:# is FX:
  560. self._fx_value += self._fx[-1].next(clock)
  561. #if self._fx[0] is not None and self._flash is None:# is FX:
  562. # self._fx_value += self._fx[0].next(clock)
  563. fx_value = self._fx_value
  564. self._last_val = value + fx_value
  565. return self._last_val
  566. Bdmx = []
  567. for i in range(512*3):
  568. Bdmx.append( DMXCH() )
  569. #print(type(dmx[i]))
  570. def split_cmd(data):
  571. if "cmd" in data:
  572. cmd = data["cmd"]
  573. #print("cmd",cmd)
  574. if "," in cmd:
  575. cmds = cmd.split(",")
  576. else:
  577. cmds = [cmd]
  578. return cmds
  579. class Main():
  580. def __init__(self):
  581. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=12)
  582. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=0)
  583. #artnet = ANN.ArtNetNode(to="2.0.0.255",univ=0)
  584. #artnet = ANN.ArtNetNode(to="10.10.10.255",univ=1)
  585. self.artnet = {}
  586. #self.artnet["0"] = ANN.ArtNetNode(to="10.10.10.255",univ=0)
  587. #self.artnet["0"].dmx[512-1] = 10
  588. #self.artnet["1"] = ANN.ArtNetNode(to="10.10.10.255",univ=1)
  589. #self.artnet["1"].dmx[512-1] = 11
  590. self.fx = {} # key is dmx address
  591. def loop(self):
  592. #dmx[205] = 255 #205 BLUE
  593. #self.artnet.send()
  594. xx = [0]*512
  595. #artnet = self.artnet["0"]
  596. #artnet.dmx = xx# [:] #dmx #[0]*512
  597. old_univ = -1
  598. while 1:
  599. t = clock.time()
  600. ii = 0
  601. for ii,dmxch in enumerate(Bdmx):
  602. i = ii%512
  603. univ = ii//512
  604. if str(univ) not in self.artnet:
  605. print("add uiv",univ)
  606. self.artnet[str(univ)] = ANN.ArtNetNode(to="10.10.10.255",univ=univ)
  607. self.artnet[str(univ)].dmx[512-1] = 100+univ
  608. if univ != old_univ:
  609. old_univ = univ
  610. #print("UNIV",ii/512)
  611. try:
  612. artnet.next()
  613. except:pass
  614. artnet = self.artnet[str(univ)]
  615. artnet.dmx = xx
  616. v = dmxch.next(t)
  617. if i == 0:
  618. #print(dmxch)
  619. if int(xx[i]*100) != int( v*100):
  620. #print("----v",x[i],v,t)
  621. pass
  622. #print("i:{:0.2f} xx:{:0.2f} v:{:0.2f} {:0.2f}----v {}".format(i,xx[i],v,t+100,dmxch))
  623. #print("i:{:0.2f} xx:{:0.2f} v:{:0.2f} {:0.2f}----v {}".format(i,xx[i],v,t+100,dmxch))
  624. xx[i] = int(v)
  625. try:
  626. artnet.next()
  627. except:pass
  628. time.sleep(0.01)
  629. main = Main()
  630. if __run_main:
  631. #thread.start_new_thread(artnet_loop,())
  632. thread.start_new_thread(main.loop,())
  633. def JCB(data): #json client input
  634. t_start = time.time()
  635. #jdatas = data["cmd"].split("\x00")
  636. jdatas = [data["cmd"]]
  637. c = clock.time()
  638. c = float(c)
  639. print("JCB",round(c,2))
  640. ftime = 0
  641. delay = 0
  642. for j in jdatas:
  643. master_fx = MASTER_FX()
  644. if not j:
  645. continue
  646. try:
  647. cprint("JCB::")#,j)
  648. jdata = j #jdatas[j]
  649. jtxt = jdata
  650. #jtxt = zlib.decompress(jtxt) #jtxt.decode())
  651. jtxt = str(jtxt,"UTF-8")
  652. cmds = json.loads(jtxt)
  653. for x in cmds:
  654. #cprint(int(clock.time()*1000)/1000,end=" ",color="yellow")#time.time())
  655. #cprint("json", x,type(x),color="yellow")#,cmds[x])
  656. if "CMD" in x:
  657. print("CMD:",x)
  658. if "SPEED-MASTER" == x["CMD"]:
  659. speed_master.val(x["NR"],x["VALUE"])
  660. if "SIZE-MASTER" == x["CMD"]:
  661. size_master.val(x["NR"],x["VALUE"])
  662. else:
  663. if "DMX" in x:
  664. DMX = int(x["DMX"])
  665. else:continue
  666. if DMX > 0:
  667. DMX -=1
  668. else:continue
  669. if "VALUE" in x:# and x["VALUE"] is not None:
  670. v = x["VALUE"]
  671. else:continue
  672. if "FX" in x:# and x["VALUE"] is not None:
  673. fx = x["FX"]
  674. else:fx=""
  675. if "FX2" in x:# and x["VALUE"] is not None:
  676. fx2 = x["FX2"]
  677. else:fx2={}
  678. if "FADE" in x:
  679. ftime = x["FADE"]
  680. else:ftime=0
  681. if "DELAY" in x:
  682. delay = x["DELAY"]
  683. else:delay=0
  684. if len(Bdmx) < DMX:
  685. continue
  686. if v is not None:
  687. if "FLASH" in x:
  688. #print("FLASH")
  689. Bdmx[DMX].flash(target=v,ftime=ftime, clock=c,delay=delay)
  690. else:
  691. #print("FADE")
  692. Bdmx[DMX].fade(target=v,ftime=ftime, clock=c,delay=delay)
  693. if type(fx2) is dict and fx2:
  694. #cprint("FX2",DMX,fx2,color="green")
  695. xtype="fade"
  696. size = 10
  697. speed = 10
  698. start = 0
  699. offset= 0
  700. width=100
  701. invert=0
  702. base = "-"
  703. if "TYPE" in fx2:
  704. xtype = fx2["TYPE"]
  705. if "SIZE" in fx2:
  706. size = fx2["SIZE"]
  707. if "SPEED" in fx2:
  708. speed = fx2["SPEED"]
  709. if "OFFSET" in fx2:
  710. offset = fx2["OFFSET"]
  711. if "BASE" in fx2:
  712. base = fx2["BASE"]
  713. if "INVERT" in fx2:
  714. invert = fx2["INVERT"]
  715. if "WIDTH" in fx2:
  716. width = fx2["WIDTH"]
  717. if "off" == x["VALUE"]: #fix fx flash off
  718. xtype= "off"
  719. if "alloff" == xtype.lower():
  720. for i in Bdmx:
  721. if i is not None:
  722. i.flash_fx(xtype="off",clock=c)
  723. i.fx(xtype="off",clock=c)
  724. if "FLASH" in x:
  725. Bdmx[DMX].flash_fx(xtype=xtype,size=size,speed=speed,invert=invert,width=width,start=start,offset=offset,base=base,clock=c,master=master_fx)
  726. else:
  727. Bdmx[DMX].fx(xtype=xtype,size=size,speed=speed,invert=invert,width=width,start=start,offset=offset,base=base,clock=c,master=master_fx)
  728. elif type(fx) is str and fx: # old fx like sinus:200:12:244
  729. ccm = str(DMX+1)+":"+fx
  730. print("fx",ccm)
  731. if "FLASH" in x:
  732. CB({"cmd":"fxf"+ccm})
  733. else:
  734. CB({"cmd":"fx"+ccm})
  735. cprint("{:0.04} sec.".format(time.time()-t_start),color="yellow")
  736. cprint("{:0.04} t.".format(time.time()),color="yellow")
  737. except Exception as e:
  738. cprint("EXCEPTION JCB",e,color="red")
  739. cprint("----",str(jdata)[:150],"...",color="red")
  740. cprint("Error on line {}".format(sys.exc_info()[-1].tb_lineno),color="red")
  741. cprint()
  742. cprint("{:0.04} sec.".format(time.time()-t_start),color="yellow")
  743. cprint("{:0.04} t.".format(time.time()),color="yellow")
  744. def CB(data): # raw/text client input
  745. #print("CB",data)
  746. cmds = split_cmd(data)
  747. c = clock.time()
  748. c = float(c)
  749. ftime = 0
  750. delay = 0
  751. for xcmd in cmds:
  752. if xcmd:
  753. cprint("CB",xcmd,end=" ")
  754. pass
  755. else:
  756. continue
  757. if xcmd.startswith("fxf"):
  758. xxcmd=xcmd[3:].split(":")
  759. #print("fxf:",xxcmd)
  760. if "alloff" == xxcmd[1].lower():
  761. for i in Bdmx:
  762. if i is not None:
  763. i.flash_fx(xtype="off",clock=c)
  764. l = xxcmd
  765. try:
  766. xtype=""
  767. size=40
  768. speed=100
  769. start=0
  770. offset=0
  771. base=""
  772. k=int(l[0])-1
  773. xtype=l[1]
  774. if len(l) >= 3:
  775. try:size=int(l[2])
  776. except:pass
  777. if len(l) >= 4:
  778. try:speed=int(l[3])
  779. except:pass
  780. if len(l) >= 5:
  781. try:start=int(l[4])
  782. except:pass
  783. if len(l) >= 6:
  784. try:offset=int(l[5])
  785. except:pass
  786. if len(l) >= 7:
  787. try:base=l[6]
  788. except:pass
  789. if len(Bdmx) > k:
  790. #Bdmx[k].fade(target=v,ftime=t, clock=c)
  791. Bdmx[k].flash_fx(xtype=xtype,size=size,speed=speed,start=start,offset=offset,base=base,clock=c)
  792. except Exception as e:
  793. print("EXCEPTION IN FX",e)
  794. print("Error on line {}".format(sys.exc_info()[-1].tb_lineno))
  795. elif xcmd.startswith("fx"):
  796. xxcmd=xcmd[2:].split(":")
  797. print("DMX:",xxcmd)
  798. if len(xxcmd) < 2:
  799. print("xxcmd err",xxcmd,xcmd)
  800. continue
  801. if "alloff" == xxcmd[1].lower():
  802. for i in Bdmx:
  803. i.fx(xtype="off",clock=c)
  804. l = xxcmd
  805. try:
  806. xtype=""
  807. size=40
  808. speed=100
  809. start=0
  810. offset=0
  811. base=""
  812. k=int(l[0])-1
  813. xtype=l[1]
  814. if len(l) >= 3:
  815. try:size=int(l[2])
  816. except:pass
  817. if len(l) >= 4:
  818. try:speed=int(l[3])
  819. except:pass
  820. if len(l) >= 5:
  821. try:start=int(l[4])
  822. except:pass
  823. if len(l) >= 6:
  824. try:offset=int(l[5])
  825. except:pass
  826. if len(l) >= 7:
  827. try:base=l[6]
  828. except:pass
  829. if len(Bdmx) > k:
  830. #Bdmx[k].fade(target=v,ftime=t, clock=c)
  831. Bdmx[k].fx(xtype=xtype,size=size,speed=speed,start=start,offset=offset,base=base,clock=c)
  832. except Exception as e:
  833. print("EXCEPTION IN FX",xcmd,e)
  834. print("Error on line {}".format(sys.exc_info()[-1].tb_lineno))
  835. if __run_main:
  836. #jchat = chat.CMD(CB,port=50001) # server listener
  837. #thread.start_new_thread(jchat.poll,())
  838. chat.cmd(JCB) # server listener
  839. #chat.cmd(JCB,port=50001) # server listener
  840. #input("END")