projectorPower.pl 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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.332' => {
  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. # Motion counts as activity when GAME is active
  112. if ($exists{'GAME'} && $mtime{'MOTION'} > $lastUser) {
  113. $lastUser = $mtime{'MOTION'};
  114. }
  115. # Motion counts as activity when PLEX is not active
  116. if (!$exists{'PLEX'} && $mtime{'MOTION'} > $lastUser) {
  117. $lastUser = $mtime{'MOTION'};
  118. }
  119. # Explict no-motion counts as activity when Plex is not foreground
  120. if (!$exists{'PLEX'} && $exists{'NO_MOTION'} && $lastUser < $now) {
  121. $lastUser = $now;
  122. }
  123. # Clear the shutdown timestamp if there is new user activity
  124. if ($shutdown && (!$exists{'PROJECTOR'} || $lastUser > $shutdown)) {
  125. $shutdown = 0;
  126. }
  127. # Record the shutdown timestamp, if the projector is on
  128. if ($newState eq 'SHUTDOWN' && $exists{'PROJECTOR'}) {
  129. if ($DEBUG) {
  130. print STDERR "Recorded shutdown timestamp\n";
  131. }
  132. $shutdown = $now;
  133. $lastUser = $now;
  134. }
  135. # Calculate the elapsed time
  136. my $elapsed = $now - $lastUser;
  137. if ($DEBUG) {
  138. print STDERR 'Time since last user action: ' . $elapsed . "\n";
  139. }
  140. # Calculate the new state
  141. $stateLast = $state;
  142. if ($exists{'PROJECTOR'}) {
  143. if ($shutdown) {
  144. $state = 'SHUTDOWN';
  145. if ($elapsed > $OFF_DELAY) {
  146. $state = 'OFF';
  147. }
  148. } else {
  149. if ($elapsed > $TIMEOUT) {
  150. $state = 'COUNTDOWN';
  151. if ($elapsed > $TIMEOUT + $COUNTDOWN) {
  152. $state = 'OFF';
  153. }
  154. } else {
  155. $state = 'ON';
  156. }
  157. }
  158. } else {
  159. if ($newState eq 'ON') {
  160. $state = 'ON';
  161. }
  162. }
  163. # Select a color set based on the lamp life
  164. if (exists($exists{'PROJECTOR_LAMP'})) {
  165. $life = $exists{'PROJECTOR_LAMP'} / $LAMP_LIFE;
  166. if ($DEBUG) {
  167. print STDERR 'Lamp life: ' . ($life * 100) . "%\n";
  168. }
  169. my $max_set_num = scalar(@color_sets) - 1;
  170. $color_set = $color_sets[$max_set_num];
  171. for (my $i = 0 ; $i < $max_set_num ; $i++) {
  172. if ($life < $color_sets[$i]) {
  173. $color_set = $color_sets[$i];
  174. last;
  175. }
  176. }
  177. if ($DEBUG) {
  178. print STDERR 'Color set: ' . $color_set . "\n";
  179. }
  180. }
  181. # Calculate the color mode
  182. # PLAY for normal playback
  183. # HIGH when playing and LIGHTS || BRIGHT
  184. # LOW for audio, and when we're half way to the timeout (to save the bulb)
  185. # HIGH when the GUI is up
  186. # PLAY if we haven't figured out what else to do
  187. my $playLights = 0;
  188. if ($newState eq 'PLAY') {
  189. $lastPlay = $now;
  190. $playLights = 1;
  191. } elsif ($newState eq 'PAUSE' && $lastPlay + $PAUSE_DELAY > $now) {
  192. $playLights = 1;
  193. } elsif ($exists{'PLAYING_TYPE'} eq 'audio') {
  194. $color = $COLORS{$color_set}{'low'};
  195. } elsif ($elapsed > $TIMEOUT / 2) {
  196. $color = $COLORS{$color_set}{'low'};
  197. } elsif ($exists{'GUI'}) {
  198. $color = $COLORS{$color_set}{'high'};
  199. } else {
  200. $playLights = 1;
  201. }
  202. # If playLights was set, choose the color mode based on the LIGHTS setting
  203. if ($playLights) {
  204. $color = $COLORS{$color_set}{'play'};
  205. if ($exists{'LIGHTS'} || $exists{'BRIGHT'}) {
  206. $color = $COLORS{$color_set}{'high'};
  207. }
  208. }
  209. if ($DEBUG) {
  210. print STDERR 'Selected color: ' . $color . "\n";
  211. }
  212. # Force updates on a periodic basis
  213. if (!$update && $now - $pushLast > $PUSH_TIMEOUT) {
  214. # Not for the projector
  215. #if ($DEBUG) {
  216. # print STDERR "Forcing periodic update\n";
  217. #}
  218. #$update = 1;
  219. }
  220. # Force updates on any state change
  221. if (!$update && $stateLast ne $state) {
  222. if ($DEBUG) {
  223. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  224. }
  225. $update = 1;
  226. }
  227. # Force updates when there is a physical state mistmatch
  228. if (!$update && $state eq 'OFF' && $exists{'PROJECTOR'}) {
  229. if ($DEBUG) {
  230. print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'PROJECTOR'} . "\n";
  231. }
  232. $update = 1;
  233. }
  234. # Set the color mode as needed
  235. if ($exists{'PROJECTOR'} && $exists{'PROJECTOR_COLOR'} ne $color) {
  236. if ($DEBUG) {
  237. print STDERR 'Setting color to: ' . $color . "\n";
  238. }
  239. $proj->send($color)
  240. or die('Unable to write command to projector socket: ' . $color . ": ${!}\n");
  241. }
  242. # Announce a pending shutdown
  243. if ($state eq 'COUNTDOWN' || $state eq 'SHUTDOWN') {
  244. my $timeLeft = 0;
  245. if ($state eq 'SHUTDOWN') {
  246. $timeLeft = $OFF_DELAY - $elapsed;
  247. } else {
  248. $timeLeft = ($TIMEOUT + $COUNTDOWN) - $elapsed;
  249. }
  250. sayShutdown($timeLeft / 60);
  251. }
  252. # Only allow updates to "ON" or "OFF" -- the projector knows no other states
  253. if ($update && ($state ne 'ON' && $state ne 'OFF')) {
  254. if ($DEBUG) {
  255. print STDERR 'Ignoring update state other than ON/OFF: ' . $state . "\n";
  256. }
  257. $update = 0;
  258. }
  259. # Only allow updates every few seconds -- the projector goes dumb during power state changes
  260. if ($update && $now < $pushLast + $CMD_DELAY) {
  261. if ($DEBUG) {
  262. print STDERR 'Ignoring overrate update: ' . $state . "\n";
  263. }
  264. $update = 0;
  265. }
  266. # Update the projector
  267. if ($update) {
  268. # Extra debugging to record pushes
  269. if ($DEBUG) {
  270. print STDERR 'State: ' . $state . "\n";
  271. }
  272. # Send master power state
  273. $proj->send($state)
  274. or die('Unable to write command to proj socket: ' . $state . ": ${!}\n");
  275. # Annouce the state change, after the fact
  276. DMX::say('Projector ' . $state);
  277. # Announce lamp life status, if near/past $LAMP_LIFE
  278. if (exists($exists{'PROJECTOR_LAMP'})) {
  279. if ($life > 0.9) {
  280. my $chance = ($life - 0.9) * 10;
  281. if (rand(1) <= $chance) {
  282. DMX::say('Projector lamp has run for ' . int($life * 100) . '% of its estimated life.');
  283. }
  284. }
  285. }
  286. # No output file
  287. # Update the push time
  288. $pushLast = $now;
  289. # Clear the lastAnnounce timer
  290. $lastAnnounce = 0;
  291. # Clear the update flag
  292. $update = 0;
  293. }
  294. }
  295. sub sayShutdown($) {
  296. my ($minutesLeft) = @_;
  297. # Only allow annoucements once per minute
  298. my $now = time();
  299. if ($now < $lastAnnounce + 60) {
  300. return;
  301. }
  302. $lastAnnounce = $now;
  303. # Determine the unit
  304. my $unit = 'minute';
  305. my $timeLeft = $minutesLeft;
  306. if ($minutesLeft < 1) {
  307. $timeLeft = $minutesLeft * 60;
  308. $unit = 'second';
  309. }
  310. # Avoid saying "0" unless we *really* mean it
  311. $timeLeft = ceil($timeLeft);
  312. # Add an "s" as needed
  313. my $plural = 's';
  314. if ($timeLeft == 1) {
  315. $plural = '';
  316. }
  317. $unit .= $plural;
  318. # Speak
  319. DMX::say('Projector shutdown in about ' . $timeLeft . ' ' . $unit);
  320. }