projectorPower.pl 6.7 KB

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