rope.pl 5.1 KB

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