projectorPower.pl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 ($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. }
  64. # Playing counts as activity
  65. if ($exists{'PLAYING'} && $lastUser < time()) {
  66. $lastUser = time();
  67. }
  68. # Record the shutdown timestamp
  69. if ($newState eq 'SHUTDOWN') {
  70. $shutdown = time();
  71. }
  72. # Clear the shutdown timestamp if there is new user activity
  73. if ($shutdown && $lastUser > $shutdown) {
  74. $shutdown = 0;
  75. }
  76. # Calculate the elapsed time, faking for $shutdown as needed
  77. my $elapsed = 0;
  78. if ($shutdown && $lastUser < $shutdown) {
  79. # Power off happens when $elapsed > $TIMEOUT + $COUNTDOWN
  80. # Adjust back from that for the $OFF_DELAY
  81. # Which gives us the number of seconds back we want to use as the start of our countdown
  82. # So subtract that from $shutdown, and we'll have a $lastUser timestamp substitute
  83. $elapsed = time() - ($shutdown - (($TIMEOUT + $COUNTDOWN) - $OFF_DELAY));
  84. } else {
  85. $elapsed = time() - $lastUser;
  86. }
  87. if ($DEBUG) {
  88. print STDERR 'Time since last user action: ' . $elapsed . "\n";
  89. }
  90. # Calculate the new state
  91. $stateLast = $state;
  92. if ($exists{'PROJECTOR'}) {
  93. if ($elapsed > $TIMEOUT) {
  94. $state = 'COUNTDOWN';
  95. if ($elapsed > $TIMEOUT + $COUNTDOWN) {
  96. $state = 'OFF';
  97. }
  98. } else {
  99. $state = 'ON';
  100. }
  101. }
  102. # Force updates on a periodic basis
  103. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  104. # Not for the projector
  105. #if ($DEBUG) {
  106. # print STDERR "Forcing periodic update\n";
  107. #}
  108. #$update = 1;
  109. }
  110. # Force updates on any state change
  111. if (!$update && $stateLast ne $state) {
  112. if ($DEBUG) {
  113. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  114. }
  115. $update = 1;
  116. }
  117. # Force updates when there is a physical state mistmatch
  118. if (!$update) {
  119. if ($state eq 'OFF' && $exists{'PROJECTOR'}) {
  120. if ($DEBUG) {
  121. print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'PROJECTOR'} . "\n";
  122. }
  123. $update = 1;
  124. }
  125. }
  126. # Update the projector
  127. if ($update) {
  128. # Extra debugging to record pushes
  129. if ($DEBUG) {
  130. print STDERR 'State: ' . $state . "\n";
  131. }
  132. # Send master power state
  133. if ($state eq 'OFF' || $state eq 'ON') {
  134. $proj->send($state)
  135. or die('Unable to write command to proj socket: ' . $state . ": ${!}\n");
  136. }
  137. # No output file
  138. # Update the push time
  139. $pushLast = time();
  140. # Clear the update flag
  141. $update = 0;
  142. }
  143. # Announce a pending shutdown every minute
  144. if ($state eq 'COUNTDOWN') {
  145. my $timeLeft = ($TIMEOUT + $COUNTDOWN) - $elapsed;
  146. $timeLeft = ceil($timeLeft / 60);
  147. if ($lastCount != $timeLeft) {
  148. my $plural = 's';
  149. if ($timeLeft == 1) {
  150. $plural = '';
  151. }
  152. system('say', 'Projector powerdown in about ' . $timeLeft . ' minute' . $plural);
  153. $lastCount = $timeLeft;
  154. }
  155. }
  156. }