state.pl 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. 'GUI' => 'MTIME',
  20. 'MOTION' => 'MTIME',
  21. 'PROJECTOR' => 'STATUS',
  22. 'AMPLIFIER' => 'STATUS',
  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_VALUE') {
  173. $text =~ s/\n$//;
  174. $file->{'status'} = $text;
  175. } else {
  176. if ($text =~ /1/) {
  177. $file->{'status'} = 1;
  178. }
  179. }
  180. if ($DEBUG) {
  181. print STDERR 'Status: ' . $file->{'name'} . ': ' . $file->{'status'} . "\n";
  182. }
  183. }
  184. # Check for the presence of EXISTS files and record their last change
  185. if ($file->{'type'} =~ /^EXISTS/) {
  186. my $mtime = mtime($file->{'path'});
  187. if ($mtime > 0) {
  188. $file->{'status'} = 1;
  189. $file->{'update'} = $mtime;
  190. } elsif ($file->{'last'}) {
  191. $file->{'update'} = time();
  192. }
  193. if ($DEBUG) {
  194. print STDERR 'Exists: ' . $file->{'name'} . ': ' . $file->{'status'} . ':' . $file->{'update'} . "\n";
  195. }
  196. }
  197. }
  198. # Set the global update timestamp
  199. foreach my $file (values(%files)) {
  200. if ($file->{'update'} > $updateLast) {
  201. $updateLast = $file->{'update'};
  202. }
  203. }
  204. # Calculate the new state
  205. $stateLast = $state;
  206. if ($files{'PROJECTOR'}->{'status'}) {
  207. # We are always either playing or paused if the projector is on
  208. if ($files{'PLAYING'}->{'status'}) {
  209. $state = 'PLAY';
  210. } else {
  211. $state = 'PAUSE';
  212. }
  213. } else {
  214. # If the projector is off, check the timeouts
  215. my $timeSinceUpdate = time() - $updateLast;
  216. if ($timeSinceUpdate > $STATE_TIMEOUT) {
  217. $state = 'OFF';
  218. } elsif ($timeSinceUpdate < $STATE_TIMEOUT) {
  219. if ($files{'NO_MOTION'}->{'status'}) {
  220. $state = 'OFF';
  221. } else {
  222. $state = 'MOTION';
  223. }
  224. }
  225. }
  226. if ($state eq 'INIT') {
  227. $state = 'OFF';
  228. }
  229. # Clear EXISTS_ON files when the main state is "OFF" (and before we append their status)
  230. foreach my $file (values(%files)) {
  231. if ($file->{'type'} eq 'EXISTS_ON' && $file->{'status'} && $state eq 'OFF') {
  232. unlink($file->{'path'});
  233. $file->{'stauts'} = 0;
  234. if ($DEBUG) {
  235. print STDERR 'Clearing exists flag for: ' . $file->{'name'} . "\n";
  236. }
  237. }
  238. }
  239. # Calculate the new status
  240. $statusLast = $status;
  241. {
  242. my @statTime = ();
  243. foreach my $file (values(%files)) {
  244. push(@statTime, $file->{'name'} . ':' . $file->{'status'} . ':' . $file->{'update'});
  245. }
  246. $status = ' (' . join(', ', @statTime) . ')';
  247. }
  248. # Clear EXISTS_CLEAR files immediately (but after we append their status)
  249. foreach my $file (values(%files)) {
  250. if ($file->{'type'} eq 'EXISTS_CLEAR' && $file->{'status'}) {
  251. unlink($file->{'path'});
  252. $file->{'stauts'} = 0;
  253. if ($DEBUG) {
  254. print STDERR 'Clearing exists flag for: ' . $file->{'name'} . "\n";
  255. }
  256. }
  257. }
  258. # Force updates on a periodic basis
  259. if (time() - $pushLast > $PUSH_TIMEOUT) {
  260. $update = 1;
  261. }
  262. # Update on any status change
  263. foreach my $file (values(%files)) {
  264. if ($file->{'status'} ne $file->{'last'}) {
  265. $update = 1;
  266. last;
  267. }
  268. }
  269. # Force updates on any state change
  270. if ($stateLast ne $state || $statusLast ne $status) {
  271. if ($DEBUG) {
  272. print STDERR 'State change: ' . $stateLast . $statusLast . ' => ' . $state . $status . "\n";
  273. }
  274. $update = 1;
  275. }
  276. # Push the update
  277. if ($update) {
  278. # Note the push
  279. $pushLast = time();
  280. # Send notifications to all subscribers
  281. foreach my $sub (@subscribers) {
  282. # Drop subscribers that are not available
  283. if (!eval { $sub->{'socket'}->send($state . $status) }) {
  284. print STDERR 'Dropping bad socket from subscriber list: ' . $sub->{'path'} . "\n";
  285. my @new_subscribers = ();
  286. foreach my $new (@subscribers) {
  287. if ($new->{'socket'} eq $sub->{'socket'}) {
  288. next;
  289. }
  290. push(@new_subscribers, $new);
  291. }
  292. @subscribers = @new_subscribers;
  293. }
  294. }
  295. # Save the state and value to disk
  296. my ($fh, $tmp) = tempfile($DATA_DIR . 'STATE.XXXXXXXX', 'UNLINK' => 0);
  297. print $fh $state . "\n";
  298. close($fh);
  299. rename($tmp, $DATA_DIR . 'STATE');
  300. }
  301. }
  302. sub mtime($) {
  303. my ($file) = @_;
  304. my $mtime = 0;
  305. if (-r $file) {
  306. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), $mtime, undef(), undef(), undef()) = stat($file);
  307. }
  308. return $mtime;
  309. }