leds.pl 5.8 KB

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