projectorPower.pl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. my $CMD_DELAY = 5;
  15. # App config
  16. my $DATA_DIR = DMX::dataDir();
  17. my $OUTPUT_FILE = $DATA_DIR . 'PROJECTOR_POWER';
  18. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  19. my $PROJ_SOCK = $DATA_DIR . 'PROJECTOR.socket';
  20. my $PUSH_TIMEOUT = 20;
  21. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  22. my $DELAY = $PULL_TIMEOUT / 2;
  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 %mtimeTmp = ();
  53. my %existsTmp = ();
  54. my $cmdState = DMX::readState($DELAY, \%existsTmp, \%mtimeTmp, undef());
  55. if (defined($cmdState)) {
  56. $newState = $cmdState;
  57. $pullLast = time();
  58. }
  59. # Only record valid exists/mtime hashes
  60. if (scalar(keys(%existsTmp)) > 0) {
  61. %mtime = %mtimeTmp;
  62. %exists = %existsTmp;
  63. }
  64. }
  65. # Die if we don't see regular updates
  66. if (time() - $pullLast > $PULL_TIMEOUT) {
  67. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  68. }
  69. # Menu changes count as activity
  70. if ($mtime{'GUI'} && $lastUser < $mtime{'GUI'}) {
  71. $lastUser = $mtime{'GUI'};
  72. }
  73. # Projector starts count as activity
  74. if ($exists{'PROJECTOR'} && $lastUser < $mtime{'PROJECTOR'}) {
  75. $lastUser = $mtime{'PROJECTOR'};
  76. } elsif ($newState eq 'ON') {
  77. $lastUser = time();
  78. }
  79. # Playing counts as activity
  80. if ($exists{'PLAYING'} && $lastUser < time()) {
  81. $lastUser = time();
  82. }
  83. # Clear the shutdown timestamp if there is new user activity
  84. if ($shutdown && (!$exists{'PROJECTOR'} || $lastUser > $shutdown)) {
  85. $shutdown = 0;
  86. }
  87. # Record the shutdown timestamp, if the projector is on
  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. }
  95. # Calculate the elapsed time
  96. my $elapsed = time() - $lastUser;
  97. if ($DEBUG) {
  98. print STDERR 'Time since last user action: ' . $elapsed . "\n";
  99. }
  100. # Calculate the new state
  101. $stateLast = $state;
  102. if ($exists{'PROJECTOR'}) {
  103. if ($shutdown) {
  104. $state = 'SHUTDOWN';
  105. if ($elapsed > $OFF_DELAY) {
  106. $state = 'OFF';
  107. }
  108. } else {
  109. if ($elapsed > $TIMEOUT) {
  110. $state = 'COUNTDOWN';
  111. if ($elapsed > $TIMEOUT + $COUNTDOWN) {
  112. $state = 'OFF';
  113. }
  114. } else {
  115. $state = 'ON';
  116. }
  117. }
  118. } else {
  119. if ($newState eq 'ON') {
  120. $state = 'ON';
  121. }
  122. }
  123. # Force updates on a periodic basis
  124. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  125. # Not for the projector
  126. #if ($DEBUG) {
  127. # print STDERR "Forcing periodic update\n";
  128. #}
  129. #$update = 1;
  130. }
  131. # Force updates on any state change
  132. if (!$update && $stateLast ne $state) {
  133. if ($DEBUG) {
  134. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  135. }
  136. $update = 1;
  137. }
  138. # Force updates when there is a physical state mistmatch
  139. if (!$update) {
  140. if ($state eq 'OFF' && $exists{'PROJECTOR'}) {
  141. if ($DEBUG) {
  142. print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'PROJECTOR'} . "\n";
  143. }
  144. $update = 1;
  145. }
  146. }
  147. # Announce a pending shutdown
  148. if ($state eq 'COUNTDOWN' || $state eq 'SHUTDOWN') {
  149. my $timeLeft = 0;
  150. if ($state eq 'SHUTDOWN') {
  151. $timeLeft = $OFF_DELAY - $elapsed;
  152. } else {
  153. $timeLeft = ($TIMEOUT + $COUNTDOWN) - $elapsed;
  154. }
  155. sayShutdown($timeLeft / 60);
  156. }
  157. # Only allow updates to "ON" or "OFF" -- the projector knows no other states
  158. if ($update && ($state ne 'ON' && $state ne 'OFF')) {
  159. if ($DEBUG) {
  160. print STDERR 'Ignoring update state other than ON/OFF: ' . $state . "\n";
  161. }
  162. $update = 0;
  163. }
  164. # Only allow updates every few seconds
  165. if ($update && time() < $pushLast + $CMD_DELAY) {
  166. if ($DEBUG) {
  167. print STDERR 'Ignoring overrate update: ' . $state . "\n";
  168. }
  169. $update = 0;
  170. }
  171. # Update the projector
  172. if ($update) {
  173. # Extra debugging to record pushes
  174. if ($DEBUG) {
  175. print STDERR 'State: ' . $state . "\n";
  176. }
  177. # Send master power state
  178. $proj->send($state)
  179. or die('Unable to write command to proj socket: ' . $state . ": ${!}\n");
  180. # Annouce the state change, after the fact
  181. say('Projector ' . $state);
  182. # No output file
  183. # Update the push time
  184. $pushLast = time();
  185. # Clear the lastAnnounce timer
  186. $lastAnnounce = 0;
  187. # Clear the update flag
  188. $update = 0;
  189. }
  190. }
  191. # Speak
  192. sub say($) {
  193. my ($str) = @_;
  194. if ($DEBUG) {
  195. print STDERR 'Say: ' . $str . "\n";
  196. }
  197. system('say', $str);
  198. }
  199. sub sayShutdown($) {
  200. my ($minutesLeft) = @_;
  201. # Only allow annoucements once per minute
  202. if (time() < $lastAnnounce + 60) {
  203. return;
  204. }
  205. $lastAnnounce = time();
  206. # Determine the unit
  207. my $unit = 'minute';
  208. my $timeLeft = $minutesLeft;
  209. if ($minutesLeft < 1) {
  210. $timeLeft = $minutesLeft * 60;
  211. $unit = 'second';
  212. }
  213. # Avoid saying "0" unless we *really* mean it
  214. $timeLeft = ceil($timeLeft);
  215. # Add an "s" as needed
  216. my $plural = 's';
  217. if ($timeLeft == 1) {
  218. $plural = '';
  219. }
  220. $unit .= $plural;
  221. # Speak
  222. say('Projector shutdown in about ' . $timeLeft . ' ' . $unit);
  223. }