projectorPower.pl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 = 15;
  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 = $PUSH_TIMEOUT / 4;
  23. # Prototypes
  24. sub say($);
  25. sub sayShutdown($);
  26. # Debug
  27. my $DEBUG = 0;
  28. if ($ENV{'DEBUG'}) {
  29. $DEBUG = 1;
  30. }
  31. # Sockets
  32. DMX::stateSocket($STATE_SOCK);
  33. DMX::stateSubscribe($STATE_SOCK);
  34. my $proj = DMX::clientSock($PROJ_SOCK);
  35. # State
  36. my $state = 'OFF';
  37. my $stateLast = $state;
  38. my %exists = ();
  39. my %mtime = ();
  40. my $pushLast = 0;
  41. my $pullLast = time();
  42. my $update = 0;
  43. my $lastAnnounce = 0;
  44. my $lastUser = time();
  45. my $shutdown = 0;
  46. # Loop forever
  47. while (1) {
  48. # State is calculated; use newState to gather data
  49. my $newState = $state;
  50. # Wait for state updates
  51. {
  52. my %existsTmp = ();
  53. my $cmdState = DMX::readState($DELAY, \%existsTmp, \%mtime, undef());
  54. if (defined($cmdState)) {
  55. $newState = $cmdState;
  56. $pullLast = time();
  57. }
  58. # Only record valid exists hashes
  59. if (scalar(keys(%existsTmp)) < 1) {
  60. %exists = %existsTmp;
  61. }
  62. }
  63. # Die if we don't see regular updates
  64. if (time() - $pullLast > $PULL_TIMEOUT) {
  65. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  66. }
  67. # Menu changes count as activity
  68. if ($mtime{'GUI'} && $lastUser < $mtime{'GUI'}) {
  69. $lastUser = $mtime{'GUI'};
  70. }
  71. # Projector starts count as activity
  72. if ($exists{'PROJECTOR'} && $lastUser < $mtime{'PROJECTOR'}) {
  73. $lastUser = $mtime{'PROJECTOR'};
  74. } elsif ($newState eq 'ON') {
  75. $lastUser = time();
  76. }
  77. # Playing counts as activity
  78. if ($exists{'PLAYING'} && $lastUser < time()) {
  79. $lastUser = time();
  80. }
  81. # Clear the shutdown timestamp if there is new user activity
  82. if ($shutdown && (!$exists{'PROJECTOR'} || $lastUser > $shutdown)) {
  83. $shutdown = 0;
  84. }
  85. # Record the shutdown timestamp, if the projector is on
  86. if ($newState eq 'SHUTDOWN' && $exists{'PROJECTOR'}) {
  87. $shutdown = time();
  88. $lastUser = $shutdown;
  89. }
  90. # Calculate the elapsed time
  91. my $elapsed = time() - $lastUser;
  92. if ($DEBUG) {
  93. print STDERR 'Time since last user action: ' . $elapsed . "\n";
  94. }
  95. # Calculate the new state
  96. $stateLast = $state;
  97. if ($exists{'PROJECTOR'}) {
  98. if ($shutdown) {
  99. $state = 'SHUTDOWN';
  100. if ($elapsed > $OFF_DELAY) {
  101. $state = 'OFF';
  102. }
  103. } else {
  104. if ($elapsed > $TIMEOUT) {
  105. $state = 'COUNTDOWN';
  106. if ($elapsed > $TIMEOUT + $COUNTDOWN) {
  107. $state = 'OFF';
  108. }
  109. } else {
  110. $state = 'ON';
  111. }
  112. }
  113. } else {
  114. if ($newState eq 'ON') {
  115. $state = 'ON';
  116. }
  117. }
  118. # Force updates on a periodic basis
  119. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  120. # Not for the projector
  121. #if ($DEBUG) {
  122. # print STDERR "Forcing periodic update\n";
  123. #}
  124. #$update = 1;
  125. }
  126. # Force updates on any state change
  127. if (!$update && $stateLast ne $state) {
  128. if ($DEBUG) {
  129. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  130. }
  131. $update = 1;
  132. }
  133. # Force updates when there is a physical state mistmatch
  134. if (!$update) {
  135. if ($state eq 'OFF' && $exists{'PROJECTOR'}) {
  136. if ($DEBUG) {
  137. print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'PROJECTOR'} . "\n";
  138. }
  139. $update = 1;
  140. }
  141. }
  142. # Announce a pending shutdown
  143. if ($state eq 'COUNTDOWN' || $state eq 'SHUTDOWN') {
  144. my $timeLeft = 0;
  145. if ($state eq 'SHUTDOWN') {
  146. $timeLeft = $OFF_DELAY - $elapsed;
  147. } else {
  148. $timeLeft = ($TIMEOUT + $COUNTDOWN) - $elapsed;
  149. }
  150. sayShutdown($timeLeft / 60);
  151. }
  152. # Only allow updates to "ON" or "OFF" -- the projector knows no other states
  153. if ($update && ($state ne 'ON' && $state ne 'OFF')) {
  154. $update = 0;
  155. }
  156. # Update the projector
  157. if ($update) {
  158. # Extra debugging to record pushes
  159. if ($DEBUG) {
  160. print STDERR 'State: ' . $state . "\n";
  161. }
  162. # Send master power state
  163. say('Projector ' . $state);
  164. $proj->send($state)
  165. or die('Unable to write command to proj socket: ' . $state . ": ${!}\n");
  166. # No output file
  167. # Update the push time
  168. $pushLast = time();
  169. # Clear the lastAnnounce timer
  170. $lastAnnounce = 0;
  171. # Clear the update flag
  172. $update = 0;
  173. # Wait before accepting a new command
  174. sleep($CMD_DELAY);
  175. }
  176. }
  177. # Speak
  178. sub say($) {
  179. my $str = (@_);
  180. system('say', $str);
  181. }
  182. sub sayShutdown($) {
  183. my $minutesLeft = (@_);
  184. # Only allow annoucements once per minute
  185. if (time() < $lastAnnounce + 60) {
  186. return;
  187. }
  188. $lastAnnounce = time();
  189. # Determine the unit
  190. my $unit = 'minute';
  191. my $timeLeft = ceil($minutesLeft);
  192. if ($minutesLeft < 1) {
  193. $timeLeft = $minutesLeft * 60;
  194. $unit = 'second';
  195. }
  196. # Add an "s" as needed
  197. my $plural = 's';
  198. if ($timeLeft == 1) {
  199. $plural = '';
  200. }
  201. $unit .= $plural;
  202. # Speak
  203. say('Projector shutdown in about ' . $timeLeft . ' ' . $unit);
  204. }