state.pl 23 KB

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