fan.pl 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IO::Select;
  5. # Local modules
  6. use Cwd qw(abs_path);
  7. use File::Basename qw(dirname);
  8. use lib dirname(abs_path($0));
  9. use DMX;
  10. # User config
  11. my %DIM = (
  12. 'OFF' => [
  13. { 'channel' => 11, 'value' => 0, 'time' => 0 }
  14. ],
  15. 'ON' => [
  16. { 'channel' => 11, 'value' => 255, 'time' => 0 }
  17. ],
  18. );
  19. # App config
  20. my $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
  21. chomp($TEMP_DIR);
  22. my $DATA_DIR = $TEMP_DIR . 'plexMonitor/';
  23. my $OUTPUT_FILE = $DATA_DIR . 'FAN';
  24. my $STATE_SOCK = $OUTPUT_FILE . '.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. # Force updates on any state change
  86. if ($stateLast ne $state) {
  87. if ($DEBUG) {
  88. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  89. }
  90. $forceUpdate = 1;
  91. }
  92. # Update the fan
  93. if ($forceUpdate) {
  94. # Update
  95. DMX::applyDataset($DIM{$state}, $state, $OUTPUT_FILE);
  96. # Update the push time
  97. $pushLast = time();
  98. }
  99. }