projectorPower.pl 5.1 KB

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