_console.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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@librelight.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. import tool.git as git
  24. rnd_id += git.get_all()
  25. if "__file__" in dir():
  26. sys.stdout.write("\x1b]2;"+str(__file__)+" "+rnd_id+"\x07") # terminal title
  27. else:
  28. sys.stdout.write("\x1b]2;"+str("__file__")+" "+rnd_if+"\x07") # terminal title
  29. __run_main = 0
  30. if __name__ == "__main__":
  31. __run_main = 1
  32. else:
  33. import __main__
  34. print(dir())
  35. if "unittest" not in dir(__main__):
  36. __run_main = 1
  37. import time
  38. import socket
  39. import struct
  40. import sys
  41. import random
  42. import math
  43. from collections import OrderedDict
  44. import lib.zchat as chat
  45. import lib.ArtNetNode as ANN
  46. import _thread as thread
  47. #thread.start_new_thread
  48. import lib.motion as motion
  49. #idmx = [0]*512 # incremental dmx
  50. dmx = [0]*512 # absolute dmx data
  51. gcolor = 1
  52. def cprint(*text,color="blue",space=" ",end="\n"):
  53. color = color.lower()
  54. #return 0 #disable print dbg
  55. if not gcolor:
  56. print(text)
  57. return 0
  58. if color == "green":
  59. txt = '\033[92m'
  60. elif color == "red":
  61. txt = '\033[0;31m\033[1m'
  62. elif color == "yellow":
  63. txt = '\033[93m\033[1m'
  64. elif color == "cyan":
  65. txt = '\033[96m'
  66. else:
  67. txt = '\033[94m'
  68. for t in text:
  69. txt += str(t ) +" "
  70. #HEADER = '\033[95m'
  71. #OKBLUE = '\033[94m'
  72. #OKCYAN = '\033[96m'
  73. #OKGREEN = '\033[92m'
  74. #WARNING = '\033[93m'
  75. #FAIL = '\033[91m'
  76. #ENDC = '\033[0m'
  77. #BOLD = '\033[1m'
  78. #UNDERLINE = '\033[4m'
  79. txt += '\033[0m'
  80. print(txt,end=end)
  81. #return txt
  82. def artnet_loop():
  83. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=12)
  84. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=0)
  85. artnet = ANN.ArtNetNode(to="10.10.10.255",univ=0)
  86. #artnet = ANN.ArtNetNode(to="2.0.0.255",univ=0)
  87. #artnet = ANN.ArtNetNode(to="10.10.10.255",univ=1)
  88. #dmx[205] = 255 #205 BLUE
  89. artnet.dmx= dmx #[0]*512
  90. artnet.send()
  91. while 1:
  92. #artnet._test_frame()
  93. artnet.next()
  94. time.sleep(0.001)
  95. class CLOCK():
  96. def __init__(self):
  97. self.__time = 0
  98. self.__start = time.time() # only for debugging
  99. self.__tick = 0.01 # incremental timer drift's on highe cpu load ?
  100. def time(self):
  101. return self.__time
  102. def get_drift(self):
  103. run_time = time.time() - self.__start
  104. tick_time = self.__time # * self.__tick
  105. print( "runtime:{:0.2f} tick_timer:{:0.2f} drift:{:0.2f}".format(run_time,tick_time,run_time-tick_time))
  106. def loop(self):
  107. while 1:
  108. self.__time +=self.__tick
  109. #if int(self.__time*100)/10. % 10 == 0:# self.__time % 2 == 0:
  110. # print( self.get_drift())
  111. #print(self.__time)
  112. #for i in range(10):
  113. time.sleep(self.__tick)
  114. class CLOCK_REAL():
  115. def __init__(self):
  116. self.__time = 0
  117. self.__start = time.time() # only for debugging
  118. self.__tick = 0.001 # incremental timer drift's on highe cpu load ?
  119. def time(self):
  120. self.__time = time.time()
  121. return self.__time
  122. def get_drift(self):
  123. run_time = time.time() - self.__start
  124. tick_time = self.__time # * self.__tick
  125. print( "runtime:{:0.2f} tick_timer:{:0.2f} drift:{:0.2f}".format(run_time,tick_time,run_time-tick_time))
  126. def loop(self):
  127. pass
  128. #clock = CLOCK()
  129. clock = CLOCK_REAL()
  130. if __run_main:
  131. thread.start_new_thread(clock.loop,())
  132. class Fade():
  133. def __init__(self,start,target,ftime,clock,delay=0):
  134. #print("init Fade ",start,target,ftime,clock)
  135. if delay < 0:
  136. delay = 0.0001
  137. if ftime <= 0:
  138. ftime = 0.0001
  139. clock += delay
  140. self.__delay = delay
  141. self.__clock = clock
  142. self.__clock_curr = clock
  143. self.__ftime = ftime
  144. self.__start = start
  145. self.__last = start
  146. self.__target = target
  147. self.abs = 0
  148. self.run = 1
  149. self.end = 0
  150. self.off = 0
  151. #print("INIT", str(self) )
  152. def __str__(self):
  153. return self.__repr__()
  154. def __repr__(self):
  155. return "<FADE Next:{:0.2f} from:{:0.2f} to:{:0.2f} ft:{:0.2f} Clock:{:0.2f} run:{} delay:{:0.2f}>".format(
  156. self.__last, self.__start,self.__target,self.__ftime,self.__clock_curr,self.run,self.__delay )
  157. def next(self,clock=None):
  158. if self.__ftime <= 0 and self.__delay <= 0:
  159. self.__last = self.__target
  160. self.end = 1
  161. self.run = 0
  162. if type(clock) is float or type(clock) is int:#not None:
  163. self.__clock_curr = clock
  164. if self.__target > self.__start:
  165. if self.__last >= self.__target:
  166. self.run = 0
  167. self.end = 1
  168. return self.__target
  169. else:
  170. if self.__last <= self.__target:
  171. self.run = 0
  172. self.end = 1
  173. return self.__target
  174. current = (self.__clock - self.__clock_curr) / self.__ftime
  175. length = self.__start - self.__target
  176. self.__last = self.__start+ length*current
  177. self.run = 1
  178. return self.__last
  179. def ctl(self,cmd="",value=None): # if x-fade cmd="%" value=50
  180. pass
  181. class _MASTER():
  182. def __init__(self,name="None"):
  183. self.__data = {}
  184. self.name = name
  185. def val(self,name,value=None):
  186. _value = 100 #%
  187. name = str(name)
  188. if name not in self.__data:
  189. self.__data[name] = 100
  190. _value = self.__data[name]
  191. if value is not None:
  192. if _value != value:
  193. print(self.name,"CHANGE MASTER:",name,"from:",_value,"to:",value)
  194. self.__data[name] = value
  195. _value = self.__data[name]
  196. return _value /100.
  197. exec_size_master = _MASTER("EXEC-SIZE")
  198. exec_speed_master = _MASTER("EXEC-SPEED")
  199. exec_offset_master = _MASTER("EXEC-OFFSET")
  200. size_master = _MASTER("SIZE")
  201. speed_master = _MASTER("SPEED")
  202. exe_master = []
  203. exe_master.append({"SIZE":100,"SPEED":100,"id":12,"link-ids":[2]})
  204. class MASTER_FX():
  205. def __init__(self):
  206. self.__data = []
  207. self.__ok = []
  208. self.i=0
  209. self.old_offsets = []
  210. self.offsets = []
  211. self.count = -1
  212. self.init = 10
  213. def add(self,fx):
  214. if fx not in self.__data:
  215. self.__data.append(fx)
  216. info = fx._get_info()
  217. offset = 0
  218. if "offset" in info:
  219. offset = info["offset"]
  220. self.old_offsets.append(offset)
  221. self.offsets.append(offset)
  222. if "xtype" in info:
  223. if info["xtype"] == "rnd":
  224. self._shuffle()
  225. def _shuffle(self):
  226. random.seed(1000)
  227. random.shuffle(self.old_offsets)
  228. def _init(self):
  229. self._shuffle()
  230. for i,v in enumerate(self.old_offsets):
  231. offset = self.old_offsets[i]
  232. self.offsets[i] = offset
  233. self.init = 0
  234. def next(self,child):
  235. i = self.__data.index(child)
  236. offset = self.old_offsets[i]
  237. self.offsets[i] = offset
  238. return offset
  239. def get(self,child,count):
  240. offset = 0
  241. if child not in self.__data:
  242. return offset
  243. if self.init:
  244. self._init()
  245. idx = self.__data.index(child)
  246. if (self.count != count and idx == 0 ) or self.init == 0:
  247. self.init = 1
  248. self._shuffle()
  249. self.count=count
  250. idx = self.__data.index(child)
  251. offset = self.offsets[idx]
  252. return offset
  253. class FX():
  254. 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):
  255. self.__xtype=xtype
  256. self.__size = size
  257. self.__start = start
  258. self.__master_id = master_id
  259. if width > 200:
  260. width = 200
  261. if width <= 0:
  262. width = 1
  263. self.__fade_in_master = 0
  264. self.__width = width
  265. self.__invert = invert
  266. self.__base = base
  267. self.__speed = speed
  268. self.__offset = offset
  269. self.__clock = clock
  270. self.__clock_curr = clock
  271. self.__clock_delta = 0
  272. self.__clock_old = self.__clock_curr
  273. self.out = 0
  274. self.old_v = -1
  275. self.run = 1
  276. self.count = -1
  277. self.abs = 0 # ABSOLUT
  278. self.__angel = self.__clock_curr*360%360
  279. if master is None:
  280. cprint(master, "MASTER_FX ERR",master,color="red")
  281. self.__master = MASTER_FX()
  282. self.__master.add(self)
  283. else:
  284. self.__master = master
  285. self.__master.add(self)
  286. if self.__xtype == "rnd":
  287. self.__offset = self.__master.get(self,-2)
  288. self.__offset = self.__master.next(self)#,count)
  289. self._exec_id = None
  290. self.next()
  291. def exec_id(self,_id=None):
  292. if type(_id) is not type(None):
  293. self._exec_id = str(_id)
  294. return self._exec_id
  295. def _get_info(self):
  296. return {"offset":self.__offset,"xtype":self.__xtype}
  297. def __str__(self):
  298. return self.__repr__()
  299. def __repr__(self):
  300. ABS = "INC"
  301. if self.abs:
  302. ABS = "ABS"
  303. return "<FX Next:{:0.2f} xtype:{} Size:{:0.2f} Speed:{:0.2f} ang:{:0.2f} base:{} Clock:{:0.2f} run:{} EXEC:{} :{}>".format(
  304. self.next(), self.__xtype, self.__size, self.__speed, self.__angel
  305. , self.__base, self.__clock_curr, self.run, self._exec_id,ABS )
  306. def _calc_fx(self,v,t,size,base):
  307. base = 0
  308. if self.__base == "-": # sub
  309. if self.__invert:
  310. v = 1-v
  311. size *=-1
  312. v *=-1
  313. elif self.__base == "+": # sub
  314. if self.__invert:
  315. v = v-1
  316. else:
  317. v = (t%1-0.5)
  318. return v
  319. def next(self,clock=None):
  320. if type(clock) is float or type(clock) is int:#not None:
  321. self.__clock_curr = clock
  322. d = (self.__clock_curr - self.__clock_old)
  323. m1 = ( speed_master.val(self.__master_id)) # global speed-master
  324. m2 = ( exec_speed_master.val(self._exec_id)) # exec start by 0
  325. shift = 0
  326. m = (m1 * m2) -1
  327. shift += d * m
  328. self.__clock_delta += shift
  329. self.__clock_old = self.__clock_curr
  330. t = self.__clock_curr
  331. t += self.__clock_delta
  332. t *= self.__speed / 60
  333. offset2 = self.__offset
  334. offset2 *= exec_offset_master.val(self._exec_id)
  335. t += offset2 / 100
  336. t += self.__start / 1024 #255
  337. tw = t%1
  338. count = t//1
  339. t = t * (100/self.__width)
  340. if tw > self.__width/100:
  341. t = 1
  342. self.__angel = t%1*360
  343. t = t%1
  344. rad = math.radians(self.__angel)
  345. self.abs = 0
  346. v=0
  347. out = 0
  348. base = 0
  349. size = self.__size
  350. if self.__base == "+": # add
  351. base = size/2
  352. elif self.__base == "-": # sub
  353. base = size/2*-1
  354. if self.__xtype == "sinus":
  355. v = math.sin( rad )
  356. v/=2
  357. elif self.__xtype == "cosinus":
  358. v = math.cos( rad )
  359. if self.__base == "+": # add
  360. size *= -1
  361. v/=2
  362. elif self.__xtype == "rnd":
  363. if self.__angel > 90 and self.__angel <=270:
  364. v=1
  365. else:
  366. v=0
  367. if count != self.count and v == 0: # and v: # % 2 == 0:#!= self.count:
  368. self.__master.next(self)#,count)
  369. self.__offset = self.__master.get(self,count)
  370. v = self._calc_fx(v,t,size,base)
  371. elif self.__xtype == "on":
  372. if self.__angel > 90 and self.__angel <=270:
  373. v=1
  374. else:
  375. v=0
  376. v = self._calc_fx(v,t,size,base)
  377. elif self.__xtype == "ramp" or self.__xtype == "bump":
  378. v = (t%1)
  379. v = self._calc_fx(v,t,size,base)
  380. elif self.__xtype == "ramp2" or self.__xtype == "bump2":
  381. v = (t%1)
  382. v = 1-v
  383. if v == 1:
  384. v=0
  385. v = self._calc_fx(v,t,size,base)
  386. elif self.__xtype == "static":
  387. self.abs = 1
  388. base = size #100
  389. v=0
  390. size=0
  391. elif self.__xtype == "fade":
  392. x = t * 2
  393. if x > 1:
  394. x = 2-x
  395. x -= 0.5
  396. v = x*2
  397. if self.__base == "+": # add
  398. pass
  399. else:
  400. v *= -1
  401. v/=2
  402. if self.__invert:
  403. v *=-1
  404. out = v *size +base
  405. self.out = out
  406. self.count = count
  407. out = out * size_master.val(self.__master_id) # master
  408. out = out * exec_size_master.val(self._exec_id) # master
  409. return out
  410. class DMXCH(object):
  411. def __init__(self,dmx=-1):
  412. self._base_value = 0
  413. self._fade = None
  414. self._fx = [None,None] # None
  415. self._fx_value = 0
  416. self._dmx = dmx
  417. self._dmx_fine = 0
  418. self._fix_id = 0
  419. self._v_master_id=0
  420. self._last_val_raw=0
  421. self._flash = None
  422. self._flash_fx = None
  423. self._flash_fx_value = 0
  424. self._last_val = None
  425. self._exec_ids = [None,None,None,None] # go, go-fx, flash, flash-fx
  426. def fade(self,target,ftime=0,clock=0,delay=0):
  427. if target != self._base_value:
  428. try:
  429. target = float(target)
  430. self._fade = Fade(self._base_value,target,ftime=ftime,clock=clock,delay=delay)
  431. except Exception as e:
  432. print( "Except:fade",e,target,ftime,clock)
  433. self.next(clock)
  434. def fx(self,xtype="sinus",size=40,speed=40,invert=0,width=100,start=0,offset=0,base="", clock=0,master=None):
  435. self._fx[0] = self._fx[1]
  436. if str(xtype).lower() == "off":
  437. fx_value = self._fx_value
  438. if fx_value != 0:
  439. cprint("???????______ FX OFF AS FADE",fx_value,0,255)
  440. if self._fx[1].abs:
  441. self._fx[1] = Fade(self._last_val_raw,0,ftime=0.5,clock=clock)
  442. else:
  443. self._fx[1] = Fade(fx_value,0,ftime=0.5,clock=clock)
  444. else:
  445. self._fx[1] = None
  446. self._fx_value = 0
  447. else:
  448. self._fx[1] = FX(xtype=xtype,size=size,speed=speed,invert=invert
  449. ,width=width,start=start,offset=offset,base=base
  450. ,clock=clock,master=master,master_id=1)
  451. self._fx[1].exec_id(self._exec_ids[1])
  452. self.next(clock)
  453. def flash(self,target,ftime=0,clock=0,delay=0):
  454. if str(target).lower() == "off":
  455. if self._flash:
  456. cur_val = self._flash.next()
  457. cur_tar = self._base_value
  458. self._flash = Fade(cur_val,cur_tar,ftime=ftime,clock=clock)
  459. self._flash.off = 1
  460. else:
  461. try:
  462. target = float(target)
  463. self._flash = Fade(self._last_val,target,ftime=ftime,clock=clock,delay=delay)
  464. self._flash = Fade(self._last_val,target,ftime=0,clock=clock,delay=delay)
  465. except Exception as e:
  466. print( "Except:flash",target,ftime,clock,__name__,e,)
  467. self.next(clock)
  468. def flash_fx(self,xtype="sinus",size=40,speed=40,invert=0,width=100,start=0,offset=0,base="",clock=0,master=None):
  469. if str(xtype).lower() == "off":
  470. fx_value = self._fx_value
  471. self._flash_fx = None
  472. self._flash_fx_value = 0
  473. else:
  474. self._flash_fx = FX(xtype=xtype,size=size,speed=speed,invert=invert
  475. ,width=width,start=start,offset=offset,base=base
  476. ,clock=clock,master=master,master_id=0)
  477. self._flash_fx.exec_id(self._exec_ids[3])
  478. self.next(clock)
  479. #print("init flash_fx",self)
  480. def fx_ctl(self,cmd=""): #start,stop,off
  481. pass
  482. def __str__(self):
  483. return self.__repr__()
  484. def exec_ids(self,_id=None):
  485. #if type(_id) is not type(None):
  486. # #self._exec_id = _id
  487. # #print("set exec_id",_id)
  488. return self._exec_ids
  489. def __repr__(self):
  490. v = self._last_val
  491. if type(self._last_val) in [int,float]:
  492. v = round(self._last_val,2)
  493. return "<BUFFER DMX:{} FINE:{} VAL:{} EXEC:{}\n fd:{}\n fx:{}\n fx_flash:{}\n".format(
  494. self._dmx,self._dmx_fine
  495. ,v,str(self._exec_ids)
  496. ,self._fade
  497. ,self._fx
  498. ,self._flash_fx)
  499. def fade_ctl(self,cmd=""): #start,stop,backw,fwd,bounce
  500. pass
  501. def next(self,clock=0):
  502. try:
  503. self._next(clock)
  504. except Exception as e:
  505. cprint("Exception DMXCH.next()" ,e)
  506. out = self._last_val
  507. return out
  508. def _next(self,clock=0):
  509. value = self._base_value
  510. if self._last_val is None:
  511. self._last_val = value
  512. fx_value = self._fx_value
  513. fx_abs = 0
  514. if self._flash is not None:
  515. value = self._flash.next(clock)
  516. value = self._flash.next(clock) #flicker bug ?!
  517. if self._flash.end == 1 and self._flash.off == 1:
  518. self._flash = None
  519. fx_value = 0
  520. elif self._fade is not None: # is Fade: # is Fade:
  521. self._base_value = self._fade.next(clock)
  522. self._base_value = self._fade.next(clock) #flicker bug ?!
  523. value = self._base_value
  524. if self._flash_fx is not None:# is FX:
  525. fx_value = self._flash_fx.next(clock)
  526. fx_abs = self._flash_fx.abs
  527. else:
  528. self._fx_value = 0
  529. if self._fx[-1] is not None and self._flash is None:# is FX:
  530. self._fx_value += self._fx[-1].next(clock)
  531. fx_abs = self._fx[-1].abs
  532. fx_value = self._fx_value
  533. if fx_abs == 1:
  534. self._last_val = fx_value
  535. else:
  536. self._last_val = value + fx_value
  537. self._last_val_raw = self._last_val
  538. if self._v_master_id in V_MASTER:
  539. vm = V_MASTER[self._v_master_id].next(clock)
  540. vm = vm/256
  541. self._last_val *= vm
  542. out = self._last_val
  543. return out
  544. V_MASTER = {} #DMXCH
  545. Bdmx = []
  546. for i in range(512*7+1):
  547. Bdmx.append( DMXCH(i) )
  548. #print(type(dmx[i]))
  549. _id = 1
  550. def split_cmd(data):
  551. if "cmd" in data:
  552. cmd = data["cmd"]
  553. #print("cmd",cmd)
  554. if "," in cmd:
  555. cmds = cmd.split(",")
  556. else:
  557. cmds = [cmd]
  558. return cmds
  559. class VDMX():
  560. """functional implementation as class for namespace encapsulation
  561. """
  562. def __init__(self):
  563. self.data = OrderedDict()
  564. self.data[4] = {"DMX":[21,22,23],"VALUE":255, "LIMIT":255} #,"DMXCH":DMXCH("V4")}
  565. for k,v in self.data.items():
  566. pass
  567. #dmxch = v["DMXCH"]
  568. #dmxch.fade(10,0)
  569. #dmxch.fx(size=200,speed=200,base="-") #self,xtype="sinus",size=40,speed=40,invert=0,width=100,start=0,offset=0,base="", clock=0,master=None):
  570. def _list_by_dmx(self,_dmx=0):
  571. data = OrderedDict()
  572. for i,link in self.data.items(): # enumerate(self.data):
  573. if _dmx in link["DMX"]:
  574. #print( "_list_by_dmx",i,link)
  575. data[i] = link
  576. return data
  577. def dmx_by_id(self,_id=0):
  578. #print("dmx by master-id:",_id)
  579. if _id in self.data:
  580. for i,link in self.data[_id].items():
  581. #print("dmx_by_id", i,link)
  582. return (i,link)
  583. return 0,{}
  584. def by_dmx(self,clock,dmx):
  585. #print("master of dmx:",dmx)
  586. val=0
  587. flag = 0
  588. data = self._list_by_dmx(dmx)
  589. for i,row in data.items():
  590. if "DMXCH" not in row:
  591. row["DMXCH"] = DMXCH("V{}".format(i))
  592. row["DMXCH"].fade(255,0)
  593. v = row["DMXCH"].next(clock)
  594. #row["DMXCH"].fade(200,20)
  595. if v >= val:
  596. val = v
  597. flag = 1
  598. out = 1.
  599. if val > 255:
  600. val = 255
  601. if flag:
  602. out = val/255.
  603. else:
  604. out = 1.
  605. return out
  606. vdmx = VDMX()
  607. class HTP_MASTER():
  608. """functional implementation as class for namespace encapsulation
  609. """
  610. def __init__(self):
  611. self.data = OrderedDict()
  612. #self.data[1] = {"DMX":[1,2,3],"VALUE":80, "LIMIT":255}
  613. #self.data[2] = {"DMX":[12,13,22],"VALUE":70, "LIMIT":255}
  614. #self.data[3] = {"DMX":[22,23,24],"VALUE":99, "LIMIT":255}
  615. self.data[4] = {"DMX":[22,23,24],"VALUE":99, "LIMIT":255,"DMXCH":DMXCH(4)}
  616. def _list_by_dmx(self,_dmx=0):
  617. data = OrderedDict()
  618. for i,link in self.data.items(): # enumerate(self.data):
  619. if _dmx in link["DMX"]:
  620. #print( "_list_by_dmx",i,link)
  621. data[i] = link
  622. return data
  623. def dmx_by_id(self,_id=0):
  624. #print("dmx by master-id:",_id)
  625. if _id in self.data:
  626. for i,link in self.data[_id].items():
  627. #print("dmx_by_id", i,link)
  628. return (i,link)
  629. return 0,{}
  630. def val_by_dmx(self,dmx=0):
  631. #print("master of dmx:",dmx)
  632. val=0
  633. flag = 0
  634. data = self._list_by_dmx(dmx)
  635. for i,link in data.items():
  636. #print("master_by_dmx", i,link)
  637. if link["VALUE"] > val:
  638. #print("master_by_dmx", i,link)
  639. val = link["VALUE"]
  640. flag=1
  641. out = 1.
  642. if flag:
  643. out = val/255.
  644. return out
  645. htp_master = HTP_MASTER()
  646. #htp_master.data[_id] = {"DMX":[1,2,3],"VALUE":80, "LIMIT":255,"DMXCH":DMXCH()}
  647. class Main():
  648. def __init__(self):
  649. self.artnet = {}
  650. self.fx = {} # key is dmx address
  651. self.lock = thread.allocate_lock()
  652. def loop(self):
  653. xx = [0]*512
  654. ii = 0
  655. old_univ = -1
  656. xx = [0]*512
  657. for ii,dmxch in enumerate(Bdmx):
  658. i = ii%512
  659. univ = ii//512
  660. if str(univ) not in self.artnet:
  661. print("add uiv",univ)
  662. self.artnet[str(univ)] = ANN.ArtNetNode(to="10.10.10.255",univ=univ)
  663. if univ != old_univ:
  664. old_univ = univ
  665. try:
  666. artnet.next()
  667. except:pass
  668. artnet = self.artnet[str(univ)]
  669. artnet.dmx = [0]*512
  670. fps_start = time.time()
  671. fps = 0
  672. dbg= 0#1
  673. while 1:
  674. self.lock.acquire_lock()
  675. start = time.time()
  676. _t=0
  677. t = clock.time()
  678. ii = 0
  679. old_univ = -1
  680. xx = [0]*512
  681. for ii,dmxch in enumerate(Bdmx):
  682. i = ii%512
  683. univ = ii//512
  684. s_univ = str(univ)
  685. if s_univ not in self.artnet:
  686. print("add uiv",univ)
  687. self.artnet[s_univ] = ANN.ArtNetNode(to="10.10.10.255",univ=univ)
  688. if dbg:
  689. end = time.time()
  690. print(" t",_t,ii,round(end-start,4))
  691. start = time.time()
  692. _t+=1
  693. old_univ = -1
  694. xx = [0]*512
  695. for ii,dmxch in enumerate(Bdmx):
  696. i = ii%512
  697. univ = ii//512
  698. if univ != old_univ:
  699. old_univ = univ
  700. artnet = self.artnet[str(univ)]
  701. xx = artnet.dmx
  702. v = dmxch.next(t)
  703. xx[i] = int(v)
  704. if dbg:
  705. end = time.time()
  706. print(" t",_t,ii,round(end-start,4))
  707. start = time.time()
  708. _t+=1
  709. old_univ = -1
  710. xx = [0]*512
  711. for ii,dmxch in enumerate(Bdmx): #fine loop
  712. dmx_fine = dmxch._dmx_fine
  713. if dmx_fine > 0:
  714. i = ii%512
  715. univ = ii//512
  716. if univ != old_univ:
  717. artnet = self.artnet[str(univ)]
  718. xx = artnet.dmx# = xx
  719. v = dmxch.next(t)
  720. vv = vdmx.by_dmx(clock=i,dmx=ii+1)
  721. try:
  722. v = v*vv # disable v-master
  723. except Exception as e:
  724. cprint("Exception v*vv",[v,vv],e)
  725. continue
  726. vf = int(v%1*255)
  727. dmx_fine = dmx_fine%512
  728. try:
  729. if v >= 255:
  730. xx[dmx_fine-1] = 255
  731. elif v < 0:
  732. xx[dmx_fine-1] = 0
  733. else:
  734. xx[dmx_fine-1] = int(v%1*255)
  735. except Exception as e:
  736. print("E dmx_fine",e,dmx_fine)
  737. if dbg:
  738. end = time.time()
  739. print(" t",_t,ii,round(end-start,4))
  740. print()
  741. start = time.time()
  742. _t+=1
  743. self.lock.release_lock()
  744. for k,artnet in self.artnet.items():
  745. artnet.next()
  746. fps += 1
  747. stop_fps = 50
  748. time.sleep(1/30)
  749. if fps >= stop_fps:
  750. fps_t = time.time()
  751. print(round(stop_fps/(fps_t-fps_start),2),"core/fps")
  752. fps = 0
  753. fps_start = time.time()
  754. main = Main()
  755. if __run_main:
  756. thread.start_new_thread(main.loop,())
  757. def _init_action(row):#Bdmx,out,DMX):
  758. Admx = row["DMXCH"]
  759. line_sub_count = 0
  760. if row["fx"]:
  761. x = row["fx"]
  762. Admx.fx(xtype=x["xtype"]
  763. ,size=x["size"]
  764. ,speed=x["speed"]
  765. ,invert=x["invert"]
  766. ,width=x["width"]
  767. ,start=x["start"]
  768. ,offset=x["offset"]
  769. ,base=x["base"]
  770. ,clock=x["clock"]
  771. ,master=x["master"])
  772. line_sub_count += 1
  773. if row["flash_fx"]:
  774. x = row["flash_fx"]
  775. Admx.flash_fx(xtype=x["xtype"]
  776. ,size=x["size"]
  777. ,speed=x["speed"]
  778. ,invert=x["invert"]
  779. ,width=x["width"]
  780. ,start=x["start"]
  781. ,offset=x["offset"]
  782. ,base=x["base"]
  783. ,clock=x["clock"]
  784. ,master=x["master"])
  785. line_sub_count += 1
  786. if row["flash"]:
  787. x = row["flash"]
  788. Admx.flash(target=x["target"]
  789. ,ftime=x["ftime"]
  790. ,clock=x["clock"]
  791. ,delay=x["delay"])
  792. line_sub_count += 1
  793. if row["fade"]:
  794. x = row["fade"]
  795. Admx.fade(target=x["target"]
  796. ,ftime=x["ftime"]
  797. ,clock=x["clock"]
  798. ,delay=x["delay"])
  799. line_sub_count += 1
  800. return line_sub_count
  801. def set_dmx_fine_ch(Admx,dmx_fine_nr):
  802. try:
  803. if int(dmx_fine_nr) > 0:
  804. Admx._dmx_fine = int(dmx_fine_nr)
  805. except Exception as e:
  806. cprint(x,color="red")
  807. cprint("except 3455",e,color="red")
  808. def _parse_cmds(cmds,clock=0,master_fx=None):
  809. c=clock
  810. out = {}
  811. for x in cmds:
  812. Admx = DMXCH() #dummy
  813. _fix_id=0
  814. _attr = ""
  815. if "CMD" in x:
  816. print("CMD:",x)
  817. if "EXEC-SPEED-MASTER" == x["CMD"]:
  818. exec_speed_master.val(x["NR"],x["VALUE"])
  819. if "EXEC-SIZE-MASTER" == x["CMD"]:
  820. exec_size_master.val(x["NR"],x["VALUE"])
  821. if "EXEC-OFFSET-MASTER" == x["CMD"]:
  822. exec_offset_master.val(x["NR"],x["VALUE"])
  823. if "SPEED-MASTER" == x["CMD"]:
  824. speed_master.val(x["NR"],x["VALUE"])
  825. if "SIZE-MASTER" == x["CMD"]:
  826. size_master.val(x["NR"],x["VALUE"])
  827. else:
  828. #print("x",x)
  829. if "DMX" in x:
  830. DMX = int(x["DMX"])
  831. else:
  832. continue
  833. if "VALUE" in x:
  834. v = x["VALUE"]
  835. else:
  836. continue
  837. _inc = 0
  838. _fix_id = 0
  839. _val = -1
  840. _clock = 0
  841. exec_id = None
  842. if "VALUE" in x:
  843. _val = x["VALUE"]
  844. if "INC" in x:
  845. _inc = x["INC"]
  846. if "FIX" in x:
  847. _fix_id = x["FIX"]
  848. if "clock" in x:
  849. _clock=x["clock"]
  850. if "ATTR" in x:
  851. _attr = x["ATTR"]
  852. if DMX <= 0: # VIRTUAL
  853. DMX = "FIX"+str(_fix_id)
  854. ok = 0
  855. if "ATTR" in x:
  856. _attr = x["ATTR"]
  857. if "DIM" == x["ATTR"]:
  858. if _fix_id not in V_MASTER:
  859. V_MASTER[_fix_id] = DMXCH()
  860. #print("_val",_val)
  861. #V_MASTER[_fix_id].fade(_val,ftime=0,clock=0,delay=0)
  862. #print(" V-MASTER",_fix_id,_val,_inc)
  863. ok = 1
  864. if _fix_id in V_MASTER:
  865. Admx = V_MASTER[_fix_id]
  866. if not ok:
  867. continue
  868. else:
  869. if DMX < len(Bdmx):
  870. Admx = Bdmx[DMX-1]
  871. else:
  872. print("DMX ADDRESS too BIG",DMX)
  873. continue
  874. if "DMX-FINE" in x and DMX > 0:
  875. set_dmx_fine_ch(Admx, x["DMX-FINE"])
  876. #print("-")
  877. if "EXEC" in x:
  878. exec_id = x["EXEC"]
  879. if "ATTR" in x:
  880. _attr = x["ATTR"]
  881. if "FIX" in x:
  882. _fix_id = x["FIX"]
  883. fx=""
  884. fx2={}
  885. ftime=0
  886. delay=0
  887. if "FX" in x:
  888. fx = x["FX"]
  889. if "FX2" in x:
  890. fx2 = x["FX2"]
  891. if "FADE" in x:
  892. ftime = x["FADE"]
  893. if "DELAY" in x:
  894. delay = x["DELAY"]
  895. #print("DO",[exec_id],x)
  896. # ids = [401,402,304,103]
  897. # exec-id, exec-fx-id, flush-id, flush-fx-id
  898. if v != "off":
  899. if "FLASH" in x:
  900. ids = Admx.exec_ids()
  901. if type(v) is int:
  902. ids[2] = exec_id
  903. if fx2:
  904. ids[3] = exec_id
  905. #print(" ",[ids, exec_id],"FL")
  906. else: # GO or ON
  907. ids = Admx.exec_ids()
  908. if type(v) is int:
  909. ids[0] = exec_id
  910. if fx2:
  911. ids[1] = exec_id
  912. #print(" ",[ids, exec_id],"GO")
  913. if v == "off":
  914. if "FLASH" in x:
  915. ids = Admx.exec_ids()
  916. stop = 0
  917. #print(" ",[ids, exec_id])
  918. if ids[2] != exec_id:
  919. stop = 1
  920. else:
  921. ids[2] = None
  922. if fx2:
  923. if ids[3] != exec_id:
  924. stop = 1
  925. else:
  926. ids[3] = None
  927. stop = 0
  928. if stop:
  929. # this FLASH cmd OFF/RELEASE is not valid anymore
  930. continue
  931. #aprint("OK")
  932. #ids = Admx.exec_ids()
  933. #print("OK ",[ids, exec_id])
  934. #Bdmx[DMX].exec_id(exec_id)
  935. out[DMX] = {"flash":{},"fade":{},"fx":{},"flash_fx":{},"fix_id":_fix_id,"attr":_attr,"DMXCH":Admx}
  936. if v is not None:
  937. if "FLASH" in x:
  938. out[DMX]["flash"] = {"target":v,"ftime":ftime, "clock":c,"delay":delay,"DMXCH":Admx}
  939. else:
  940. out[DMX]["fade"] = {"target":v,"ftime":ftime, "clock":c,"delay":delay,"DMXCH":Admx}
  941. if type(fx2) is dict and fx2:
  942. xtype="fade"
  943. size = 10
  944. speed = 10
  945. start = 0
  946. offset= 0
  947. width=100
  948. invert=0
  949. base = "-"
  950. if "TYPE" in fx2:
  951. xtype = fx2["TYPE"]
  952. if "SIZE" in fx2:
  953. size = fx2["SIZE"]
  954. if "SPEED" in fx2:
  955. speed = fx2["SPEED"]
  956. if "OFFSET" in fx2:
  957. offset = fx2["OFFSET"]
  958. if "BASE" in fx2:
  959. base = fx2["BASE"]
  960. if "INVERT" in fx2:
  961. invert = fx2["INVERT"]
  962. if "WIDTH" in fx2:
  963. width = fx2["WIDTH"]
  964. if "off" == x["VALUE"]: #fix fx flash off
  965. xtype= "off"
  966. if "alloff" == xtype.lower():
  967. for dmxch in Bdmx:
  968. if dmxch is not None:
  969. dmxch.flash_fx(xtype="off",clock=c)
  970. dmxch.fx(xtype="off",clock=c)
  971. for j in V_MASTER:
  972. dmxch = V_MASTER[j]
  973. if j is not None:
  974. dmxch.flash_fx(xtype="off",clock=c)
  975. dmxch.fx(xtype="off",clock=c)
  976. if "FLASH" in x:
  977. out[DMX]["flash_fx"] = {"xtype":xtype,"size":size,"speed":speed,
  978. "invert":invert,"width":width,"start":start
  979. ,"offset":offset,"base":base,"clock":c,"master":master_fx}
  980. else:
  981. out[DMX]["fx"] = {"xtype":xtype,"size":size,"speed":speed
  982. ,"invert":invert,"width":width,"start":start
  983. ,"offset":offset,"base":base,"clock":c,"master":master_fx}
  984. elif type(fx) is str and fx:
  985. # old fx like sinus:200:12:244
  986. ccm = str(DMX+1)+":"+fx
  987. print("fx",ccm)
  988. if "FLASH" in x:
  989. pass#CB({"cmd":"fxf"+ccm})
  990. else:
  991. pass#CB({"cmd":"fx"+ccm})
  992. return out
  993. import hashlib
  994. JCB_GLOB_BUF = {}
  995. def JCB(data,sock=None): #json client input
  996. t_start = time.time()
  997. s = time.time()
  998. e = time.time()
  999. ct = int(e*100)/100
  1000. print()
  1001. msg = "{} JCB START: {:0.02f} sizeof:{}"
  1002. msg = msg.format(ct,e-s,sys.getsizeof(data) )
  1003. print(msg)
  1004. jdatas = []
  1005. l2 = 0
  1006. for line in data:
  1007. data2 = json.loads(line)
  1008. l2 += len(data2)
  1009. #print("line:",line)
  1010. jdatas.append(data2) #["CMD"])
  1011. print("INPUT JCB =>",len(data),":",l2)
  1012. c = clock.time()
  1013. c = float(c)
  1014. ftime = 0
  1015. delay = 0
  1016. out = {}
  1017. line=""
  1018. for cmds in jdatas:
  1019. for line in cmds: # run first
  1020. #print(line)
  1021. if "FLASH" in line:
  1022. cprint("FLUSH",end=" ",color="CYAN")
  1023. if "VALUE" in line:
  1024. if line["VALUE"] == "off":
  1025. cprint("OFF",end=" ",color="red")
  1026. else:
  1027. cprint("ON",end=" ",color="green")
  1028. print("")
  1029. else:
  1030. cprint("FADE",color="CYAN")
  1031. break
  1032. for cmds in jdatas:
  1033. for line in cmds:
  1034. if "time" in line:
  1035. jt_start = line["time"]
  1036. latenz = round(t_start-jt_start,4)
  1037. if latenz > 0.5:
  1038. cprint("latenz 0.5 >",latenz,color="red")
  1039. break
  1040. for cmds in jdatas:
  1041. #line = json.dumps(cmds)
  1042. #md5 = hashlib.md5.hexdigest(line)
  1043. master_fx = MASTER_FX()
  1044. if not cmds:
  1045. continue
  1046. try:
  1047. out = _parse_cmds(cmds,clock=c,master_fx=master_fx)
  1048. #cprint("-","{:0.04} sec.".format(time.time()-t_start),color="yellow")
  1049. # ------- ----------------------------------------------------
  1050. except Exception as e:
  1051. cprint("EXCEPTION JCB",e,color="red")
  1052. cprint("----",str(cmds)[:150],"...",color="red")
  1053. cprint("Error on line {}".format(sys.exc_info()[-1].tb_lineno),color="red")
  1054. raise e
  1055. line_count = 0
  1056. line_count_sub = 0
  1057. line_size = 0
  1058. attr_count = {}
  1059. attr_count2 = 0
  1060. if out:
  1061. try:
  1062. try: # second loop to sync-start all dmxch's
  1063. main.lock.acquire_lock()
  1064. for _id in out:
  1065. row = out[_id]
  1066. #print(row)
  1067. line_size += sys.getsizeof(row)
  1068. #print("_id",_id)
  1069. Admx = row["DMXCH"]
  1070. #print("Admx",Admx)
  1071. if "attr" in row:
  1072. if row["attr"] not in attr_count:
  1073. attr_count[row["attr"]] = 0
  1074. attr_count[row["attr"]] += 1
  1075. attr_count2 +=1
  1076. if row["fix_id"]:
  1077. _fix_id = row["fix_id"]
  1078. Admx._fix_id = _fix_id
  1079. if "attr" in row:
  1080. if row["attr"] in ["RED","GREEN","BLUE","WHITE","AMBER"]: #CYAN,MAGENTA,YELLOW
  1081. Admx._v_master_id = _fix_id
  1082. #print("SET V_MASTER",row)
  1083. line_count_sub += _init_action(row)
  1084. line_count += 1
  1085. e = time.time()
  1086. print(" sub-JCB TIME:","{:0.02f}".format(e-s),int(e*100)/100)
  1087. finally:
  1088. main.lock.release_lock()
  1089. #time.sleep(1/30)
  1090. except Exception as e:
  1091. cprint("EXCEPTION JCB",e,color="red")
  1092. cprint("----",str(cmds)[:150],"...",color="red")
  1093. cprint("Error on line {}".format(sys.exc_info()[-1].tb_lineno),color="red")
  1094. raise e
  1095. #cprint(" ","{:0.04} sec.".format(time.time()-t_start),color="yellow")
  1096. print("attr_count:",attr_count)
  1097. #print(line_count,line_size)
  1098. e = time.time()
  1099. ct = int(e*100)/100
  1100. msg = "{} JCB: END {:0.02f} sizeof:{} fix-count:{} attr-count:{}"
  1101. msg = msg.format(ct,e-s,line_size,line_count,line_count_sub )
  1102. print(msg)
  1103. time.sleep(1/60)
  1104. if __run_main:
  1105. s = chat.Server(cb=JCB)
  1106. while 1:
  1107. s.poll()
  1108. time.sleep(0.001)