serial.pl 4.7 KB

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