state.pl 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IO::Select;
  5. use IO::Socket::UNIX;
  6. use File::Basename;
  7. use File::Temp qw( tempfile );
  8. # Local modules
  9. use Cwd qw(abs_path);
  10. use File::Basename qw(dirname);
  11. use lib dirname(abs_path($0));
  12. use DMX;
  13. # Prototypes
  14. sub mtime($);
  15. # User config
  16. my $STATE_TIMEOUT = 180;
  17. my %MON_FILES = (
  18. 'GUI' => 'MTIME',
  19. 'MOTION' => 'MTIME',
  20. 'PROJECTOR' => 'STATUS',
  21. 'AMPLIFIER' => 'STATUS_NOMTIME',
  22. 'PLAYING' => 'PLAYING',
  23. 'LIGHTS' => 'EXISTS',
  24. 'FAN_CMD' => 'EXISTS',
  25. 'RAVE' => 'EXISTS_OFF',
  26. '/mnt/media/DMX/cmd/GARAGE_CMD' => 'EXISTS_CLEAR',
  27. );
  28. # App config
  29. my $SOCK_TIMEOUT = 5;
  30. my $DATA_DIR = DMX::dataDir();
  31. my $CMD_FILE = $DATA_DIR . 'STATE.socket';
  32. my $MAX_CMD_LEN = 4096;
  33. my $RESET_CMD = $ENV{'HOME'} . '/bin/video/dmx/reset.sh';
  34. my $PUSH_TIMEOUT = 20;
  35. # Debug
  36. my $DEBUG = 0;
  37. if ($ENV{'DEBUG'}) {
  38. $DEBUG = 1;
  39. }
  40. # Command-line arguments
  41. my ($DELAY) = @ARGV;
  42. if (!$DELAY) {
  43. $DELAY = 0.5;
  44. }
  45. # Socket init
  46. my $select = DMX::stateSocket($CMD_FILE);
  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 $updateLast = 0;
  55. my $pushLast = 0;
  56. # Init the file tracking structure
  57. my %files = ();
  58. foreach my $file (keys(%MON_FILES)) {
  59. my %tmp = (
  60. 'name' => basename($file),
  61. 'type' => $MON_FILES{$file},
  62. 'path' => $DATA_DIR . $file,
  63. 'update' => 0,
  64. 'status' => 0,
  65. 'last' => 0,
  66. );
  67. # Allow absolute paths to override the $DATA_DIR path
  68. if ($file =~ /^\//) {
  69. $tmp{'path'} = $file;
  70. }
  71. # Record the directory name for folder monitoring
  72. $tmp{'dir'} = dirname($tmp{'path'});
  73. # Push a hash ref
  74. $files{$file} = \%tmp;
  75. }
  76. # Loop forever
  77. while (1) {
  78. # Reset the folder monitor state
  79. my %folders = ();
  80. # Provide a method to force updates even when the state does not change
  81. my $update = 0;
  82. # Check for queued commands
  83. my @ready_clients = $select->can_read($DELAY);
  84. foreach my $fh (@ready_clients) {
  85. # Grab the inbound text
  86. my $path = undef();
  87. $fh->recv($path, $MAX_CMD_LEN);
  88. $path =~ s/^\s+//;
  89. $path =~ s/\s+$//;
  90. if ($DEBUG) {
  91. print STDERR 'Got path: ' . $path . "\n";
  92. }
  93. # Only accept valid paths
  94. if (!-r $path) {
  95. print STDERR 'Invalid socket path: ' . $path . "\n";
  96. next;
  97. }
  98. # Open the new socket
  99. my $sub = IO::Socket::UNIX->new(
  100. 'Peer' => $path,
  101. 'Type' => SOCK_DGRAM,
  102. 'Timeout' => $SOCK_TIMEOUT
  103. );
  104. if (!$sub) {
  105. print STDERR 'Unable to open socket: ' . $path . ": ${@}\n";
  106. next;
  107. }
  108. # Add the socket to our subscriber list
  109. my %tmp = (
  110. 'path' => $path,
  111. 'socket' => $sub,
  112. );
  113. push(@subscribers, \%tmp);
  114. # Force an update
  115. $update = 1;
  116. }
  117. # Monitor files of all types
  118. foreach my $file (values(%files)) {
  119. # Always record the previous status and clear the new one
  120. $file->{'last'} = $file->{'status'};
  121. $file->{'status'} = 0;
  122. # Record the last update
  123. if ($file->{'type'} eq 'STATUS' || $file->{'type'} =~ /^MTIME/) {
  124. $file->{'update'} = mtime($file->{'path'});
  125. if ($file->{'type'} =~ /^MTIME/) {
  126. $file->{'status'} = $file->{'update'};
  127. }
  128. if ($DEBUG) {
  129. print STDERR 'Last change: ' . $file->{'name'} . ': ' . localtime($file->{'update'}) . "\n";
  130. }
  131. }
  132. # Grab the new status
  133. if ($file->{'type'} =~ /^STATUS/ || $file->{'type'} eq 'PLAYING') {
  134. my $text = undef();
  135. {
  136. my $fh;
  137. open($fh, $file->{'path'})
  138. or die('Unable to open ' . $file->{'path'} . "\n");
  139. local $/;
  140. $text = <$fh>;
  141. close($fh);
  142. }
  143. if ($file->{'type'} =~ /^STATUS/) {
  144. if ($text =~ /1/) {
  145. $file->{'status'} = 1;
  146. }
  147. } else {
  148. if ($text =~ /PlayStatus\:Playing/) {
  149. $file->{'status'} = 1;
  150. }
  151. }
  152. if ($DEBUG) {
  153. print STDERR 'Status: ' . $file->{'name'} . ': ' . $file->{'status'} . "\n";
  154. }
  155. }
  156. # Check for the presence of a file
  157. if ($file->{'type'} =~ /^EXISTS/) {
  158. # Use opendir/readdir before calling -e, to ensure fresh results
  159. if (!$folders{ $file->{'dir'} }) {
  160. my $dh = undef();
  161. if (opendir($dh, $file->{'dir'})) {
  162. if ($DEBUG) {
  163. print STDERR 'Refreshed folder: ' . $file->{'dir'} . "\n";
  164. }
  165. my @files = readdir($dh);
  166. closedir($dh);
  167. }
  168. $folders{ $file->{'dir'} } = 1;
  169. }
  170. if (-e $file->{'path'}) {
  171. $file->{'status'} = 1;
  172. }
  173. if ($DEBUG) {
  174. print STDERR 'Exists: ' . $file->{'name'} . ': ' . $file->{'status'} . "\n";
  175. }
  176. }
  177. }
  178. # Set the global update timestamp
  179. foreach my $file (values(%files)) {
  180. if ($file->{'update'} > $updateLast) {
  181. $updateLast = $file->{'update'};
  182. }
  183. }
  184. # Calculate the new state
  185. $stateLast = $state;
  186. if ($files{'PROJECTOR'}->{'status'}) {
  187. # We are always either playing or paused if the projector is on
  188. if ($files{'PLAYING'}->{'status'}) {
  189. $state = 'PLAY';
  190. } else {
  191. $state = 'PAUSE';
  192. }
  193. } else {
  194. # If the projector is off, check the timeouts
  195. my $timeSinceUpdate = time() - $updateLast;
  196. if ($timeSinceUpdate > $STATE_TIMEOUT) {
  197. $state = 'OFF';
  198. } elsif ($timeSinceUpdate < $STATE_TIMEOUT) {
  199. $state = 'MOTION';
  200. }
  201. }
  202. if ($state eq 'INIT') {
  203. $state = 'OFF';
  204. }
  205. # Clear exists files when the main state is "OFF" (and before we append their status)
  206. foreach my $file (values(%files)) {
  207. if ($file->{'type'} eq 'EXISTS' && $file->{'status'} && $state eq 'OFF') {
  208. unlink($file->{'path'});
  209. $file->{'stauts'} = 0;
  210. if ($DEBUG) {
  211. print STDERR 'Clearing exists flag for: ' . $file->{'name'} . "\n";
  212. }
  213. }
  214. }
  215. # Append the status of all files
  216. {
  217. my @status = ();
  218. foreach my $file (values(%files)) {
  219. push(@status, $file->{'name'} . ':' . $file->{'status'});
  220. }
  221. $state .= ' (' . join(', ', @status) . ')';
  222. }
  223. # Clear exists_clear files immediately (but after we append their status)
  224. foreach my $file (values(%files)) {
  225. if ($file->{'type'} eq 'EXISTS_CLEAR' && $file->{'status'}) {
  226. unlink($file->{'path'});
  227. $file->{'stauts'} = 0;
  228. if ($DEBUG) {
  229. print STDERR 'Clearing exists flag for: ' . $file->{'name'} . "\n";
  230. }
  231. }
  232. }
  233. # Force updates on a periodic basis
  234. if (time() - $pushLast > $PUSH_TIMEOUT) {
  235. $update = 1;
  236. }
  237. # Update on any status change
  238. foreach my $file (values(%files)) {
  239. if ($file->{'status'} != $file->{'last'}) {
  240. $update = 1;
  241. last;
  242. }
  243. }
  244. # Force updates on any state change
  245. if ($stateLast ne $state) {
  246. if ($DEBUG) {
  247. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  248. }
  249. $update = 1;
  250. }
  251. # Push the update
  252. if ($update) {
  253. # Note the push
  254. $pushLast = time();
  255. # Send notifications to all subscribers
  256. foreach my $sub (@subscribers) {
  257. # Drop subscribers that are not available
  258. if (!eval { $sub->{'socket'}->send($state) }) {
  259. print STDERR 'Dropping bad socket from subscriber list: ' . $sub->{'path'} . "\n";
  260. my @new_subscribers = ();
  261. foreach my $new (@subscribers) {
  262. if ($new->{'socket'} eq $sub->{'socket'}) {
  263. next;
  264. }
  265. push(@new_subscribers, $new);
  266. }
  267. @subscribers = @new_subscribers;
  268. }
  269. }
  270. # Save the state and value to disk
  271. my ($fh, $tmp) = tempfile($DATA_DIR . 'STATE.XXXXXXXX', 'UNLINK' => 0);
  272. print $fh $state . "\n";
  273. close($fh);
  274. rename($tmp, $DATA_DIR . 'STATE');
  275. }
  276. }
  277. sub mtime($) {
  278. my ($file) = @_;
  279. my $mtime = 0;
  280. if (-r $file) {
  281. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), $mtime, undef(), undef(), undef()) = stat($file);
  282. }
  283. return $mtime;
  284. }