projectorCtrl.pl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 = 1600;
  24. my %COLORS = (
  25. '0.332' => {
  26. 'high' => 'THEATER',
  27. 'play' => 'THEATER_BLACK_1',
  28. 'low' => 'THEATER_BLACK_1',
  29. },
  30. '0.550' => {
  31. 'high' => 'NATURAL',
  32. 'play' => 'THEATER',
  33. 'low' => 'THEATER_BLACK_1',
  34. },
  35. '0.625' => {
  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, unless we're about to turn off
  120. if ($mtime{'MOTION'} > $lastUser) {
  121. if (!$shutdown || $timeLeftLast > $OFF_DELAY) {
  122. $lastUser = $mtime{'MOTION'};
  123. }
  124. }
  125. # Motion counts as activity when PLEX is not active
  126. if (!$exists{'PLEX'} && $mtime{'MOTION'} > $lastUser) {
  127. $lastUser = $mtime{'MOTION'};
  128. }
  129. # Explict no-motion counts as activity when Plex is not foreground
  130. if (!$exists{'PLEX'} && $exists{'NO_MOTION'} && $lastUser < $now) {
  131. $lastUser = $now;
  132. }
  133. # Clear the shutdown timestamp if there is new user activity
  134. if ($shutdown && (!$exists{'PROJECTOR'} || $lastUser > $shutdown)) {
  135. $shutdown = 0;
  136. }
  137. # Record the shutdown timestamp, if the projector is on
  138. if ($newState eq 'SHUTDOWN' && $exists{'PROJECTOR'}) {
  139. if ($DEBUG) {
  140. print STDERR "Recorded shutdown timestamp\n";
  141. }
  142. $shutdown = $now;
  143. $lastUser = $now;
  144. }
  145. # Calculate the elapsed time
  146. my $elapsed = $now - $lastUser;
  147. if ($DEBUG) {
  148. print STDERR 'Time since last user action: ' . $elapsed . "\n";
  149. }
  150. # Calculate the new state
  151. if ($exists{'PROJECTOR'}) {
  152. if ($shutdown) {
  153. $state = 'SHUTDOWN';
  154. if ($elapsed > $OFF_DELAY) {
  155. $state = 'OFF';
  156. }
  157. } else {
  158. if ($elapsed > $TIMEOUT) {
  159. $state = 'COUNTDOWN';
  160. if ($elapsed > $TIMEOUT + $COUNTDOWN) {
  161. $state = 'OFF';
  162. }
  163. } else {
  164. $state = 'ON';
  165. }
  166. }
  167. } else {
  168. if ($newState eq 'ON') {
  169. $state = 'ON';
  170. }
  171. }
  172. # Select a color set based on the lamp life
  173. if (exists($exists{'PROJECTOR_LAMP'})) {
  174. $life = $exists{'PROJECTOR_LAMP'} / $LAMP_LIFE;
  175. if ($DEBUG) {
  176. print STDERR 'Lamp life: ' . ($life * 100) . "%\n";
  177. }
  178. my $max_set_num = scalar(@color_sets) - 1;
  179. $color_set = $color_sets[$max_set_num];
  180. for (my $i = 0 ; $i < $max_set_num ; $i++) {
  181. if ($life < $color_sets[$i]) {
  182. $color_set = $color_sets[$i];
  183. last;
  184. }
  185. }
  186. if ($DEBUG) {
  187. print STDERR 'Color set: ' . $color_set . "\n";
  188. }
  189. }
  190. # Calculate the color mode
  191. # PLAY for normal playback
  192. # HIGH when playing and LIGHTS || BRIGHT
  193. # LOW for audio, and when we're half way to the timeout (to save the bulb)
  194. # HIGH when the GUI is up
  195. # PLAY if we haven't figured out what else to do
  196. my $playLights = 0;
  197. if ($newState eq 'PLAY') {
  198. $lastPlay = $now;
  199. $playLights = 1;
  200. } elsif ($newState eq 'PAUSE' && $lastPlay + $PAUSE_DELAY > $now) {
  201. $playLights = 1;
  202. } elsif ($exists{'PLAYING_TYPE'} eq 'audio') {
  203. $color = $COLORS{$color_set}{'low'};
  204. } elsif ($elapsed > $TIMEOUT / 2) {
  205. $color = $COLORS{$color_set}{'low'};
  206. } elsif ($exists{'GUI'}) {
  207. $color = $COLORS{$color_set}{'high'};
  208. } else {
  209. $playLights = 1;
  210. }
  211. # If playLights was set, choose the color mode based on the LIGHTS setting
  212. if ($playLights) {
  213. $color = $COLORS{$color_set}{'play'};
  214. if ($exists{'LIGHTS'} || $exists{'BRIGHT'}) {
  215. $color = $COLORS{$color_set}{'high'};
  216. }
  217. }
  218. if ($DEBUG) {
  219. print STDERR 'Selected color: ' . $color . "\n";
  220. }
  221. # Calculate the shutdown counter
  222. $timeLeft = 0;
  223. if ($state eq 'SHUTDOWN') {
  224. $timeLeft = $OFF_DELAY - $elapsed;
  225. } elsif ($state eq 'COUNTDOWN') {
  226. $timeLeft = ($TIMEOUT + $COUNTDOWN) - $elapsed;
  227. }
  228. # Update power when there is a state mismatch
  229. if (($state eq 'OFF' && $exists{'PROJECTOR'}) || ($state eq 'ON' && !$exists{'PROJECTOR'})) {
  230. if ($DEBUG) {
  231. print STDERR 'Power state mismatch: ' . $state . ':' . $exists{'PROJECTOR'} . "\n";
  232. }
  233. $powerCmd = $state;
  234. }
  235. # Update color when there is a state mismatch and the projector is on
  236. if ($exists{'PROJECTOR'} && $exists{'PROJECTOR_COLOR'} ne $color) {
  237. if ($DEBUG) {
  238. print STDERR 'Color state mismatch: ' . $color . ':' . $exists{'PROJECTOR_COLOR'} . "\n";
  239. }
  240. $colorCmd = $color;
  241. }
  242. # Save the power state to disk
  243. if ($state ne $stateLast) {
  244. my ($fh, $tmp) = tempfile($POWER_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  245. print $fh $state . "\n";
  246. close($fh);
  247. rename($tmp, $POWER_FILE);
  248. $stateLast = $state;
  249. }
  250. # Save the color state to disk
  251. if ($color ne $colorLast) {
  252. my ($fh, $tmp) = tempfile($COLOR_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  253. print $fh $color . "\n";
  254. close($fh);
  255. rename($tmp, $COLOR_FILE);
  256. $colorLast = $color;
  257. }
  258. # Save the lamp life to disk
  259. if ($life != $lifeLast) {
  260. my ($fh, $tmp) = tempfile($LIFE_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  261. print $fh $life . "\n";
  262. close($fh);
  263. rename($tmp, $LIFE_FILE);
  264. $lifeLast = $life;
  265. }
  266. # Save the shutdown time to disk
  267. if ($timeLeft != $timeLeftLast) {
  268. my ($fh, $tmp) = tempfile($TIMER_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  269. print $fh $timeLeft . "\n";
  270. close($fh);
  271. rename($tmp, $TIMER_FILE);
  272. $timeLeftLast = $timeLeft;
  273. }
  274. # Send the color mode, if requested
  275. if ($colorCmd) {
  276. if ($DEBUG) {
  277. print STDERR 'Setting color mode: ' . $color . "\n";
  278. }
  279. $proj->send($color)
  280. or die('Unable to write command to projector socket: ' . $color . ": ${!}\n");
  281. $colorCmd = undef();
  282. }
  283. # Send master power state, if requested
  284. # Only allow updates every few seconds -- the projector goes dumb during power state changes
  285. if ($powerCmd && $now > $powerLast + $CMD_DELAY) {
  286. if ($DEBUG) {
  287. print STDERR 'Setting power state: ' . $state . "\n";
  288. }
  289. $proj->send($state)
  290. or die('Unable to write command to proj socket: ' . $state . ": ${!}\n");
  291. $powerLast = $now;
  292. $powerCmd = undef();
  293. }
  294. }