rope.pl 6.7 KB

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