state.pl 24 KB

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