fan.pl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IO::Socket;
  5. use Time::HiRes qw( usleep );
  6. use File::Temp qw( tempfile );
  7. # Prototypes
  8. sub dim($);
  9. # User config
  10. my %DIM = (
  11. 'OFF' => [
  12. { 'channel' => 11, 'value' => 0, 'time' => 0 }
  13. ],
  14. 'ON' => [
  15. { 'channel' => 11, 'value' => 255, 'time' => 0 }
  16. ],
  17. );
  18. # App config
  19. my $SOCK_TIMEOUT = 5;
  20. my $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
  21. chomp($TEMP_DIR);
  22. my $DATA_DIR = $TEMP_DIR . 'plexMonitor/';
  23. my $CMD_FILE = $DATA_DIR . 'DMX.socket';
  24. my $PUSH_TIMEOUT = 20;
  25. # Debug
  26. my $DEBUG = 0;
  27. if ($ENV{'DEBUG'}) {
  28. $DEBUG = 1;
  29. }
  30. # Command-line arguments
  31. my ($DELAY) = @ARGV;
  32. if (!$DELAY) {
  33. $DELAY = 0.5;
  34. }
  35. $DELAY *= 1000000; # Microseconds;
  36. # Sanity check
  37. if (!-d $DATA_DIR || !-S $CMD_FILE) {
  38. die("Bad config\n");
  39. }
  40. # Socket init
  41. my $sock = IO::Socket::UNIX->new(
  42. 'Peer' => $CMD_FILE,
  43. 'Type' => SOCK_DGRAM,
  44. 'Timeout' => $SOCK_TIMEOUT
  45. ) or die('Unable to open socket: ' . $CMD_FILE . ": ${@}\n");
  46. # State
  47. my $state = 'INIT';
  48. my $stateLast = $state;
  49. my $fan = 0;
  50. my $projector = 0;
  51. my $pushLast = 0;
  52. # Always force lights out at launch
  53. dim({ 'channel' => 11, 'value' => 0, 'time' => 0 });
  54. # Loop forever
  55. while (1) {
  56. # Set anywhere to force an update this cycle
  57. my $forceUpdate = 0;
  58. # Monitor the PROJECTOR file for state
  59. {
  60. $projector = 0;
  61. my $fh;
  62. open($fh, $DATA_DIR . 'PROJECTOR')
  63. or die("Unable to open PROJECTOR\n");
  64. my $text = <$fh>;
  65. close($fh);
  66. if ($text =~ /1/) {
  67. $projector = 1;
  68. }
  69. if ($DEBUG) {
  70. print STDERR 'Projector: ' . $projector . "\n";
  71. }
  72. }
  73. # Monitor the FAN_CMD file for presence
  74. {
  75. # Clear the flag when the projector is off
  76. if ($fan && !$projector) {
  77. unlink($DATA_DIR . 'FAN_CMD');
  78. }
  79. $fan = 0;
  80. if (-e $DATA_DIR . 'FAN_CMD') {
  81. $fan = 1;
  82. }
  83. if ($DEBUG) {
  84. print STDERR 'Fan: ' . $fan . "\n";
  85. }
  86. }
  87. # Calculate the new state
  88. $stateLast = $state;
  89. if ($fan) {
  90. if ($projector) {
  91. $state = 'ON';
  92. } else {
  93. $state = 'OFF';
  94. }
  95. } else {
  96. $state = 'OFF';
  97. }
  98. if ($state eq 'INIT') {
  99. $state = 'OFF';
  100. }
  101. # Force updates on a periodic basis
  102. if (time() - $pushLast > $PUSH_TIMEOUT) {
  103. $forceUpdate = 1;
  104. }
  105. # Update the fan state
  106. if ($forceUpdate || $stateLast ne $state) {
  107. if ($DEBUG) {
  108. print STDERR 'State: ' . $stateLast . ' => ' . $state . "\n";
  109. foreach my $data (@{ $DIM{$state} }) {
  110. print STDERR "\t" . $data->{'channel'} . ' => ' . $data->{'value'} . ' @ ' . $data->{'time'} . "\n";
  111. }
  112. }
  113. # Send the dim command
  114. my @values = ();
  115. foreach my $data (@{ $DIM{$state} }) {
  116. dim($data);
  117. push(@values, $data->{'channel'} . ' => ' . $data->{'value'} . ' @ ' . $data->{'time'});
  118. }
  119. # Save the state and value to disk
  120. my ($fh, $tmp) = tempfile($DATA_DIR . 'FAN.XXXXXXXX', 'UNLINK' => 0);
  121. print $fh 'State: ' . $state . "\n" . join("\n", @values) . "\n";
  122. close($fh);
  123. rename($tmp, $DATA_DIR . 'FAN');
  124. # Update the push time
  125. $pushLast = time();
  126. }
  127. # Wait and loop
  128. usleep($DELAY);
  129. }
  130. # Send the command
  131. sub dim($) {
  132. my ($args) = @_;
  133. if (!defined($args->{'delay'})) {
  134. $args->{'delay'} = 0;
  135. }
  136. if (!defined($args->{'channel'}) || !defined($args->{'time'}) || !defined($args->{'value'})) {
  137. die('Invalid command for socket: ' . join(', ', keys(%{$args})) . ': ' . join(', ', values(%{$args})) . "\n");
  138. }
  139. my $cmd = join(':', $args->{'channel'}, $args->{'time'}, $args->{'value'}, $args->{'delay'});
  140. $sock->send($cmd)
  141. or die('Unable to write command to socket: ' . $CMD_FILE . ': ' . $cmd . ": ${!}\n");
  142. }