projectorPower.pl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. # Local modules
  5. use Cwd qw(abs_path);
  6. use File::Basename qw(dirname);
  7. use lib dirname(abs_path($0));
  8. use DMX;
  9. # Config
  10. my $TIMEOUT = 900;
  11. my $COUNTDOWN = 300;
  12. my $OFF_DELAY = 15;
  13. my $CMD_DELAY = 20;
  14. my $COLOR_DELAY = 60;
  15. my $COLOR_HIGH = 'DYNAMIC';
  16. my $COLOR_PLAY = 'THEATER';
  17. my $COLOR_LOW = 'THEATER_BLACK_1';
  18. # App config
  19. my $DATA_DIR = DMX::dataDir();
  20. my $OUTPUT_FILE = $DATA_DIR . 'PROJECTOR_POWER';
  21. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  22. my $PROJ_SOCK = $DATA_DIR . 'PROJECTOR.socket';
  23. my $PUSH_TIMEOUT = 20;
  24. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  25. my $DELAY = $PULL_TIMEOUT / 2;
  26. # Prototypes
  27. sub sayShutdown($);
  28. # Debug
  29. my $DEBUG = 0;
  30. if ($ENV{'DEBUG'}) {
  31. $DEBUG = 1;
  32. }
  33. # Sockets
  34. DMX::stateSocket($STATE_SOCK);
  35. DMX::stateSubscribe($STATE_SOCK);
  36. my $proj = DMX::clientSock($PROJ_SOCK);
  37. # State
  38. my $state = 'OFF';
  39. my $stateLast = $state;
  40. my %exists = ();
  41. my %mtime = ();
  42. my $pushLast = 0;
  43. my $pullLast = time();
  44. my $update = 0;
  45. my $lastAnnounce = 0;
  46. my $lastUser = time();
  47. my $shutdown = 0;
  48. my $color = $COLOR_LOW;
  49. # Loop forever
  50. while (1) {
  51. # State is calculated; use newState to gather data
  52. my $newState = $state;
  53. # Wait for state updates
  54. {
  55. my $cmdState = DMX::readState($DELAY, \%exists, \%mtime, undef());
  56. if (defined($cmdState)) {
  57. $newState = $cmdState;
  58. $pullLast = time();
  59. }
  60. }
  61. # Die if we don't see regular updates
  62. if (time() - $pullLast > $PULL_TIMEOUT) {
  63. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  64. }
  65. # Menu changes count as activity
  66. if ($mtime{'GUI'} && $lastUser < $mtime{'GUI'}) {
  67. $lastUser = $mtime{'GUI'};
  68. }
  69. # Projector starts count as activity
  70. if ($exists{'PROJECTOR'} && $lastUser < $mtime{'PROJECTOR'}) {
  71. $lastUser = $mtime{'PROJECTOR'};
  72. } elsif ($newState eq 'ON') {
  73. $lastUser = time();
  74. }
  75. # Playing counts as activity
  76. if ($exists{'PLAYING'} && $lastUser < time()) {
  77. $lastUser = time();
  78. }
  79. # Clear the shutdown timestamp if there is new user activity
  80. if ($shutdown && (!$exists{'PROJECTOR'} || $lastUser > $shutdown)) {
  81. $shutdown = 0;
  82. }
  83. # Record the shutdown timestamp, if the projector is on
  84. if ($newState eq 'SHUTDOWN' && $exists{'PROJECTOR'}) {
  85. if ($DEBUG) {
  86. print STDERR "Recorded shutdown timestamp\n";
  87. }
  88. $shutdown = time();
  89. $lastUser = $shutdown;
  90. }
  91. # Calculate the elapsed time
  92. my $elapsed = time() - $lastUser;
  93. if ($DEBUG) {
  94. print STDERR 'Time since last user action: ' . $elapsed . "\n";
  95. }
  96. # Calculate the new state
  97. $stateLast = $state;
  98. if ($exists{'PROJECTOR'}) {
  99. if ($shutdown) {
  100. $state = 'SHUTDOWN';
  101. if ($elapsed > $OFF_DELAY) {
  102. $state = 'OFF';
  103. }
  104. } else {
  105. if ($elapsed > $TIMEOUT) {
  106. $state = 'COUNTDOWN';
  107. if ($elapsed > $TIMEOUT + $COUNTDOWN) {
  108. $state = 'OFF';
  109. }
  110. } else {
  111. $state = 'ON';
  112. }
  113. }
  114. } else {
  115. if ($newState eq 'ON') {
  116. $state = 'ON';
  117. }
  118. }
  119. # Calculate the color mode
  120. # PLAY for playback
  121. # HIGH when playing and LIGHTS
  122. # LOW for audio, and when we're half way to the timeout (to save the bulb)
  123. # HIGH when the GUI is up
  124. # LOW if we haven't figured out what else to do
  125. if ($newState eq 'PLAY') {
  126. if ($exists{'LIGHTS'}) {
  127. $color = $COLOR_HIGH;
  128. } else {
  129. $color = $COLOR_PLAY;
  130. }
  131. } elsif ($exists{'PLAYING_TYPE'} eq 'Audio') {
  132. $color = $COLOR_LOW;
  133. } elsif ($elapsed > $TIMEOUT / 2) {
  134. $color = $COLOR_LOW;
  135. } elsif ($exists{'GUI'}) {
  136. $color = $COLOR_HIGH;
  137. } else {
  138. $color = $COLOR_LOW;
  139. }
  140. # Force updates on a periodic basis
  141. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  142. # Not for the projector
  143. #if ($DEBUG) {
  144. # print STDERR "Forcing periodic update\n";
  145. #}
  146. #$update = 1;
  147. }
  148. # Force updates on any state change
  149. if (!$update && $stateLast ne $state) {
  150. if ($DEBUG) {
  151. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  152. }
  153. $update = 1;
  154. }
  155. # Force updates when there is a physical state mistmatch
  156. if (!$update) {
  157. if ($state eq 'OFF' && $exists{'PROJECTOR'}) {
  158. if ($DEBUG) {
  159. print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'PROJECTOR'} . "\n";
  160. }
  161. $update = 1;
  162. }
  163. }
  164. # Set the color mode as needed
  165. if ($exists{'PROJECTOR'} && $exists{'PROJECTOR_COLOR'} ne $color) {
  166. if ($DEBUG) {
  167. print STDERR 'Setting color to: ' . $color . "\n";
  168. }
  169. $proj->send($color)
  170. or die('Unable to write command to projector socket: ' . $color . ": ${!}\n");
  171. }
  172. # Announce a pending shutdown
  173. if ($state eq 'COUNTDOWN' || $state eq 'SHUTDOWN') {
  174. my $timeLeft = 0;
  175. if ($state eq 'SHUTDOWN') {
  176. $timeLeft = $OFF_DELAY - $elapsed;
  177. } else {
  178. $timeLeft = ($TIMEOUT + $COUNTDOWN) - $elapsed;
  179. }
  180. sayShutdown($timeLeft / 60);
  181. }
  182. # Only allow updates to "ON" or "OFF" -- the projector knows no other states
  183. if ($update && ($state ne 'ON' && $state ne 'OFF')) {
  184. if ($DEBUG) {
  185. print STDERR 'Ignoring update state other than ON/OFF: ' . $state . "\n";
  186. }
  187. $update = 0;
  188. }
  189. # Only allow updates every few seconds -- the projector goes dumb during power state changes
  190. if ($update && time() < $pushLast + $CMD_DELAY) {
  191. if ($DEBUG) {
  192. print STDERR 'Ignoring overrate update: ' . $state . "\n";
  193. }
  194. $update = 0;
  195. }
  196. # Update the projector
  197. if ($update) {
  198. # Extra debugging to record pushes
  199. if ($DEBUG) {
  200. print STDERR 'State: ' . $state . "\n";
  201. }
  202. # Send master power state
  203. $proj->send($state)
  204. or die('Unable to write command to proj socket: ' . $state . ": ${!}\n");
  205. # Annouce the state change, after the fact
  206. DMX::say('Projector ' . $state);
  207. # No output file
  208. # Update the push time
  209. $pushLast = time();
  210. # Clear the lastAnnounce timer
  211. $lastAnnounce = 0;
  212. # Clear the update flag
  213. $update = 0;
  214. }
  215. }
  216. sub sayShutdown($) {
  217. my ($minutesLeft) = @_;
  218. # Only allow annoucements once per minute
  219. if (time() < $lastAnnounce + 60) {
  220. return;
  221. }
  222. $lastAnnounce = time();
  223. # Determine the unit
  224. my $unit = 'minute';
  225. my $timeLeft = $minutesLeft;
  226. if ($minutesLeft < 1) {
  227. $timeLeft = $minutesLeft * 60;
  228. $unit = 'second';
  229. }
  230. # Avoid saying "0" unless we *really* mean it
  231. $timeLeft = ceil($timeLeft);
  232. # Add an "s" as needed
  233. my $plural = 's';
  234. if ($timeLeft == 1) {
  235. $plural = '';
  236. }
  237. $unit .= $plural;
  238. # Speak
  239. DMX::say('Projector shutdown in about ' . $timeLeft . ' ' . $unit);
  240. }