fan.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IO::Select;
  5. use File::Temp qw( tempfile );
  6. # Local modules
  7. use Cwd qw(abs_path);
  8. use File::Basename qw(dirname);
  9. use lib dirname(abs_path($0));
  10. use DMX;
  11. # User config
  12. my %DIM = (
  13. 'OFF' => [
  14. { 'channel' => 11, 'value' => 0, 'time' => 0 }
  15. ],
  16. 'ON' => [
  17. { 'channel' => 11, 'value' => 255, 'time' => 0 }
  18. ],
  19. );
  20. # App config
  21. my $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
  22. chomp($TEMP_DIR);
  23. my $DATA_DIR = $TEMP_DIR . 'plexMonitor/';
  24. my $STATE_SOCK = $DATA_DIR . 'ROPE.socket';
  25. my $PUSH_TIMEOUT = 20;
  26. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  27. # Debug
  28. my $DEBUG = 0;
  29. if ($ENV{'DEBUG'}) {
  30. $DEBUG = 1;
  31. }
  32. # Command-line arguments
  33. my ($DELAY) = @ARGV;
  34. if (!$DELAY) {
  35. $DELAY = $PULL_TIMEOUT / 2;
  36. }
  37. # Sanity check
  38. if (!-d $DATA_DIR) {
  39. die("Bad config\n");
  40. }
  41. # Sockets
  42. my $select = DMX::stateSocket($STATE_SOCK);
  43. DMX::stateSubscribe($STATE_SOCK);
  44. my $dmx_fh = DMX::dmxSock();
  45. # State
  46. my $state = 'OFF';
  47. my $stateLast = $state;
  48. my %exists = ();
  49. my $pushLast = 0;
  50. my $pullLast = time();
  51. # Always force lights out at launch
  52. DMX::dim({ 'channel' => 11, 'value' => 0, 'time' => 0 });
  53. # Loop forever
  54. while (1) {
  55. # Set anywhere to force an update this cycle
  56. my $forceUpdate = 0;
  57. # State is calculated; use newState to gather data
  58. my $newState = $state;
  59. # Wait for state updates
  60. my @ready_clients = $select->can_read($DELAY);
  61. foreach my $fh (@ready_clients) {
  62. # Read the global state
  63. my $cmdState = DMX::parseState($fh, \%exists);
  64. # We don't really care about the master state for the fan
  65. # But we have to parse the exists records, so we might as well grab it
  66. # Propogate the most recent command state
  67. $newState = $cmdState;
  68. $pullLast = time();
  69. }
  70. # Calculate the new state
  71. $stateLast = $state;
  72. if ($exists{'FAN_CMD'}) {
  73. $state = 'ON';
  74. } else {
  75. $state = 'OFF';
  76. }
  77. # Force updates on a periodic basis
  78. if (time() - $pushLast > $PUSH_TIMEOUT) {
  79. $forceUpdate = 1;
  80. }
  81. # Die if we don't see regular updates
  82. if (time() - $pullLast > $PULL_TIMEOUT) {
  83. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  84. }
  85. # Update the fan state
  86. if ($forceUpdate || $stateLast ne $state) {
  87. if ($DEBUG) {
  88. print STDERR 'State: ' . $stateLast . ' => ' . $state . "\n";
  89. DMX::printDataset($DIM{$state});
  90. }
  91. # Send the dim command
  92. my @values = ();
  93. foreach my $data (@{ $DIM{$state} }) {
  94. DMX::dim($data);
  95. push(@values, $data->{'channel'} . ' => ' . $data->{'value'} . ' @ ' . $data->{'time'});
  96. }
  97. # Save the state and value to disk
  98. my ($fh, $tmp) = tempfile($DATA_DIR . 'FAN.XXXXXXXX', 'UNLINK' => 0);
  99. print $fh 'State: ' . $state . "\n" . join("\n", @values) . "\n";
  100. close($fh);
  101. rename($tmp, $DATA_DIR . 'FAN');
  102. # Update the push time
  103. $pushLast = time();
  104. }
  105. }