_console.py 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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. exe_master = []
  197. exe_master.append({"SIZE":100,"SPEED":100,"id":12,"link-ids":[2]})
  198. class MASTER_FX():
  199. def __init__(self):
  200. #cprint(self,"MASTER_FX INIT !",color="green")
  201. self.__data = []
  202. self.__ok = []
  203. self.i=0
  204. self.old_offsets = []
  205. self.offsets = []
  206. self.count = -1
  207. self.init = 10
  208. def add(self,fx):
  209. if fx not in self.__data:
  210. #cprint(self,"ADD TO MASTER !",color="green")
  211. self.__data.append(fx)
  212. info = fx._get_info()
  213. #cprint(self,"ADD" ,info,color="green")
  214. offset = 0
  215. if "offset" in info:
  216. offset = info["offset"]
  217. self.old_offsets.append(offset)
  218. self.offsets.append(offset)
  219. if "xtype" in info:
  220. if info["xtype"] == "rnd":
  221. self._shuffle()
  222. #self.init += 1
  223. def _shuffle(self):
  224. #cprint(self,"REORDER RANDOM !",color="green")
  225. #self.init = 0
  226. #cprint(self.old_offsets)
  227. random.shuffle(self.old_offsets)
  228. #cprint(self.old_offsets)
  229. def _init(self):
  230. self._shuffle()
  231. #self.offsets = []
  232. for i,v in enumerate(self.old_offsets):
  233. offset = self.old_offsets[i]
  234. self.offsets[i] = offset
  235. self.init = 0
  236. def next(self,child):
  237. i = self.__data.index(child)
  238. offset = self.old_offsets[i]
  239. self.offsets[i] = offset
  240. return offset
  241. #for i,v in enumerate(self.old_offsets):
  242. # offset = self.old_offsets[i]
  243. # self.offsets[i] = offset
  244. def get(self,child,count):
  245. offset = 0
  246. if child not in self.__data:
  247. return offset
  248. if self.init:
  249. self._init()
  250. idx = self.__data.index(child)
  251. if (self.count != count and idx == 0 ) or self.init == 0:
  252. self.init = 1
  253. self._shuffle()
  254. #print( count)
  255. self.count=count
  256. idx = self.__data.index(child)
  257. offset = self.offsets[idx]
  258. return offset
  259. class FX():
  260. 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):
  261. self.__xtype=xtype
  262. self.__size = size
  263. self.__start = start
  264. self.__master_id = master_id
  265. if width > 200:
  266. width = 200
  267. if width <= 0:
  268. width = 1
  269. self.__fade_in_master = 0
  270. self.__width = width
  271. self.__invert = invert
  272. self.__base = base
  273. self.__speed = speed
  274. self.__offset = offset
  275. self.__clock = clock
  276. self.__clock_curr = clock
  277. self.__clock_delta = 0
  278. self.__clock_old = self.__clock_curr
  279. self.out = 0
  280. self.old_v = -1
  281. self.run = 1
  282. self.count = -1
  283. self.__angel = self.__clock_curr*360%360
  284. if master is None:
  285. cprint(master, "MASTER_FX ERR",master,color="red")
  286. self.__master = MASTER_FX()
  287. self.__master.add(self)
  288. else:
  289. #cprint( "MASTER_FX OK",master,color="red")
  290. self.__master = master
  291. self.__master.add(self)
  292. if self.__xtype == "rnd":
  293. self.__offset = self.__master.get(self,-2)
  294. self.__offset = self.__master.next(self)#,count)
  295. self.next()
  296. #print("init FX",self)
  297. def _get_info(self):
  298. print(self.__offset)
  299. return {"offset":self.__offset,"xtype":self.__xtype}
  300. #return self.next(),self.__xtype, self.__size,self.__speed,self.__angel, self.__base,self.__clock_curr,self.run
  301. def __str__(self):
  302. return self.__repr__()
  303. def __repr__(self):
  304. return "<FX Next:{:0.2f} xtype:{} Size:{:0.2f} Speed:{:0.2f} ang:{:0.2f} base:{} Clock:{:0.2f} run:{}>".format(
  305. self.next(),self.__xtype, self.__size,self.__speed,self.__angel, self.__base,self.__clock_curr,self.run )
  306. def next(self,clock=None):
  307. if type(clock) is float or type(clock) is int:#not None:
  308. self.__clock_curr = clock
  309. self.__clock_delta += (self.__clock_curr - self.__clock_old) * ( speed_master.val(self.__master_id)-1)
  310. #print(self.__clock_delta )
  311. self.__clock_old = self.__clock_curr
  312. t = self.__clock_curr
  313. t += self.__clock_delta
  314. t *= self.__speed / 60
  315. t += self.__offset / 100 #255 #1024 #255
  316. t += self.__start / 1024 #255
  317. #t = t*speed_master.val(self.__master_id)
  318. tw = t%1
  319. count = t//1
  320. t = t * (100/self.__width)
  321. if tw > self.__width/100:
  322. t = 1
  323. self.__angel = t%1*360
  324. t = t%1
  325. rad = math.radians(self.__angel)
  326. v=0
  327. out = 0
  328. base = 0
  329. size = self.__size
  330. if self.__base == "+": # add
  331. base = size/2
  332. elif self.__base == "-": # sub
  333. base = size/2*-1
  334. if self.__xtype == "sinus":
  335. v = math.sin( rad )
  336. v/=2
  337. elif self.__xtype == "cosinus":
  338. v = math.cos( rad )
  339. if self.__base == "+": # add
  340. size *= -1
  341. v/=2
  342. elif self.__xtype == "rnd":
  343. #base = 0
  344. if self.__angel > 90 and self.__angel <=270:
  345. v=1
  346. else:
  347. v=0
  348. #if count != self.count and v: # % 2 == 0:#!= self.count:
  349. # #self.__offset = random.randint(0,1024)# /1024
  350. # self.__master._shuffle()
  351. if count != self.count and v == 0: # and v: # % 2 == 0:#!= self.count:
  352. self.__master.next(self)#,count)
  353. #self.__master.next(self)#,count)
  354. self.__offset = self.__master.get(self,count)
  355. base = 0
  356. if self.__base == "-": # sub
  357. if self.__invert:
  358. v = 1-v
  359. #base = -size
  360. size *=-1
  361. v *=-1
  362. elif self.__base == "+": # sub
  363. if self.__invert:
  364. v = v-1
  365. else:
  366. v = (t%1-0.5)
  367. elif self.__xtype == "on":
  368. #base = 0
  369. if self.__angel > 90 and self.__angel <=270:
  370. v=1
  371. else:
  372. v=0
  373. base = 0
  374. if self.__base == "-": # sub
  375. if self.__invert:
  376. v = 1-v
  377. #base = -size
  378. size *=-1
  379. v *=-1
  380. elif self.__base == "+": # sub
  381. if self.__invert:
  382. v = v-1
  383. else:
  384. v = (t%1-0.5)
  385. elif self.__xtype == "ramp" or self.__xtype == "ramp":
  386. v = (t%1)
  387. base = 0
  388. if self.__base == "-": # sub
  389. if self.__invert:
  390. v = 1-v
  391. #base = -size
  392. size *=-1
  393. v *=-1
  394. elif self.__base == "+": # sub
  395. if self.__invert:
  396. v = v-1
  397. else:
  398. v = (t%1-0.5)
  399. elif self.__xtype == "ramp2" or self.__xtype == "bump2":
  400. v = (t%1)
  401. v = 1-v
  402. if v == 1:
  403. v=0
  404. base = 0
  405. if self.__base == "-": # sub
  406. if self.__invert:
  407. v = 1-v
  408. #base = -size
  409. size *=-1
  410. v *=-1
  411. elif self.__base == "+": # sub
  412. if self.__invert:
  413. v = v-1
  414. else:
  415. v = (t%1-0.5)
  416. elif self.__xtype == "fade":
  417. x = t * 2
  418. if x > 1:
  419. x = 2-x
  420. x -= 0.5
  421. v = x*2
  422. #base /= 2
  423. #base *=2
  424. if self.__base == "+": # add
  425. pass#base /= 2
  426. else:
  427. v *= -1
  428. v/=2
  429. if self.__invert:
  430. v *=-1
  431. #if self.__fade_in_master < 255:
  432. # self.__fade_in_master += v*size
  433. out = v *size +base
  434. self.out = out
  435. self.count = count
  436. return out * size_master.val(self.__master_id) #* (self.__fade_in_master /255.)
  437. #= master_id
  438. class DMXCH(object):
  439. def __init__(self,dmx=-1):
  440. self._base_value = 0
  441. self._fade = None
  442. self._fx = [None,None] # None
  443. self._fx_value = 0
  444. self._dmx = dmx
  445. self._flash = None
  446. self._flash_fx = None
  447. self._flash_fx_value = 0
  448. self._last_val = None
  449. #self.next(clock)
  450. #print("init",self)
  451. def fade(self,target,ftime=0,clock=0,delay=0):
  452. if target != self._base_value:
  453. try:
  454. target = float(target)
  455. self._fade = Fade(self._base_value,target,ftime=ftime,clock=clock,delay=delay)
  456. #self._fade.next()
  457. #self._fade.next()
  458. except Exception as e:
  459. print( "Except:fade",e,target,ftime,clock)
  460. self.next(clock)
  461. print("init",self)
  462. def fx(self,xtype="sinus",size=40,speed=40,invert=0,width=100,start=0,offset=0,base="", clock=0,master=None):
  463. print([self,xtype,size,speed,start,offset,base, clock])
  464. self._fx[0] = self._fx[1]
  465. if str(xtype).lower() == "off":
  466. fx_value = self._fx_value
  467. if fx_value != 0:
  468. cprint("???????______ FX OFF AS FADE",fx_value,0,255)
  469. self._fx[1] = Fade(fx_value,0,ftime=0.5,clock=clock)
  470. else:
  471. self._fx[1] = None
  472. self._fx_value = 0
  473. else:
  474. 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)
  475. self.next(clock)
  476. print("init",self)
  477. def flash(self,target,ftime=0,clock=0,delay=0):
  478. if str(target).lower() == "off":
  479. self._flash = None
  480. else:#elif target != self._base_value:
  481. try:
  482. target = float(target)
  483. self._flash = Fade(self._last_val,target,ftime=ftime,clock=clock,delay=delay)
  484. except Exception as e:
  485. print( "Except:flash",target,ftime,clock,__name__,e,)
  486. self.next(clock)
  487. print("init",self)
  488. def flash_fx(self,xtype="sinus",size=40,speed=40,invert=0,width=100,start=0,offset=0,base="",clock=0,master=None):
  489. if str(xtype).lower() == "off":
  490. fx_value = self._fx_value
  491. self._flash_fx = None
  492. self._flash_fx_value = 0
  493. else:
  494. 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)
  495. self.next(clock)
  496. print("init",self)
  497. def fx_ctl(self,cmd=""): #start,stop,off
  498. pass
  499. def __str__(self):
  500. return self.__repr__()
  501. def __repr__(self):
  502. return "<DMXCH {} {:0.2f} > [{}] {}".format(self._dmx, self._last_val,self._fx,self._fade)
  503. def fade_ctl(self,cmd=""): #start,stop,backw,fwd,bounce
  504. pass
  505. def next(self,clock=0):
  506. value = self._base_value
  507. if self._last_val is None:
  508. self._last_val = value
  509. fx_value = self._fx_value
  510. if self._flash is not None:
  511. value = self._flash.next(clock)
  512. #flicker bug ?!
  513. value = self._flash.next(clock)
  514. fx_value = 0
  515. elif self._fade is not None:#is Fade:# is Fade:
  516. self._base_value = self._fade.next(clock)
  517. self._base_value = self._fade.next(clock) #flicker bug ?!
  518. value = self._base_value
  519. if self._flash_fx is not None:# is FX:
  520. fx_value = self._flash_fx.next(clock)
  521. else:
  522. self._fx_value = 0
  523. if self._fx[-1] is not None and self._flash is None:# is FX:
  524. self._fx_value += self._fx[-1].next(clock)
  525. fx_value = self._fx_value
  526. self._last_val = value + fx_value
  527. #out = self._last_val * htp_master.master_by_dmx(self._dmx)
  528. out = self._last_val
  529. return out
  530. Bdmx = []
  531. for i in range(512*3):
  532. Bdmx.append( DMXCH(i) )
  533. #print(type(dmx[i]))
  534. _id = 1
  535. def split_cmd(data):
  536. if "cmd" in data:
  537. cmd = data["cmd"]
  538. #print("cmd",cmd)
  539. if "," in cmd:
  540. cmds = cmd.split(",")
  541. else:
  542. cmds = [cmd]
  543. return cmds
  544. class VDMX():
  545. """functional implementation as class for namespace encapsulation
  546. """
  547. def __init__(self):
  548. self.data = OrderedDict()
  549. self.data[4] = {"DMX":[21,22,23],"VALUE":255, "LIMIT":255} #,"DMXCH":DMXCH("V4")}
  550. for k,v in self.data.items():
  551. pass
  552. #dmxch = v["DMXCH"]
  553. #dmxch.fade(10,0)
  554. #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):
  555. def _list_by_dmx(self,_dmx=0):
  556. data = OrderedDict()
  557. for i,link in self.data.items(): # enumerate(self.data):
  558. if _dmx in link["DMX"]:
  559. #print( "_list_by_dmx",i,link)
  560. data[i] = link
  561. return data
  562. def dmx_by_id(self,_id=0):
  563. #print("dmx by master-id:",_id)
  564. if _id in self.data:
  565. for i,link in self.data[_id].items():
  566. #print("dmx_by_id", i,link)
  567. return (i,link)
  568. return 0,{}
  569. def by_dmx(self,clock,dmx):
  570. #print("master of dmx:",dmx)
  571. val=0
  572. flag = 0
  573. data = self._list_by_dmx(dmx)
  574. for i,row in data.items():
  575. if "DMXCH" not in row:
  576. row["DMXCH"] = DMXCH("V{}".format(i))
  577. row["DMXCH"].fade(255,0)
  578. v = row["DMXCH"].next(clock)
  579. #row["DMXCH"].fade(200,20)
  580. if v >= val:
  581. val = v
  582. flag = 1
  583. out = 1.
  584. if val > 255:
  585. val = 255
  586. if flag:
  587. out = val/255.
  588. else:
  589. out = 1.
  590. return out
  591. vdmx = VDMX()
  592. class HTP_MASTER():
  593. """functional implementation as class for namespace encapsulation
  594. """
  595. def __init__(self):
  596. self.data = OrderedDict()
  597. #self.data[1] = {"DMX":[1,2,3],"VALUE":80, "LIMIT":255}
  598. #self.data[2] = {"DMX":[12,13,22],"VALUE":70, "LIMIT":255}
  599. #self.data[3] = {"DMX":[22,23,24],"VALUE":99, "LIMIT":255}
  600. self.data[4] = {"DMX":[22,23,24],"VALUE":99, "LIMIT":255,"DMXCH":DMXCH(4)}
  601. def _list_by_dmx(self,_dmx=0):
  602. data = OrderedDict()
  603. for i,link in self.data.items(): # enumerate(self.data):
  604. if _dmx in link["DMX"]:
  605. #print( "_list_by_dmx",i,link)
  606. data[i] = link
  607. return data
  608. def dmx_by_id(self,_id=0):
  609. #print("dmx by master-id:",_id)
  610. if _id in self.data:
  611. for i,link in self.data[_id].items():
  612. #print("dmx_by_id", i,link)
  613. return (i,link)
  614. return 0,{}
  615. def val_by_dmx(self,dmx=0):
  616. #print("master of dmx:",dmx)
  617. val=0
  618. flag = 0
  619. data = self._list_by_dmx(dmx)
  620. for i,link in data.items():
  621. #print("master_by_dmx", i,link)
  622. if link["VALUE"] > val:
  623. #print("master_by_dmx", i,link)
  624. val = link["VALUE"]
  625. flag=1
  626. out = 1.
  627. if flag:
  628. out = val/255.
  629. return out
  630. htp_master = HTP_MASTER()
  631. #htp_master.data[_id] = {"DMX":[1,2,3],"VALUE":80, "LIMIT":255,"DMXCH":DMXCH()}
  632. class Main():
  633. def __init__(self):
  634. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=12)
  635. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=0)
  636. #artnet = ANN.ArtNetNode(to="2.0.0.255",univ=0)
  637. #artnet = ANN.ArtNetNode(to="10.10.10.255",univ=1)
  638. self.artnet = {}
  639. #self.artnet["0"] = ANN.ArtNetNode(to="10.10.10.255",univ=0)
  640. #self.artnet["0"].dmx[512-1] = 10
  641. #self.artnet["1"] = ANN.ArtNetNode(to="10.10.10.255",univ=1)
  642. #self.artnet["1"].dmx[512-1] = 11
  643. self.fx = {} # key is dmx address
  644. def loop(self):
  645. #dmx[205] = 255 #205 BLUE
  646. #self.artnet.send()
  647. xx = [0]*512
  648. #artnet = self.artnet["0"]
  649. #artnet.dmx = xx# [:] #dmx #[0]*512
  650. old_univ = -1
  651. while 1:
  652. t = clock.time()
  653. ii = 0
  654. for ii,dmxch in enumerate(Bdmx):
  655. i = ii%512
  656. univ = ii//512
  657. if str(univ) not in self.artnet:
  658. print("add uiv",univ)
  659. self.artnet[str(univ)] = ANN.ArtNetNode(to="10.10.10.255",univ=univ)
  660. self.artnet[str(univ)].dmx[512-1] = 100+univ
  661. if univ != old_univ:
  662. old_univ = univ
  663. #print("UNIV",ii/512)
  664. try:
  665. artnet.next()
  666. except:pass
  667. artnet = self.artnet[str(univ)]
  668. artnet.dmx = xx
  669. v = dmxch.next(t)
  670. vv = vdmx.by_dmx(clock=i,dmx=ii+1)
  671. v = v*vv # disable v-master
  672. xx[i] = int(v)
  673. try:
  674. artnet.next()
  675. except:pass
  676. time.sleep(0.01)
  677. main = Main()
  678. if __run_main:
  679. #thread.start_new_thread(artnet_loop,())
  680. thread.start_new_thread(main.loop,())
  681. def JCB(data): #json client input
  682. t_start = time.time()
  683. #jdatas = data["cmd"].split("\x00")
  684. jdatas = [data["cmd"]]
  685. c = clock.time()
  686. c = float(c)
  687. print("JCB",round(c,2))
  688. ftime = 0
  689. delay = 0
  690. for j in jdatas:
  691. master_fx = MASTER_FX()
  692. if not j:
  693. continue
  694. try:
  695. cprint("JCB::")#,j)
  696. jdata = j #jdatas[j]
  697. jtxt = jdata
  698. #jtxt = zlib.decompress(jtxt) #jtxt.decode())
  699. jtxt = str(jtxt,"UTF-8")
  700. cmds = json.loads(jtxt)
  701. for x in cmds:
  702. #cprint(int(clock.time()*1000)/1000,end=" ",color="yellow")#time.time())
  703. #cprint("json", x,type(x),color="yellow")#,cmds[x])
  704. if "CMD" in x:
  705. print("CMD:",x)
  706. if "SPEED-MASTER" == x["CMD"]:
  707. speed_master.val(x["NR"],x["VALUE"])
  708. if x["NR"] == 2:
  709. pass
  710. #vdmx.data[4]["DMXCH"].fade(x["VALUE"],3)#,clock=clock.time())
  711. if x["NR"] == 3:
  712. pass
  713. #vdmx.data[4]["DMXCH"].fx(size=255,speed=x["VALUE"],base="-",offset=0)#xtype=xtype,size=size,speed=speed,invert=invert,width=width,start=start,offset=offset,base=base,clock=c,master=master_fx)
  714. if "SIZE-MASTER" == x["CMD"]:
  715. size_master.val(x["NR"],x["VALUE"])
  716. else:
  717. if "DMX" in x:
  718. DMX = int(x["DMX"])
  719. else:continue
  720. if DMX > 0:
  721. DMX -=1
  722. else:continue
  723. if "VALUE" in x:# and x["VALUE"] is not None:
  724. v = x["VALUE"]
  725. else:continue
  726. if "FX" in x:# and x["VALUE"] is not None:
  727. fx = x["FX"]
  728. else:fx=""
  729. if "FX2" in x:# and x["VALUE"] is not None:
  730. fx2 = x["FX2"]
  731. else:fx2={}
  732. if "FADE" in x:
  733. ftime = x["FADE"]
  734. else:ftime=0
  735. if "DELAY" in x:
  736. delay = x["DELAY"]
  737. else:delay=0
  738. if len(Bdmx) < DMX:
  739. continue
  740. if v is not None:
  741. if "FLASH" in x:
  742. #print("FLASH")
  743. Bdmx[DMX].flash(target=v,ftime=ftime, clock=c,delay=delay)
  744. else:
  745. #print("FADE")
  746. Bdmx[DMX].fade(target=v,ftime=ftime, clock=c,delay=delay)
  747. if type(fx2) is dict and fx2:
  748. #cprint("FX2",DMX,fx2,color="green")
  749. xtype="fade"
  750. size = 10
  751. speed = 10
  752. start = 0
  753. offset= 0
  754. width=100
  755. invert=0
  756. base = "-"
  757. if "TYPE" in fx2:
  758. xtype = fx2["TYPE"]
  759. if "SIZE" in fx2:
  760. size = fx2["SIZE"]
  761. if "SPEED" in fx2:
  762. speed = fx2["SPEED"]
  763. if "OFFSET" in fx2:
  764. offset = fx2["OFFSET"]
  765. if "BASE" in fx2:
  766. base = fx2["BASE"]
  767. if "INVERT" in fx2:
  768. invert = fx2["INVERT"]
  769. if "WIDTH" in fx2:
  770. width = fx2["WIDTH"]
  771. if "off" == x["VALUE"]: #fix fx flash off
  772. xtype= "off"
  773. if "alloff" == xtype.lower():
  774. for i in Bdmx:
  775. if i is not None:
  776. i.flash_fx(xtype="off",clock=c)
  777. i.fx(xtype="off",clock=c)
  778. if "FLASH" in x:
  779. 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)
  780. else:
  781. Bdmx[DMX].fx(xtype=xtype,size=size,speed=speed,invert=invert,width=width,start=start,offset=offset,base=base,clock=c,master=master_fx)
  782. elif type(fx) is str and fx: # old fx like sinus:200:12:244
  783. ccm = str(DMX+1)+":"+fx
  784. print("fx",ccm)
  785. if "FLASH" in x:
  786. CB({"cmd":"fxf"+ccm})
  787. else:
  788. CB({"cmd":"fx"+ccm})
  789. cprint("{:0.04} sec.".format(time.time()-t_start),color="yellow")
  790. cprint("{:0.04} t.".format(time.time()),color="yellow")
  791. except Exception as e:
  792. cprint("EXCEPTION JCB",e,color="red")
  793. cprint("----",str(jdata)[:150],"...",color="red")
  794. cprint("Error on line {}".format(sys.exc_info()[-1].tb_lineno),color="red")
  795. cprint()
  796. cprint("{:0.04} sec.".format(time.time()-t_start),color="yellow")
  797. cprint("{:0.04} t.".format(time.time()),color="yellow")
  798. def CB(data): # raw/text client input
  799. #print("CB",data)
  800. cmds = split_cmd(data)
  801. c = clock.time()
  802. c = float(c)
  803. ftime = 0
  804. delay = 0
  805. for xcmd in cmds:
  806. if xcmd:
  807. cprint("CB",xcmd,end=" ")
  808. pass
  809. else:
  810. continue
  811. if xcmd.startswith("fxf"):
  812. xxcmd=xcmd[3:].split(":")
  813. #print("fxf:",xxcmd)
  814. if "alloff" == xxcmd[1].lower():
  815. for i in Bdmx:
  816. if i is not None:
  817. i.flash_fx(xtype="off",clock=c)
  818. l = xxcmd
  819. try:
  820. xtype=""
  821. size=40
  822. speed=100
  823. start=0
  824. offset=0
  825. base=""
  826. k=int(l[0])-1
  827. xtype=l[1]
  828. if len(l) >= 3:
  829. try:size=int(l[2])
  830. except:pass
  831. if len(l) >= 4:
  832. try:speed=int(l[3])
  833. except:pass
  834. if len(l) >= 5:
  835. try:start=int(l[4])
  836. except:pass
  837. if len(l) >= 6:
  838. try:offset=int(l[5])
  839. except:pass
  840. if len(l) >= 7:
  841. try:base=l[6]
  842. except:pass
  843. if len(Bdmx) > k:
  844. #Bdmx[k].fade(target=v,ftime=t, clock=c)
  845. Bdmx[k].flash_fx(xtype=xtype,size=size,speed=speed,start=start,offset=offset,base=base,clock=c)
  846. except Exception as e:
  847. print("EXCEPTION IN FX",e)
  848. print("Error on line {}".format(sys.exc_info()[-1].tb_lineno))
  849. elif xcmd.startswith("fx"):
  850. xxcmd=xcmd[2:].split(":")
  851. print("DMX:",xxcmd)
  852. if len(xxcmd) < 2:
  853. print("xxcmd err",xxcmd,xcmd)
  854. continue
  855. if "alloff" == xxcmd[1].lower():
  856. for i in Bdmx:
  857. i.fx(xtype="off",clock=c)
  858. l = xxcmd
  859. try:
  860. xtype=""
  861. size=40
  862. speed=100
  863. start=0
  864. offset=0
  865. base=""
  866. k=int(l[0])-1
  867. xtype=l[1]
  868. if len(l) >= 3:
  869. try:size=int(l[2])
  870. except:pass
  871. if len(l) >= 4:
  872. try:speed=int(l[3])
  873. except:pass
  874. if len(l) >= 5:
  875. try:start=int(l[4])
  876. except:pass
  877. if len(l) >= 6:
  878. try:offset=int(l[5])
  879. except:pass
  880. if len(l) >= 7:
  881. try:base=l[6]
  882. except:pass
  883. if len(Bdmx) > k:
  884. #Bdmx[k].fade(target=v,ftime=t, clock=c)
  885. Bdmx[k].fx(xtype=xtype,size=size,speed=speed,start=start,offset=offset,base=base,clock=c)
  886. except Exception as e:
  887. print("EXCEPTION IN FX",xcmd,e)
  888. print("Error on line {}".format(sys.exc_info()[-1].tb_lineno))
  889. if __run_main:
  890. #jchat = chat.CMD(CB,port=50001) # server listener
  891. #thread.start_new_thread(jchat.poll,())
  892. chat.cmd(JCB) # server listener
  893. #chat.cmd(JCB,port=50001) # server listener
  894. #input("END")