rope.pl 4.4 KB

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