rope.pl 5.5 KB

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