state.pl 18 KB

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