rope.pl 7.3 KB

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