projectorPower.pl 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 = 5;
  15. my $COLOR_DELAY = 60;
  16. my $COLOR_HIGH = 'DYNAMIC';
  17. my $COLOR_LOW = 'THEATER_BLACK_1';
  18. my $STATE_DELAY = 15;
  19. # App config
  20. my $DATA_DIR = DMX::dataDir();
  21. my $OUTPUT_FILE = $DATA_DIR . 'PROJECTOR_POWER';
  22. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  23. my $PROJ_SOCK = $DATA_DIR . 'PROJECTOR.socket';
  24. my $PUSH_TIMEOUT = 20;
  25. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  26. my $DELAY = $PULL_TIMEOUT / 2;
  27. # Prototypes
  28. sub say($);
  29. sub sayShutdown($);
  30. # Debug
  31. my $DEBUG = 0;
  32. if ($ENV{'DEBUG'}) {
  33. $DEBUG = 1;
  34. }
  35. # Sockets
  36. DMX::stateSocket($STATE_SOCK);
  37. DMX::stateSubscribe($STATE_SOCK);
  38. my $proj = DMX::clientSock($PROJ_SOCK);
  39. # State
  40. my $state = 'OFF';
  41. my $stateLast = $state;
  42. my %exists = ();
  43. my %mtime = ();
  44. my $pushLast = 0;
  45. my $pullLast = time();
  46. my $update = 0;
  47. my $lastAnnounce = 0;
  48. my $lastUser = time();
  49. my $shutdown = 0;
  50. my $color = $COLOR_LOW;
  51. # Loop forever
  52. while (1) {
  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. # Always LOW for playback
  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 (!$exists{'GUI'} || $newState eq 'PLAY') {
  133. $color = $COLOR_LOW;
  134. } elsif ($elapsed > $TIMEOUT / 2) {
  135. $color = $COLOR_LOW;
  136. } else {
  137. $color = $COLOR_HIGH;
  138. }
  139. # Force updates on a periodic basis
  140. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  141. # Not for the projector
  142. #if ($DEBUG) {
  143. # print STDERR "Forcing periodic update\n";
  144. #}
  145. #$update = 1;
  146. }
  147. # Force updates on any state change
  148. if (!$update && $stateLast ne $state) {
  149. if ($DEBUG) {
  150. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  151. }
  152. $update = 1;
  153. }
  154. # Force updates when there is a physical state mistmatch
  155. if (!$update) {
  156. if ($state eq 'OFF' && $exists{'PROJECTOR'}) {
  157. if ($DEBUG) {
  158. print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'PROJECTOR'} . "\n";
  159. }
  160. $update = 1;
  161. }
  162. }
  163. # Set the color mode as needed
  164. if ($exists{'PROJECTOR'} && $exists{'PROJECTOR_COLOR'} ne $color) {
  165. if ($DEBUG) {
  166. print STDERR 'Setting color to: ' . $color . "\n";
  167. }
  168. $proj->send($color)
  169. or die('Unable to write command to projector socket: ' . $color . ": ${!}\n");
  170. }
  171. # Announce a pending shutdown
  172. if ($state eq 'COUNTDOWN' || $state eq 'SHUTDOWN') {
  173. my $timeLeft = 0;
  174. if ($state eq 'SHUTDOWN') {
  175. $timeLeft = $OFF_DELAY - $elapsed;
  176. } else {
  177. $timeLeft = ($TIMEOUT + $COUNTDOWN) - $elapsed;
  178. }
  179. sayShutdown($timeLeft / 60);
  180. }
  181. # Only allow updates to "ON" or "OFF" -- the projector knows no other states
  182. if ($update && ($state ne 'ON' && $state ne 'OFF')) {
  183. if ($DEBUG) {
  184. print STDERR 'Ignoring update state other than ON/OFF: ' . $state . "\n";
  185. }
  186. $update = 0;
  187. }
  188. # Only allow updates every few seconds
  189. if ($update && time() < $pushLast + $CMD_DELAY) {
  190. if ($DEBUG) {
  191. print STDERR 'Ignoring overrate update: ' . $state . "\n";
  192. }
  193. $update = 0;
  194. }
  195. # Update the projector
  196. if ($update) {
  197. # Extra debugging to record pushes
  198. if ($DEBUG) {
  199. print STDERR 'State: ' . $state . "\n";
  200. }
  201. # Send master power state
  202. $proj->send($state)
  203. or die('Unable to write command to proj socket: ' . $state . ": ${!}\n");
  204. # Wait -- the Epson UB6500 does not respond to queries while changing power states
  205. # This is pretty long, but we don't set the update clock until it returns so we won't timeout
  206. # We should delay until a projector mtime update, but that's a lot of work for this simple script
  207. if ($DEBUG) {
  208. print STDERR 'Delaying ' . $STATE_DELAY . " seconds while projector changes state\n";
  209. }
  210. sleep($STATE_DELAY);
  211. # Annouce the state change, after the fact
  212. say('Projector ' . $state);
  213. # No output file
  214. # Update the push time
  215. $pushLast = time();
  216. # Clear the lastAnnounce timer
  217. $lastAnnounce = 0;
  218. # Clear the update flag
  219. $update = 0;
  220. }
  221. }
  222. # Speak
  223. sub say($) {
  224. my ($str) = @_;
  225. if ($DEBUG) {
  226. print STDERR 'Say: ' . $str . "\n";
  227. }
  228. system('say', $str);
  229. }
  230. sub sayShutdown($) {
  231. my ($minutesLeft) = @_;
  232. # Only allow annoucements once per minute
  233. if (time() < $lastAnnounce + 60) {
  234. return;
  235. }
  236. $lastAnnounce = time();
  237. # Determine the unit
  238. my $unit = 'minute';
  239. my $timeLeft = $minutesLeft;
  240. if ($minutesLeft < 1) {
  241. $timeLeft = $minutesLeft * 60;
  242. $unit = 'second';
  243. }
  244. # Avoid saying "0" unless we *really* mean it
  245. $timeLeft = ceil($timeLeft);
  246. # Add an "s" as needed
  247. my $plural = 's';
  248. if ($timeLeft == 1) {
  249. $plural = '';
  250. }
  251. $unit .= $plural;
  252. # Speak
  253. say('Projector shutdown in about ' . $timeLeft . ' ' . $unit);
  254. }