state.pl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IO::Select;
  5. use IO::Socket::UNIX;
  6. use File::Temp qw( tempfile );
  7. # Prototypes
  8. sub mtime($);
  9. # User config
  10. my $STATE_TIMEOUT = 180;
  11. # App config
  12. my $SOCK_TIMEOUT = 5;
  13. my $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
  14. chomp($TEMP_DIR);
  15. my $DATA_DIR = $TEMP_DIR . 'plexMonitor/';
  16. my $CMD_FILE = $DATA_DIR . 'STATE.socket';
  17. my $MAX_CMD_LEN = 4096;
  18. # Debug
  19. my $DEBUG = 0;
  20. if ($ENV{'DEBUG'}) {
  21. $DEBUG = 1;
  22. }
  23. # Command-line arguments
  24. my ($DELAY) = @ARGV;
  25. if (!$DELAY) {
  26. $DELAY = 0.5;
  27. }
  28. # Sanity check
  29. if (!-d $DATA_DIR) {
  30. die("Bad config\n");
  31. }
  32. # Socket init
  33. if (-e $CMD_FILE) {
  34. unlink($CMD_FILE);
  35. }
  36. my $sock = IO::Socket::UNIX->new(
  37. 'Local' => $CMD_FILE,
  38. 'Type' => SOCK_DGRAM
  39. ) or die('Unable to open socket: ' . $CMD_FILE . ": ${@}\n");
  40. if (!-S $CMD_FILE) {
  41. die('Failed to create socket: ' . $CMD_FILE . "\n");
  42. }
  43. my $select = IO::Select->new($sock)
  44. or die('Unable to select socket: ' . $CMD_FILE . ": ${!}\n");
  45. # Subscribers
  46. my @subscribers = ();
  47. # State
  48. my $state = 'INIT';
  49. my $stateLast = $state;
  50. my $playing = 0;
  51. my $projector = 0;
  52. my $updateLast = 0;
  53. # Loop forever
  54. while (1) {
  55. # Check for queued commands
  56. my @ready_clients = $select->can_read($DELAY);
  57. foreach my $fh (@ready_clients) {
  58. # Grab the inbound text
  59. my $path = undef();
  60. $fh->recv($path, $MAX_CMD_LEN);
  61. $path =~ s/^\s+//;
  62. $path =~ s/\s+$//;
  63. if ($DEBUG) {
  64. print STDERR 'Got path: ' . $path . "\n";
  65. }
  66. # Only accept valid paths
  67. if (!-r $path) {
  68. print STDERR 'Invalid socket path: ' . $path . "\n";
  69. next;
  70. }
  71. # Open the new socket
  72. my $sub = IO::Socket::UNIX->new(
  73. 'Peer' => $path,
  74. 'Type' => SOCK_DGRAM,
  75. 'Timeout' => $SOCK_TIMEOUT
  76. );
  77. if (!$sub) {
  78. print STDERR 'Unable to open socket: ' . $path . ": ${@}\n";
  79. next;
  80. }
  81. # Add the socket to our subscriber list
  82. my %tmp = (
  83. 'path' => $path,
  84. 'socket' => $sub,
  85. );
  86. push(@subscribers, \%tmp);
  87. }
  88. # Monitor the PLAY_STATUS file for changes and state
  89. {
  90. my $mtime = mtime($DATA_DIR . 'PLAY_STATUS');
  91. if ($mtime > $updateLast) {
  92. $updateLast = $mtime;
  93. }
  94. # Grab the PLAY_STATUS value
  95. $playing = 0;
  96. my $fh;
  97. open($fh, $DATA_DIR . 'PLAY_STATUS')
  98. or die("Unable to open PLAY_STATUS\n");
  99. my $text = <$fh>;
  100. close($fh);
  101. if ($text =~ /1/) {
  102. $playing = 1;
  103. }
  104. if ($DEBUG) {
  105. print STDERR 'Playing: ' . $playing . "\n";
  106. }
  107. }
  108. # Monitor the PROJECTOR file for changes and state
  109. {
  110. my $mtime = mtime($DATA_DIR . 'PROJECTOR');
  111. if ($mtime > $updateLast) {
  112. $updateLast = $mtime;
  113. }
  114. # Grab the PROJECTOR value
  115. $projector = 0;
  116. my $fh;
  117. open($fh, $DATA_DIR . 'PROJECTOR')
  118. or die("Unable to open PROJECTOR\n");
  119. my $text = <$fh>;
  120. close($fh);
  121. if ($text =~ /1/) {
  122. $projector = 1;
  123. }
  124. if ($DEBUG) {
  125. print STDERR 'Projector: ' . $projector . "\n";
  126. }
  127. }
  128. # Monitor the GUI, PLAYING, and MOTION files for changes only
  129. {
  130. my $mtime = mtime($DATA_DIR . 'PLAYING');
  131. if ($mtime > $updateLast) {
  132. $updateLast = $mtime;
  133. }
  134. $mtime = mtime($DATA_DIR . 'GUI');
  135. if ($mtime > $updateLast) {
  136. $updateLast = $mtime;
  137. }
  138. $mtime = mtime($DATA_DIR . 'MOTION');
  139. if ($mtime > $updateLast) {
  140. $updateLast = $mtime;
  141. }
  142. }
  143. # Calculate the new state
  144. $stateLast = $state;
  145. if ($projector) {
  146. # We are always either playing or paused if the projector is on
  147. if ($playing) {
  148. $state = 'PLAY';
  149. } else {
  150. $state = 'PAUSE';
  151. }
  152. } else {
  153. # If the projector is off, check the timeouts
  154. my $timeSinceUpdate = time() - $updateLast;
  155. if ($timeSinceUpdate > $STATE_TIMEOUT) {
  156. $state = 'OFF';
  157. } elsif ($timeSinceUpdate < $STATE_TIMEOUT) {
  158. $state = 'MOTION';
  159. }
  160. }
  161. if ($state eq 'INIT') {
  162. $state = 'OFF';
  163. }
  164. # Update the state
  165. if ($stateLast ne $state) {
  166. if ($DEBUG) {
  167. print STDERR 'State: ' . $stateLast . ' => ' . $state . "\n";
  168. }
  169. # Send notifications to all subscribers
  170. foreach my $sub (@subscribers) {
  171. # Drop subscribers that are not available
  172. if (!$sub->{'socket'}->send($state)) {
  173. print STDERR 'Unable to write state to socket: ' . $sub->{'path'} . ': ' . $state . ": ${!}\n";
  174. print STDERR 'Dropping bad socket from subscriber list: ' . $sub->{'path'} . "\n";
  175. my @new_subscribers = ();
  176. foreach my $new (@subscribers) {
  177. if ($new->{'socket'} eq $sub->{'socket'}) {
  178. next;
  179. }
  180. push(@new_subscribers, $new);
  181. }
  182. @subscribers = @new_subscribers;
  183. }
  184. }
  185. # Save the state and value to disk
  186. my ($fh, $tmp) = tempfile($DATA_DIR . 'STATE.XXXXXXXX', 'UNLINK' => 0);
  187. print $fh $state . "\n";
  188. close($fh);
  189. rename($tmp, $DATA_DIR . 'STATE');
  190. }
  191. }
  192. sub mtime($) {
  193. my ($file) = @_;
  194. my $mtime = 0;
  195. if (-r $file) {
  196. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), $mtime, undef(), undef(), undef()) = stat($file);
  197. }
  198. return $mtime;
  199. }