projectorCtrl.pl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. # Local modules
  5. use Cwd qw( abs_path );
  6. use File::Temp qw( tempfile );
  7. use File::Basename qw( dirname );
  8. use lib dirname(abs_path($0));
  9. use DMX;
  10. # Config
  11. my $TIMEOUT = 900;
  12. my $COUNTDOWN = 119;
  13. my $OFF_DELAY = 15;
  14. my $CMD_DELAY = 20;
  15. my $PAUSE_DELAY = 10;
  16. # Available color modes, bright to dark:
  17. # DYNAMIC
  18. # NATURAL
  19. # THEATER
  20. # THEATER_BLACK_1
  21. #
  22. # Color modes by proportion of lamp life
  23. my $LAMP_LIFE = 1625;
  24. my %COLORS = (
  25. '0.332' => {
  26. 'high' => 'THEATER',
  27. 'play' => 'THEATER_BLACK_1',
  28. 'low' => 'THEATER_BLACK_1',
  29. },
  30. '0.600' => {
  31. 'high' => 'NATURAL',
  32. 'play' => 'THEATER',
  33. 'low' => 'THEATER_BLACK_1',
  34. },
  35. '0.700' => {
  36. 'high' => 'DYNAMIC',
  37. 'play' => 'NATURAL',
  38. 'low' => 'THEATER_BLACK_1',
  39. },
  40. '0.800' => {
  41. 'high' => 'DYNAMIC',
  42. 'play' => 'DYNAMIC',
  43. 'low' => 'THEATER_BLACK_1',
  44. },
  45. );
  46. # App config
  47. my $DATA_DIR = DMX::dataDir();
  48. my $STATE_SOCK = 'PROJECTOR_CTRL';
  49. my $PROJ_SOCK = 'PROJECTOR';
  50. my $OUTPUT_FILE = $DATA_DIR . $STATE_SOCK;
  51. my $POWER_FILE = $OUTPUT_FILE . '_POWER';
  52. my $LIFE_FILE = $OUTPUT_FILE . '_LIFE';
  53. my $TIMER_FILE = $OUTPUT_FILE . '_TIMER';
  54. my $COLOR_FILE = $OUTPUT_FILE . '_COLOR';
  55. my $PUSH_TIMEOUT = 20;
  56. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  57. my $DELAY = $PULL_TIMEOUT / 2;
  58. # Debug
  59. my $DEBUG = 0;
  60. if ($ENV{'DEBUG'}) {
  61. $DEBUG = 1;
  62. }
  63. # Sockets
  64. DMX::stateSocket($STATE_SOCK);
  65. DMX::stateSubscribe($STATE_SOCK);
  66. my $proj = DMX::clientSock($PROJ_SOCK);
  67. # State
  68. my $state = 'OFF';
  69. my $stateLast = $state;
  70. my %exists = ();
  71. my %mtime = ();
  72. my $pullLast = time();
  73. my $lastAnnounce = 0;
  74. my $lastUser = time();
  75. my $shutdown = 0;
  76. my $life = 0;
  77. my $lifeLast = 0;
  78. my @color_sets = sort { $a <=> $b } keys(%COLORS);
  79. my $color_set = $color_sets[0];
  80. my $color = $COLORS{$color_set}{'low'};
  81. my $lastPlay = 0;
  82. my $timeLeft = 0;
  83. my $timeLeftLast = 0;
  84. my $colorCmd = undef();
  85. my $colorLast = 0;
  86. my $powerCmd = undef();
  87. my $powerLast = 0;
  88. # Loop forever
  89. while (1) {
  90. # State is calculated; use newState to gather data
  91. my $newState = $state;
  92. # Wait for state updates
  93. my $cmdState = DMX::readState($DELAY, \%exists, \%mtime, undef());
  94. # Avoid repeated calls to time()
  95. my $now = time();
  96. # Record only valid states
  97. if (defined($cmdState)) {
  98. $newState = $cmdState;
  99. $pullLast = $now;
  100. }
  101. # Die if we don't see regular updates
  102. if ($now - $pullLast > $PULL_TIMEOUT) {
  103. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  104. }
  105. # Menu changes count as activity
  106. if ($mtime{'GUI'} && $lastUser < $mtime{'GUI'}) {
  107. $lastUser = $mtime{'GUI'};
  108. }
  109. # Projector starts count as activity
  110. if ($exists{'PROJECTOR'} && $lastUser < $mtime{'PROJECTOR'}) {
  111. $lastUser = $mtime{'PROJECTOR'};
  112. } elsif ($newState eq 'ON') {
  113. $lastUser = $now;
  114. }
  115. # Playing counts as activity
  116. if ($exists{'PLAYING'} && $lastUser < $now) {
  117. $lastUser = $now;
  118. }
  119. # Motion counts as activity when GAME is active
  120. if ($exists{'GAME'} && $mtime{'MOTION'} > $lastUser) {
  121. $lastUser = $mtime{'MOTION'};
  122. }
  123. # Motion counts as activity when PLEX is not active
  124. if (!$exists{'PLEX'} && $mtime{'MOTION'} > $lastUser) {
  125. $lastUser = $mtime{'MOTION'};
  126. }
  127. # Explict no-motion counts as activity when Plex is not foreground
  128. if (!$exists{'PLEX'} && $exists{'NO_MOTION'} && $lastUser < $now) {
  129. $lastUser = $now;
  130. }
  131. # Clear the shutdown timestamp if there is new user activity
  132. if ($shutdown && (!$exists{'PROJECTOR'} || $lastUser > $shutdown)) {
  133. $shutdown = 0;
  134. }
  135. # Record the shutdown timestamp, if the projector is on
  136. if ($newState eq 'SHUTDOWN' && $exists{'PROJECTOR'}) {
  137. if ($DEBUG) {
  138. print STDERR "Recorded shutdown timestamp\n";
  139. }
  140. $shutdown = $now;
  141. $lastUser = $now;
  142. }
  143. # Calculate the elapsed time
  144. my $elapsed = $now - $lastUser;
  145. if ($DEBUG) {
  146. print STDERR 'Time since last user action: ' . $elapsed . "\n";
  147. }
  148. # Calculate the new state
  149. if ($exists{'PROJECTOR'}) {
  150. if ($shutdown) {
  151. $state = 'SHUTDOWN';
  152. if ($elapsed > $OFF_DELAY) {
  153. $state = 'OFF';
  154. }
  155. } else {
  156. if ($elapsed > $TIMEOUT) {
  157. $state = 'COUNTDOWN';
  158. if ($elapsed > $TIMEOUT + $COUNTDOWN) {
  159. $state = 'OFF';
  160. }
  161. } else {
  162. $state = 'ON';
  163. }
  164. }
  165. } else {
  166. if ($newState eq 'ON') {
  167. $state = 'ON';
  168. }
  169. }
  170. # Select a color set based on the lamp life
  171. if (exists($exists{'PROJECTOR_LAMP'})) {
  172. $life = $exists{'PROJECTOR_LAMP'} / $LAMP_LIFE;
  173. if ($DEBUG) {
  174. print STDERR 'Lamp life: ' . ($life * 100) . "%\n";
  175. }
  176. my $max_set_num = scalar(@color_sets) - 1;
  177. $color_set = $color_sets[$max_set_num];
  178. for (my $i = 0 ; $i < $max_set_num ; $i++) {
  179. if ($life < $color_sets[$i]) {
  180. $color_set = $color_sets[$i];
  181. last;
  182. }
  183. }
  184. if ($DEBUG) {
  185. print STDERR 'Color set: ' . $color_set . "\n";
  186. }
  187. }
  188. # Calculate the color mode
  189. # PLAY for normal playback
  190. # HIGH when playing and LIGHTS || BRIGHT
  191. # LOW for audio, and when we're half way to the timeout (to save the bulb)
  192. # HIGH when the GUI is up
  193. # PLAY if we haven't figured out what else to do
  194. my $playLights = 0;
  195. if ($newState eq 'PLAY') {
  196. $lastPlay = $now;
  197. $playLights = 1;
  198. } elsif ($newState eq 'PAUSE' && $lastPlay + $PAUSE_DELAY > $now) {
  199. $playLights = 1;
  200. } elsif ($exists{'PLAYING_TYPE'} eq 'audio') {
  201. $color = $COLORS{$color_set}{'low'};
  202. } elsif ($elapsed > $TIMEOUT / 2) {
  203. $color = $COLORS{$color_set}{'low'};
  204. } elsif ($exists{'GUI'}) {
  205. $color = $COLORS{$color_set}{'high'};
  206. } else {
  207. $playLights = 1;
  208. }
  209. # If playLights was set, choose the color mode based on the LIGHTS setting
  210. if ($playLights) {
  211. $color = $COLORS{$color_set}{'play'};
  212. if ($exists{'LIGHTS'} || $exists{'BRIGHT'}) {
  213. $color = $COLORS{$color_set}{'high'};
  214. }
  215. }
  216. if ($DEBUG) {
  217. print STDERR 'Selected color: ' . $color . "\n";
  218. }
  219. # Calculate the shutdown counter
  220. $timeLeft = 0;
  221. if ($state eq 'SHUTDOWN') {
  222. $timeLeft = $OFF_DELAY - $elapsed;
  223. } elsif ($state eq 'COUNTDOWN') {
  224. $timeLeft = ($TIMEOUT + $COUNTDOWN) - $elapsed;
  225. }
  226. # Update power when there is a state mismatch
  227. if (($state eq 'OFF' && $exists{'PROJECTOR'}) || ($state eq 'ON' && !$exists{'PROJECTOR'})) {
  228. if ($DEBUG) {
  229. print STDERR 'Power state mismatch: ' . $state . ':' . $exists{'PROJECTOR'} . "\n";
  230. }
  231. $powerCmd = $state;
  232. }
  233. # Update color when there is a state mismatch and the projector is on
  234. if ($exists{'PROJECTOR'} && $exists{'PROJECTOR_COLOR'} ne $color) {
  235. if ($DEBUG) {
  236. print STDERR 'Color state mismatch: ' . $color . ':' . $exists{'PROJECTOR_COLOR'} . "\n";
  237. }
  238. $colorCmd = $color;
  239. }
  240. # Save the power state to disk
  241. if ($state ne $stateLast) {
  242. my ($fh, $tmp) = tempfile($POWER_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  243. print $fh $state . "\n";
  244. close($fh);
  245. rename($tmp, $POWER_FILE);
  246. $stateLast = $state;
  247. }
  248. # Save the color state to disk
  249. if ($color ne $colorLast) {
  250. my ($fh, $tmp) = tempfile($COLOR_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  251. print $fh $color . "\n";
  252. close($fh);
  253. rename($tmp, $COLOR_FILE);
  254. $colorLast = $color;
  255. }
  256. # Save the lamp life to disk
  257. if ($life != $lifeLast) {
  258. my ($fh, $tmp) = tempfile($LIFE_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  259. print $fh $life . "\n";
  260. close($fh);
  261. rename($tmp, $LIFE_FILE);
  262. $lifeLast = $life;
  263. }
  264. # Save the shutdown time to disk
  265. if ($timeLeft != $timeLeftLast) {
  266. my ($fh, $tmp) = tempfile($TIMER_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  267. print $fh $timeLeft . "\n";
  268. close($fh);
  269. rename($tmp, $TIMER_FILE);
  270. $timeLeftLast = $timeLeft;
  271. }
  272. # Send the color mode, if requested
  273. if ($colorCmd) {
  274. if ($DEBUG) {
  275. print STDERR 'Setting color mode: ' . $color . "\n";
  276. }
  277. $proj->send($color)
  278. or die('Unable to write command to projector socket: ' . $color . ": ${!}\n");
  279. $colorCmd = undef();
  280. }
  281. # Send master power state, if requested
  282. # Only allow updates every few seconds -- the projector goes dumb during power state changes
  283. if ($powerCmd && $now > $powerLast + $CMD_DELAY) {
  284. if ($DEBUG) {
  285. print STDERR 'Setting power state: ' . $state . "\n";
  286. }
  287. $proj->send($state)
  288. or die('Unable to write command to proj socket: ' . $state . ": ${!}\n");
  289. $powerLast = $now;
  290. $powerCmd = undef();
  291. }
  292. }