projector.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. # Debug
  21. my $DEBUG = 0;
  22. if ($ENV{'DEBUG'}) {
  23. $DEBUG = 1;
  24. }
  25. # Command-line arguments
  26. my ($DELAY) = @ARGV;
  27. if (!$DELAY) {
  28. $DELAY = 15;
  29. }
  30. # Sanity check
  31. if (!-r $PORT || !-d $DATA_DIR) {
  32. die("Bad config\n");
  33. }
  34. # Port init
  35. my $port = new Device::SerialPort($PORT)
  36. or die("Unable to open projector serial connection\n");
  37. $port->read_const_time($BYTE_TIMEOUT);
  38. $port->lookclear();
  39. # Projector init (clear any previous state)
  40. sendQuery($port, '');
  41. # Track the projector power state
  42. my $power = -1;
  43. my $powerLast = $power;
  44. while (1) {
  45. # Check the power state
  46. $powerLast = $power;
  47. $power = 0;
  48. my $result = sendQuery($port, 'PWR?');
  49. if ($result =~ /PWR\=(\d+)/) {
  50. if ($1 > 0) {
  51. $power = 1;
  52. }
  53. }
  54. # If something has changed, save the state to disk
  55. if ($powerLast != $power) {
  56. if ($DEBUG) {
  57. print STDERR 'New projector power state: ' . $power . "\n";
  58. }
  59. my ($fh, $tmp) = tempfile($DATA_DIR . '/PROJECTOR.XXXXXXXX', 'UNLINK' => 0);
  60. print $fh $power . "\n";
  61. close($fh);
  62. rename($tmp, $DATA_DIR . '/PROJECTOR');
  63. }
  64. # Delay and loop
  65. sleep($DELAY);
  66. }
  67. # Cleanup
  68. $port->close();
  69. undef($port);
  70. exit(0);
  71. sub sendQuery($$) {
  72. my ($port, $query) = @_;
  73. $port->lookclear();
  74. my $bytes = $port->write($query . $CRLF);
  75. if ($DEBUG) {
  76. print STDERR "\tWrote (" . $bytes . '): ' . $query . "\n";
  77. }
  78. my $data = collectUntil($port, $DELIMITER);
  79. return $data;
  80. }
  81. sub collectUntil($$) {
  82. my ($port, $char) = @_;
  83. if (length($char) != 1) {
  84. die('Invalid collection delimiter: ' . $char . "\n");
  85. }
  86. # This byte-by-byte reading is not efficient, but it's safe
  87. # Allow reading forever as long as we don't exceed the silence timeout
  88. my $count = 0;
  89. my $string = '';
  90. while ($count < $SILENCE_TIMEOUT / $BYTE_TIMEOUT) {
  91. my $byte = $port->read(1);
  92. if ($byte) {
  93. $count = 0;
  94. $string .= $byte;
  95. if ($DEBUG) {
  96. print STDERR "\tRead: " . $byte . "\n";
  97. }
  98. if ($byte eq $char) {
  99. last;
  100. }
  101. } else {
  102. $count++;
  103. }
  104. }
  105. # Return undef if there was no data (as opposed to just a delimiter and/or whitespace)
  106. if (length($string) < 1) {
  107. if ($DEBUG) {
  108. print STDERR "Read: <NO DATA>\n";
  109. }
  110. return undef();
  111. }
  112. # Strip leading or trailing whitespace
  113. $string =~ s/^\s+//;
  114. $string =~ s/\s+$//;
  115. # Strip the trailing delimiter
  116. $string =~ s/${char}$//;
  117. # Return our clean string
  118. if ($DEBUG) {
  119. print STDERR 'Read: ' . $string . "\n";
  120. }
  121. return $string;
  122. }