fan.pl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IO::Select;
  5. use IO::Socket::UNIX;
  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 $DMX_SOCK = $DATA_DIR . 'DMX.socket';
  24. my $SUB_SOCK = $DATA_DIR . 'STATE.socket';
  25. my $STATE_SOCK = $DATA_DIR . 'ROPE.socket';
  26. my $MAX_CMD_LEN = 1024;
  27. my $PUSH_TIMEOUT = 20;
  28. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  29. # Debug
  30. my $DEBUG = 0;
  31. if ($ENV{'DEBUG'}) {
  32. $DEBUG = 1;
  33. }
  34. # Command-line arguments
  35. my ($DELAY) = @ARGV;
  36. if (!$DELAY) {
  37. $DELAY = $PULL_TIMEOUT / 2;
  38. }
  39. # Sanity check
  40. if (!-d $DATA_DIR || !-S $DMX_SOCK || !-S $SUB_SOCK) {
  41. die("Bad config\n");
  42. }
  43. # State socket init
  44. if (-e $STATE_SOCK) {
  45. unlink($STATE_SOCK);
  46. }
  47. my $state_fh = IO::Socket::UNIX->new(
  48. 'Local' => $STATE_SOCK,
  49. 'Type' => SOCK_DGRAM
  50. ) or die('Unable to open socket: ' . $STATE_SOCK . ": ${@}\n");
  51. my $select = IO::Select->new($state_fh)
  52. or die('Unable to select socket: ' . $STATE_SOCK . ": ${!}\n");
  53. # DMX socket init
  54. my $dmx_fh = IO::Socket::UNIX->new(
  55. 'Peer' => $DMX_SOCK,
  56. 'Type' => SOCK_DGRAM,
  57. 'Timeout' => $SOCK_TIMEOUT
  58. ) or die('Unable to open socket: ' . $DMX_SOCK . ": ${@}\n");
  59. # Subscribe to state updates
  60. my $sub_fh = IO::Socket::UNIX->new(
  61. 'Peer' => $SUB_SOCK,
  62. 'Type' => SOCK_DGRAM,
  63. 'Timeout' => $SOCK_TIMEOUT
  64. ) or die('Unable to open socket: ' . $SUB_SOCK . ": ${@}\n");
  65. $sub_fh->send($STATE_SOCK)
  66. or die('Unable to subscribe: ' . $! . "\n");
  67. shutdown($sub_fh, 2);
  68. undef($sub_fh);
  69. # State
  70. my $state = 'INIT';
  71. my $stateLast = $state;
  72. my %exists = ();
  73. my $pushLast = 0;
  74. my $pullLast = time();
  75. # Always force lights out at launch
  76. dim({ 'channel' => 11, 'value' => 0, 'time' => 0 });
  77. # Loop forever
  78. while (1) {
  79. # Set anywhere to force an update this cycle
  80. my $forceUpdate = 0;
  81. # State is calculated; use newState to gather data
  82. my $newState = $state;
  83. # Wait for state updates
  84. my @ready_clients = $select->can_read($DELAY);
  85. foreach my $fh (@ready_clients) {
  86. # Grab the inbound text
  87. my $text = undef();
  88. $fh->recv($text, $MAX_CMD_LEN);
  89. # Parse the string
  90. %exists = ();
  91. my ($cmdState, $exists_text) = $text =~ /^(\w+)\s+\(([^\)]+)\)/;
  92. if (!defined($cmdState) || !defined($exists_text)) {
  93. print STDERR 'State parse error: ' . $text . "\n";
  94. next;
  95. }
  96. foreach my $exists (split(/\s*,\s*/, $exists_text)) {
  97. my ($name, $value) = $exists =~ /(\w+)\:(0|1)/;
  98. if (!defined($name) || !defined($value)) {
  99. print STDERR 'State parse error (exists): ' . $text . "\n";
  100. next;
  101. }
  102. $exists{$name} = $value;
  103. }
  104. if ($DEBUG) {
  105. my @exists_tmp = ();
  106. foreach my $key (keys(%exists)) {
  107. push(@exists_tmp, $key . ':' . $exists{$key});
  108. }
  109. print STDERR 'Got state: ' . $cmdState . ' (' . join(', ', @exists_tmp) . ")\n";
  110. }
  111. # We don't really care about the master state for the fan
  112. # But we have to parse the exists records, so we might as well grab it
  113. # Propogate the most recent command state
  114. $newState = $cmdState;
  115. $pullLast = time();
  116. }
  117. # Calculate the new state
  118. $stateLast = $state;
  119. if ($exists{'FAN_CMD'}) {
  120. $state = 'ON';
  121. } else {
  122. $state = 'OFF';
  123. }
  124. if ($state eq 'INIT') {
  125. $state = 'OFF';
  126. }
  127. # Force updates on a periodic basis
  128. if (time() - $pushLast > $PUSH_TIMEOUT) {
  129. $forceUpdate = 1;
  130. }
  131. # Die if we don't see regular updates
  132. if (time() - $pullLast > $PULL_TIMEOUT) {
  133. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  134. }
  135. # Update the fan state
  136. if ($forceUpdate || $stateLast ne $state) {
  137. if ($DEBUG) {
  138. print STDERR 'State: ' . $stateLast . ' => ' . $state . "\n";
  139. foreach my $data (@{ $DIM{$state} }) {
  140. print STDERR "\t" . $data->{'channel'} . ' => ' . $data->{'value'} . ' @ ' . $data->{'time'} . "\n";
  141. }
  142. }
  143. # Send the dim command
  144. my @values = ();
  145. foreach my $data (@{ $DIM{$state} }) {
  146. dim($data);
  147. push(@values, $data->{'channel'} . ' => ' . $data->{'value'} . ' @ ' . $data->{'time'});
  148. }
  149. # Save the state and value to disk
  150. my ($fh, $tmp) = tempfile($DATA_DIR . 'FAN.XXXXXXXX', 'UNLINK' => 0);
  151. print $fh 'State: ' . $state . "\n" . join("\n", @values) . "\n";
  152. close($fh);
  153. rename($tmp, $DATA_DIR . 'FAN');
  154. # Update the push time
  155. $pushLast = time();
  156. }
  157. }
  158. # Send the command
  159. sub dim($) {
  160. my ($args) = @_;
  161. if (!defined($args->{'delay'})) {
  162. $args->{'delay'} = 0;
  163. }
  164. if (!defined($args->{'channel'}) || !defined($args->{'time'}) || !defined($args->{'value'})) {
  165. die('Invalid command for socket: ' . join(', ', keys(%{$args})) . ': ' . join(', ', values(%{$args})) . "\n");
  166. }
  167. my $cmd = join(':', $args->{'channel'}, $args->{'time'}, $args->{'value'}, $args->{'delay'});
  168. $dmx_fh->send($cmd)
  169. or die('Unable to write command to socket: ' . $DMX_SOCK . ': ' . $cmd . ": ${!}\n");
  170. }