projectorPower.pl 3.5 KB

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