off.pl 2.7 KB

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