leds.pl 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IO::Select;
  5. use IO::Socket::UNIX;
  6. use File::Temp qw( tempfile );
  7. # Prototypes
  8. sub dim($);
  9. sub red_alert();
  10. sub red_flash();
  11. # Effects states
  12. my %EFFECTS = (
  13. 'RED_ALERT' => \&red_alert,
  14. 'RED_FLASH' => \&red_flash,
  15. );
  16. # User config
  17. my $COLOR_TIMEOUT = 60;
  18. my $COLOR_MIN = $COLOR_TIMEOUT / 4;
  19. my %DIM = (
  20. 'OFF' => [
  21. # Handled by rope.pl
  22. ],
  23. 'PLAY' => [
  24. { 'channel' => 13, 'value' => 10, 'time' => 500 },
  25. { 'channel' => 14, 'value' => 12, 'time' => 500 },
  26. { 'channel' => 15, 'value' => 8, 'time' => 500 },
  27. ],
  28. 'PLAY_HIGH' => [
  29. { 'channel' => 13, 'value' => 64, 'time' => 1000, 'delay' => 3000 },
  30. { 'channel' => 14, 'value' => 73, 'time' => 1000, 'delay' => 1500 },
  31. { 'channel' => 15, 'value' => 76, 'time' => 1000, 'delay' => 0 },
  32. ],
  33. 'PAUSE' => [
  34. { 'channel' => 13, 'value' => 96, 'time' => 3000, 'delay' => 3000 },
  35. { 'channel' => 14, 'value' => 109, 'time' => 3000, 'delay' => 0 },
  36. { 'channel' => 15, 'value' => 114, 'time' => 3000, 'delay' => 7000 },
  37. ],
  38. 'MOTION' => [
  39. { 'channel' => 13, 'value' => 144, 'time' => 1000 },
  40. { 'channel' => 14, 'value' => 164, 'time' => 1000 },
  41. { 'channel' => 15, 'value' => 172, 'time' => 1000 },
  42. ],
  43. );
  44. # App config
  45. my $SOCK_TIMEOUT = 5;
  46. my $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
  47. chomp($TEMP_DIR);
  48. my $DATA_DIR = $TEMP_DIR . 'plexMonitor/';
  49. my $DMX_SOCK = $DATA_DIR . 'DMX.socket';
  50. my $SUB_SOCK = $DATA_DIR . 'STATE.socket';
  51. my $STATE_SOCK = $DATA_DIR . 'LED.socket';
  52. my $MAX_CMD_LEN = 1024;
  53. my $PUSH_TIMEOUT = 20;
  54. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  55. # Debug
  56. my $DEBUG = 0;
  57. if ($ENV{'DEBUG'}) {
  58. $DEBUG = 1;
  59. }
  60. # Command-line arguments
  61. my ($DELAY) = @ARGV;
  62. if (!$DELAY) {
  63. $DELAY = $PULL_TIMEOUT / 2;
  64. }
  65. # Sanity check
  66. if (!-d $DATA_DIR || !-S $DMX_SOCK || !-S $SUB_SOCK) {
  67. die("Bad config\n");
  68. }
  69. # State socket init
  70. if (-e $STATE_SOCK) {
  71. unlink($STATE_SOCK);
  72. }
  73. my $state_fh = IO::Socket::UNIX->new(
  74. 'Local' => $STATE_SOCK,
  75. 'Type' => SOCK_DGRAM
  76. ) or die('Unable to open socket: ' . $STATE_SOCK . ": ${@}\n");
  77. my $select = IO::Select->new($state_fh)
  78. or die('Unable to select socket: ' . $STATE_SOCK . ": ${!}\n");
  79. # DMX socket init
  80. my $dmx_fh = IO::Socket::UNIX->new(
  81. 'Peer' => $DMX_SOCK,
  82. 'Type' => SOCK_DGRAM,
  83. 'Timeout' => $SOCK_TIMEOUT
  84. ) or die('Unable to open socket: ' . $DMX_SOCK . ": ${@}\n");
  85. # Subscribe to state updates
  86. my $sub_fh = IO::Socket::UNIX->new(
  87. 'Peer' => $SUB_SOCK,
  88. 'Type' => SOCK_DGRAM,
  89. 'Timeout' => $SOCK_TIMEOUT
  90. ) or die('Unable to open socket: ' . $SUB_SOCK . ": ${@}\n");
  91. $sub_fh->send($STATE_SOCK)
  92. or die('Unable to subscribe: ' . $! . "\n");
  93. shutdown($sub_fh, 2);
  94. undef($sub_fh);
  95. # State
  96. my $state = 'INIT';
  97. my $stateLast = $state;
  98. my %exists = ();
  99. my %existsLast = %exists;
  100. my $pushLast = 0;
  101. my $pullLast = time();
  102. my $colorChange = time();
  103. # Always force lights out at launch
  104. dim({ 'channel' => 13, 'value' => 0, 'time' => 0 });
  105. dim({ 'channel' => 14, 'value' => 0, 'time' => 0 });
  106. dim({ 'channel' => 15, 'value' => 0, 'time' => 0 });
  107. # Loop forever
  108. while (1) {
  109. # Record the last state/exists data for diffs/resets
  110. $stateLast = $state;
  111. %existsLast = %exists;
  112. # Set anywhere to force an update this cycle
  113. my $forceUpdate = 0;
  114. # State is calculated; use newState to gather data
  115. my $newState = $state;
  116. # Wait for state updates
  117. my @ready_clients = $select->can_read($DELAY);
  118. foreach my $fh (@ready_clients) {
  119. # Grab the inbound text
  120. my $text = undef();
  121. $fh->recv($text, $MAX_CMD_LEN);
  122. # Parse the string
  123. %exists = ();
  124. my ($cmdState, $exists_text) = $text =~ /^(\w+)\s+\(([^\)]+)\)/;
  125. if (!defined($cmdState) || !defined($exists_text)) {
  126. print STDERR 'State parse error: ' . $text . "\n";
  127. next;
  128. }
  129. foreach my $exists (split(/\s*,\s*/, $exists_text)) {
  130. my ($name, $value) = $exists =~ /(\w+)\:(0|1)/;
  131. if (!defined($name) || !defined($value)) {
  132. print STDERR 'State parse error (exists): ' . $text . "\n";
  133. next;
  134. }
  135. $exists{$name} = $value;
  136. }
  137. if ($DEBUG) {
  138. my @exists_tmp = ();
  139. foreach my $key (keys(%exists)) {
  140. push(@exists_tmp, $key . ':' . $exists{$key});
  141. }
  142. print STDERR 'Got state: ' . $cmdState . ' (' . join(', ', @exists_tmp) . ")\n";
  143. }
  144. # Translate INIT to OFF
  145. if ($cmdState eq 'INIT') {
  146. $cmdState = 'OFF';
  147. }
  148. # Only accept valid states
  149. if (!defined($DIM{$cmdState}) && !defined($EFFECTS{$cmdState})) {
  150. print STDERR 'Invalid state: ' . $cmdState . "\n";
  151. next;
  152. }
  153. # Propogate the most recent command state
  154. $newState = $cmdState;
  155. $pullLast = time();
  156. }
  157. # Special handling for effects states
  158. if (defined($EFFECTS{$newState})) {
  159. # Dispatch the handler
  160. $EFFECTS{$newState}->();
  161. # Force an update back to the original state
  162. $newState = $stateLast;
  163. %exists = %existsLast;
  164. $forceUpdate = 1;
  165. }
  166. # Calculate the new state
  167. if ($exists{'LIGHTS'}) {
  168. if ($newState eq 'PLAY') {
  169. $newState = 'PLAY_HIGH';
  170. }
  171. } else {
  172. if ($newState eq 'PLAY_HIGH') {
  173. $newState = 'PLAY';
  174. }
  175. }
  176. $state = $newState;
  177. # Color changes
  178. my @COLOR = ();
  179. if (time() - $colorChange > $COLOR_TIMEOUT && $state ne 'PLAY' && $state ne 'OFF') {
  180. my $totalLums = 0;
  181. foreach my $data (@{ $DIM{$state} }) {
  182. $totalLums += $data->{'value'};
  183. }
  184. my $red = int(rand($totalLums / 3));
  185. my $blue = int(rand($totalLums / 3));
  186. my $green = $totalLums - ($red + $blue);
  187. my $time = int(rand($COLOR_TIMEOUT - $COLOR_MIN)) + $COLOR_MIN;
  188. if ($DEBUG) {
  189. print STDERR 'New color: ' . join(':', $red, $green, $blue) . ' @ ' . $time . "\n";
  190. }
  191. @COLOR = [ { 'channel' => 13, 'value' => $red, 'time' => $time }, { 'channel' => 14, 'value' => $blue, 'time' => $time }, { 'channel' => 15, 'value' => $green, 'time' => $time }, ];
  192. $forceUpdate = 1;
  193. $colorChange = time();
  194. }
  195. # Force updates on a periodic basis
  196. if (time() - $pushLast > $PUSH_TIMEOUT) {
  197. $forceUpdate = 1;
  198. }
  199. # Die if we don't see regular updates
  200. if (time() - $pullLast > $PULL_TIMEOUT) {
  201. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  202. }
  203. # Force updates on any state change
  204. if ($stateLast ne $state) {
  205. $forceUpdate = 1;
  206. # Also reset the color change timer, so we always spend 1 cycle at white
  207. $colorChange = time() + $COLOR_TIMEOUT;
  208. }
  209. # Update the lighting
  210. if ($forceUpdate) {
  211. if ($DEBUG) {
  212. print STDERR 'State: ' . $stateLast . ' => ' . $state . "\n";
  213. foreach my $data (@{ $DIM{$state} }) {
  214. print STDERR "\t" . $data->{'channel'} . ' => ' . $data->{'value'} . ' @ ' . $data->{'time'} . "\n";
  215. }
  216. }
  217. # Select a data set (color or standard)
  218. my @data_set = ();
  219. if (scalar(@COLOR)) {
  220. @data_set = @COLOR;
  221. } else {
  222. @data_set = @{ $DIM{$state} };
  223. }
  224. # Send the dim command
  225. my @values = ();
  226. foreach my $data (@data_set) {
  227. dim($data);
  228. push(@values, $data->{'channel'} . ' => ' . $data->{'value'} . ' @ ' . $data->{'time'});
  229. }
  230. # Save the state and value to disk
  231. my ($fh, $tmp) = tempfile($DATA_DIR . 'LED.XXXXXXXX', 'UNLINK' => 0);
  232. print $fh 'State: ' . $state . "\n" . join("\n", @values) . "\n";
  233. close($fh);
  234. rename($tmp, $DATA_DIR . 'LED');
  235. # Update the push time
  236. $pushLast = time();
  237. }
  238. }
  239. # Send the command
  240. sub dim($) {
  241. my ($args) = @_;
  242. if (!defined($args->{'delay'})) {
  243. $args->{'delay'} = 0;
  244. }
  245. if (!defined($args->{'channel'}) || !defined($args->{'time'}) || !defined($args->{'value'})) {
  246. die('Invalid command for socket: ' . join(', ', keys(%{$args})) . ': ' . join(', ', values(%{$args})) . "\n");
  247. }
  248. my $cmd = join(':', $args->{'channel'}, $args->{'time'}, $args->{'value'}, $args->{'delay'});
  249. $dmx_fh->send($cmd)
  250. or die('Unable to write command to socket: ' . $DMX_SOCK . ': ' . $cmd . ": ${!}\n");
  251. }
  252. # ======================================
  253. # Effects routines
  254. # These are blocking, so be careful
  255. # ======================================
  256. sub red_alert() {
  257. }
  258. sub red_flash() {
  259. }