rope.pl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. # Local modules
  5. use Cwd qw(abs_path);
  6. use File::Basename qw(dirname);
  7. use lib dirname(abs_path($0));
  8. use DMX;
  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 $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
  57. chomp($TEMP_DIR);
  58. my $DATA_DIR = $TEMP_DIR . 'plexMonitor/';
  59. my $OUTPUT_FILE = $DATA_DIR . 'ROPE';
  60. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  61. my $PUSH_TIMEOUT = 20;
  62. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  63. # Debug
  64. my $DEBUG = 0;
  65. if ($ENV{'DEBUG'}) {
  66. $DEBUG = 1;
  67. }
  68. # Command-line arguments
  69. my ($DELAY) = @ARGV;
  70. if (!$DELAY) {
  71. $DELAY = $PULL_TIMEOUT / 2;
  72. }
  73. # Sanity check
  74. if (!-d $DATA_DIR) {
  75. die("Bad config\n");
  76. }
  77. # Construct a list of valid states
  78. my %VALID = ();
  79. foreach my $key (keys(%DIM)) {
  80. $VALID{$key} = 1;
  81. }
  82. # Sockets
  83. DMX::stateSocket($STATE_SOCK);
  84. DMX::stateSubscribe($STATE_SOCK);
  85. # State
  86. my $state = 'OFF';
  87. my $stateLast = $state;
  88. my %exists = ();
  89. my $pushLast = 0;
  90. my $pullLast = time();
  91. my $update = 0;
  92. # Always force lights out at launch
  93. DMX::dim({ 'channel' => 0, 'value' => 0, 'time' => 0 });
  94. # Loop forever
  95. while (1) {
  96. # State is calculated; use newState to gather data
  97. my $newState = $state;
  98. # Wait for state updates
  99. my $cmdState = DMX::readState($DELAY, \%exists, \%VALID);
  100. if (defined($cmdState)) {
  101. $newState = $cmdState;
  102. $pullLast = time();
  103. }
  104. # Skip processing when in RAVE mode
  105. if ($exists{'RAVE'}) {
  106. if ($DEBUG) {
  107. print STDERR "Suspending normal operation while in RAVE mode\n";
  108. }
  109. $update = 1;
  110. next;
  111. }
  112. # Calculate the new state
  113. $stateLast = $state;
  114. if ($exists{'LIGHTS'}) {
  115. if ($newState eq 'PLAY') {
  116. $newState = 'PLAY_HIGH';
  117. }
  118. } else {
  119. if ($newState eq 'PLAY_HIGH') {
  120. $newState = 'PLAY';
  121. }
  122. }
  123. $state = $newState;
  124. # Force updates on a periodic basis
  125. if (time() - $pushLast > $PUSH_TIMEOUT) {
  126. $update = 1;
  127. }
  128. # Die if we don't see regular updates
  129. if (time() - $pullLast > $PULL_TIMEOUT) {
  130. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  131. }
  132. # Force updates on any state change
  133. if ($stateLast ne $state) {
  134. if ($DEBUG) {
  135. print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
  136. }
  137. $update = 1;
  138. }
  139. # Update the lighting
  140. if ($update) {
  141. # Update
  142. DMX::applyDataset($DIM{$state}, $state, $OUTPUT_FILE);
  143. # Update the push time
  144. $pushLast = time();
  145. # Clear the update flag
  146. $update = 0;
  147. }
  148. }