projector.pl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Device::SerialPort;
  5. use File::Temp qw( tempfile );
  6. # Prototypes
  7. sub sendQuery($$);
  8. sub collectUntil($$);
  9. # Serial parameters
  10. my $PORT = '/dev/tty.Projector-DevB';
  11. # Protocol parameters
  12. my $CRLF = "\r\n";
  13. my $DELIMITER = ':';
  14. # App config
  15. my $BYTE_TIMEOUT = 500;
  16. my $SILENCE_TIMEOUT = $BYTE_TIMEOUT * 10;
  17. my $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
  18. chomp($TEMP_DIR);
  19. my $DATA_DIR = $TEMP_DIR . '/plexMonitor';
  20. my $CMD_FILE = $DATA_DIR . '/PROJECTOR_CMD';
  21. # Debug
  22. my $DEBUG = 0;
  23. if ($ENV{'DEBUG'}) {
  24. $DEBUG = 1;
  25. }
  26. # Command-line arguments
  27. my ($DELAY) = @ARGV;
  28. if (!$DELAY) {
  29. $DELAY = 15;
  30. }
  31. # Sanity check
  32. if (!-r $PORT || !-d $DATA_DIR) {
  33. die("Bad config\n");
  34. }
  35. # Port init
  36. my $port = new Device::SerialPort($PORT)
  37. or die("Unable to open projector serial connection\n");
  38. $port->read_const_time($BYTE_TIMEOUT);
  39. $port->lookclear();
  40. # Projector init (clear any previous state)
  41. sendQuery($port, '');
  42. # Track the projector power state
  43. my $power = -1;
  44. my $powerLast = $power;
  45. while (1) {
  46. # Check for queued commands
  47. my $cmd = undef();
  48. if (-r $CMD_FILE) {
  49. if ($DEBUG) {
  50. print STDERR "Found projector command file\n";
  51. }
  52. # Open and immediately unlink the file (to de-queue the command)
  53. my $fh;
  54. open($fh, $CMD_FILE);
  55. unlink($CMD_FILE);
  56. # If we got the file open before it disappeared
  57. if ($fh) {
  58. my $text = <$fh>;
  59. chomp($text);
  60. close($fh);
  61. if ($DEBUG) {
  62. print STDERR 'Got raw command: ' . $text . "\n";
  63. }
  64. # Check for known commands
  65. if ($text =~ /OFF/i) {
  66. $cmd = 'OFF';
  67. } elsif ($text =~ /ON/i) {
  68. $cmd = 'ON';
  69. }
  70. if ($DEBUG) {
  71. print STDERR 'Found command: ' . $cmd . "\n";
  72. }
  73. }
  74. # If we got a command, validate it
  75. if ($cmd) {
  76. if ($cmd eq 'ON' && $power) {
  77. $cmd = undef();
  78. } elsif ($cmd eq 'OFF' && !$power) {
  79. $cmd = undef();
  80. }
  81. }
  82. # Send the command
  83. if ($cmd) {
  84. if ($DEBUG) {
  85. print STDERR 'Sending command: ' . $cmd . "\n";
  86. }
  87. my $result = undef();
  88. if ($cmd eq 'OFF') {
  89. $result = sendQuery($port, 'PWR OFF');
  90. } elsif ($cmd eq 'ON') {
  91. $result = sendQuery($port, 'PWR ON');
  92. }
  93. }
  94. }
  95. # Check the power state
  96. $powerLast = $power;
  97. $power = 0;
  98. my $result = sendQuery($port, 'PWR?');
  99. if ($result =~ /PWR\=(\d+)/) {
  100. if ($1 > 0) {
  101. $power = 1;
  102. }
  103. }
  104. # If something has changed, save the state to disk
  105. if ($powerLast != $power) {
  106. if ($DEBUG) {
  107. print STDERR 'New projector power state: ' . $power . "\n";
  108. }
  109. my ($fh, $tmp) = tempfile($DATA_DIR . '/PROJECTOR.XXXXXXXX', 'UNLINK' => 0);
  110. print $fh $power . "\n";
  111. close($fh);
  112. rename($tmp, $DATA_DIR . '/PROJECTOR');
  113. }
  114. # Delay and loop
  115. sleep($DELAY);
  116. }
  117. # Cleanup
  118. $port->close();
  119. undef($port);
  120. exit(0);
  121. sub sendQuery($$) {
  122. my ($port, $query) = @_;
  123. $port->lookclear();
  124. my $bytes = $port->write($query . $CRLF);
  125. if ($DEBUG) {
  126. print STDERR "\tWrote (" . $bytes . '): ' . $query . "\n";
  127. }
  128. my $data = collectUntil($port, $DELIMITER);
  129. return $data;
  130. }
  131. sub collectUntil($$) {
  132. my ($port, $char) = @_;
  133. if (length($char) != 1) {
  134. die('Invalid collection delimiter: ' . $char . "\n");
  135. }
  136. # This byte-by-byte reading is not efficient, but it's safe
  137. # Allow reading forever as long as we don't exceed the silence timeout
  138. my $count = 0;
  139. my $string = '';
  140. while ($count < $SILENCE_TIMEOUT / $BYTE_TIMEOUT) {
  141. my $byte = $port->read(1);
  142. if ($byte) {
  143. $count = 0;
  144. $string .= $byte;
  145. if ($DEBUG) {
  146. print STDERR "\tRead: " . $byte . "\n";
  147. }
  148. if ($byte eq $char) {
  149. last;
  150. }
  151. } else {
  152. $count++;
  153. }
  154. }
  155. # Return undef if there was no data (as opposed to just a delimiter and/or whitespace)
  156. if (length($string) < 1) {
  157. if ($DEBUG) {
  158. print STDERR "Read: <NO DATA>\n";
  159. }
  160. return undef();
  161. }
  162. # Strip leading or trailing whitespace
  163. $string =~ s/^\s+//;
  164. $string =~ s/\s+$//;
  165. # Strip the trailing delimiter
  166. $string =~ s/${char}$//;
  167. # Return our clean string
  168. if ($DEBUG) {
  169. print STDERR 'Read: ' . $string . "\n";
  170. }
  171. return $string;
  172. }