rope.pl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 $playingLast = $playing;
  48. my $valueLast = $DIM{'OFF'}{'value'};
  49. my $updateLast = 0;
  50. # Loop forever
  51. while (1) {
  52. # Monitor the PLAY_STATUS file for changes and state
  53. $playingLast = $playing;
  54. {
  55. my $mtime = mtime($DATA_DIR . '/PLAY_STATUS');
  56. if ($mtime > $updateLast) {
  57. $updateLast = $mtime;
  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. }
  69. }
  70. # Monitor the GUI and PLAYING files for changes only
  71. {
  72. my $mtime = mtime($DATA_DIR . '/PLAYING');
  73. if ($mtime > $updateLast) {
  74. $updateLast = $mtime;
  75. }
  76. $mtime = mtime($DATA_DIR . '/GUI');
  77. if ($mtime > $updateLast) {
  78. $updateLast = $mtime;
  79. }
  80. }
  81. # Calculate the new state
  82. $stateLast = $state;
  83. if ($playing != $playingLast) {
  84. if ($DEBUG) {
  85. print STDERR "Play state changed\n";
  86. }
  87. # We always need to update when the play state changes
  88. if ($playing) {
  89. $state = 'PLAY';
  90. } else {
  91. $state = 'PAUSE';
  92. }
  93. } elsif (!$playing) {
  94. # If we're not playing, check the timeout
  95. my $timeSinceUpdate = time() - $updateLast;
  96. if ($state ne 'OFF' && $timeSinceUpdate > $TIMEOUT) {
  97. $state = 'OFF';
  98. } elsif ($state eq 'OFF' && $timeSinceUpdate < $TIMEOUT) {
  99. $state = 'PAUSE';
  100. } elsif ($state eq 'INIT') {
  101. $state = 'OFF';
  102. }
  103. }
  104. # Update the lighting state
  105. if ($stateLast ne $state) {
  106. if ($DEBUG) {
  107. print STDERR 'New state: ' . $state . "\n";
  108. print STDERR "\tTime: " . $DIM{$state}{'time'} . "\n";
  109. print STDERR "\tFrom: " . $valueLast . "\n";
  110. print STDERR "\tTo: " . $DIM{$state}{'value'} . "\n";
  111. }
  112. if ($valueLast != $DIM{$state}{'value'}) {
  113. system($EXEC_DIR . '/dimChannels.py', $DIM{$state}{'time'}, $valueLast, $DIM{$state}{'value'});
  114. } elsif ($DEBUG) {
  115. print STDERR "Skipping noop dim request\n";
  116. }
  117. $valueLast = $DIM{$state}{'value'};
  118. # Save the state and value to disk
  119. my ($fh, $tmp) = tempfile($DATA_DIR . '/ROPE.XXXXXXXX', 'UNLINK' => 0);
  120. print $fh 'State: ' . $state . "\nValue: " . $valueLast . "\n";
  121. close($fh);
  122. rename($tmp, $DATA_DIR . '/ROPE');
  123. }
  124. # Wait and loop
  125. usleep($DELAY);
  126. }
  127. sub mtime($) {
  128. my ($file) = @_;
  129. my (undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), $mtime, undef(), undef(), undef()) = stat($file);
  130. return $mtime;
  131. }