DMX.pm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use File::Temp;
  5. use IO::Select;
  6. use IO::Socket::UNIX;
  7. # Package name
  8. package DMX;
  9. # Debug
  10. my $DEBUG = 0;
  11. if ($ENV{'DEBUG'}) {
  12. $DEBUG = 1;
  13. }
  14. # Config
  15. my $SOCK_TIMEOUT = 5;
  16. my $MAX_CMD_LEN = 1024;
  17. my $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
  18. chomp($TEMP_DIR);
  19. my $DATA_DIR = $TEMP_DIR . 'plexMonitor/';
  20. my $DMX_SOCK = $DATA_DIR . 'DMX.socket';
  21. my $SUB_SOCK = $DATA_DIR . 'STATE.socket';
  22. my %CHANNEL_ADJ = (
  23. '13' => 1.00,
  24. '14' => 1.14,
  25. '15' => 1.19,
  26. );
  27. # Sanity check
  28. if (!-d $DATA_DIR) {
  29. die('Data directory not available: ' . $DATA_DIR . "\n");
  30. }
  31. # State
  32. my $DMX_FH = undef();
  33. my $SELECT = undef();
  34. # Data directory
  35. sub dataDir() {
  36. return $DATA_DIR;
  37. }
  38. # DMX socket init
  39. sub dmxSock() {
  40. if (!defined($DMX_FH)) {
  41. $DMX_FH = IO::Socket::UNIX->new(
  42. 'Peer' => $DMX_SOCK,
  43. 'Type' => IO::Socket::UNIX::SOCK_DGRAM,
  44. 'Timeout' => $SOCK_TIMEOUT,
  45. ) or die('Unable to open DMX socket: ' . $DMX_SOCK . ": ${@}\n");
  46. }
  47. return $DMX_FH;
  48. }
  49. # Subscribe to state updates
  50. sub stateSubscribe($) {
  51. my ($STATE_SOCK) = @_;
  52. my $sub_fh = IO::Socket::UNIX->new(
  53. 'Peer' => $SUB_SOCK,
  54. 'Type' => IO::Socket::UNIX::SOCK_DGRAM,
  55. 'Timeout' => $SOCK_TIMEOUT
  56. ) or die('Unable to open state subscription socket: ' . $SUB_SOCK . ": ${@}\n");
  57. $sub_fh->send($STATE_SOCK)
  58. or die('Unable to subscribe: ' . $! . "\n");
  59. shutdown($sub_fh, 2);
  60. undef($sub_fh);
  61. }
  62. # State client socket init
  63. sub stateSocket($) {
  64. my ($STATE_SOCK) = @_;
  65. if (-e $STATE_SOCK) {
  66. unlink($STATE_SOCK);
  67. }
  68. my $state_fh = IO::Socket::UNIX->new(
  69. 'Local' => $STATE_SOCK,
  70. 'Type' => IO::Socket::UNIX::SOCK_DGRAM,
  71. ) or die('Unable to open state client socket: ' . $STATE_SOCK . ": ${@}\n");
  72. $SELECT = IO::Select->new($state_fh)
  73. or die('Unable to select state client socket: ' . $STATE_SOCK . ": ${!}\n");
  74. return $SELECT;
  75. }
  76. # Parse the state->client comm string
  77. sub parseState($$) {
  78. my ($text, $exists) = @_;
  79. my $cmdState = undef();
  80. my $exists_text = undef();
  81. # Parse the string
  82. my %tmp = ();
  83. %{$exists} = ();
  84. ($cmdState, $exists_text) = $text =~ /^(\w+)(?:\s+\(([^\)]+)\))?/;
  85. if (!defined($cmdState)) {
  86. print STDERR 'State parse error: ' . $text . "\n";
  87. next;
  88. }
  89. if (defined($exists_text)) {
  90. foreach my $exists_val (split(/\s*,\s*/, $exists_text)) {
  91. my ($name, $value) = $exists_val =~ /(\w+)\:(0|1)/;
  92. if (!defined($name) || !defined($value)) {
  93. print STDERR 'State parse error (exists): ' . $text . "\n";
  94. next;
  95. }
  96. $exists->{$name} = $value;
  97. }
  98. }
  99. if ($DEBUG) {
  100. my @exists_tmp = ();
  101. foreach my $key (keys(%{$exists})) {
  102. push(@exists_tmp, $key . ':' . $exists->{$key});
  103. }
  104. print STDERR 'Got state: ' . $cmdState . ' (' . join(', ', @exists_tmp) . ")\n";
  105. }
  106. # Translate INIT to OFF
  107. if ($cmdState eq 'INIT') {
  108. $cmdState = 'OFF';
  109. }
  110. # Return
  111. return $cmdState;
  112. }
  113. sub dim($) {
  114. my ($args) = @_;
  115. if (!defined($args->{'delay'})) {
  116. $args->{'delay'} = 0;
  117. }
  118. if (!defined($args->{'channel'}) || !defined($args->{'time'}) || !defined($args->{'value'})) {
  119. die('Invalid command for socket: ' . join(', ', keys(%{$args})) . ': ' . join(', ', values(%{$args})) . "\n");
  120. }
  121. # Silently skip invalid channels
  122. if ($args->{'channel'} < 0) {
  123. return;
  124. }
  125. # Adjust for the color curve (for channels where we have such data)
  126. my $value = $args->{'value'};
  127. if ($CHANNEL_ADJ{ $args->{'channel'} }) {
  128. $value *= $CHANNEL_ADJ{ $args->{'channel'} };
  129. }
  130. # 8-bit values
  131. $value = int($value);
  132. if ($value > 255) {
  133. $value = 255;
  134. } elsif ($value < 0) {
  135. $value = 0;
  136. }
  137. # We only deal in ints
  138. my $time = int($args->{'time'});
  139. my $delay = int($args->{'delay'});
  140. my $cmd = join(':', $args->{'channel'}, $time, $value, $delay);
  141. dmxSock();
  142. $DMX_FH->send($cmd)
  143. or die('Unable to write command to DMX socket: ' . $cmd . ": ${!}\n");
  144. }
  145. sub printDataset($) {
  146. my ($data_set) = @_;
  147. my $sum = 0;
  148. foreach my $data (@{$data_set}) {
  149. $sum += $data->{'value'};
  150. my $str = $data->{'channel'} . ' => ' . int($data->{'value'}) . ' @ ' . $data->{'time'};
  151. if ($data->{'delay'}) {
  152. $str .= ' (Delay: ' . $data->{'delay'} . ')';
  153. }
  154. print STDERR "\t" . $str . "\n";
  155. }
  156. print STDERR "\tTotal: " . int($sum) . "\n";
  157. }
  158. sub applyDataset($$$) {
  159. my ($data_set, $state, $file) = @_;
  160. if (!defined($state)) {
  161. $state = 'NULL';
  162. }
  163. # Debug
  164. if ($DEBUG) {
  165. print STDERR 'State: ' . $state . "\n";
  166. printDataset($data_set);
  167. }
  168. # Send the dim command
  169. my @values = ();
  170. foreach my $data (@{$data_set}) {
  171. dim($data);
  172. my $str = $data->{'channel'} . ' => ' . $data->{'value'} . ' @ ' . $data->{'time'};
  173. if ($data->{'delay'}) {
  174. $str .= ' (Delay: ' . $data->{'delay'} . ')';
  175. }
  176. push(@values, $str);
  177. }
  178. # Save the state and value to disk
  179. if (length($file)) {
  180. my ($fh, $tmp) = File::Temp::tempfile($file . '.XXXXXXXX', 'UNLINK' => 0);
  181. print $fh 'State: ' . $state . "\n" . join("\n", @values) . "\n";
  182. close($fh);
  183. rename($tmp, $file);
  184. }
  185. }
  186. sub readState($$$) {
  187. my ($delay, $exists, $valid) = @_;
  188. my $cmdState = undef();
  189. # Wait for state updates
  190. my @ready_clients = $SELECT->can_read($delay);
  191. foreach my $fh (@ready_clients) {
  192. # Ensure we won't block on recv()
  193. $fh->blocking(0);
  194. # Grab the inbound text
  195. while (defined($fh->recv(my $text, $MAX_CMD_LEN))) {
  196. # Parse the string
  197. my $state = parseState($text, $exists);
  198. # Ignore invalid states
  199. if (defined($valid)) {
  200. if (!defined($valid->{$state})) {
  201. print STDERR 'Invalid state: ' . $state . "\n";
  202. next;
  203. }
  204. }
  205. # Propogate valid states
  206. if ($state) {
  207. $cmdState = $state;
  208. }
  209. }
  210. }
  211. # Return the final valid state
  212. return $cmdState;
  213. }
  214. # Always return true
  215. 1;