DMX.pm 6.4 KB

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