projectorPower.pl 8.4 KB

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