state.pl 23 KB

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