projectorPower.pl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. print STDERR 'NewState: ' . $newState . "\n";
  88. if ($newState eq 'SHUTDOWN' && $exists{'PROJECTOR'}) {
  89. if ($DEBUG) {
  90. print STDERR "Recorded shutdown timestamp\n";
  91. }
  92. $shutdown = time();
  93. $lastUser = $shutdown;
  94. } elsif ($newState eq 'SHUTDOWN') {
  95. print STDERR "Not recording shutdown because exists is false?\n";
  96. }
  97. # Calculate the elapsed time
  98. my $elapsed = time() - $lastUser;
  99. if ($DEBUG) {
  100. print STDERR 'Time since last user action: ' . $elapsed . "\n";
  101. }
  102. # Calculate the new state
  103. $stateLast = $state;
  104. if ($exists{'PROJECTOR'}) {
  105. if ($shutdown) {
  106. $state = 'SHUTDOWN';
  107. if ($elapsed > $OFF_DELAY) {
  108. $state = 'OFF';
  109. }
  110. } else {
  111. if ($elapsed > $TIMEOUT) {
  112. $state = 'COUNTDOWN';
  113. if ($elapsed > $TIMEOUT + $COUNTDOWN) {
  114. $state = 'OFF';
  115. }
  116. } else {
  117. $state = 'ON';
  118. }
  119. }
  120. } else {
  121. if ($newState eq 'ON') {
  122. $state = 'ON';
  123. }
  124. }
  125. # Force updates on a periodic basis
  126. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  127. # Not for the projector
  128. #if ($DEBUG) {
  129. # print STDERR "Forcing periodic update\n";
  130. #}
  131. #$update = 1;
  132. }
  133. # Force updates on any state change
  134. if (!$update && $stateLast ne $state) {
  135. if ($DEBUG) {
  136. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  137. }
  138. $update = 1;
  139. }
  140. # Force updates when there is a physical state mistmatch
  141. if (!$update) {
  142. if ($state eq 'OFF' && $exists{'PROJECTOR'}) {
  143. if ($DEBUG) {
  144. print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'PROJECTOR'} . "\n";
  145. }
  146. $update = 1;
  147. }
  148. }
  149. # Announce a pending shutdown
  150. if ($state eq 'COUNTDOWN' || $state eq 'SHUTDOWN') {
  151. my $timeLeft = 0;
  152. if ($state eq 'SHUTDOWN') {
  153. $timeLeft = $OFF_DELAY - $elapsed;
  154. } else {
  155. $timeLeft = ($TIMEOUT + $COUNTDOWN) - $elapsed;
  156. }
  157. sayShutdown($timeLeft / 60);
  158. }
  159. # Only allow updates to "ON" or "OFF" -- the projector knows no other states
  160. if ($update && ($state ne 'ON' && $state ne 'OFF')) {
  161. $update = 0;
  162. }
  163. # Update the projector
  164. if ($update) {
  165. # Extra debugging to record pushes
  166. if ($DEBUG) {
  167. print STDERR 'State: ' . $state . "\n";
  168. }
  169. # Send master power state
  170. say('Projector ' . $state);
  171. $proj->send($state)
  172. or die('Unable to write command to proj socket: ' . $state . ": ${!}\n");
  173. # No output file
  174. # Update the push time
  175. $pushLast = time();
  176. # Clear the lastAnnounce timer
  177. $lastAnnounce = 0;
  178. # Clear the update flag
  179. $update = 0;
  180. }
  181. }
  182. # Speak
  183. sub say($) {
  184. my ($str) = @_;
  185. if ($DEBUG) {
  186. print STDERR 'Say: ' . $str . "\n";
  187. }
  188. system('say', $str);
  189. }
  190. sub sayShutdown($) {
  191. my ($minutesLeft) = @_;
  192. # Only allow annoucements once per minute
  193. if (time() < $lastAnnounce + 60) {
  194. return;
  195. }
  196. $lastAnnounce = time();
  197. # Determine the unit
  198. my $unit = 'minute';
  199. my $timeLeft = ceil($minutesLeft);
  200. if ($minutesLeft < 1) {
  201. $timeLeft = $minutesLeft * 60;
  202. $unit = 'second';
  203. }
  204. # Add an "s" as needed
  205. my $plural = 's';
  206. if ($timeLeft == 1) {
  207. $plural = '';
  208. }
  209. $unit .= $plural;
  210. # Speak
  211. say('Projector shutdown in about ' . $timeLeft . ' ' . $unit);
  212. }