state.pl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IO::Select;
  5. use File::Basename;
  6. use File::Temp qw( tempfile );
  7. use Sys::Hostname;
  8. use IPC::System::Simple qw( system capture );
  9. # Local modules
  10. use Cwd qw(abs_path);
  11. use File::Basename qw(dirname);
  12. use lib dirname( abs_path($0) );
  13. use DMX;
  14. # Prototypes
  15. sub mtime($);
  16. # User config
  17. my %DISPLAY_DEVS = ( 'TV' => 1, 'PROJECTOR' => 1 );
  18. my $DISPLAY = undef();
  19. my $STATE_TIMEOUT = 180;
  20. my $EXISTS_TIMEOUT = 900;
  21. my $START_DELAY = 15;
  22. my %MON_FILES = ();
  23. # Host-specific config
  24. # This is necessary to avoid contention on shared disks
  25. my $HOST = Sys::Hostname::hostname();
  26. if ( $HOST =~ /loki/i ) {
  27. # Ensure the media path is available before we get going
  28. my $MEDIA_PATH = undef();
  29. {
  30. my $mount = $ENV{'HOME'} . '/bin/video/mediaPath';
  31. $MEDIA_PATH = capture($mount);
  32. }
  33. if ( !$MEDIA_PATH || !-d $MEDIA_PATH ) {
  34. die("Unable to access media path\n");
  35. }
  36. # Door remote command
  37. $MON_FILES{ $MEDIA_PATH . '/DMX/cmd/DOOR_CMD' } = 'EXISTS-LINE-CLEAR';
  38. # Rumble pack
  39. $MON_FILES{'RUMBLE'} = 'LINE-NOUPDATE';
  40. $MON_FILES{ $MEDIA_PATH . '/DMX/cmd/RUMBLE_CMD' } = 'LINE-NOUPDATE';
  41. $MON_FILES{ $MEDIA_PATH . '/DMX/cmd/RUMBLE_VALUE' } = 'LINE-NOUPDATE';
  42. $MON_FILES{ $MEDIA_PATH . '/DMX/cmd/RUMBLE_DELAY_MIN' } = 'LINE-NOUPDATE';
  43. $MON_FILES{ $MEDIA_PATH . '/DMX/cmd/RUMBLE_DELAY_MAX' } = 'LINE-NOUPDATE';
  44. }
  45. # Available state files
  46. {
  47. # Plex
  48. $MON_FILES{'PLEX'} = 'NONE';
  49. $MON_FILES{'GUI'} = 'NONE';
  50. $MON_FILES{'PLAYING'} = 'PLAYING';
  51. # RiffTrax
  52. $MON_FILES{'RIFF'} = 'LINE-NOUPDATE';
  53. # Motion detection
  54. $MON_FILES{'MOTION'} = 'MTIME';
  55. $MON_FILES{'MOTION_STAIRS'} = 'MTIME-NOUPDATE';
  56. $MON_FILES{'MOTION_GARAGE'} = 'MTIME-NOUPDATE';
  57. $MON_FILES{'NO_MOTION'} = 'EXISTS-NOUPDATE';
  58. # Projector
  59. $MON_FILES{'PROJECTOR'} = 'STATUS';
  60. $MON_FILES{'PROJECTOR_COLOR'} = 'LINE-NOUPDATE';
  61. $MON_FILES{'PROJECTOR_INPUT'} = 'LINE-NOUPDATE';
  62. $MON_FILES{'PROJECTOR_LAMP'} = 'LINE-NOUPDATE';
  63. # Projector Control
  64. $MON_FILES{'PROJECTOR_CTRL_COLOR'} = 'LINE-NOUPDATE';
  65. $MON_FILES{'PROJECTOR_CTRL_LIFE'} = 'LINE-NOUPDATE';
  66. $MON_FILES{'PROJECTOR_CTRL_POWER'} = 'LINE-NOUPDATE';
  67. $MON_FILES{'PROJECTOR_CTRL_TIMER'} = 'LINE-NOUPDATE';
  68. # Amplifier
  69. $MON_FILES{'AMPLIFIER'} = 'STATUS';
  70. $MON_FILES{'AMPLIFIER_VOL'} = 'LINE-NOUPDATE';
  71. $MON_FILES{'AMPLIFIER_MODE'} = 'LINE-NOUPDATE';
  72. $MON_FILES{'AMPLIFIER_INPUT'} = 'LINE-NOUPDATE';
  73. $MON_FILES{'AUDIO_AMP'} = 'EXISTS-ON';
  74. $MON_FILES{'STEREO_CMD'} = 'EXISTS-ON';
  75. # A/V Effects
  76. $MON_FILES{'LIGHTS'} = 'EXISTS-TIMEOUT';
  77. $MON_FILES{'BRIGHT'} = 'EXISTS-TIMEOUT';
  78. $MON_FILES{'RAVE'} = 'EXISTS';
  79. $MON_FILES{'EFFECT'} = 'EXISTS';
  80. # Fan
  81. $MON_FILES{'FAN'} = 'LINE-NOUPDATE';
  82. $MON_FILES{'FAN_CMD'} = 'EXISTS-TIMEOUT';
  83. # Garage
  84. $MON_FILES{'OIL'} = 'LINE-NOUPDATE';
  85. $MON_FILES{'DOOR'} = 'LINE-NOUPDATE';
  86. $MON_FILES{'LOCK'} = 'EXISTS-NOUPDATE';
  87. # Alarm
  88. $MON_FILES{'ALARM'} = 'NONE';
  89. # OS State
  90. $MON_FILES{'FRONT_APP'} = 'LINE-NOUPDATE';
  91. $MON_FILES{'COLOR'} = 'LINE-NOUPDATE';
  92. $MON_FILES{'AUDIO'} = 'LINE-NOUPDATE';
  93. $MON_FILES{'AUDIO_STATE'} = 'LINE-NOUPDATE';
  94. # Game System
  95. $MON_FILES{'GAME'} = 'EXISTS';
  96. $MON_FILES{'VR'} = 'LINE-NOUPDATE';
  97. # Lights
  98. $MON_FILES{'LED'} = 'LINE-NOUPDATE';
  99. $MON_FILES{'OVERHEAD'} = 'LINE-NOUPDATE';
  100. $MON_FILES{'ROPE'} = 'LINE-NOUPDATE';
  101. $MON_FILES{'STAIRS'} = 'LINE-NOUPDATE';
  102. $MON_FILES{'GARAGE'} = 'LINE-NOUPDATE';
  103. $MON_FILES{'GARAGE_LEDS'} = 'LINE-NOUPDATE';
  104. $MON_FILES{'GARAGE_SHELF'} = 'LINE-NOUPDATE';
  105. # TV
  106. $MON_FILES{'TV'} = 'STATUS';
  107. $MON_FILES{'TV_VOL'} = 'LINE-NOUPDATE';
  108. $MON_FILES{'TV_INPUT'} = 'LINE-NOUPDATE';
  109. }
  110. my %EXTRAS = (
  111. 'PLAYING' => {
  112. 'URL' => qr/^file:(.+)$/m,
  113. 'YEAR' => qr/^year:(\d+)/m,
  114. 'SERIES' => qr/^showtitle:(.+)$/m,
  115. 'SEASON' => qr/^season:(\d+)/m,
  116. 'EPISODE' => qr/^episode:(\d+)/m,
  117. 'TITLE' => qr/^title:(.+)$/m,
  118. 'LENGTH' => qr/^duration:(\d+)/m,
  119. 'POSITION' => qr/^time:(\d+(?:\.\d+)?)/m,
  120. 'TYPE' => qr/^type:(.+)$/m,
  121. 'ALBUM' => qr/^album:(.+)$/m,
  122. 'ARTIST' => qr/^artist:(.+)$/m,
  123. 'SELECTION' => qr/^selection:(.+)$/m,
  124. 'WINDOW' => qr/^window:(.+)$/m,
  125. 'WINDOWID' => qr/^windowid:(\d+)/m,
  126. # Available, but big and mostly useless
  127. #'IMAGE' => qr/^fanart:(.+)$/m,
  128. #'THUMB' => qr/^thumbnail:(.+)$/m,
  129. }
  130. );
  131. # App config
  132. my $SOCK_TIMEOUT = 5;
  133. my $DATA_DIR = DMX::dataDir();
  134. my $CMD_FILE = 'STATE';
  135. my $MAX_CMD_LEN = DMX::maxCmdLen();
  136. my $RESET_CMD = $ENV{'HOME'} . '/bin/video/dmx/reset.sh';
  137. my $PUSH_TIMEOUT = 20;
  138. # Debug
  139. my $DEBUG = 0;
  140. if ( $ENV{'DEBUG'} ) {
  141. $DEBUG = 1;
  142. }
  143. # Command-line arguments
  144. my ($DELAY) = @ARGV;
  145. if ( !$DELAY ) {
  146. $DELAY = 0.5;
  147. }
  148. # Socket init
  149. my $select = DMX::stateSocket($CMD_FILE);
  150. # Add the extras to the main file list
  151. foreach my $file ( keys(%EXTRAS) ) {
  152. if ( exists( $MON_FILES{$file} ) ) {
  153. foreach my $extra ( keys( %{ $EXTRAS{$file} } ) ) {
  154. $MON_FILES{ $file . '_' . $extra } = 'EXTRA';
  155. }
  156. }
  157. }
  158. # Init the file tracking structure
  159. my %files = ();
  160. foreach my $name ( keys(%MON_FILES) ) {
  161. my %file = (
  162. 'name' => basename($name),
  163. 'type' => $MON_FILES{$name},
  164. 'path' => $DATA_DIR . $name,
  165. 'update' => 0,
  166. 'value' => 0,
  167. 'last' => 0,
  168. );
  169. # Allow absolute paths to override the $DATA_DIR path
  170. if ( $name =~ /^\// ) {
  171. $file{'path'} = $name;
  172. }
  173. # Record the directory name for folder monitoring
  174. $file{'dir'} = dirname( $file{'path'} );
  175. # Set the attribute bits, for use in later update handling logic
  176. my %attr = (
  177. 'available' => 0,
  178. 'status' => 0,
  179. 'exists' => 0,
  180. 'value' => 0,
  181. 'line' => 0,
  182. 'clear' => 0,
  183. 'clear_off' => 0,
  184. 'mtime' => 0,
  185. 'playing' => 0,
  186. 'no_update' => 0,
  187. 'no_value' => 0,
  188. 'extra' => 0,
  189. );
  190. if ( $file{'type'} =~ /\bSTATUS\b/i ) {
  191. $attr{'status'} = 1;
  192. }
  193. if ( $file{'type'} =~ /\bEXISTS\b/i ) {
  194. $attr{'exists'} = 1;
  195. }
  196. if ( $file{'type'} =~ /\bVALUE\b/i ) {
  197. $attr{'value'} = 1;
  198. }
  199. if ( $file{'type'} =~ /\bLINE\b/i ) {
  200. $attr{'line'} = 1;
  201. }
  202. if ( $file{'type'} =~ /\bCLEAR\b/i ) {
  203. $attr{'clear'} = 1;
  204. }
  205. if ( $file{'type'} =~ /\bON\b/i ) {
  206. $attr{'clear_off'} = 1;
  207. }
  208. if ( $file{'type'} =~ /\bTIMEOUT\b/i ) {
  209. $attr{'clear_timeout'} = 1;
  210. }
  211. if ( $file{'type'} =~ /\bMTIME\b/i ) {
  212. $attr{'mtime'} = 1;
  213. }
  214. if ( $file{'type'} =~ /\bPLAYING\b/i ) {
  215. $attr{'playing'} = 1;
  216. }
  217. if ( $file{'type'} =~ /\bNOUPDATE\b/i ) {
  218. $attr{'no_update'} = 1;
  219. }
  220. if ( $file{'type'} =~ /\bNONE\b/i ) {
  221. $attr{'no_value'} = 1;
  222. }
  223. if ( $file{'type'} =~ /\bEXTRA\b/i ) {
  224. $attr{'extra'} = 1;
  225. }
  226. # Cross-match some data types for easy of use
  227. if ( $attr{'extra'} ) {
  228. $attr{'no_value'} = 1;
  229. }
  230. if ( $attr{'no_value'} ) {
  231. $attr{'no_update'} = 1;
  232. }
  233. if ( $attr{'line'} ) {
  234. $attr{'value'} = 1;
  235. }
  236. if ( $attr{'value'} || $attr{'playing'} ) {
  237. $attr{'status'} = 1;
  238. }
  239. if ( $attr{'status'} || $attr{'exists'} ) {
  240. $attr{'mtime'} = 1;
  241. }
  242. # Push ATTR into the file hash
  243. $file{'attr'} = \%attr;
  244. # Push FILE into the files hash
  245. $files{$name} = \%file;
  246. }
  247. # Delete any existing input files, to ensure our state is reset
  248. foreach my $file ( values(%files) ) {
  249. if ( -e $file->{'path'} ) {
  250. if ($DEBUG) {
  251. print STDERR 'Deleting '
  252. . $file->{'name'} . ': '
  253. . $file->{'path'} . "\n";
  254. }
  255. unlink( $file->{'path'} );
  256. }
  257. }
  258. # Reset all dependents at starts
  259. system($RESET_CMD);
  260. # Subscribers
  261. my @subscribers = ();
  262. # State
  263. my $state = 'INIT';
  264. my $stateLast = $state;
  265. my $status = '';
  266. my $statusLast = $status;
  267. my $updateLast = 0;
  268. my $timeSinceUpdate = 0;
  269. my $startTime = time() + $START_DELAY;
  270. my $pushLast = 0;
  271. # Announce startup
  272. DMX::say('System initializing');
  273. # Loop forever
  274. while (1) {
  275. # Provide a method to force updates
  276. my $update = 0;
  277. # Avoid repeated calls to time()
  278. my $now = time();
  279. # Check for queued commands
  280. my @ready_clients = $select->can_read($DELAY);
  281. foreach my $fh (@ready_clients) {
  282. # Grab the inbound text
  283. my $path = undef();
  284. $fh->recv( $path, $MAX_CMD_LEN );
  285. $path =~ s/^\s+//;
  286. $path =~ s/\s+$//;
  287. $path =~ s/\W/_/g;
  288. if ($DEBUG) {
  289. print STDERR 'Got path: ' . $path . "\n";
  290. }
  291. # Open the new socket
  292. my $sub = eval { DMX::clientSock($path); };
  293. if ( !$sub ) {
  294. print STDERR 'Unable to open socket: ' . $path . ": ${@}\n";
  295. next;
  296. }
  297. # Add the socket to our subscriber list
  298. my %tmp = (
  299. 'path' => $path,
  300. 'socket' => $sub,
  301. );
  302. push( @subscribers, \%tmp );
  303. # Send immediately when someone subscribes
  304. if ( !$update ) {
  305. if ($DEBUG) {
  306. print STDERR "New subscriber\n";
  307. }
  308. $update = 1;
  309. }
  310. }
  311. # Use opendir/readdir in each folder to ensure fresh results
  312. my %folders = ();
  313. foreach my $file ( values(%files) ) {
  314. if ( !$folders{ $file->{'dir'} } ) {
  315. my $dh = undef();
  316. opendir( $dh, $file->{'dir'} )
  317. or die( 'Unable to open directory: ' . $file->{'dir'} );
  318. if ($DEBUG) {
  319. print STDERR 'Refreshed folder: ' . $file->{'dir'} . "\n";
  320. }
  321. my @files = readdir($dh);
  322. closedir($dh);
  323. $folders{ $file->{'dir'} } = 1;
  324. }
  325. }
  326. # Monitor files of all types
  327. foreach my $file ( values(%files) ) {
  328. # Record the previous status and clear the new one, unless the file is type EXTRA
  329. if ( !$file->{'attr'}->{'extra'} ) {
  330. $file->{'last'} = $file->{'value'};
  331. $file->{'value'} = 0;
  332. }
  333. # Skip NO_VALUE files -- their values are abstracted from other data
  334. if ( $file->{'attr'}->{'no_value'} ) {
  335. next;
  336. }
  337. # Track available/unavailable status in each cycle
  338. {
  339. my $wasAvailable = $file->{'attr'}->{'available'};
  340. $file->{'attr'}->{'available'} = -r $file->{'path'} ? 1 : 0;
  341. if ( $wasAvailable != $file->{'attr'}->{'available'} ) {
  342. # Reset and extras for this file
  343. if ( exists( $EXTRAS{ $file->{'name'} } ) ) {
  344. foreach
  345. my $extra ( keys( %{ $EXTRAS{ $file->{'name'} } } ) )
  346. {
  347. my $name = $file->{'name'} . '_' . $extra;
  348. $files{$name}{'last'} = $files{$name}{'value'};
  349. $files{$name}{'value'} = 0;
  350. }
  351. }
  352. # Determine which display device we have (if any)
  353. if ( exists( $DISPLAY_DEVS{ $file->{'name'} } ) ) {
  354. $DISPLAY = $file->{'name'};
  355. if ($DEBUG) {
  356. print STDERR 'Selecting display device: ' . $DISPLAY
  357. . "\n";
  358. }
  359. }
  360. if ($DEBUG) {
  361. if ( $file->{'attr'}->{'available'} ) {
  362. print STDERR 'Added file '
  363. . $file->{'name'} . ': '
  364. . $file->{'path'} . "\n";
  365. }
  366. else {
  367. print STDERR 'Dropped file '
  368. . $file->{'name'} . ': '
  369. . $file->{'path'} . "\n";
  370. }
  371. }
  372. }
  373. }
  374. # Skip unavailable files
  375. if ( !$file->{'attr'}->{'available'} ) {
  376. next;
  377. }
  378. # Record the last update for MTIME files
  379. if ( $file->{'attr'}->{'mtime'} ) {
  380. my $mtime = mtime( $file->{'path'} );
  381. # All EXISTS files have MTIME set, and mtime returns 0 if the file does not exist, so we can overload this check
  382. if ( $file->{'attr'}->{'exists'} ) {
  383. if ( $mtime > 0 ) {
  384. $file->{'update'} = $mtime;
  385. $file->{'value'} = 1;
  386. }
  387. elsif ( $file->{'last'} ) {
  388. $file->{'update'} = $now;
  389. }
  390. }
  391. else {
  392. $file->{'update'} = $mtime;
  393. # Set the value if the update is recent -- VALUE and STATUS will overwrite as needed
  394. if ( $mtime >= $now ) {
  395. $file->{'value'} = 1;
  396. }
  397. }
  398. if ($DEBUG) {
  399. print STDERR 'Mtime: '
  400. . $file->{'name'} . ': '
  401. . $file->{'value'} . ':'
  402. . $file->{'update'} . "\n";
  403. }
  404. }
  405. # Grab the state from STATUS files (including VALUE files)
  406. if ( $file->{'attr'}->{'status'} ) {
  407. # Overwrite the mtime-based value
  408. $file->{'value'} = 0;
  409. my $text = '';
  410. {
  411. if ( !open( my $fh, $file->{'path'} ) ) {
  412. warn( 'Unable to open ' . $file->{'path'} . "\n" );
  413. }
  414. else {
  415. local $/;
  416. $text = <$fh>;
  417. close($fh);
  418. }
  419. }
  420. # Parse the value for the file
  421. if ( $file->{'attr'}->{'value'} ) {
  422. if ( $file->{'attr'}->{'line'} ) {
  423. ($text) = $text =~ /^([^\n]+)\s*\n/;
  424. }
  425. else {
  426. $text =~ s/\s+$//;
  427. }
  428. $file->{'value'} = $text;
  429. }
  430. elsif ( $file->{'attr'}->{'playing'} ) {
  431. if ( $text =~ /^playing:1/m ) {
  432. $file->{'value'} = 1;
  433. }
  434. }
  435. else {
  436. if ( $text =~ /1/ ) {
  437. $file->{'value'} = 1;
  438. }
  439. }
  440. if ($DEBUG) {
  441. print STDERR 'Status: '
  442. . $file->{'name'} . ': '
  443. . $file->{'value'} . "\n";
  444. }
  445. # Handle extras, if any
  446. if ( exists( $EXTRAS{ $file->{'name'} } ) ) {
  447. foreach my $extra ( keys( %{ $EXTRAS{ $file->{'name'} } } ) ) {
  448. my $name = $file->{'name'} . '_' . $extra;
  449. $files{$name}{'last'} = $files{$name}{'value'};
  450. $files{$name}{'value'} = 0;
  451. $files{$name}{'update'} = $file->{'update'};
  452. if ( $text =~ $EXTRAS{ $file->{'name'} }{$extra} ) {
  453. $files{$name}{'value'} = $1;
  454. }
  455. if ($DEBUG) {
  456. print STDERR "\t"
  457. . $name . ': '
  458. . $files{$name}{'value'} . "\n";
  459. }
  460. }
  461. }
  462. }
  463. }
  464. # Simulate the old GUI indicator
  465. {
  466. my $gui = 0;
  467. if ( exists( $files{'PLAYING_WINDOW'} )
  468. && $files{'PLAYING_WINDOW'}->{'value'} ne 'Fullscreen video' )
  469. {
  470. $gui = 1;
  471. }
  472. $files{'GUI'}->{'value'} = $gui;
  473. }
  474. if ( exists( $files{'PLAYING'} ) ) {
  475. $files{'GUI'}->{'update'} = $files{'PLAYING'}->{'update'};
  476. }
  477. # Ignore the MOTION file if NO_MOTION is set
  478. if ( exists( $files{'NO_MOTION'} ) && $files{'NO_MOTION'}->{'value'} ) {
  479. if ( exists( $files{'MOTION'} ) ) {
  480. $files{'MOTION'}->{'value'} = 0;
  481. $files{'MOTION'}->{'update'} = 0;
  482. }
  483. }
  484. # Set the global update timestamp, excluding files marked NOUPDATE
  485. foreach my $file ( values(%files) ) {
  486. if ( $file->{'update'} > $updateLast
  487. && !$file->{'attr'}->{'no_update'}
  488. && $file->{'update'} > $startTime )
  489. {
  490. $updateLast = $file->{'update'};
  491. }
  492. }
  493. $timeSinceUpdate = $now - $updateLast;
  494. # Calculate the PLEX state
  495. if ( exists( $files{'FRONT_APP'} ) ) {
  496. if ( $files{'FRONT_APP'}->{'value'} =~ 'com.plexapp.plexhometheater'
  497. || $files{'FRONT_APP'}->{'value'} eq
  498. 'com.apple.ScreenSaver.Engine' )
  499. {
  500. if ( !exists( $files{'GAME'} ) || !$files{'GAME'}->{'value'} ) {
  501. $files{'PLEX'}->{'value'} = 1;
  502. }
  503. }
  504. }
  505. if ( $files{'PLEX'}->{'last'} != $files{'PLEX'}->{'value'} ) {
  506. $files{'PLEX'}->{'update'} = $now;
  507. }
  508. # Determine some intermediate state data
  509. my $playing = 0;
  510. if ( exists( $files{'PLAYING'} ) && $files{'PLAYING'}->{'value'} ) {
  511. $playing = 1;
  512. }
  513. my $video = 0;
  514. if ( exists( $files{'PLAYING_TYPE'} )
  515. && $files{'PLAYING_TYPE'}->{'value'} ne 'audio' )
  516. {
  517. $video = 1;
  518. }
  519. elsif ( !exists( $files{'PLAYING_TYPE'} ) ) {
  520. $video = 1;
  521. }
  522. # Non-plex apps are always "playing" and "video"
  523. if ( exists( $files{'PLEX'} ) && !$files{'PLEX'}->{'value'} ) {
  524. $playing = 1;
  525. $video = 1;
  526. }
  527. # Calculate the new state
  528. $stateLast = $state;
  529. if ( !defined($DISPLAY) || !exists( $files{$DISPLAY} ) ) {
  530. # If there's no display the state is always PLAY or PAUSE, as indicated by the master
  531. if ($playing) {
  532. $state = 'PLAY';
  533. }
  534. else {
  535. $state = 'PAUSE';
  536. }
  537. }
  538. elsif ( $files{$DISPLAY}->{'value'} ) {
  539. # If a display exists and is on:
  540. # PLAY when video is active and playing
  541. # PAUSE any other time (including when audio is active)
  542. if ( $playing && $video ) {
  543. $state = 'PLAY';
  544. }
  545. else {
  546. $state = 'PAUSE';
  547. }
  548. }
  549. else {
  550. # If the display exists but is off, check the timeouts to determine MOTION vs. OFF
  551. if ( $timeSinceUpdate > $STATE_TIMEOUT ) {
  552. $state = 'OFF';
  553. }
  554. elsif ( $timeSinceUpdate < $STATE_TIMEOUT ) {
  555. $state = 'MOTION';
  556. }
  557. }
  558. if ( $state eq 'INIT' ) {
  559. $state = 'OFF';
  560. }
  561. # ALARM if LOCK exists and the master state is not OFF or any motion exists
  562. {
  563. my $alarm = 0;
  564. if ( exists( $files{'LOCK'} ) && $files{'LOCK'}->{'value'} ) {
  565. if (
  566. $state ne 'OFF'
  567. || ( exists( $files{'MOTION'} ) && $files{'MOTION'}->{'value'} )
  568. || ( exists( $files{'MOTION_STAIRS'} )
  569. && $files{'MOTION_STAIRS'}->{'value'} )
  570. || ( exists( $files{'MOTION_GARAGE'} )
  571. && $files{'MOTION_GARAGE'}->{'value'} )
  572. )
  573. {
  574. $alarm = 1;
  575. }
  576. }
  577. if ( !exists( $files{'ALARM'}->{'value'} )
  578. || $files{'ALARM'}->{'value'} != $alarm )
  579. {
  580. $files{'ALARM'}->{'update'} = $now;
  581. }
  582. $files{'ALARM'}->{'value'} = $alarm;
  583. }
  584. # Clear EXISTS_ON files when the main state is "OFF" (and before we append their status)
  585. foreach my $file ( values(%files) ) {
  586. if ( $file->{'attr'}->{'clear_off'}
  587. && $file->{'value'}
  588. && $state eq 'OFF' )
  589. {
  590. unlink( $file->{'path'} );
  591. $file->{'status'} = 0;
  592. if ($DEBUG) {
  593. print STDERR 'Clearing exists flag for: '
  594. . $file->{'name'} . "\n";
  595. }
  596. }
  597. }
  598. # Clear EXISTS_TIMEOUT files when the main state is "OFF" and EXISTS_TIMEOUT has expired (and before we append their status)
  599. foreach my $file ( values(%files) ) {
  600. if ( $file->{'attr'}->{'clear_timeout'}
  601. && $file->{'value'}
  602. && $state eq 'OFF'
  603. && $timeSinceUpdate > $EXISTS_TIMEOUT )
  604. {
  605. unlink( $file->{'path'} );
  606. $file->{'status'} = 0;
  607. if ($DEBUG) {
  608. print STDERR 'Clearing exists flag for: '
  609. . $file->{'name'} . "\n";
  610. }
  611. }
  612. }
  613. # Calculate the new status
  614. $statusLast = $status;
  615. {
  616. my @statTime = ();
  617. foreach my $file ( values(%files) ) {
  618. my $text = $file->{'value'};
  619. $text =~ s/\s/ /g;
  620. $text =~ s/\|/-/g;
  621. push( @statTime,
  622. $file->{'name'} . '|' . $text . '|' . $file->{'update'} );
  623. }
  624. $status = "\n" . join( "\n", @statTime );
  625. }
  626. # Clear EXISTS_CLEAR files immediately (but after we append their status)
  627. foreach my $file ( values(%files) ) {
  628. if ( $file->{'attr'}->{'clear'} && $file->{'value'} ) {
  629. unlink( $file->{'path'} );
  630. $file->{'status'} = 0;
  631. if ($DEBUG) {
  632. print STDERR 'Clearing exists flag for: '
  633. . $file->{'name'} . "\n";
  634. }
  635. }
  636. }
  637. # Update on a periodic basis, so we don't timeout
  638. if ( !$update && $now - $pushLast > $PUSH_TIMEOUT ) {
  639. if ($DEBUG) {
  640. print STDERR "Periodic update\n";
  641. }
  642. $update = 1;
  643. }
  644. # Update on a master state change
  645. if ( !$update && $stateLast ne $state ) {
  646. if ($DEBUG) {
  647. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  648. }
  649. $update = 1;
  650. }
  651. # Update on any mtime change
  652. if ( !$update ) {
  653. foreach my $file ( values(%files) ) {
  654. if ( $file->{'update'} > $pushLast ) {
  655. if ($DEBUG) {
  656. print STDERR 'Mtime change: '
  657. . $file->{'name'} . ' ('
  658. . $file->{'update'} . ")\n";
  659. }
  660. $update = 1;
  661. last;
  662. }
  663. }
  664. }
  665. # Update on any value change
  666. if ( !$update ) {
  667. foreach my $file ( values(%files) ) {
  668. if ( $file->{'value'} ne $file->{'last'} ) {
  669. if ($DEBUG) {
  670. print STDERR 'Value change: '
  671. . $file->{'name'} . ' ('
  672. . $file->{'last'} . ' => '
  673. . $file->{'value'} . ")\n";
  674. }
  675. $update = 1;
  676. last;
  677. }
  678. }
  679. }
  680. # Push the update
  681. if ($update) {
  682. # Note the push
  683. $pushLast = $now;
  684. # Send notifications to all subscribers
  685. my $msg = $state . $status;
  686. if ($DEBUG) {
  687. print STDERR '<STATE ('
  688. . length($msg) . ")>\n"
  689. . $msg
  690. . "\n<END STATE>\n\n";
  691. }
  692. foreach my $sub (@subscribers) {
  693. # Drop subscribers that are not available
  694. if ( !eval { $sub->{'socket'}->send( $state . $status ) } ) {
  695. print STDERR 'Dropping bad socket from subscriber list: '
  696. . $sub->{'path'} . "\n";
  697. my @new_subscribers = ();
  698. foreach my $new (@subscribers) {
  699. if ( $new->{'socket'} eq $sub->{'socket'} ) {
  700. next;
  701. }
  702. push( @new_subscribers, $new );
  703. }
  704. @subscribers = @new_subscribers;
  705. }
  706. }
  707. # Save the state and value to disk
  708. my ( $fh, $tmp ) =
  709. tempfile( $DATA_DIR . 'STATE.XXXXXXXX', 'UNLINK' => 0 );
  710. print $fh $state . "\n";
  711. close($fh);
  712. rename( $tmp, $DATA_DIR . 'STATE' );
  713. }
  714. }
  715. sub mtime($) {
  716. my ($file) = @_;
  717. my $mtime = 0;
  718. if ( -r $file ) {
  719. (
  720. undef(), undef(), undef(), undef(), undef(), undef(), undef(),
  721. undef(), undef(), $mtime, undef(), undef(), undef()
  722. ) = stat($file);
  723. }
  724. return $mtime;
  725. }