rope.pl 4.0 KB

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