rope.pl 7.1 KB

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