overhead.pl 5.3 KB

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