projectorPower.pl 8.1 KB

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