rope.pl 4.2 KB

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