state.pl 6.3 KB

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