rope.pl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. # Grab the PLAY_STATUS value
  58. $playing = 0;
  59. my $fh;
  60. open($fh, $DATA_DIR . '/PLAY_STATUS')
  61. or die("Unable to open PLAY_STATUS\n");
  62. my $text = <$fh>;
  63. close($fh);
  64. if ($text =~ /1/) {
  65. $playing = 1;
  66. }
  67. }
  68. }
  69. # Monitor the PROJECTOR file for changes and state
  70. {
  71. my $mtime = mtime($DATA_DIR . '/PROJECTOR');
  72. if ($mtime > $updateLast) {
  73. $updateLast = $mtime;
  74. # Grab the PROJECTOR value
  75. $projector = 0;
  76. my $fh;
  77. open($fh, $DATA_DIR . '/PROJECTOR')
  78. or die("Unable to open PROJECTOR\n");
  79. my $text = <$fh>;
  80. close($fh);
  81. if ($text =~ /1/) {
  82. $projector = 1;
  83. }
  84. }
  85. }
  86. # Monitor the GUI and PLAYING files for changes only
  87. {
  88. my $mtime = mtime($DATA_DIR . '/PLAYING');
  89. if ($mtime > $updateLast) {
  90. $updateLast = $mtime;
  91. }
  92. $mtime = mtime($DATA_DIR . '/GUI');
  93. if ($mtime > $updateLast) {
  94. $updateLast = $mtime;
  95. }
  96. }
  97. # Calculate the new state
  98. $stateLast = $state;
  99. if ($projector) {
  100. # We are always either playing or paused if the projector is on
  101. if ($playing) {
  102. $state = 'PLAY';
  103. } else {
  104. $state = 'PAUSE';
  105. }
  106. } else {
  107. # If the projector is off, check the timeouts
  108. my $timeSinceUpdate = time() - $updateLast;
  109. if ($state ne 'OFF' && $timeSinceUpdate > $TIMEOUT) {
  110. $state = 'OFF';
  111. } elsif ($state eq 'OFF' && $timeSinceUpdate < $TIMEOUT) {
  112. $state = 'PAUSE';
  113. }
  114. }
  115. if ($state eq 'INIT') {
  116. $state = 'OFF';
  117. }
  118. # Update the lighting state
  119. if ($stateLast ne $state) {
  120. if ($DEBUG) {
  121. print STDERR 'New state: ' . $state . "\n";
  122. print STDERR "\tTime: " . $DIM{$state}{'time'} . "\n";
  123. print STDERR "\tFrom: " . $valueLast . "\n";
  124. print STDERR "\tTo: " . $DIM{$state}{'value'} . "\n";
  125. }
  126. if ($valueLast != $DIM{$state}{'value'}) {
  127. system($EXEC_DIR . '/dimChannels.py', $DIM{$state}{'time'}, $valueLast, $DIM{$state}{'value'});
  128. } elsif ($DEBUG) {
  129. print STDERR "Skipping noop dim request\n";
  130. }
  131. $valueLast = $DIM{$state}{'value'};
  132. # Save the state and value to disk
  133. my ($fh, $tmp) = tempfile($DATA_DIR . '/ROPE.XXXXXXXX', 'UNLINK' => 0);
  134. print $fh 'State: ' . $state . "\nValue: " . $valueLast . "\n";
  135. close($fh);
  136. rename($tmp, $DATA_DIR . '/ROPE');
  137. }
  138. # Wait and loop
  139. usleep($DELAY);
  140. }
  141. sub mtime($) {
  142. my ($file) = @_;
  143. my $mtime = 0;
  144. if (-r $file) {
  145. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), $mtime, undef(), undef(), undef()) = stat($file);
  146. }
  147. return $mtime;
  148. }