state.pl 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. 'PLAYING' => 'MTIME',
  19. 'GUI' => 'MTIME',
  20. 'MOTION' => 'MTIME',
  21. 'PROJECTOR' => 'STATUS',
  22. 'AMPLIFIER' => 'STATUS_NOMTIME',
  23. 'PLAY_STATUS' => 'STATUS',
  24. 'LIGHTS' => 'EXISTS',
  25. 'FAN_CMD' => 'EXISTS',
  26. 'RAVE' => 'EXISTS_OFF',
  27. '/mnt/media/DMX/cmd/GARAGE_CMD' => 'EXISTS_CLEAR',
  28. );
  29. # App config
  30. my $SOCK_TIMEOUT = 5;
  31. my $DATA_DIR = DMX::dataDir();
  32. my $CMD_FILE = $DATA_DIR . 'STATE.socket';
  33. my $MAX_CMD_LEN = 4096;
  34. my $RESET_CMD = $ENV{'HOME'} . '/bin/video/dmx/reset.sh';
  35. my $PUSH_TIMEOUT = 20;
  36. # Debug
  37. my $DEBUG = 0;
  38. if ($ENV{'DEBUG'}) {
  39. $DEBUG = 1;
  40. }
  41. # Command-line arguments
  42. my ($DELAY) = @ARGV;
  43. if (!$DELAY) {
  44. $DELAY = 0.5;
  45. }
  46. # Socket init
  47. my $select = DMX::stateSocket($CMD_FILE);
  48. # Reset all dependents at starts
  49. system($RESET_CMD);
  50. # Subscribers
  51. my @subscribers = ();
  52. # State
  53. my $state = 'INIT';
  54. my $stateLast = $state;
  55. my $playing = 0;
  56. my $updateLast = 0;
  57. my $pushLast = 0;
  58. # Init the file tracking structure
  59. my %files = ();
  60. foreach my $file (keys(%MON_FILES)) {
  61. my %tmp = (
  62. 'name' => basename($file),
  63. 'type' => $MON_FILES{$file},
  64. 'path' => $DATA_DIR . $file,
  65. 'update' => 0,
  66. 'status' => 0,
  67. 'last' => 0,
  68. );
  69. # Allow absolute paths to override the $DATA_DIR path
  70. if ($file =~ /^\//) {
  71. $tmp{'path'} = $file;
  72. }
  73. # Record the directory name for folder monitoring
  74. $tmp{'dir'} = dirname($tmp{'path'});
  75. # Push a hash ref
  76. $files{$file} = \%tmp;
  77. }
  78. # Loop forever
  79. while (1) {
  80. # Reset the folder monitor state
  81. my %folders = ();
  82. # Provide a method to force updates even when the state does not change
  83. my $update = 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. $update = 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'} =~ /^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'} =~ /^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. # Use opendir/readdir before calling -e, to ensure fresh results
  148. if (!$folders{ $file->{'dir'} }) {
  149. my $dh = undef();
  150. if (opendir($dh, $file->{'dir'})) {
  151. if ($DEBUG) {
  152. print STDERR 'Refreshed folder: ' . $file->{'dir'} . "\n";
  153. }
  154. my @files = readdir($dh);
  155. closedir($dh);
  156. }
  157. $folders{ $file->{'dir'} } = 1;
  158. }
  159. if (-e $file->{'path'}) {
  160. $file->{'status'} = 1;
  161. }
  162. if ($DEBUG) {
  163. print STDERR 'Exists: ' . $file->{'name'} . ': ' . $file->{'status'} . "\n";
  164. }
  165. }
  166. }
  167. # Set the global update timestamp
  168. foreach my $file (values(%files)) {
  169. if ($file->{'update'} > $updateLast) {
  170. $updateLast = $file->{'update'};
  171. }
  172. }
  173. # Calculate the new state
  174. $stateLast = $state;
  175. if ($files{'PROJECTOR'}->{'status'}) {
  176. # We are always either playing or paused if the projector is on
  177. if ($files{'PLAY_STATUS'}->{'status'}) {
  178. $state = 'PLAY';
  179. } else {
  180. $state = 'PAUSE';
  181. }
  182. } else {
  183. # If the projector is off, check the timeouts
  184. my $timeSinceUpdate = time() - $updateLast;
  185. if ($timeSinceUpdate > $STATE_TIMEOUT) {
  186. $state = 'OFF';
  187. } elsif ($timeSinceUpdate < $STATE_TIMEOUT) {
  188. $state = 'MOTION';
  189. }
  190. }
  191. if ($state eq 'INIT') {
  192. $state = 'OFF';
  193. }
  194. # Clear exists files when the main state is "OFF" (and before we append their status)
  195. foreach my $file (values(%files)) {
  196. if ($file->{'type'} eq 'EXISTS' && $file->{'status'} && $state eq 'OFF') {
  197. unlink($file->{'path'});
  198. $file->{'stauts'} = 0;
  199. if ($DEBUG) {
  200. print STDERR 'Clearing exists flag for: ' . $file->{'name'} . "\n";
  201. }
  202. }
  203. }
  204. # Append the status of all files
  205. {
  206. my @status = ();
  207. foreach my $file (values(%files)) {
  208. push(@status, $file->{'name'} . ':' . $file->{'status'});
  209. }
  210. $state .= ' (' . join(', ', @status) . ')';
  211. }
  212. # Clear exists_clear files immediately (but after we append their status)
  213. foreach my $file (values(%files)) {
  214. if ($file->{'type'} eq 'EXISTS_CLEAR' && $file->{'status'}) {
  215. unlink($file->{'path'});
  216. $file->{'stauts'} = 0;
  217. if ($DEBUG) {
  218. print STDERR 'Clearing exists flag for: ' . $file->{'name'} . "\n";
  219. }
  220. }
  221. }
  222. # Force updates on a periodic basis
  223. if (time() - $pushLast > $PUSH_TIMEOUT) {
  224. $update = 1;
  225. }
  226. # Update on any status change
  227. foreach my $file (values(%files)) {
  228. if ($file->{'status'} != $file->{'last'}) {
  229. $update = 1;
  230. last;
  231. }
  232. }
  233. # Force updates on any state change
  234. if ($stateLast ne $state) {
  235. if ($DEBUG) {
  236. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  237. }
  238. $update = 1;
  239. }
  240. # Push the update
  241. if ($update) {
  242. # Note the push
  243. $pushLast = time();
  244. # Send notifications to all subscribers
  245. foreach my $sub (@subscribers) {
  246. # Drop subscribers that are not available
  247. if (!eval { $sub->{'socket'}->send($state) }) {
  248. print STDERR 'Dropping bad socket from subscriber list: ' . $sub->{'path'} . "\n";
  249. my @new_subscribers = ();
  250. foreach my $new (@subscribers) {
  251. if ($new->{'socket'} eq $sub->{'socket'}) {
  252. next;
  253. }
  254. push(@new_subscribers, $new);
  255. }
  256. @subscribers = @new_subscribers;
  257. }
  258. }
  259. # Save the state and value to disk
  260. my ($fh, $tmp) = tempfile($DATA_DIR . 'STATE.XXXXXXXX', 'UNLINK' => 0);
  261. print $fh $state . "\n";
  262. close($fh);
  263. rename($tmp, $DATA_DIR . 'STATE');
  264. }
  265. }
  266. sub mtime($) {
  267. my ($file) = @_;
  268. my $mtime = 0;
  269. if (-r $file) {
  270. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), $mtime, undef(), undef(), undef()) = stat($file);
  271. }
  272. return $mtime;
  273. }