projectorPower.pl 7.8 KB

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