projectorPower.pl 6.4 KB

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