off.pl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 + $COUNTDOWN) {
  71. $state = 'OFF';
  72. } elsif ($timeSinceUpdate > $TIMEOUT) {
  73. $state = 'COUNTDOWN';
  74. }
  75. } else {
  76. if ($timeSinceUpdate < $TIMEOUT) {
  77. $state = 'ON';
  78. }
  79. }
  80. # If the state changed, do something about if
  81. if ($state ne $stateLast) {
  82. if ($DEBUG) {
  83. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  84. }
  85. if ($state eq 'OFF') {
  86. # Turn off the projector
  87. my ($fh, $tmp) = tempfile($DATA_DIR . '/PROJECTOR_CMD.XXXXXXXX', 'UNLINK' => 0);
  88. print $fh "OFF\n";
  89. close($fh);
  90. rename($tmp, $DATA_DIR . '/PROJECTOR_CMD');
  91. } elsif ($state eq 'ON') {
  92. # Do nothing, at least for the moment
  93. }
  94. }
  95. # Always announce a pending shutdown
  96. if ($state eq 'COUNTDOWN') {
  97. my $timeLeft = ($TIMEOUT + $COUNTDOWN) - $timeSinceUpdate;
  98. $timeLeft = ceil($timeLeft / 60);
  99. my $plural = 's';
  100. if ($timeLeft == 1) {
  101. $plural = '';
  102. }
  103. system('say', 'Projector powerdown in about ' . $timeLeft . ' minute' . $plural);
  104. }
  105. # Wait and loop
  106. sleep($DELAY);
  107. }
  108. sub mtime($) {
  109. my ($file) = @_;
  110. my $mtime = 0;
  111. if (-r $file) {
  112. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), $mtime, undef(), undef(), undef()) = stat($file);
  113. }
  114. return $mtime;
  115. }