projectorPower.pl 7.7 KB

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