state.pl 5.1 KB

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