state.pl 7.1 KB

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