DMX.pm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 ($fh, $exists) = @_;
  79. # Grab the inbound text
  80. my $text = undef();
  81. $fh->recv($text, $MAX_CMD_LEN);
  82. # Parse the string
  83. my %tmp = ();
  84. %{$exists} = ();
  85. my ($cmdState, $exists_text) = $text =~ /^(\w+)(?:\s+\(([^\)]+)\))?/;
  86. if (!defined($cmdState)) {
  87. print STDERR 'State parse error: ' . $text . "\n";
  88. next;
  89. }
  90. if (defined($exists_text)) {
  91. foreach my $exists_val (split(/\s*,\s*/, $exists_text)) {
  92. my ($name, $value) = $exists_val =~ /(\w+)\:(0|1)/;
  93. if (!defined($name) || !defined($value)) {
  94. print STDERR 'State parse error (exists): ' . $text . "\n";
  95. next;
  96. }
  97. $exists->{$name} = $value;
  98. }
  99. }
  100. if ($DEBUG) {
  101. my @exists_tmp = ();
  102. foreach my $key (keys(%{ $exists })) {
  103. push(@exists_tmp, $key . ':' . $exists->{$key});
  104. }
  105. print STDERR 'Got state: ' . $cmdState . ' (' . join(', ', @exists_tmp) . ")\n";
  106. }
  107. # Translate INIT to OFF
  108. if ($cmdState eq 'INIT') {
  109. $cmdState = 'OFF';
  110. }
  111. # Return the state
  112. return $cmdState;
  113. }
  114. sub dim($) {
  115. my ($args) = @_;
  116. if (!defined($args->{'delay'})) {
  117. $args->{'delay'} = 0;
  118. }
  119. if (!defined($args->{'channel'}) || !defined($args->{'time'}) || !defined($args->{'value'})) {
  120. die('Invalid command for socket: ' . join(', ', keys(%{$args})) . ': ' . join(', ', values(%{$args})) . "\n");
  121. }
  122. # Silently skip invalid channels
  123. if ($args->{'channel'} < 0) {
  124. return;
  125. }
  126. # Adjust for the color curve (for channels where we have such data)
  127. my $value = $args->{'value'};
  128. if ($CHANNEL_ADJ{$args->{'channel'}}) {
  129. $value *= $CHANNEL_ADJ{$args->{'channel'}};
  130. }
  131. # 8-bit values
  132. $value = int($value);
  133. if ($value > 255) {
  134. $value = 255;
  135. } elsif ($value < 0) {
  136. $value = 0;
  137. }
  138. # We only deal in ints
  139. my $time = int($args->{'time'});
  140. my $delay = int($args->{'delay'});
  141. my $cmd = join(':', $args->{'channel'}, $time, $value, $delay);
  142. dmxSock();
  143. $DMX_FH->send($cmd)
  144. or die('Unable to write command to DMX socket: ' . $cmd . ": ${!}\n");
  145. }
  146. sub printDataset($) {
  147. my ($data_set) = @_;
  148. my $sum = 0;
  149. foreach my $data (@{ $data_set }) {
  150. $sum += $data->{'value'};
  151. my $str = $data->{'channel'} . ' => ' . int($data->{'value'}) . ' @ ' . $data->{'time'};
  152. if ($data->{'delay'}) {
  153. $str .= ' (Delay: ' . $data->{'delay'} . ')';
  154. }
  155. print STDERR "\t" . $str . "\n";
  156. }
  157. print STDERR "\tTotal: " . int($sum) . "\n";
  158. }
  159. sub applyDataset($$$) {
  160. my ($data_set, $state, $file) = @_;
  161. if (!defined($state)) {
  162. $state = 'NULL';
  163. }
  164. # Debug
  165. if ($DEBUG) {
  166. print STDERR 'State: ' . $state . "\n";
  167. printDataset($data_set);
  168. }
  169. # Send the dim command
  170. my @values = ();
  171. foreach my $data (@{ $data_set }) {
  172. dim($data);
  173. my $str = $data->{'channel'} . ' => ' . $data->{'value'} . ' @ ' . $data->{'time'};
  174. if ($data->{'delay'}) {
  175. $str .= ' (Delay: ' . $data->{'delay'} . ')';
  176. }
  177. push(@values, $str);
  178. }
  179. # Save the state and value to disk
  180. if (length($file)) {
  181. my ($fh, $tmp) = File::Temp::tempfile($file . '.XXXXXXXX', 'UNLINK' => 0);
  182. print $fh 'State: ' . $state . "\n" . join("\n", @values) . "\n";
  183. close($fh);
  184. rename($tmp, $file);
  185. }
  186. }
  187. sub readState($$$) {
  188. my ($delay, $exists, $valid) = @_;
  189. my $cmdState = undef();
  190. # Wait for state updates
  191. my @ready_clients = $SELECT->can_read($delay);
  192. foreach my $fh (@ready_clients) {
  193. # Read the global state
  194. $cmdState = parseState($fh, $exists);
  195. # Only accept valid states
  196. if (defined($valid)) {
  197. if (!defined($valid->{$cmdState})) {
  198. print STDERR 'Invalid state: ' . $cmdState . "\n";
  199. next;
  200. }
  201. }
  202. }
  203. return $cmdState;
  204. }
  205. # Always return true
  206. 1;