rope.pl 4.4 KB

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