state.pl 24 KB

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