state.pl 6.3 KB

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