state.pl 24 KB

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