leds.pl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 mtime($);
  9. sub dim($);
  10. # User config
  11. my %DIM = (
  12. 'OFF' => [
  13. # Handled by rope.pl
  14. ],
  15. 'PLAY' => [
  16. { 'channel' => 13, 'value' => 128, 'time' => 1000 },
  17. { 'channel' => 14, 'value' => 128, 'time' => 2000 },
  18. { 'channel' => 15, 'value' => 128, 'time' => 3000 },
  19. ],
  20. 'PLAY_HIGH' => [
  21. { 'channel' => 13, 'value' => 128, 'time' => 1000 },
  22. { 'channel' => 14, 'value' => 128, 'time' => 2000 },
  23. { 'channel' => 15, 'value' => 128, 'time' => 3000 },
  24. ],
  25. 'PAUSE' => [
  26. { 'channel' => 13, 'value' => 128, 'time' => 1000, 'delay' => 3000 },
  27. { 'channel' => 14, 'value' => 128, 'time' => 1000, 'delay' => 6000 },
  28. { 'channel' => 15, 'value' => 128, 'time' => 1000, 'delay' => 9000 },
  29. ],
  30. 'MOTION' => [
  31. { 'channel' => 13, 'value' => 128, 'time' => 1000 },
  32. { 'channel' => 14, 'value' => 128, 'time' => 2000 },
  33. { 'channel' => 15, 'value' => 128, 'time' => 3000 },
  34. ],
  35. );
  36. # App config
  37. my $SOCK_TIMEOUT = 5;
  38. my $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
  39. chomp($TEMP_DIR);
  40. my $DATA_DIR = $TEMP_DIR . 'plexMonitor/';
  41. my $DMX_SOCK = $DATA_DIR . 'DMX.socket';
  42. my $SUB_SOCK = $DATA_DIR . 'STATE.socket';
  43. my $STATE_SOCK = $DATA_DIR . 'LED.socket';
  44. my $MAX_CMD_LEN = 1024;
  45. # Debug
  46. my $DEBUG = 0;
  47. if ($ENV{'DEBUG'}) {
  48. $DEBUG = 1;
  49. }
  50. # Command-line arguments
  51. my ($DELAY) = @ARGV;
  52. if (!$DELAY) {
  53. $DELAY = 0.5;
  54. }
  55. # Sanity check
  56. if (!-d $DATA_DIR || !-S $DMX_SOCK || !-S $SUB_SOCK) {
  57. die("Bad config\n");
  58. }
  59. # State socket init
  60. if (-e $STATE_SOCK) {
  61. unlink($STATE_SOCK);
  62. }
  63. my $state_fh = IO::Socket::UNIX->new(
  64. 'Local' => $STATE_SOCK,
  65. 'Type' => SOCK_DGRAM
  66. ) or die('Unable to open socket: ' . $STATE_SOCK . ": ${@}\n");
  67. my $select = IO::Select->new($state_fh)
  68. or die('Unable to select socket: ' . $STATE_SOCK . ": ${!}\n");
  69. # DMX socket init
  70. my $dmx_fh = IO::Socket::UNIX->new(
  71. 'Peer' => $DMX_SOCK,
  72. 'Type' => SOCK_DGRAM,
  73. 'Timeout' => $SOCK_TIMEOUT
  74. ) or die('Unable to open socket: ' . $DMX_SOCK . ": ${@}\n");
  75. # Subscribe to state updates
  76. my $sub_fh = IO::Socket::UNIX->new(
  77. 'Peer' => $SUB_SOCK,
  78. 'Type' => SOCK_DGRAM,
  79. 'Timeout' => $SOCK_TIMEOUT
  80. ) or die('Unable to open socket: ' . $SUB_SOCK . ": ${@}\n");
  81. $sub_fh->send($STATE_SOCK)
  82. or die('Unable to subscribe: ' . $! . "\n");
  83. shutdown($sub_fh, 2);
  84. undef($sub_fh);
  85. # State
  86. my $state = 'INIT';
  87. my $stateLast = $state;
  88. my $lights = 0;
  89. my $updateLast = 0;
  90. # Always force lights out at launch
  91. dim({'channel' => 13, 'value' => 0, 'time' => 0});
  92. dim({'channel' => 14, 'value' => 0, 'time' => 0});
  93. dim({'channel' => 15, 'value' => 0, 'time' => 0});
  94. # Loop forever
  95. while (1) {
  96. # State is calculated; use newState to gather data
  97. my $newState = $state;
  98. # Wait for state updates
  99. my @ready_clients = $select->can_read($DELAY);
  100. foreach my $fh (@ready_clients) {
  101. # Grab the inbound text
  102. my $cmdState = undef();
  103. $fh->recv($cmdState, $MAX_CMD_LEN);
  104. $cmdState =~ s/^\s+//;
  105. $cmdState =~ s/\s+$//;
  106. if ($DEBUG) {
  107. print STDERR 'Got state: ' . $cmdState . "\n";
  108. }
  109. # Translate INIT to OFF
  110. if ($cmdState eq 'INIT') {
  111. $cmdState = 'OFF';
  112. }
  113. # Only accept valid states
  114. if (!defined($DIM{$cmdState})) {
  115. print STDERR 'Invalid state: ' . $cmdState . "\n";
  116. next;
  117. }
  118. # Propogate the most recent command state
  119. $newState = $cmdState;
  120. }
  121. # Monitor the LIGHTS file for presence
  122. {
  123. $lights = 0;
  124. if (-e $DATA_DIR . 'LIGHTS') {
  125. $lights = 1;
  126. }
  127. if ($DEBUG) {
  128. print STDERR 'Lights: ' . $lights . "\n";
  129. }
  130. # Clear the override when the main state is "OFF"
  131. if ($lights && $newState eq 'OFF') {
  132. unlink($DATA_DIR . 'LIGHTS');
  133. }
  134. }
  135. # Calculate the new state
  136. $stateLast = $state;
  137. if ($lights) {
  138. if ($newState eq 'PLAY') {
  139. $newState = 'PLAY_HIGH';
  140. }
  141. } else {
  142. if ($newState eq 'PLAY_HIGH') {
  143. $newState = 'PLAY';
  144. }
  145. }
  146. $state = $newState;
  147. # Update the lighting
  148. if ($stateLast ne $state) {
  149. if ($DEBUG) {
  150. print STDERR 'State: ' . $stateLast . ' => ' . $state . "\n";
  151. foreach my $data (@{ $DIM{$state} }) {
  152. print STDERR "\t" . $data->{'channel'} . ' => ' . $data->{'value'} . ' @ ' . $data->{'time'} . "\n";
  153. }
  154. }
  155. # Send the dim command
  156. my @values = ();
  157. foreach my $data (@{ $DIM{$state} }) {
  158. dim($data);
  159. push(@values, $data->{'channel'} . ' => ' . $data->{'value'} . ' @ ' . $data->{'time'});
  160. }
  161. # Save the state and value to disk
  162. my ($fh, $tmp) = tempfile($DATA_DIR . 'LED.XXXXXXXX', 'UNLINK' => 0);
  163. print $fh 'State: ' . $state . "\n" . join("\n", @values) . "\n";
  164. close($fh);
  165. rename($tmp, $DATA_DIR . 'LED');
  166. }
  167. }
  168. sub mtime($) {
  169. my ($file) = @_;
  170. my $mtime = 0;
  171. if (-r $file) {
  172. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), $mtime, undef(), undef(), undef()) = stat($file);
  173. }
  174. return $mtime;
  175. }
  176. # Send the command
  177. sub dim($) {
  178. my ($args) = @_;
  179. if (!defined($args->{'delay'})) {
  180. $args->{'delay'} = 0;
  181. }
  182. if (!defined($args->{'channel'}) || !defined($args->{'time'}) || !defined($args->{'value'})) {
  183. die('Invalid command for socket: ' . join(', ', keys(%{$args})) . ': ' . join(', ', values(%{$args})) . "\n");
  184. }
  185. my $cmd = join(':', $args->{'channel'}, $args->{'time'}, $args->{'value'}, $args->{'delay'});
  186. $dmx_fh->send($cmd)
  187. or die('Unable to write command to socket: ' . $DMX_SOCK . ': ' . $cmd . ": ${!}\n");
  188. }