projectorPower.pl 4.6 KB

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