dmx.pl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/opt/local/bin/python2.7
  2. # Append the path to help find our OLA packages
  3. import sys
  4. sys.path.append('/opt/local/lib/python2.7/site-packages')
  5. sys.path.append('/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages')
  6. import os
  7. import array
  8. import select
  9. import socket
  10. import string
  11. import subprocess
  12. from ola.ClientWrapper import ClientWrapper
  13. # ====================================
  14. # Globals
  15. # ====================================
  16. max_channels = 512
  17. wrapper = None
  18. state = [ 0 ] * max_channels
  19. cmds = { 'value' : [ 0 ] * max_channels, 'ticks' : [ 0 ] * max_channels }
  20. sock = None
  21. max_mesg_len = 1024
  22. # ====================================
  23. # Defaults
  24. # ====================================
  25. interval = 50
  26. universe = 0
  27. # ====================================
  28. # Wrapper callback -- exit on errors
  29. # ====================================
  30. def DmxSent(state):
  31. # Stop on errors
  32. if not state.Succeeded():
  33. wrapper.Stop()
  34. # ====================================
  35. # Main calculation
  36. # ====================================
  37. def SendDMXFrame():
  38. # Re-schedule ourselves in interval ms (do this first to keep the timing consistent)
  39. wrapper.AddEvent(interval, SendDMXFrame)
  40. # Check for new commands
  41. ready = select.select([sock], [], [], interval / 2000)[0]
  42. for fh in ready:
  43. cmd = fh.recvfrom(max_mesg_len)[0]
  44. channel, duration, intensity = string.split(cmd, ':')
  45. try:
  46. channel = int(channel)
  47. duration = int(duration)
  48. intensity = int(intensity)
  49. except ValueError:
  50. print 'Invalid command:', cmd
  51. channel = -1
  52. if (channel >= 0 and channel <= 512 and intensity >= 0 and intensity <= 255 and duration >= 0 and duration <= 60000):
  53. if (channel > 0):
  54. cmds['value'][channel - 1] = intensity
  55. cmds['ticks'][channel - 1] = duration / interval
  56. else:
  57. for i in range(len(state)):
  58. cmds['value'][i] = intensity
  59. cmds['ticks'][i] = duration / interval
  60. else:
  61. print 'Invalid command parameters:', channel, duration, intensity
  62. # Update values for each channel
  63. for i in range(len(cmds['value'])):
  64. if (cmds['value'][i] != state[i]):
  65. diff = cmds['value'][i] - state[i]
  66. if (cmds['ticks'][i] < 1):
  67. delta = diff
  68. else:
  69. delta = float(diff) / float(cmds['ticks'][i])
  70. state[i] += int(delta)
  71. cmds['ticks'][i] -= 1
  72. # Send all DMX channels
  73. data = array.array('B')
  74. for i in range(len(state)):
  75. data.append(state[i])
  76. wrapper.Client().SendDmx(universe, data, DmxSent)
  77. # ====================================
  78. # Main
  79. # ====================================
  80. # Pick a universe (default from above, or as specified in the environment)
  81. if 'UNIVERSE' in os.environ:
  82. universe = int(os.environ['UNIVERSE'])
  83. # Pick a tick interval (default from above, or as specified in the environment)
  84. if 'INTERVAL' in os.environ:
  85. universe = int(os.environ['INTERVAL'])
  86. # Pick a socket file ($TMPDIR/plexMonitor/DMX.socket, or as specified in the environment)
  87. cmd_file = None
  88. data_dir = None
  89. if 'SOCKET' in os.environ:
  90. cmd_file = os.environ['SOCKET']
  91. data_dir = os.dirname(cmd_file)
  92. else:
  93. proc = subprocess.Popen(['getconf', 'DARWIN_USER_TEMP_DIR'], stdout=subprocess.PIPE, shell=False)
  94. (tmp_dir, err) = proc.communicate()
  95. tmp_dir = tmp_dir.strip()
  96. data_dir = tmp_dir + 'plexMonitor/'
  97. cmd_file = data_dir + 'DMX.socket'
  98. # Sanity checks
  99. if (not os.path.isdir(data_dir)):
  100. raise Exception('Bad config: ' + data_dir)
  101. # Open the socket
  102. if (os.path.exists(cmd_file)):
  103. os.unlink(cmd_file)
  104. sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
  105. sock.bind(cmd_file)
  106. # Start the DMX loop
  107. wrapper = ClientWrapper()
  108. wrapper.AddEvent(interval, SendDMXFrame)
  109. wrapper.Run()