projectorPower.pl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 = 119;
  12. my $OFF_DELAY = 15;
  13. my $CMD_DELAY = 20;
  14. my $COLOR_HIGH = 'NATURAL';
  15. my $COLOR_PLAY = 'THEATER_BLACK_1';
  16. my $COLOR_LOW = 'THEATER_BLACK_1';
  17. my $LAMP_LIFE = 2000;
  18. # App config
  19. my $DATA_DIR = DMX::dataDir();
  20. my $STATE_SOCK = 'PROJECTOR_POWER';
  21. my $PROJ_SOCK = 'PROJECTOR';
  22. my $OUTPUT_FILE = $DATA_DIR . $STATE_SOCK;
  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 normal 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. # PLAY if we haven't figured out what else to do
  125. my $playLights = 0;
  126. if ($newState eq 'PLAY') {
  127. $playLights = 1;
  128. } elsif ($exists{'PLAYING_TYPE'} eq 'Audio') {
  129. $color = $COLOR_LOW;
  130. } elsif ($elapsed > $TIMEOUT / 2) {
  131. $color = $COLOR_LOW;
  132. } elsif ($exists{'GUI'}) {
  133. $color = $COLOR_HIGH;
  134. } else {
  135. $playLights = 1;
  136. }
  137. # If playLights was set, choose the color mode based on the LIGHTS setting
  138. if ($playLights) {
  139. $color = $COLOR_PLAY;
  140. if ($exists{'LIGHTS'}) {
  141. $color = $COLOR_HIGH;
  142. }
  143. }
  144. # Force updates on a periodic basis
  145. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  146. # Not for the projector
  147. #if ($DEBUG) {
  148. # print STDERR "Forcing periodic update\n";
  149. #}
  150. #$update = 1;
  151. }
  152. # Force updates on any state change
  153. if (!$update && $stateLast ne $state) {
  154. if ($DEBUG) {
  155. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  156. }
  157. $update = 1;
  158. }
  159. # Force updates when there is a physical state mistmatch
  160. if (!$update) {
  161. if ($state eq 'OFF' && $exists{'PROJECTOR'}) {
  162. if ($DEBUG) {
  163. print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'PROJECTOR'} . "\n";
  164. }
  165. $update = 1;
  166. }
  167. }
  168. # Set the color mode as needed
  169. if ($exists{'PROJECTOR'} && $exists{'PROJECTOR_COLOR'} ne $color) {
  170. if ($DEBUG) {
  171. print STDERR 'Setting color to: ' . $color . "\n";
  172. }
  173. $proj->send($color)
  174. or die('Unable to write command to projector socket: ' . $color . ": ${!}\n");
  175. }
  176. # Announce a pending shutdown
  177. if ($state eq 'COUNTDOWN' || $state eq 'SHUTDOWN') {
  178. my $timeLeft = 0;
  179. if ($state eq 'SHUTDOWN') {
  180. $timeLeft = $OFF_DELAY - $elapsed;
  181. } else {
  182. $timeLeft = ($TIMEOUT + $COUNTDOWN) - $elapsed;
  183. }
  184. sayShutdown($timeLeft / 60);
  185. }
  186. # Only allow updates to "ON" or "OFF" -- the projector knows no other states
  187. if ($update && ($state ne 'ON' && $state ne 'OFF')) {
  188. if ($DEBUG) {
  189. print STDERR 'Ignoring update state other than ON/OFF: ' . $state . "\n";
  190. }
  191. $update = 0;
  192. }
  193. # Only allow updates every few seconds -- the projector goes dumb during power state changes
  194. if ($update && time() < $pushLast + $CMD_DELAY) {
  195. if ($DEBUG) {
  196. print STDERR 'Ignoring overrate update: ' . $state . "\n";
  197. }
  198. $update = 0;
  199. }
  200. # Update the projector
  201. if ($update) {
  202. # Extra debugging to record pushes
  203. if ($DEBUG) {
  204. print STDERR 'State: ' . $state . "\n";
  205. }
  206. # Send master power state
  207. $proj->send($state)
  208. or die('Unable to write command to proj socket: ' . $state . ": ${!}\n");
  209. # Annouce the state change, after the fact
  210. DMX::say('Projector ' . $state);
  211. # Announce lamp life status, if near/past $LAMP_LIFE
  212. if (exists($exists{'PROJECTOR_LAMP'})) {
  213. my $life = $exists{'PROJECTOR_LAMP'} / $LAMP_LIFE;
  214. if ($DEBUG) {
  215. print STDERR 'Lamp life: ' . $life . "%\n";
  216. }
  217. if ($life > 0.9) {
  218. my $chance = ($life - 0.9) * 10;
  219. if (rand(1) <= $chance) {
  220. DMX::say('Projector lamp has run for ' . int($life * 100) . '% of its projected life.');
  221. }
  222. }
  223. }
  224. # No output file
  225. # Update the push time
  226. $pushLast = time();
  227. # Clear the lastAnnounce timer
  228. $lastAnnounce = 0;
  229. # Clear the update flag
  230. $update = 0;
  231. }
  232. }
  233. sub sayShutdown($) {
  234. my ($minutesLeft) = @_;
  235. # Only allow annoucements once per minute
  236. if (time() < $lastAnnounce + 60) {
  237. return;
  238. }
  239. $lastAnnounce = time();
  240. # Determine the unit
  241. my $unit = 'minute';
  242. my $timeLeft = $minutesLeft;
  243. if ($minutesLeft < 1) {
  244. $timeLeft = $minutesLeft * 60;
  245. $unit = 'second';
  246. }
  247. # Avoid saying "0" unless we *really* mean it
  248. $timeLeft = ceil($timeLeft);
  249. # Add an "s" as needed
  250. my $plural = 's';
  251. if ($timeLeft == 1) {
  252. $plural = '';
  253. }
  254. $unit .= $plural;
  255. # Speak
  256. DMX::say('Projector shutdown in about ' . $timeLeft . ' ' . $unit);
  257. }