projectorPower.pl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use POSIX qw(ceil floor);
  5. # Local modules
  6. use Cwd qw(abs_path);
  7. use File::Basename qw(dirname);
  8. use lib dirname(abs_path($0));
  9. use DMX;
  10. # Config
  11. my $TIMEOUT = 900;
  12. my $COUNTDOWN = 300;
  13. my $OFF_DELAY = 60;
  14. # App config
  15. my $DATA_DIR = DMX::dataDir();
  16. my $OUTPUT_FILE = $DATA_DIR . 'PROJECTOR_POWER';
  17. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  18. my $PROJ_SOCK = $DATA_DIR . 'PROJECTOR.socket';
  19. my $PUSH_TIMEOUT = 20;
  20. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  21. my $DELAY = $PULL_TIMEOUT / 2;
  22. my $CMD_DELAY = 15;
  23. # Debug
  24. my $DEBUG = 0;
  25. if ($ENV{'DEBUG'}) {
  26. $DEBUG = 1;
  27. }
  28. # Sockets
  29. DMX::stateSocket($STATE_SOCK);
  30. DMX::stateSubscribe($STATE_SOCK);
  31. my $proj = DMX::clientSock($PROJ_SOCK);
  32. # State
  33. my $state = 'OFF';
  34. my $stateLast = $state;
  35. my %exists = ();
  36. my %mtime = ();
  37. my $pushLast = 0;
  38. my $pullLast = time();
  39. my $update = 0;
  40. my $lastCount = -1;
  41. my $lastUser = time();
  42. my $shutdown = 0;
  43. # Loop forever
  44. while (1) {
  45. # State is calculated; use newState to gather data
  46. my $newState = $state;
  47. # Wait for state updates
  48. my $cmdState = DMX::readState($DELAY, \%exists, \%mtime, undef());
  49. if (defined($cmdState)) {
  50. $newState = $cmdState;
  51. $pullLast = time();
  52. }
  53. # Die if we don't see regular updates
  54. if (time() - $pullLast > $PULL_TIMEOUT) {
  55. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  56. }
  57. # Menu changes count as activity
  58. if ($mtime{'GUI'} && $lastUser < $mtime{'GUI'}) {
  59. $lastUser = $mtime{'GUI'};
  60. }
  61. # Projector starts count as activity
  62. if ($exists{'PROJECTOR'} && $lastUser < $mtime{'PROJECTOR'}) {
  63. $lastUser = $mtime{'PROJECTOR'};
  64. } elsif ($newState eq 'ON') {
  65. $lastUser = time();
  66. }
  67. # Playing counts as activity
  68. if ($exists{'PLAYING'} && $lastUser < time()) {
  69. $lastUser = time();
  70. }
  71. # Clear the shutdown timestamp if there is new user activity
  72. if ($shutdown && (!$exists{'PROJECTOR'} || $lastUser > $shutdown)) {
  73. $shutdown = 0;
  74. }
  75. # Record the shutdown timestamp
  76. if ($newState eq 'SHUTDOWN') {
  77. $shutdown = time();
  78. # If the projector is likely to be on, fake the flag to force a state calculation
  79. if ($stateLast ne 'OFF') {
  80. $exists{'PROJECTOR'} = 1;
  81. }
  82. }
  83. # Calculate the elapsed time, faking for $shutdown as needed
  84. my $elapsed = 0;
  85. if ($shutdown && $lastUser <= $shutdown) {
  86. # Power off happens when $elapsed > $TIMEOUT + $COUNTDOWN
  87. # Adjust back from that for the $OFF_DELAY
  88. # Which gives us the number of seconds back we want to use as the start of our countdown
  89. # So subtract that from $shutdown, and we'll have a $lastUser timestamp substitute
  90. $elapsed = time() - ($shutdown - (($TIMEOUT + $COUNTDOWN) - $OFF_DELAY));
  91. } else {
  92. $elapsed = time() - $lastUser;
  93. }
  94. if ($DEBUG) {
  95. print STDERR 'Time since last user action: ' . $elapsed . "\n";
  96. if ($shutdown) {
  97. print STDERR 'Time since shutdown command: ' . (time() - $shutdown) . "\n";
  98. }
  99. }
  100. # Calculate the new state
  101. $stateLast = $state;
  102. if ($exists{'PROJECTOR'}) {
  103. if ($elapsed > $TIMEOUT) {
  104. $state = 'COUNTDOWN';
  105. if ($elapsed > $TIMEOUT + $COUNTDOWN) {
  106. $state = 'OFF';
  107. }
  108. } else {
  109. $state = 'ON';
  110. }
  111. } else {
  112. if ($newState eq 'ON') {
  113. $state = 'ON';
  114. }
  115. }
  116. # Force updates on a periodic basis
  117. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  118. # Not for the projector
  119. #if ($DEBUG) {
  120. # print STDERR "Forcing periodic update\n";
  121. #}
  122. #$update = 1;
  123. }
  124. # Force updates on any state change
  125. if (!$update && $stateLast ne $state) {
  126. if ($DEBUG) {
  127. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  128. }
  129. $update = 1;
  130. }
  131. # Force updates when there is a physical state mistmatch
  132. if (!$update) {
  133. if ($state eq 'OFF' && $exists{'PROJECTOR'}) {
  134. if ($DEBUG) {
  135. print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'PROJECTOR'} . "\n";
  136. }
  137. $update = 1;
  138. }
  139. }
  140. # Update the projector
  141. if ($update) {
  142. # Extra debugging to record pushes
  143. if ($DEBUG) {
  144. print STDERR 'State: ' . $state . "\n";
  145. }
  146. # Send master power state
  147. if ($state eq 'OFF' || $state eq 'ON') {
  148. system('say', 'Projector ' . $state);
  149. $proj->send($state)
  150. or die('Unable to write command to proj socket: ' . $state . ": ${!}\n");
  151. }
  152. # No output file
  153. # Update the push time
  154. $pushLast = time();
  155. # Clear the update flag
  156. $update = 0;
  157. # Clear the lastCount
  158. $lastCount = -1;
  159. # Wait before accepting a new command
  160. sleep($CMD_DELAY)
  161. }
  162. # Announce a pending shutdown every minute
  163. if ($state eq 'COUNTDOWN') {
  164. my $timeLeft = ($TIMEOUT + $COUNTDOWN) - $elapsed;
  165. $timeLeft = ceil($timeLeft / 60);
  166. if ($lastCount != $timeLeft) {
  167. my $plural = 's';
  168. if ($timeLeft == 1) {
  169. $plural = '';
  170. }
  171. system('say', 'Projector powerdown in about ' . $timeLeft . ' minute' . $plural);
  172. $lastCount = $timeLeft;
  173. }
  174. }
  175. }