projectorPower.pl 6.6 KB

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