projectorPower.pl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 = 300;
  12. my $OFF_DELAY = 15;
  13. my $CMD_DELAY = 20;
  14. my $COLOR_DELAY = 60;
  15. my $COLOR_HIGH = 'DYNAMIC';
  16. my $COLOR_LOW = 'THEATER_BLACK_1';
  17. # App config
  18. my $DATA_DIR = DMX::dataDir();
  19. my $OUTPUT_FILE = $DATA_DIR . 'PROJECTOR_POWER';
  20. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  21. my $PROJ_SOCK = $DATA_DIR . 'PROJECTOR.socket';
  22. my $PUSH_TIMEOUT = 20;
  23. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  24. my $DELAY = $PULL_TIMEOUT / 2;
  25. # Prototypes
  26. sub sayShutdown($);
  27. # Debug
  28. my $DEBUG = 0;
  29. if ($ENV{'DEBUG'}) {
  30. $DEBUG = 1;
  31. }
  32. # Sockets
  33. DMX::stateSocket($STATE_SOCK);
  34. DMX::stateSubscribe($STATE_SOCK);
  35. my $proj = DMX::clientSock($PROJ_SOCK);
  36. # State
  37. my $state = 'OFF';
  38. my $stateLast = $state;
  39. my %exists = ();
  40. my %mtime = ();
  41. my $pushLast = 0;
  42. my $pullLast = time();
  43. my $update = 0;
  44. my $lastAnnounce = 0;
  45. my $lastUser = time();
  46. my $shutdown = 0;
  47. my $color = $COLOR_LOW;
  48. # Loop forever
  49. while (1) {
  50. # State is calculated; use newState to gather data
  51. my $newState = $state;
  52. # Wait for state updates
  53. {
  54. my $cmdState = DMX::readState($DELAY, \%exists, \%mtime, undef());
  55. if (defined($cmdState)) {
  56. $newState = $cmdState;
  57. $pullLast = time();
  58. }
  59. }
  60. # Die if we don't see regular updates
  61. if (time() - $pullLast > $PULL_TIMEOUT) {
  62. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  63. }
  64. # Menu changes count as activity
  65. if ($mtime{'GUI'} && $lastUser < $mtime{'GUI'}) {
  66. $lastUser = $mtime{'GUI'};
  67. }
  68. # Projector starts count as activity
  69. if ($exists{'PROJECTOR'} && $lastUser < $mtime{'PROJECTOR'}) {
  70. $lastUser = $mtime{'PROJECTOR'};
  71. } elsif ($newState eq 'ON') {
  72. $lastUser = time();
  73. }
  74. # Playing counts as activity
  75. if ($exists{'PLAYING'} && $lastUser < time()) {
  76. $lastUser = time();
  77. }
  78. # Clear the shutdown timestamp if there is new user activity
  79. if ($shutdown && (!$exists{'PROJECTOR'} || $lastUser > $shutdown)) {
  80. $shutdown = 0;
  81. }
  82. # Record the shutdown timestamp, if the projector is on
  83. if ($newState eq 'SHUTDOWN' && $exists{'PROJECTOR'}) {
  84. if ($DEBUG) {
  85. print STDERR "Recorded shutdown timestamp\n";
  86. }
  87. $shutdown = time();
  88. $lastUser = $shutdown;
  89. }
  90. # Calculate the elapsed time
  91. my $elapsed = time() - $lastUser;
  92. if ($DEBUG) {
  93. print STDERR 'Time since last user action: ' . $elapsed . "\n";
  94. }
  95. # Calculate the new state
  96. $stateLast = $state;
  97. if ($exists{'PROJECTOR'}) {
  98. if ($shutdown) {
  99. $state = 'SHUTDOWN';
  100. if ($elapsed > $OFF_DELAY) {
  101. $state = 'OFF';
  102. }
  103. } else {
  104. if ($elapsed > $TIMEOUT) {
  105. $state = 'COUNTDOWN';
  106. if ($elapsed > $TIMEOUT + $COUNTDOWN) {
  107. $state = 'OFF';
  108. }
  109. } else {
  110. $state = 'ON';
  111. }
  112. }
  113. } else {
  114. if ($newState eq 'ON') {
  115. $state = 'ON';
  116. }
  117. }
  118. # Calculate the color mode
  119. # LOW for playback, including audio
  120. # HIGH when playing and LIGHTS
  121. # HIGH when the GUI is up
  122. # LOW again when the GUI is up for half the timeout (to save the bulb)
  123. if ($newState eq 'PLAY') {
  124. if ($exists{'LIGHTS'}) {
  125. $color = $COLOR_HIGH;
  126. } else {
  127. $color = $COLOR_LOW;
  128. }
  129. } elsif ($exists{'PLAYING_TYPE'} eq 'Audio') {
  130. $color = $COLOR_LOW;
  131. } elsif (!$exists{'GUI'}) {
  132. $color = $COLOR_LOW;
  133. } elsif ($elapsed > $TIMEOUT / 2) {
  134. $color = $COLOR_LOW;
  135. } else {
  136. $color = $COLOR_HIGH;
  137. }
  138. # Force updates on a periodic basis
  139. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  140. # Not for the projector
  141. #if ($DEBUG) {
  142. # print STDERR "Forcing periodic update\n";
  143. #}
  144. #$update = 1;
  145. }
  146. # Force updates on any state change
  147. if (!$update && $stateLast ne $state) {
  148. if ($DEBUG) {
  149. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  150. }
  151. $update = 1;
  152. }
  153. # Force updates when there is a physical state mistmatch
  154. if (!$update) {
  155. if ($state eq 'OFF' && $exists{'PROJECTOR'}) {
  156. if ($DEBUG) {
  157. print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'PROJECTOR'} . "\n";
  158. }
  159. $update = 1;
  160. }
  161. }
  162. # Set the color mode as needed
  163. if ($exists{'PROJECTOR'} && $exists{'PROJECTOR_COLOR'} ne $color) {
  164. if ($DEBUG) {
  165. print STDERR 'Setting color to: ' . $color . "\n";
  166. }
  167. $proj->send($color)
  168. or die('Unable to write command to projector socket: ' . $color . ": ${!}\n");
  169. }
  170. # Announce a pending shutdown
  171. if ($state eq 'COUNTDOWN' || $state eq 'SHUTDOWN') {
  172. my $timeLeft = 0;
  173. if ($state eq 'SHUTDOWN') {
  174. $timeLeft = $OFF_DELAY - $elapsed;
  175. } else {
  176. $timeLeft = ($TIMEOUT + $COUNTDOWN) - $elapsed;
  177. }
  178. sayShutdown($timeLeft / 60);
  179. }
  180. # Only allow updates to "ON" or "OFF" -- the projector knows no other states
  181. if ($update && ($state ne 'ON' && $state ne 'OFF')) {
  182. if ($DEBUG) {
  183. print STDERR 'Ignoring update state other than ON/OFF: ' . $state . "\n";
  184. }
  185. $update = 0;
  186. }
  187. # Only allow updates every few seconds -- the projector goes dumb during power state changes
  188. if ($update && time() < $pushLast + $CMD_DELAY) {
  189. if ($DEBUG) {
  190. print STDERR 'Ignoring overrate update: ' . $state . "\n";
  191. }
  192. $update = 0;
  193. }
  194. # Update the projector
  195. if ($update) {
  196. # Extra debugging to record pushes
  197. if ($DEBUG) {
  198. print STDERR 'State: ' . $state . "\n";
  199. }
  200. # Send master power state
  201. $proj->send($state)
  202. or die('Unable to write command to proj socket: ' . $state . ": ${!}\n");
  203. # Annouce the state change, after the fact
  204. DMX::say('Projector ' . $state);
  205. # No output file
  206. # Update the push time
  207. $pushLast = time();
  208. # Clear the lastAnnounce timer
  209. $lastAnnounce = 0;
  210. # Clear the update flag
  211. $update = 0;
  212. }
  213. }
  214. sub sayShutdown($) {
  215. my ($minutesLeft) = @_;
  216. # Only allow annoucements once per minute
  217. if (time() < $lastAnnounce + 60) {
  218. return;
  219. }
  220. $lastAnnounce = time();
  221. # Determine the unit
  222. my $unit = 'minute';
  223. my $timeLeft = $minutesLeft;
  224. if ($minutesLeft < 1) {
  225. $timeLeft = $minutesLeft * 60;
  226. $unit = 'second';
  227. }
  228. # Avoid saying "0" unless we *really* mean it
  229. $timeLeft = ceil($timeLeft);
  230. # Add an "s" as needed
  231. my $plural = 's';
  232. if ($timeLeft == 1) {
  233. $plural = '';
  234. }
  235. $unit .= $plural;
  236. # Speak
  237. DMX::say('Projector shutdown in about ' . $timeLeft . ' ' . $unit);
  238. }