rope.pl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IO::Socket;
  5. use Time::HiRes qw( usleep );
  6. use File::Temp qw( tempfile );
  7. # Prototypes
  8. sub mtime($);
  9. sub dim($$$);
  10. # User config
  11. my %DIM = (
  12. 'OFF' => [ { 'value' => 0, 'time' => 60000 }, { 'value' => 0, 'time' => 60000 } ],
  13. 'PLAY' => [ { 'value' => 64, 'time' => 500 }, { 'value' => 32, 'time' => 500 } ],
  14. 'PAUSE' => [ { 'value' => 255, 'time' => 1000 }, { 'value' => 192, 'time' => 10000 } ]
  15. );
  16. my $TIMEOUT = 600; # Seconds
  17. my $NUM_CHANNELS = 2;
  18. # App config
  19. my $SOCK_TIMEOUT = 5;
  20. my $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
  21. chomp($TEMP_DIR);
  22. my $DATA_DIR = $TEMP_DIR . 'plexMonitor/';
  23. my $CMD_FILE = $DATA_DIR . 'DMX.socket';
  24. # Debug
  25. my $DEBUG = 0;
  26. if ($ENV{'DEBUG'}) {
  27. $DEBUG = 1;
  28. }
  29. # Command-line arguments
  30. my ($DELAY) = @ARGV;
  31. if (!$DELAY) {
  32. $DELAY = 0.5;
  33. }
  34. $DELAY *= 1000000; # Microseconds;
  35. # Sanity check
  36. if (!-d $DATA_DIR || !-S $CMD_FILE) {
  37. die("Bad config\n");
  38. }
  39. # Socket init
  40. my $sock = IO::Socket::UNIX->new(
  41. 'Peer' => $CMD_FILE,
  42. 'Type' => SOCK_DGRAM,
  43. 'Timeout' => $SOCK_TIMEOUT
  44. ) or die('Unable to open socket: ' . $CMD_FILE . ": ${@}\n");
  45. # State
  46. my $state = 'INIT';
  47. my $stateLast = $state;
  48. my $playing = 0;
  49. my $projector = 0;
  50. my $updateLast = 0;
  51. my @valueLast = ();
  52. for (my $i = 0 ; $i < $NUM_CHANNELS ; $i++) {
  53. push(@valueLast, 0);
  54. }
  55. # Always force lights out at launch
  56. dim(0, 0, 0);
  57. # Loop forever
  58. while (1) {
  59. # Monitor the PLAY_STATUS file for changes and state
  60. {
  61. my $mtime = mtime($DATA_DIR . 'PLAY_STATUS');
  62. if ($mtime > $updateLast) {
  63. $updateLast = $mtime;
  64. }
  65. # Grab the PLAY_STATUS value
  66. $playing = 0;
  67. my $fh;
  68. open($fh, $DATA_DIR . 'PLAY_STATUS')
  69. or die("Unable to open PLAY_STATUS\n");
  70. my $text = <$fh>;
  71. close($fh);
  72. if ($text =~ /1/) {
  73. $playing = 1;
  74. }
  75. if ($DEBUG) {
  76. print STDERR 'Playing: ' . $playing . "\n";
  77. }
  78. }
  79. # Monitor the PROJECTOR file for changes and state
  80. {
  81. my $mtime = mtime($DATA_DIR . 'PROJECTOR');
  82. if ($mtime > $updateLast) {
  83. $updateLast = $mtime;
  84. }
  85. # Grab the PROJECTOR value
  86. $projector = 0;
  87. my $fh;
  88. open($fh, $DATA_DIR . 'PROJECTOR')
  89. or die("Unable to open PROJECTOR\n");
  90. my $text = <$fh>;
  91. close($fh);
  92. if ($text =~ /1/) {
  93. $projector = 1;
  94. }
  95. if ($DEBUG) {
  96. print STDERR 'Projector: ' . $projector . "\n";
  97. }
  98. }
  99. # Monitor the GUI and PLAYING files for changes only
  100. {
  101. my $mtime = mtime($DATA_DIR . 'PLAYING');
  102. if ($mtime > $updateLast) {
  103. $updateLast = $mtime;
  104. }
  105. $mtime = mtime($DATA_DIR . 'GUI');
  106. if ($mtime > $updateLast) {
  107. $updateLast = $mtime;
  108. }
  109. }
  110. # Calculate the new state
  111. $stateLast = $state;
  112. if ($projector) {
  113. # We are always either playing or paused if the projector is on
  114. if ($playing) {
  115. $state = 'PLAY';
  116. } else {
  117. $state = 'PAUSE';
  118. }
  119. } else {
  120. # If the projector is off, check the timeouts
  121. my $timeSinceUpdate = time() - $updateLast;
  122. if ($state ne 'OFF' && $timeSinceUpdate > $TIMEOUT) {
  123. $state = 'OFF';
  124. } elsif ($state eq 'OFF' && $timeSinceUpdate < $TIMEOUT) {
  125. $state = 'PAUSE';
  126. }
  127. }
  128. if ($state eq 'INIT') {
  129. $state = 'OFF';
  130. }
  131. # Update the lighting state
  132. if ($stateLast ne $state) {
  133. if ($DEBUG) {
  134. print STDERR 'State: ' . $stateLast . ' => ' . $state . "\n";
  135. for (my $i = 0 ; $i < $NUM_CHANNELS ; $i++) {
  136. print STDERR 'Channel: ' . ($i + 1) . "\n";
  137. print STDERR "\tFrom: " . $valueLast[$i] . "\n";
  138. print STDERR "\tTo: " . $DIM{$state}[$i]{'value'} . "\n";
  139. print STDERR "\tOver: " . $DIM{$state}[$i]{'time'} . "\n";
  140. }
  141. }
  142. # Send the dim command
  143. for (my $i = 0 ; $i < $NUM_CHANNELS ; $i++) {
  144. dim($i + 1, $DIM{$state}[$i]{'time'}, $DIM{$state}[$i]{'value'});
  145. $valueLast[$i] = $DIM{$state}[$i]{'value'};
  146. }
  147. # Save the state and value to disk
  148. my ($fh, $tmp) = tempfile($DATA_DIR . 'ROPE.XXXXXXXX', 'UNLINK' => 0);
  149. print $fh 'State: ' . $state . "\nValue: " . join(',', @valueLast) . "\n";
  150. close($fh);
  151. rename($tmp, $DATA_DIR . 'ROPE');
  152. }
  153. # Wait and loop
  154. usleep($DELAY);
  155. }
  156. sub mtime($) {
  157. my ($file) = @_;
  158. my $mtime = 0;
  159. if (-r $file) {
  160. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), $mtime, undef(), undef(), undef()) = stat($file);
  161. }
  162. return $mtime;
  163. }
  164. # Send the command
  165. sub dim($$$) {
  166. my ($channel, $duration, $intensity) = @_;
  167. my $cmd = join(':', $channel, $duration, $intensity);
  168. $sock->send($cmd)
  169. or die('Unable to write command to socket: ' . $CMD_FILE . ': ' . $cmd . ": ${!}\n");
  170. }