projectorPower.pl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. # App config
  14. my $DATA_DIR = DMX::dataDir();
  15. my $OUTPUT_FILE = $DATA_DIR . 'PROJECTOR_POWER';
  16. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  17. my $PROJ_SOCK = $DATA_DIR . 'PROJECTOR.socket';
  18. my $PUSH_TIMEOUT = 20;
  19. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  20. my $DELAY = $PULL_TIMEOUT / 2;
  21. # Debug
  22. my $DEBUG = 0;
  23. if ($ENV{'DEBUG'}) {
  24. $DEBUG = 1;
  25. }
  26. # Sockets
  27. DMX::stateSocket($STATE_SOCK);
  28. DMX::stateSubscribe($STATE_SOCK);
  29. my $proj = DMX::clientSock($PROJ_SOCK);
  30. # State
  31. my $state = 'OFF';
  32. my $stateLast = $state;
  33. my %exists = ();
  34. my $pushLast = 0;
  35. my $pullLast = time();
  36. my $update = 0;
  37. my $lastCount = 0;
  38. my $lastUser = time();
  39. # Loop forever
  40. while (1) {
  41. # State is calculated; use newState to gather data
  42. my $newState = $state;
  43. # Wait for state updates
  44. my $cmdState = DMX::readState($DELAY, \%exists, undef());
  45. if (defined($cmdState)) {
  46. $newState = $cmdState;
  47. $pullLast = time();
  48. }
  49. # Record the lastUser time -- last GUI update or last time we were playing
  50. if ($lastUser < $exists{'GUI'}) {
  51. $lastUser = $exists{'GUI'};
  52. }
  53. my $now = time();
  54. if ($lastUser < $now) {
  55. if ($exists{'PLAYING'}) {
  56. $lastUser = $now;
  57. }
  58. }
  59. # Treat a transition from "OFF" to "PAUSE" as user activity (i.e. someone fired up the projector)
  60. if ($lastUser < $now) {
  61. if ($newState eq 'PAUSE' && $stateLast eq 'OFF') {
  62. $lastUser = $now;
  63. }
  64. }
  65. # Calculate the elapsed time
  66. my $elapsed = time() - $lastUser;
  67. if ($DEBUG) {
  68. print STDERR 'Time since last user action: ' . $elapsed . "\n";
  69. }
  70. # Calculate the new state
  71. $stateLast = $state;
  72. if ($exists{'PROJECTOR'}) {
  73. if ($elapsed > $TIMEOUT) {
  74. $state = 'COUNTDOWN';
  75. if ($elapsed > $TIMEOUT + $COUNTDOWN) {
  76. $state = 'OFF';
  77. }
  78. } else {
  79. $state = 'ON';
  80. }
  81. }
  82. # Force updates when there is a physical state mistmatch
  83. # This will need an update if we ever handle "ON"
  84. if ($state eq 'OFF') {
  85. if ($exists{'PROJECTOR'}) {
  86. $update = 1;
  87. }
  88. }
  89. # Force updates on a periodic basis
  90. if (time() - $pushLast > $PUSH_TIMEOUT) {
  91. # Not for the projector
  92. #$update = 1;
  93. }
  94. # Die if we don't see regular updates
  95. if (time() - $pullLast > $PULL_TIMEOUT) {
  96. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  97. }
  98. # Force updates on any state change
  99. if ($stateLast ne $state) {
  100. if ($DEBUG) {
  101. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  102. }
  103. $update = 1;
  104. }
  105. # Update the projector
  106. if ($update) {
  107. # Extra debugging to record pushes
  108. if ($DEBUG) {
  109. print STDERR 'State: ' . $state . "\n";
  110. }
  111. # Send master power state
  112. if ($state eq 'OFF' || $state eq 'ON') {
  113. $proj->send($state)
  114. or die('Unable to write command to proj socket: ' . $state . ": ${!}\n");
  115. }
  116. # No output file
  117. # Update the push time
  118. $pushLast = time();
  119. # Clear the update flag
  120. $update = 0;
  121. }
  122. # Announce a pending shutdown every minute
  123. if ($state eq 'COUNTDOWN') {
  124. my $timeLeft = ($TIMEOUT + $COUNTDOWN) - $elapsed;
  125. $timeLeft = ceil($timeLeft / 60);
  126. if ($lastCount != $timeLeft) {
  127. my $plural = 's';
  128. if ($timeLeft == 1) {
  129. $plural = '';
  130. }
  131. system('say', 'Projector powerdown in about ' . $timeLeft . ' minute' . $plural);
  132. $lastCount = $timeLeft;
  133. }
  134. }
  135. }