rope.pl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IO::Socket;
  5. use Time::HiRes qw( usleep );
  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 => <Unused>
  16. # 5 => Bar-side hidden
  17. # 6 => Bar-side bar-shelves
  18. # 7 => Bar-side exposed
  19. # 8 => Bar-side ceiling
  20. # 9 => <Unused>
  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' => 1500 },
  33. { 'channel' => 5, 'value' => 64, 'time' => 500 },
  34. { 'channel' => 6, 'value' => 36, 'time' => 500 },
  35. { 'channel' => 7, 'value' => 32, 'time' => 500 },
  36. { 'channel' => 8, 'value' => 16, 'time' => 500 },
  37. { 'channel' => 12, 'value' => 255, 'time' => 0 },
  38. ],
  39. 'PLAY_HIGH' => [
  40. { 'channel' => 1, 'value' => 255, 'time' => 500 },
  41. { 'channel' => 2, 'value' => 128, 'time' => 500 },
  42. { 'channel' => 3, 'value' => 192, 'time' => 1500 },
  43. { 'channel' => 5, 'value' => 128, 'time' => 500 },
  44. { 'channel' => 6, 'value' => 36, 'time' => 500 },
  45. { 'channel' => 7, 'value' => 128, 'time' => 500 },
  46. { 'channel' => 8, 'value' => 16, 'time' => 500 },
  47. { 'channel' => 12, 'value' => 255, 'time' => 0 },
  48. ],
  49. 'PAUSE' => [
  50. { 'channel' => 1, 'value' => 255, 'time' => 1000 },
  51. { 'channel' => 2, 'value' => 192, 'time' => 10000 },
  52. { 'channel' => 3, 'value' => 192, 'time' => 5000 },
  53. { 'channel' => 5, 'value' => 255, 'time' => 1000 },
  54. { 'channel' => 6, 'value' => 104, 'time' => 12000 },
  55. { 'channel' => 7, 'value' => 192, 'time' => 10000 },
  56. { 'channel' => 8, 'value' => 92, 'time' => 16000 },
  57. { 'channel' => 12, 'value' => 255, 'time' => 0 },
  58. ],
  59. 'MOTION' => [
  60. { 'channel' => 1, 'value' => 255, 'time' => 1000 },
  61. { 'channel' => 2, 'value' => 192, 'time' => 1000 },
  62. { 'channel' => 3, 'value' => 192, 'time' => 1000 },
  63. { 'channel' => 5, 'value' => 192, 'time' => 1000 },
  64. { 'channel' => 6, 'value' => 104, 'time' => 1000 },
  65. { 'channel' => 7, 'value' => 192, 'time' => 1000 },
  66. { 'channel' => 8, 'value' => 92, 'time' => 1000 },
  67. { 'channel' => 12, 'value' => 255, 'time' => 0 },
  68. ],
  69. );
  70. my $TIMEOUT = 180;
  71. # App config
  72. my $SOCK_TIMEOUT = 5;
  73. my $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
  74. chomp($TEMP_DIR);
  75. my $DATA_DIR = $TEMP_DIR . 'plexMonitor/';
  76. my $CMD_FILE = $DATA_DIR . 'DMX.socket';
  77. # Debug
  78. my $DEBUG = 0;
  79. if ($ENV{'DEBUG'}) {
  80. $DEBUG = 1;
  81. }
  82. # Command-line arguments
  83. my ($DELAY) = @ARGV;
  84. if (!$DELAY) {
  85. $DELAY = 0.5;
  86. }
  87. $DELAY *= 1000000; # Microseconds;
  88. # Sanity check
  89. if (!-d $DATA_DIR || !-S $CMD_FILE) {
  90. die("Bad config\n");
  91. }
  92. # Socket init
  93. my $sock = IO::Socket::UNIX->new(
  94. 'Peer' => $CMD_FILE,
  95. 'Type' => SOCK_DGRAM,
  96. 'Timeout' => $SOCK_TIMEOUT
  97. ) or die('Unable to open socket: ' . $CMD_FILE . ": ${@}\n");
  98. # State
  99. my $state = 'INIT';
  100. my $stateLast = $state;
  101. my $playing = 0;
  102. my $projector = 0;
  103. my $lights = 0;
  104. my $updateLast = 0;
  105. # Always force lights out at launch
  106. dim(0, 0, 0);
  107. # Loop forever
  108. while (1) {
  109. # Monitor the PLAY_STATUS file for changes and state
  110. {
  111. my $mtime = mtime($DATA_DIR . 'PLAY_STATUS');
  112. if ($mtime > $updateLast) {
  113. $updateLast = $mtime;
  114. }
  115. # Grab the PLAY_STATUS value
  116. $playing = 0;
  117. my $fh;
  118. open($fh, $DATA_DIR . 'PLAY_STATUS')
  119. or die("Unable to open PLAY_STATUS\n");
  120. my $text = <$fh>;
  121. close($fh);
  122. if ($text =~ /1/) {
  123. $playing = 1;
  124. }
  125. if ($DEBUG) {
  126. print STDERR 'Playing: ' . $playing . "\n";
  127. }
  128. }
  129. # Monitor the PROJECTOR file for changes and state
  130. {
  131. my $mtime = mtime($DATA_DIR . 'PROJECTOR');
  132. if ($mtime > $updateLast) {
  133. $updateLast = $mtime;
  134. }
  135. # Grab the PROJECTOR value
  136. $projector = 0;
  137. my $fh;
  138. open($fh, $DATA_DIR . 'PROJECTOR')
  139. or die("Unable to open PROJECTOR\n");
  140. my $text = <$fh>;
  141. close($fh);
  142. if ($text =~ /1/) {
  143. $projector = 1;
  144. }
  145. if ($DEBUG) {
  146. print STDERR 'Projector: ' . $projector . "\n";
  147. }
  148. }
  149. # Monitor the LIGHTS file for presence
  150. {
  151. $lights = 0;
  152. if (-e $DATA_DIR . 'LIGHTS') {
  153. $lights = 1;
  154. }
  155. if ($DEBUG) {
  156. print STDERR 'Lights: ' . $lights . "\n";
  157. }
  158. # Clear the override when the projector is off
  159. if ($lights && !$projector) {
  160. unlink($DATA_DIR . 'LIGHTS');
  161. }
  162. }
  163. # Monitor the GUI, PLAYING, and MOTION files for changes only
  164. {
  165. my $mtime = mtime($DATA_DIR . 'PLAYING');
  166. if ($mtime > $updateLast) {
  167. $updateLast = $mtime;
  168. }
  169. $mtime = mtime($DATA_DIR . 'GUI');
  170. if ($mtime > $updateLast) {
  171. $updateLast = $mtime;
  172. }
  173. $mtime = mtime($DATA_DIR . 'MOTION');
  174. if ($mtime > $updateLast) {
  175. $updateLast = $mtime;
  176. }
  177. }
  178. # Calculate the new state
  179. $stateLast = $state;
  180. if ($projector) {
  181. # We are always either playing or paused if the projector is on
  182. if ($playing) {
  183. # Allow an override to higher brightness during playback
  184. if ($lights) {
  185. $state = 'PLAY_HIGH';
  186. } else {
  187. $state = 'PLAY';
  188. }
  189. } else {
  190. $state = 'PAUSE';
  191. }
  192. } else {
  193. # If the projector is off, check the timeouts
  194. my $timeSinceUpdate = time() - $updateLast;
  195. if ($timeSinceUpdate > $TIMEOUT) {
  196. $state = 'OFF';
  197. } elsif ($timeSinceUpdate < $TIMEOUT) {
  198. $state = 'MOTION';
  199. }
  200. }
  201. if ($state eq 'INIT') {
  202. $state = 'OFF';
  203. }
  204. # Update the lighting state
  205. if ($stateLast ne $state) {
  206. if ($DEBUG) {
  207. print STDERR 'State: ' . $stateLast . ' => ' . $state . "\n";
  208. foreach my $data (@{ $DIM{$state} }) {
  209. print STDERR "\t" . $data->{'channel'} . ' => ' . $data->{'value'} . ' @ ' . $data->{'time'} . "\n";
  210. }
  211. }
  212. # Send the dim command
  213. my @values = ();
  214. foreach my $data (@{ $DIM{$state} }) {
  215. dim($data->{'channel'}, $data->{'time'}, $data->{'value'});
  216. push(@values, $data->{'channel'} . ' => ' . $data->{'value'} . ' @ ' . $data->{'time'});
  217. }
  218. # Save the state and value to disk
  219. my ($fh, $tmp) = tempfile($DATA_DIR . 'ROPE.XXXXXXXX', 'UNLINK' => 0);
  220. print $fh 'State: ' . $state . "\n" . join("\n", @values) . "\n";
  221. close($fh);
  222. rename($tmp, $DATA_DIR . 'ROPE');
  223. }
  224. # Wait and loop
  225. usleep($DELAY);
  226. }
  227. sub mtime($) {
  228. my ($file) = @_;
  229. my $mtime = 0;
  230. if (-r $file) {
  231. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), $mtime, undef(), undef(), undef()) = stat($file);
  232. }
  233. return $mtime;
  234. }
  235. # Send the command
  236. sub dim($$$) {
  237. my ($channel, $duration, $intensity) = @_;
  238. my $cmd = join(':', $channel, $duration, $intensity);
  239. $sock->send($cmd)
  240. or die('Unable to write command to socket: ' . $CMD_FILE . ': ' . $cmd . ": ${!}\n");
  241. }