state.pl 24 KB

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