rope.pl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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' => [ { 'channel' => 0, 'value' => 0, 'time' => 60000 } ],
  13. 'PLAY' => [ { 'channel' => 1, 'value' => 64, 'time' => 500 }, { 'channel' => 2, 'value' => 32, 'time' => 500 }, { 'channel' => 3, 'value' => 192, 'time' => 1000 }, { 'channel' => 5, 'value' => 32, 'time' => 500 }, { 'channel' => 6, 'value' => 32, 'time' => 500 } ],
  14. 'PAUSE' => [ { 'channel' => 1, 'value' => 255, 'time' => 1000 }, { 'channel' => 2, 'value' => 192, 'time' => 10000 }, { 'channel' => 3, 'value' => 192, 'time' => 1000 }, { 'channel' => 5, 'value' => 192, 'time' => 10000 }, { 'channel' => 6, 'value' => 192, 'time' => 10000 } ],
  15. 'MOTION' => [ { 'channel' => 1, 'value' => 255, 'time' => 1000 }, { 'channel' => 2, 'value' => 192, 'time' => 1000 }, { 'channel' => 3, 'value' => 192, 'time' => 1000 }, { 'channel' => 5, 'value' => 192, 'time' => 1000 }, { 'channel' => 6, 'value' => 192, 'time' => 1000 } ],
  16. );
  17. my $TIMEOUT = 300; # Seconds
  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, PLAYING, and MOTION 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. $mtime = mtime($DATA_DIR . 'MOTION');
  106. if ($mtime > $updateLast) {
  107. $updateLast = $mtime;
  108. }
  109. }
  110. # Calculate the new state
  111. $stateLast = $state;
  112. if ($projector) {
  113. # We are always either playing or paused if the projector is on
  114. if ($playing) {
  115. $state = 'PLAY';
  116. } else {
  117. $state = 'PAUSE';
  118. }
  119. } else {
  120. # If the projector is off, check the timeouts
  121. my $timeSinceUpdate = time() - $updateLast;
  122. if ($state ne 'OFF' && $timeSinceUpdate > $TIMEOUT) {
  123. $state = 'OFF';
  124. } elsif ($state eq 'OFF' && $timeSinceUpdate < $TIMEOUT) {
  125. $state = 'MOTION';
  126. }
  127. }
  128. if ($state eq 'INIT') {
  129. $state = 'OFF';
  130. }
  131. # Update the lighting state
  132. if ($stateLast ne $state) {
  133. if ($DEBUG) {
  134. print STDERR 'State: ' . $stateLast . ' => ' . $state . "\n";
  135. foreach my $data (@{ $DIM{$state} }) {
  136. print STDERR "\t" . $data->{'channel'} . ' => ' . $data->{'value'} . ' @ ' . $data->{'time'} . "\n";
  137. }
  138. }
  139. # Send the dim command
  140. my @values = ();
  141. foreach my $data (@{ $DIM{$state} }) {
  142. dim($data->{'channel'}, $data->{'time'}, $data->{'value'});
  143. push(@values, $data->{'channel'} . ' => ' . $data->{'value'} . ' @ ' . $data->{'time'});
  144. }
  145. # Save the state and value to disk
  146. my ($fh, $tmp) = tempfile($DATA_DIR . 'ROPE.XXXXXXXX', 'UNLINK' => 0);
  147. print $fh 'State: ' . $state . "\nValues: " . join("\n", @values) . "\n";
  148. close($fh);
  149. rename($tmp, $DATA_DIR . 'ROPE');
  150. }
  151. # Wait and loop
  152. usleep($DELAY);
  153. }
  154. sub mtime($) {
  155. my ($file) = @_;
  156. my $mtime = 0;
  157. if (-r $file) {
  158. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), $mtime, undef(), undef(), undef()) = stat($file);
  159. }
  160. return $mtime;
  161. }
  162. # Send the command
  163. sub dim($$$) {
  164. my ($channel, $duration, $intensity) = @_;
  165. my $cmd = join(':', $channel, $duration, $intensity);
  166. $sock->send($cmd)
  167. or die('Unable to write command to socket: ' . $CMD_FILE . ': ' . $cmd . ": ${!}\n");
  168. }