state.pl 18 KB

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