projectorPower.pl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. # Die if we don't see regular updates
  50. if (time() - $pullLast > $PULL_TIMEOUT) {
  51. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  52. }
  53. # Record the lastUser time -- last GUI update or last time we were playing
  54. if ($lastUser < $exists{'GUI'}) {
  55. $lastUser = $exists{'GUI'};
  56. }
  57. my $now = time();
  58. if ($lastUser < $now) {
  59. if ($exists{'PLAYING'}) {
  60. $lastUser = $now;
  61. }
  62. }
  63. # Treat a transition from "OFF" to "PAUSE" as user activity (i.e. someone fired up the projector)
  64. if ($lastUser < $now) {
  65. if ($newState eq 'PAUSE' && $stateLast eq 'OFF') {
  66. $lastUser = $now;
  67. }
  68. }
  69. # Calculate the elapsed time
  70. my $elapsed = time() - $lastUser;
  71. if ($DEBUG) {
  72. print STDERR 'Time since last user action: ' . $elapsed . "\n";
  73. }
  74. # Calculate the new state
  75. $stateLast = $state;
  76. if ($exists{'PROJECTOR'}) {
  77. if ($elapsed > $TIMEOUT) {
  78. $state = 'COUNTDOWN';
  79. if ($elapsed > $TIMEOUT + $COUNTDOWN) {
  80. $state = 'OFF';
  81. }
  82. } else {
  83. $state = 'ON';
  84. }
  85. }
  86. # Force updates on a periodic basis
  87. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  88. # Not for the projector
  89. #if ($DEBUG) {
  90. # print STDERR "Forcing periodic update\n";
  91. #}
  92. #$update = 1;
  93. }
  94. # Force updates on any state change
  95. if (!$update && $stateLast ne $state) {
  96. if ($DEBUG) {
  97. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  98. }
  99. $update = 1;
  100. }
  101. # Force updates when there is a physical state mistmatch
  102. if (!$update) {
  103. if ($state eq 'OFF' && $exists{'PROJECTOR'}) {
  104. if ($DEBUG) {
  105. print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'PROJECTOR'} . "\n";
  106. }
  107. $update = 1;
  108. }
  109. }
  110. # Update the projector
  111. if ($update) {
  112. # Extra debugging to record pushes
  113. if ($DEBUG) {
  114. print STDERR 'State: ' . $state . "\n";
  115. }
  116. # Send master power state
  117. if ($state eq 'OFF' || $state eq 'ON') {
  118. $proj->send($state)
  119. or die('Unable to write command to proj socket: ' . $state . ": ${!}\n");
  120. }
  121. # No output file
  122. # Update the push time
  123. $pushLast = time();
  124. # Clear the update flag
  125. $update = 0;
  126. }
  127. # Announce a pending shutdown every minute
  128. if ($state eq 'COUNTDOWN') {
  129. my $timeLeft = ($TIMEOUT + $COUNTDOWN) - $elapsed;
  130. $timeLeft = ceil($timeLeft / 60);
  131. if ($lastCount != $timeLeft) {
  132. my $plural = 's';
  133. if ($timeLeft == 1) {
  134. $plural = '';
  135. }
  136. system('say', 'Projector powerdown in about ' . $timeLeft . ' minute' . $plural);
  137. $lastCount = $timeLeft;
  138. }
  139. }
  140. }