projectorCtrl.pl 8.1 KB

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