projectorPower.pl 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. # Avoid repeated calls to time()
  83. my $now = time();
  84. # Wait for state updates
  85. {
  86. my $cmdState = DMX::readState($DELAY, \%exists, \%mtime, undef());
  87. if (defined($cmdState)) {
  88. $newState = $cmdState;
  89. $pullLast = $now;
  90. }
  91. }
  92. # Die if we don't see regular updates
  93. if ($now - $pullLast > $PULL_TIMEOUT) {
  94. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  95. }
  96. # Menu changes count as activity
  97. if ($mtime{'GUI'} && $lastUser < $mtime{'GUI'}) {
  98. $lastUser = $mtime{'GUI'};
  99. }
  100. # Projector starts count as activity
  101. if ($exists{'PROJECTOR'} && $lastUser < $mtime{'PROJECTOR'}) {
  102. $lastUser = $mtime{'PROJECTOR'};
  103. } elsif ($newState eq 'ON') {
  104. $lastUser = $now;
  105. }
  106. # Playing counts as activity
  107. if ($exists{'PLAYING'} && $lastUser < $now) {
  108. $lastUser = $now;
  109. }
  110. # Clear the shutdown timestamp if there is new user activity
  111. if ($shutdown && (!$exists{'PROJECTOR'} || $lastUser > $shutdown)) {
  112. $shutdown = 0;
  113. }
  114. # Record the shutdown timestamp, if the projector is on
  115. if ($newState eq 'SHUTDOWN' && $exists{'PROJECTOR'}) {
  116. if ($DEBUG) {
  117. print STDERR "Recorded shutdown timestamp\n";
  118. }
  119. $shutdown = $now;
  120. $lastUser = $now;
  121. }
  122. # Calculate the elapsed time
  123. my $elapsed = $now - $lastUser;
  124. if ($DEBUG) {
  125. print STDERR 'Time since last user action: ' . $elapsed . "\n";
  126. }
  127. # Calculate the new state
  128. $stateLast = $state;
  129. if ($exists{'PROJECTOR'}) {
  130. if ($shutdown) {
  131. $state = 'SHUTDOWN';
  132. if ($elapsed > $OFF_DELAY) {
  133. $state = 'OFF';
  134. }
  135. } else {
  136. if ($elapsed > $TIMEOUT) {
  137. $state = 'COUNTDOWN';
  138. if ($elapsed > $TIMEOUT + $COUNTDOWN) {
  139. $state = 'OFF';
  140. }
  141. } else {
  142. $state = 'ON';
  143. }
  144. }
  145. } else {
  146. if ($newState eq 'ON') {
  147. $state = 'ON';
  148. }
  149. }
  150. # Select a color set based on the lamp life
  151. if (exists($exists{'PROJECTOR_LAMP'})) {
  152. $life = $exists{'PROJECTOR_LAMP'} / $LAMP_LIFE;
  153. if ($DEBUG) {
  154. print STDERR 'Lamp life: ' . ($life * 100) . "%\n";
  155. }
  156. my $max_set_num = scalar(@color_sets) - 1;
  157. $color_set = $color_sets[$max_set_num];
  158. for (my $i = 0 ; $i < $max_set_num ; $i++) {
  159. if ($life < $color_sets[$i]) {
  160. $color_set = $color_sets[$i];
  161. last;
  162. }
  163. }
  164. if ($DEBUG) {
  165. print STDERR 'Color set: ' . $color_set . "\n";
  166. }
  167. }
  168. # Calculate the color mode
  169. # PLAY for normal playback
  170. # HIGH when playing and LIGHTS || BRIGHT
  171. # LOW for audio, and when we're half way to the timeout (to save the bulb)
  172. # HIGH when the GUI is up
  173. # PLAY if we haven't figured out what else to do
  174. my $playLights = 0;
  175. if ($newState eq 'PLAY') {
  176. $playLights = 1;
  177. } elsif ($exists{'PLAYING_TYPE'} eq 'audio') {
  178. $color = $COLORS{$color_set}{'low'};
  179. } elsif ($elapsed > $TIMEOUT / 2) {
  180. $color = $COLORS{$color_set}{'low'};
  181. } elsif ($exists{'GUI'}) {
  182. $color = $COLORS{$color_set}{'high'};
  183. } else {
  184. $playLights = 1;
  185. }
  186. # If playLights was set, choose the color mode based on the LIGHTS setting
  187. if ($playLights) {
  188. $color = $COLORS{$color_set}{'play'};
  189. if ($exists{'LIGHTS'} || $exists{'BRIGHT'}) {
  190. $color = $COLORS{$color_set}{'high'};
  191. }
  192. }
  193. if ($DEBUG) {
  194. print STDERR 'Selected color: ' . $color . "\n";
  195. }
  196. # Force updates on a periodic basis
  197. if (!$update && $now - $pushLast > $PUSH_TIMEOUT) {
  198. # Not for the projector
  199. #if ($DEBUG) {
  200. # print STDERR "Forcing periodic update\n";
  201. #}
  202. #$update = 1;
  203. }
  204. # Force updates on any state change
  205. if (!$update && $stateLast ne $state) {
  206. if ($DEBUG) {
  207. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  208. }
  209. $update = 1;
  210. }
  211. # Force updates when there is a physical state mistmatch
  212. if (!$update && $state eq 'OFF' && $exists{'PROJECTOR'}) {
  213. if ($DEBUG) {
  214. print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'PROJECTOR'} . "\n";
  215. }
  216. $update = 1;
  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 && $now < $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. my $chance = ($life - 0.9) * 10;
  265. if (rand(1) <= $chance) {
  266. DMX::say('Projector lamp has run for ' . int($life * 100) . '% of its estimated life.');
  267. }
  268. }
  269. }
  270. # No output file
  271. # Update the push time
  272. $pushLast = $now;
  273. # Clear the lastAnnounce timer
  274. $lastAnnounce = 0;
  275. # Clear the update flag
  276. $update = 0;
  277. }
  278. }
  279. sub sayShutdown($) {
  280. my ($minutesLeft) = @_;
  281. # Only allow annoucements once per minute
  282. my $now = time();
  283. if ($now < $lastAnnounce + 60) {
  284. return;
  285. }
  286. $lastAnnounce = $now;
  287. # Determine the unit
  288. my $unit = 'minute';
  289. my $timeLeft = $minutesLeft;
  290. if ($minutesLeft < 1) {
  291. $timeLeft = $minutesLeft * 60;
  292. $unit = 'second';
  293. }
  294. # Avoid saying "0" unless we *really* mean it
  295. $timeLeft = ceil($timeLeft);
  296. # Add an "s" as needed
  297. my $plural = 's';
  298. if ($timeLeft == 1) {
  299. $plural = '';
  300. }
  301. $unit .= $plural;
  302. # Speak
  303. DMX::say('Projector shutdown in about ' . $timeLeft . ' ' . $unit);
  304. }