state.pl 17 KB

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