projectorPower.pl 6.4 KB

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