state.pl 6.5 KB

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