state.pl 19 KB

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