rope.pl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IO::Select;
  5. use File::Temp qw( tempfile );
  6. # Local modules
  7. use Cwd qw(abs_path);
  8. use File::Basename qw(dirname);
  9. use lib dirname(abs_path($0));
  10. use DMX;
  11. # Prototypes
  12. sub dim($);
  13. # User config
  14. my %DIM = (
  15. 'OFF' => [
  16. { 'channel' => 0, 'value' => 0, 'time' => 60000 }
  17. ],
  18. 'PLAY' => [
  19. { 'channel' => 1, 'value' => 64, 'time' => 500 },
  20. { 'channel' => 2, 'value' => 32, 'time' => 500 },
  21. { 'channel' => 3, 'value' => 80, 'time' => 2500 },
  22. { 'channel' => 5, 'value' => 64, 'time' => 500 },
  23. { 'channel' => 6, 'value' => 36, 'time' => 500 },
  24. { 'channel' => 7, 'value' => 32, 'time' => 500 },
  25. { 'channel' => 8, 'value' => 16, 'time' => 500 },
  26. { 'channel' => 9, 'value' => 16, 'time' => 500 },
  27. ],
  28. 'PLAY_HIGH' => [
  29. { 'channel' => 1, 'value' => 255, 'time' => 500 },
  30. { 'channel' => 2, 'value' => 128, 'time' => 500 },
  31. { 'channel' => 3, 'value' => 192, 'time' => 1500 },
  32. { 'channel' => 5, 'value' => 128, 'time' => 500 },
  33. { 'channel' => 6, 'value' => 36, 'time' => 500 },
  34. { 'channel' => 7, 'value' => 128, 'time' => 500 },
  35. { 'channel' => 8, 'value' => 16, 'time' => 500 },
  36. { 'channel' => 9, 'value' => 96, 'time' => 500 },
  37. ],
  38. 'PAUSE' => [
  39. { 'channel' => 1, 'value' => 255, 'time' => 1000 },
  40. { 'channel' => 2, 'value' => 192, 'time' => 10000 },
  41. { 'channel' => 3, 'value' => 192, 'time' => 5000 },
  42. { 'channel' => 5, 'value' => 255, 'time' => 1000 },
  43. { 'channel' => 6, 'value' => 104, 'time' => 6000, 'delay' => 3000 },
  44. { 'channel' => 7, 'value' => 192, 'time' => 10000 },
  45. { 'channel' => 8, 'value' => 96, 'time' => 6000, 'delay' => 6000 },
  46. { 'channel' => 9, 'value' => 96, 'time' => 6000, 'delay' => 3000 },
  47. ],
  48. 'MOTION' => [
  49. { 'channel' => 1, 'value' => 255, 'time' => 1000 },
  50. { 'channel' => 2, 'value' => 192, 'time' => 1000 },
  51. { 'channel' => 3, 'value' => 192, 'time' => 1000 },
  52. { 'channel' => 5, 'value' => 192, 'time' => 1000 },
  53. { 'channel' => 6, 'value' => 104, 'time' => 1000 },
  54. { 'channel' => 7, 'value' => 192, 'time' => 1000 },
  55. { 'channel' => 8, 'value' => 96, 'time' => 2000 },
  56. { 'channel' => 9, 'value' => 96, 'time' => 1500 },
  57. ],
  58. );
  59. # App config
  60. my $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
  61. chomp($TEMP_DIR);
  62. my $DATA_DIR = $TEMP_DIR . 'plexMonitor/';
  63. my $STATE_SOCK = $DATA_DIR . 'ROPE.socket';
  64. my $PUSH_TIMEOUT = 20;
  65. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  66. # Debug
  67. my $DEBUG = 0;
  68. if ($ENV{'DEBUG'}) {
  69. $DEBUG = 1;
  70. }
  71. # Command-line arguments
  72. my ($DELAY) = @ARGV;
  73. if (!$DELAY) {
  74. $DELAY = $PULL_TIMEOUT / 2;
  75. }
  76. # Sanity check
  77. if (!-d $DATA_DIR) {
  78. die("Bad config\n");
  79. }
  80. # Sockets
  81. my $select = DMX::stateSocket($STATE_SOCK);
  82. DMX::stateSubscribe($STATE_SOCK);
  83. my $dmx_fh = DMX::dmxSock();
  84. # State
  85. my $state = 'INIT';
  86. my $stateLast = $state;
  87. my %exists = ();
  88. my $pushLast = 0;
  89. my $pullLast = time();
  90. # Always force lights out at launch
  91. dim({ 'channel' => 0, 'value' => 0, 'time' => 0 });
  92. # Loop forever
  93. while (1) {
  94. # Set anywhere to force an update this cycle
  95. my $forceUpdate = 0;
  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. # Read the global state
  102. my $cmdState = DMX::parseState($fh, \%exists);
  103. # Only accept valid states
  104. if (!defined($DIM{$cmdState})) {
  105. print STDERR 'Invalid state: ' . $cmdState . "\n";
  106. next;
  107. }
  108. # Propogate the most recent command state
  109. $newState = $cmdState;
  110. $pullLast = time();
  111. }
  112. # Calculate the new state
  113. $stateLast = $state;
  114. if ($exists{'LIGHTS'}) {
  115. if ($newState eq 'PLAY') {
  116. $newState = 'PLAY_HIGH';
  117. }
  118. } else {
  119. if ($newState eq 'PLAY_HIGH') {
  120. $newState = 'PLAY';
  121. }
  122. }
  123. $state = $newState;
  124. # Force updates on a periodic basis
  125. if (time() - $pushLast > $PUSH_TIMEOUT) {
  126. $forceUpdate = 1;
  127. }
  128. # Die if we don't see regular updates
  129. if (time() - $pullLast > $PULL_TIMEOUT) {
  130. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  131. }
  132. # Update the lighting
  133. if ($forceUpdate || $stateLast ne $state) {
  134. if ($DEBUG) {
  135. print STDERR 'State: ' . $stateLast . ' => ' . $state . "\n";
  136. foreach my $data (@{ $DIM{$state} }) {
  137. my $delay = '';
  138. if ($data->{'delay'}) {
  139. $delay = ' (Delay: ' . $data->{'delay'} . ')';
  140. }
  141. print STDERR "\t" . $data->{'channel'} . ' => ' . $data->{'value'} . ' @ ' . $data->{'time'} . $delay . "\n";
  142. }
  143. }
  144. # Send the dim command
  145. my @values = ();
  146. foreach my $data (@{ $DIM{$state} }) {
  147. dim($data);
  148. push(@values, $data->{'channel'} . ' => ' . $data->{'value'} . ' @ ' . $data->{'time'});
  149. }
  150. # Save the state and value to disk
  151. my ($fh, $tmp) = tempfile($DATA_DIR . 'ROPE.XXXXXXXX', 'UNLINK' => 0);
  152. print $fh 'State: ' . $state . "\n" . join("\n", @values) . "\n";
  153. close($fh);
  154. rename($tmp, $DATA_DIR . 'ROPE');
  155. # Update the push time
  156. $pushLast = time();
  157. }
  158. }
  159. # Send the command
  160. sub dim($) {
  161. my ($args) = @_;
  162. if (!defined($args->{'delay'})) {
  163. $args->{'delay'} = 0;
  164. }
  165. if (!defined($args->{'channel'}) || !defined($args->{'time'}) || !defined($args->{'value'})) {
  166. die('Invalid command for socket: ' . join(', ', keys(%{$args})) . ': ' . join(', ', values(%{$args})) . "\n");
  167. }
  168. my $cmd = join(':', $args->{'channel'}, $args->{'time'}, $args->{'value'}, $args->{'delay'});
  169. $dmx_fh->send($cmd)
  170. or die('Unable to write command to DMX socket: ' . $cmd . ": ${!}\n");
  171. }