state.pl 8.4 KB

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