rope.pl 6.9 KB

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