state.pl 4.9 KB

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