Просмотр исходного кода

Add support for out-of-band commands via a queue file. There is no need for a FIFO, as the projector is very slow to react -- something on the order of seconds -- and in most cases it does not make sense to internally queue commands.

Zachary Kotlarek 14 лет назад
Родитель
Сommit
eaff7e4eda
1 измененных файлов с 56 добавлено и 0 удалено
  1. 56 0
      projector/projector.pl

+ 56 - 0
projector/projector.pl

@@ -21,6 +21,7 @@ my $SILENCE_TIMEOUT = $BYTE_TIMEOUT * 10;
 my $TEMP_DIR        = `getconf DARWIN_USER_TEMP_DIR`;
 my $TEMP_DIR        = `getconf DARWIN_USER_TEMP_DIR`;
 chomp($TEMP_DIR);
 chomp($TEMP_DIR);
 my $DATA_DIR = $TEMP_DIR . '/plexMonitor';
 my $DATA_DIR = $TEMP_DIR . '/plexMonitor';
+my $CMD_FILE = $DATA_DIR . '/PROJECTOR_CMD';
 
 
 # Debug
 # Debug
 my $DEBUG = 0;
 my $DEBUG = 0;
@@ -53,6 +54,61 @@ my $power     = -1;
 my $powerLast = $power;
 my $powerLast = $power;
 while (1) {
 while (1) {
 
 
+	# Check for queued commands
+	my $cmd = undef();
+	if (-r $CMD_FILE) {
+		if ($DEBUG) {
+			print STDERR "Found projector command file\n";
+		}
+
+		# Open and immediately unlink the file (to de-queue the command)
+		my $fh;
+		open($fh, $CMD_FILE);
+		unlink($CMD_FILE);
+
+		# If we got the file open before it disappeared
+		if ($fh) {
+			my $text = <$fh>;
+			chomp($text);
+			close($fh);
+			if ($DEBUG) {
+				print STDERR 'Got raw command: ' . $text . "\n";
+			}
+
+			# Check for known commands
+			if ($text =~ /OFF/i) {
+				$cmd = 'OFF';
+			} elsif ($text =~ /ON/i) {
+				$cmd = 'ON';
+			}
+			if ($DEBUG) {
+				print STDERR 'Found command: ' . $cmd . "\n";
+			}
+		}
+
+		# If we got a command, validate it
+		if ($cmd) {
+			if ($cmd eq 'ON' && $power) {
+				$cmd = undef();
+			} elsif ($cmd eq 'OFF' && !$power) {
+				$cmd = undef();
+			}
+		}
+
+		# Send the command
+		if ($cmd) {
+			if ($DEBUG) {
+				print STDERR 'Sending command: ' . $cmd . "\n";
+			}
+			my $result = undef();
+			if ($cmd eq 'OFF') {
+				$result = sendQuery($port, 'PWR OFF');
+			} elsif ($cmd eq 'ON') {
+				$result = sendQuery($port, 'PWR ON');
+			}
+		}
+	}
+
 	# Check the power state
 	# Check the power state
 	$powerLast = $power;
 	$powerLast = $power;
 	$power     = 0;
 	$power     = 0;