state.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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 } = 'NONE';
  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. );
  163. if ($file{'type'} =~ /\bSTATUS\b/i) {
  164. $attr{'status'} = 1;
  165. }
  166. if ($file{'type'} =~ /\bEXISTS\b/i) {
  167. $attr{'exists'} = 1;
  168. }
  169. if ($file{'type'} =~ /\bVALUE\b/i) {
  170. $attr{'value'} = 1;
  171. }
  172. if ($file{'type'} =~ /\bLINE\b/i) {
  173. $attr{'line'} = 1;
  174. }
  175. if ($file{'type'} =~ /\bCLEAR\b/i) {
  176. $attr{'clear'} = 1;
  177. }
  178. if ($file{'type'} =~ /\bON\b/i) {
  179. $attr{'clear_off'} = 1;
  180. }
  181. if ($file{'type'} =~ /\bTIMEOUT\b/i) {
  182. $attr{'clear_timeout'} = 1;
  183. }
  184. if ($file{'type'} =~ /\bMTIME\b/i) {
  185. $attr{'mtime'} = 1;
  186. }
  187. if ($file{'type'} =~ /\bPLAYING\b/i) {
  188. $attr{'playing'} = 1;
  189. }
  190. if ($file{'type'} =~ /\bNOUPDATE\b/i) {
  191. $attr{'no_update'} = 1;
  192. }
  193. # Cross-match some data types for easy of use
  194. if ($attr{'line'}) {
  195. $attr{'value'} = 1;
  196. }
  197. if ($attr{'value'} || $attr{'playing'}) {
  198. $attr{'status'} = 1;
  199. }
  200. if ($attr{'status'} || $attr{'exists'}) {
  201. $attr{'mtime'} = 1;
  202. }
  203. # Push ATTR into the file hash
  204. $file{'attr'} = \%attr;
  205. # Push FILE into the files hash
  206. $files{$name} = \%file;
  207. }
  208. # Delete any existing input files, to ensure our state is reset
  209. foreach my $file (values(%files)) {
  210. if (-e $file->{'path'}) {
  211. if ($DEBUG) {
  212. print STDERR 'Deleting ' . $file->{'name'} . ': ' . $file->{'path'} . "\n";
  213. }
  214. unlink($file->{'path'});
  215. }
  216. }
  217. # Reset all dependents at starts
  218. system($RESET_CMD);
  219. # Subscribers
  220. my @subscribers = ();
  221. # State
  222. my $state = 'INIT';
  223. my $stateLast = $state;
  224. my $status = '';
  225. my $statusLast = $status;
  226. my $updateLast = 0;
  227. my $timeSinceUpdate = 0;
  228. my $startTime = time() + $START_DELAY;
  229. my $pushLast = 0;
  230. # Loop forever
  231. while (1) {
  232. # Provide a method to force updates even when the state does not change
  233. my $update = 0;
  234. # Check for queued commands
  235. my @ready_clients = $select->can_read($DELAY);
  236. foreach my $fh (@ready_clients) {
  237. # Grab the inbound text
  238. my $path = undef();
  239. $fh->recv($path, $MAX_CMD_LEN);
  240. $path =~ s/^\s+//;
  241. $path =~ s/\s+$//;
  242. $path =~ s/\W/_/g;
  243. if ($DEBUG) {
  244. print STDERR 'Got path: ' . $path . "\n";
  245. }
  246. # Open the new socket
  247. my $sub = eval { DMX::clientSock($path); };
  248. if (!$sub) {
  249. print STDERR 'Unable to open socket: ' . $path . ": ${@}\n";
  250. next;
  251. }
  252. # Add the socket to our subscriber list
  253. my %tmp = (
  254. 'path' => $path,
  255. 'socket' => $sub,
  256. );
  257. push(@subscribers, \%tmp);
  258. # Force an update
  259. $update = 1;
  260. }
  261. # Use opendir/readdir in each folder to ensure fresh results
  262. my %folders = ();
  263. foreach my $file (values(%files)) {
  264. if (!$folders{ $file->{'dir'} }) {
  265. my $dh = undef();
  266. opendir($dh, $file->{'dir'})
  267. or die('Unable to open directory: ' . $file->{'dir'});
  268. if ($DEBUG) {
  269. print STDERR 'Refreshed folder: ' . $file->{'dir'} . "\n";
  270. }
  271. my @files = readdir($dh);
  272. closedir($dh);
  273. $folders{ $file->{'dir'} } = 1;
  274. }
  275. }
  276. # Monitor files of all types
  277. foreach my $file (values(%files)) {
  278. # Skip "NONE" files -- they are data stores handled in other actions
  279. if ($file->{'type'} eq 'NONE') {
  280. next;
  281. }
  282. # Always record the previous status and clear the new one
  283. $file->{'last'} = $file->{'value'};
  284. $file->{'value'} = 0;
  285. # Track available/unavailable status in each cycle
  286. {
  287. my $wasAvailable = $file->{'attr'}->{'available'};
  288. $file->{'attr'}->{'available'} = -r $file->{'path'} ? 1 : 0;
  289. if ($wasAvailable != $file->{'attr'}->{'available'}) {
  290. # Reset and extras for this file
  291. if (exists($EXTRAS{ $file->{'name'} })) {
  292. foreach my $extra (keys(%{ $EXTRAS{ $file->{'name'} } })) {
  293. my $name = $file->{'name'} . '_' . $extra;
  294. $files{$name}{'last'} = $files{$name}{'value'};
  295. $files{$name}{'value'} = 0;
  296. }
  297. }
  298. # Determine which display device we have (if any)
  299. if (exists($DISPLAY_DEVS{ $file->{'name'} })) {
  300. $DISPLAY = $file->{'name'};
  301. if ($DEBUG) {
  302. print STDERR 'Selecting display device: ' . $DISPLAY . "\n";
  303. }
  304. }
  305. if ($DEBUG) {
  306. if ($file->{'attr'}->{'available'}) {
  307. print STDERR 'Added file ' . $file->{'name'} . ': ' . $file->{'path'} . "\n";
  308. } else {
  309. print STDERR 'Dropped file ' . $file->{'name'} . ': ' . $file->{'path'} . "\n";
  310. }
  311. }
  312. }
  313. }
  314. # Skip unavailable files
  315. if (!$file->{'attr'}->{'available'}) {
  316. next;
  317. }
  318. # Record the last update for MTIME files
  319. if ($file->{'attr'}->{'mtime'}) {
  320. my $mtime = mtime($file->{'path'});
  321. # All EXISTS files have MTIME set, and mtime returns 0 if the file does not exist, so we can overload this check
  322. if ($file->{'attr'}->{'exists'}) {
  323. if ($mtime > 0) {
  324. $file->{'update'} = $mtime;
  325. $file->{'value'} = 1;
  326. } elsif ($file->{'last'}) {
  327. $file->{'update'} = time();
  328. }
  329. } else {
  330. $file->{'update'} = $mtime;
  331. }
  332. if ($DEBUG) {
  333. print STDERR 'Mtime: ' . $file->{'name'} . ': ' . $file->{'value'} . ':' . $file->{'update'} . "\n";
  334. }
  335. }
  336. # Grab the state from STATUS files
  337. if ($file->{'attr'}->{'status'}) {
  338. my $text = '';
  339. {
  340. if (!open(my $fh, $file->{'path'})) {
  341. warn('Unable to open ' . $file->{'path'} . "\n");
  342. } else {
  343. local $/;
  344. $text = <$fh>;
  345. close($fh);
  346. }
  347. }
  348. # Parse the value for the file
  349. if ($file->{'attr'}->{'value'}) {
  350. if ($file->{'attr'}->{'line'}) {
  351. ($text) = $text =~ /^([^\n]+)\s*\n/;
  352. } else {
  353. $text =~ s/\s+$//;
  354. }
  355. $file->{'value'} = $text;
  356. } elsif ($file->{'attr'}->{'playing'}) {
  357. if ($text =~ /^playing:1/m) {
  358. $file->{'value'} = 1;
  359. }
  360. } else {
  361. if ($text =~ /1/) {
  362. $file->{'value'} = 1;
  363. }
  364. }
  365. if ($DEBUG) {
  366. print STDERR 'Status: ' . $file->{'name'} . ': ' . $file->{'value'} . "\n";
  367. }
  368. # Handle extras, if any
  369. if (exists($EXTRAS{ $file->{'name'} })) {
  370. foreach my $extra (keys(%{ $EXTRAS{ $file->{'name'} } })) {
  371. my $name = $file->{'name'} . '_' . $extra;
  372. $files{$name}{'last'} = $files{$name}{'value'};
  373. $files{$name}{'value'} = 0;
  374. $files{$name}{'update'} = $file->{'update'};
  375. if ($text =~ $EXTRAS{ $file->{'name'} }{$extra}) {
  376. $files{$name}{'value'} = $1;
  377. }
  378. if ($DEBUG) {
  379. print STDERR 'Status (extra): ' . $name . ': ' . $files{$name}{'value'} . "\n";
  380. }
  381. }
  382. }
  383. }
  384. }
  385. # Simulate the old GUI indicator
  386. {
  387. my $gui = 0;
  388. if (exists($files{'PLAYING_WINDOW'}) && $files{'PLAYING_WINDOW'}->{'value'} ne 'Fullscreen video') {
  389. $gui = 1;
  390. }
  391. $files{'GUI'}->{'value'} = $gui;
  392. }
  393. if (exists($files{'PLAYING'})) {
  394. $files{'GUI'}->{'update'} = $files{'PLAYING'}->{'update'};
  395. }
  396. # Ignore the MOTION file if NO_MOTION is set
  397. if (exists($files{'NO_MOTION'}) && $files{'NO_MOTION'}->{'value'}) {
  398. if (exists($files{'MOTION'})) {
  399. $files{'MOTION'}->{'value'} = 0;
  400. $files{'MOTION'}->{'update'} = 0;
  401. }
  402. }
  403. # Set the global update timestamp, excluding files marked NOUPDATE
  404. foreach my $file (values(%files)) {
  405. if ( $file->{'update'} > $updateLast
  406. && !$file->{'attr'}->{'no_update'}
  407. && $file->{'update'} > $startTime)
  408. {
  409. $updateLast = $file->{'update'};
  410. }
  411. }
  412. $timeSinceUpdate = time() - $updateLast;
  413. # Calculate the PLEX state
  414. $files{'PLEX'}->{'last'} = $files{'PLEX'}->{'value'};
  415. $files{'PLEX'}->{'value'} = 0;
  416. if (exists($files{'FRONT_APP'})) {
  417. if ( $files{'FRONT_APP'}->{'value'} =~ 'com.plexapp.plexhometheater'
  418. || $files{'FRONT_APP'}->{'value'} eq 'com.apple.ScreenSaver.Engine')
  419. {
  420. $files{'PLEX'}->{'value'} = 1;
  421. }
  422. }
  423. if ($files{'PLEX'}->{'last'} != $files{'PLEX'}->{'value'}) {
  424. $files{'PLEX'}->{'update'} = time();
  425. }
  426. # Determine some intermediate state data
  427. my $playing = 0;
  428. if (exists($files{'PLAYING'}) && $files{'PLAYING'}->{'value'}) {
  429. $playing = 1;
  430. }
  431. my $video = 0;
  432. if (exists($files{'PLAYING_TYPE'}) && $files{'PLAYING_TYPE'}->{'value'} ne 'audio') {
  433. $video = 1;
  434. } elsif (!exists($files{'PLAYING_TYPE'})) {
  435. $video = 1;
  436. }
  437. # Non-plex apps are always "playing" and "video"
  438. if (exists($files{'PLEX'}) && !$files{'PLEX'}->{'value'}) {
  439. $playing = 1;
  440. $video = 1;
  441. }
  442. # Calculate the new state
  443. $stateLast = $state;
  444. if (!defined($DISPLAY) || !exists($files{$DISPLAY})) {
  445. # If there's no display the state is always PLAY or PAUSE, as indicated by the master
  446. if ($playing) {
  447. $state = 'PLAY';
  448. } else {
  449. $state = 'PAUSE';
  450. }
  451. } elsif ($files{$DISPLAY}->{'value'}) {
  452. # If a display exists and is on:
  453. # PLAY when video is active and playing
  454. # PAUSE any other time (including when audio is active)
  455. if ($playing && $video) {
  456. $state = 'PLAY';
  457. } else {
  458. $state = 'PAUSE';
  459. }
  460. } else {
  461. # If the display exists but is off, check the timeouts to determine MOTION vs. OFF
  462. if ($timeSinceUpdate > $STATE_TIMEOUT) {
  463. $state = 'OFF';
  464. } elsif ($timeSinceUpdate < $STATE_TIMEOUT) {
  465. $state = 'MOTION';
  466. }
  467. }
  468. if ($state eq 'INIT') {
  469. $state = 'OFF';
  470. }
  471. # Clear EXISTS_ON files when the main state is "OFF" (and before we append their status)
  472. foreach my $file (values(%files)) {
  473. if ( $file->{'attr'}->{'clear_off'}
  474. && $file->{'value'}
  475. && $state eq 'OFF')
  476. {
  477. unlink($file->{'path'});
  478. $file->{'status'} = 0;
  479. if ($DEBUG) {
  480. print STDERR 'Clearing exists flag for: ' . $file->{'name'} . "\n";
  481. }
  482. }
  483. }
  484. # Clear EXISTS_TIMEOUT files when the main state is "OFF" and EXISTS_TIMEOUT has expired (and before we append their status)
  485. foreach my $file (values(%files)) {
  486. if ( $file->{'attr'}->{'clear_timeout'}
  487. && $file->{'value'}
  488. && $state eq 'OFF'
  489. && $timeSinceUpdate > $EXISTS_TIMEOUT)
  490. {
  491. unlink($file->{'path'});
  492. $file->{'status'} = 0;
  493. if ($DEBUG) {
  494. print STDERR 'Clearing exists flag for: ' . $file->{'name'} . "\n";
  495. }
  496. }
  497. }
  498. # Calculate the new status
  499. $statusLast = $status;
  500. {
  501. my @statTime = ();
  502. foreach my $file (values(%files)) {
  503. my $text = $file->{'value'};
  504. $text =~ s/\s/ /g;
  505. $text =~ s/\|/-/g;
  506. push(@statTime, $file->{'name'} . '|' . $text . '|' . $file->{'update'});
  507. }
  508. $status = "\n" . join("\n", @statTime);
  509. }
  510. # Clear EXISTS_CLEAR files immediately (but after we append their status)
  511. foreach my $file (values(%files)) {
  512. if ($file->{'attr'}->{'clear'} && $file->{'value'}) {
  513. unlink($file->{'path'});
  514. $file->{'status'} = 0;
  515. if ($DEBUG) {
  516. print STDERR 'Clearing exists flag for: ' . $file->{'name'} . "\n";
  517. }
  518. }
  519. }
  520. # Force updates on a periodic basis
  521. if (time() - $pushLast > $PUSH_TIMEOUT) {
  522. $update = 1;
  523. }
  524. # Update on any status change
  525. foreach my $file (values(%files)) {
  526. if ($file->{'value'} ne $file->{'last'}) {
  527. $update = 1;
  528. last;
  529. }
  530. }
  531. # Force updates on any state change
  532. if ($stateLast ne $state || $statusLast ne $status) {
  533. if ($DEBUG) {
  534. print STDERR 'State change: ' . $stateLast . $statusLast . ' => ' . $state . $status . "\n";
  535. }
  536. $update = 1;
  537. }
  538. # Push the update
  539. if ($update) {
  540. # Note the push
  541. $pushLast = time();
  542. # Send notifications to all subscribers
  543. my $msg = $state . $status;
  544. if ($DEBUG) {
  545. print STDERR '<STATE (' . length($msg) . ")>\n" . $msg . "\n<END STATE>\n\n";
  546. }
  547. foreach my $sub (@subscribers) {
  548. # Drop subscribers that are not available
  549. my $send = $sub->{'socket'}->send($msg);
  550. if (!defined($send) || $send < 1) {
  551. print STDERR 'Dropping bad socket from subscriber list: ' . $sub->{'path'} . "\n";
  552. my @new_subscribers = ();
  553. foreach my $new (@subscribers) {
  554. if ($new->{'socket'} eq $sub->{'socket'}) {
  555. next;
  556. }
  557. push(@new_subscribers, $new);
  558. }
  559. @subscribers = @new_subscribers;
  560. }
  561. }
  562. # Save the state and value to disk
  563. my ($fh, $tmp) = tempfile($DATA_DIR . 'STATE.XXXXXXXX', 'UNLINK' => 0);
  564. print $fh $state . "\n";
  565. close($fh);
  566. rename($tmp, $DATA_DIR . 'STATE');
  567. }
  568. }
  569. sub mtime($) {
  570. my ($file) = @_;
  571. my $mtime = 0;
  572. if (-r $file) {
  573. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), $mtime, undef(), undef(), undef()) = stat($file);
  574. }
  575. return $mtime;
  576. }