projectorPower.pl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IO::Socket::UNIX;
  5. use POSIX qw(ceil floor);
  6. # Prototypes
  7. sub mtime($);
  8. # Config
  9. my $TIMEOUT = 900;
  10. my $COUNTDOWN = 300;
  11. # App config
  12. my $DELAY = 60;
  13. my $CMD_TIMEOUT = 5;
  14. my $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
  15. chomp($TEMP_DIR);
  16. my $DATA_DIR = $TEMP_DIR . 'plexMonitor/';
  17. my $CMD_FILE = $DATA_DIR . 'PROJECTOR.socket';
  18. # Debug
  19. my $DEBUG = 0;
  20. if ($ENV{'DEBUG'}) {
  21. $DEBUG = 1;
  22. }
  23. # Sanity check
  24. if (!-d $DATA_DIR || !-S $CMD_FILE) {
  25. die("Bad config\n");
  26. }
  27. # Socket init
  28. my $sock = IO::Socket::UNIX->new(
  29. 'Peer' => $CMD_FILE,
  30. 'Type' => SOCK_DGRAM,
  31. 'Timeout' => $CMD_TIMEOUT
  32. ) or die('Unable to open socket: ' . $CMD_FILE . ": ${@}\n");
  33. # State
  34. my $state = 'INIT';
  35. my $stateLast = $state;
  36. my $updateLast = time();
  37. my $projector = 0;
  38. # Loop forever
  39. while (1) {
  40. # Monitor the GUI and PLAYING files for changes only
  41. {
  42. my $mtime = mtime($DATA_DIR . 'PLAYING');
  43. if ($mtime > $updateLast) {
  44. $updateLast = $mtime;
  45. }
  46. $mtime = mtime($DATA_DIR . 'GUI');
  47. if ($mtime > $updateLast) {
  48. $updateLast = $mtime;
  49. }
  50. }
  51. # Monitor the PROJECTOR file for state and changes
  52. {
  53. $projector = 0;
  54. my $fh;
  55. open($fh, $DATA_DIR . 'PROJECTOR')
  56. or die("Unable to open PROJECTOR\n");
  57. my $text = <$fh>;
  58. close($fh);
  59. if ($text =~ /1/) {
  60. $projector = 1;
  61. }
  62. if ($DEBUG) {
  63. print STDERR 'Projector: ' . $projector . "\n";
  64. }
  65. my $mtime = mtime($DATA_DIR . 'PROJECTOR');
  66. if ($mtime > $updateLast) {
  67. $updateLast = $mtime;
  68. }
  69. }
  70. # Calculate the new state
  71. my $stateLast = $state;
  72. my $timeSinceUpdate = time() - $updateLast;
  73. if ($DEBUG) {
  74. print STDERR 'Time since update: ' . $timeSinceUpdate . "\n";
  75. }
  76. if ($state eq 'INIT') {
  77. $state = 'COUNTDOWN';
  78. if ($projector) {
  79. $state = 'ON';
  80. }
  81. } elsif ($projector) {
  82. if ($timeSinceUpdate > $TIMEOUT) {
  83. $state = 'COUNTDOWN';
  84. if ($timeSinceUpdate > $TIMEOUT + $COUNTDOWN) {
  85. $state = 'OFF';
  86. }
  87. } else {
  88. $state = 'ON';
  89. }
  90. } else {
  91. if ($timeSinceUpdate < $TIMEOUT) {
  92. $state = 'ON';
  93. }
  94. }
  95. # If the state changed, do something about if
  96. if ($state ne $stateLast) {
  97. if ($DEBUG) {
  98. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  99. }
  100. if ($state eq 'OFF') {
  101. # Turn off the projector
  102. $sock->send($state)
  103. or die('Unable to write command to socket: ' . $CMD_FILE . ': ' . $state . ": ${!}\n");
  104. } elsif ($state eq 'ON') {
  105. # Do nothing, at least for the moment
  106. }
  107. }
  108. # Always announce a pending shutdown
  109. if ($state eq 'COUNTDOWN') {
  110. my $timeLeft = ($TIMEOUT + $COUNTDOWN) - $timeSinceUpdate;
  111. $timeLeft = ceil($timeLeft / 60);
  112. my $plural = 's';
  113. if ($timeLeft == 1) {
  114. $plural = '';
  115. }
  116. system('say', 'Projector powerdown in about ' . $timeLeft . ' minute' . $plural);
  117. }
  118. # Wait and loop
  119. sleep($DELAY);
  120. }
  121. # Cleanup
  122. $sock->close();
  123. undef($sock);
  124. exit(0);
  125. sub mtime($) {
  126. my ($file) = @_;
  127. my $mtime = 0;
  128. if (-r $file) {
  129. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), $mtime, undef(), undef(), undef()) = stat($file);
  130. }
  131. return $mtime;
  132. }