projectorPower.pl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 only
  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. }
  66. # Calculate the new state
  67. my $stateLast = $state;
  68. my $timeSinceUpdate = time() - $updateLast;
  69. if ($DEBUG) {
  70. print STDERR 'Time since update: ' . $timeSinceUpdate . "\n";
  71. }
  72. if ($state eq 'INIT') {
  73. $state = 'COUNTDOWN';
  74. if ($projector) {
  75. $state = 'ON';
  76. }
  77. } elsif ($projector) {
  78. if ($timeSinceUpdate > $TIMEOUT) {
  79. $state = 'COUNTDOWN';
  80. if ($timeSinceUpdate > $TIMEOUT + $COUNTDOWN) {
  81. $state = 'OFF';
  82. }
  83. } else {
  84. $state = 'ON';
  85. }
  86. } else {
  87. if ($timeSinceUpdate < $TIMEOUT) {
  88. $state = 'ON';
  89. }
  90. }
  91. # If the state changed, do something about if
  92. if ($state ne $stateLast) {
  93. if ($DEBUG) {
  94. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  95. }
  96. if ($state eq 'OFF') {
  97. # Turn off the projector
  98. $sock->send($state)
  99. or die('Unable to write command to socket: ' . $CMD_FILE . ': ' . $state . ": ${!}\n");
  100. } elsif ($state eq 'ON') {
  101. # Do nothing, at least for the moment
  102. }
  103. }
  104. # Always announce a pending shutdown
  105. if ($state eq 'COUNTDOWN') {
  106. my $timeLeft = ($TIMEOUT + $COUNTDOWN) - $timeSinceUpdate;
  107. $timeLeft = ceil($timeLeft / 60);
  108. my $plural = 's';
  109. if ($timeLeft == 1) {
  110. $plural = '';
  111. }
  112. system('say', 'Projector powerdown in about ' . $timeLeft . ' minute' . $plural);
  113. }
  114. # Wait and loop
  115. sleep($DELAY);
  116. }
  117. # Cleanup
  118. $sock->close();
  119. undef($sock);
  120. exit(0);
  121. sub mtime($) {
  122. my ($file) = @_;
  123. my $mtime = 0;
  124. if (-r $file) {
  125. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), $mtime, undef(), undef(), undef()) = stat($file);
  126. }
  127. return $mtime;
  128. }