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

Stop all this TYPE parsing and just set a bitmap for all the possible file attributes

Zachary Kotlarek 13 лет назад
Родитель
Сommit
806755eed9
1 измененных файлов с 122 добавлено и 77 удалено
  1. 122 77
      dmx/state.pl

+ 122 - 77
dmx/state.pl

@@ -25,11 +25,11 @@ my %MON_FILES     = ();
 {
 
 	# Plex
-	$MON_FILES{'GUI'}     = 'STATUS_GUI';
-	$MON_FILES{'PLAYING'} = 'STATUS_PLAYING';
+	$MON_FILES{'GUI'}     = 'GUI';
+	$MON_FILES{'PLAYING'} = 'PLAYING';
 
 	# RiffTrax
-	$MON_FILES{'RIFF'} = 'STATUS_VALUE';
+	$MON_FILES{'RIFF'} = 'VALUE';
 
 	# Motion detection
 	$MON_FILES{'MOTION'}    = 'MTIME';
@@ -37,35 +37,35 @@ my %MON_FILES     = ();
 
 	# Projector
 	$MON_FILES{'PROJECTOR'}       = 'STATUS';
-	$MON_FILES{'PROJECTOR_COLOR'} = 'STATUS_VALUE';
-	$MON_FILES{'PROJECTOR_INPUT'} = 'STATUS_VALUE';
-	$MON_FILES{'PROJECTOR_LAMP'}  = 'STATUS_VALUE';
+	$MON_FILES{'PROJECTOR_COLOR'} = 'VALUE';
+	$MON_FILES{'PROJECTOR_INPUT'} = 'VALUE';
+	$MON_FILES{'PROJECTOR_LAMP'}  = 'VALUE';
 
 	# Amplifier
 	$MON_FILES{'AMPLIFIER'}       = 'STATUS';
-	$MON_FILES{'AMPLIFIER_VOL'}   = 'STATUS_VALUE';
-	$MON_FILES{'AMPLIFIER_MODE'}  = 'STATUS_VALUE';
-	$MON_FILES{'AMPLIFIER_INPUT'} = 'STATUS_VALUE';
-	$MON_FILES{'AUDIO_AMP'}       = 'EXISTS_ON';
-	$MON_FILES{'STEREO_CMD'}      = 'EXISTS_ON';
+	$MON_FILES{'AMPLIFIER_VOL'}   = 'VALUE';
+	$MON_FILES{'AMPLIFIER_MODE'}  = 'VALUE';
+	$MON_FILES{'AMPLIFIER_INPUT'} = 'VALUE';
+	$MON_FILES{'AUDIO_AMP'}       = 'EXISTS-ON';
+	$MON_FILES{'STEREO_CMD'}      = 'EXISTS-ON';
 
 	# A/V Effects
-	$MON_FILES{'LIGHTS'} = 'EXISTS_ON';
+	$MON_FILES{'LIGHTS'} = 'EXISTS-ON';
 	$MON_FILES{'RAVE'}   = 'EXISTS';
 	$MON_FILES{'EFFECT'} = 'EXISTS';
 
 	# Equipment
-	$MON_FILES{'FAN_CMD'} = 'EXISTS_ON';
-	$MON_FILES{ $MEDIA_PATH . '/DMX/cmd/GARAGE_CMD' } = 'EXISTS_CLEAR';
+	$MON_FILES{'FAN_CMD'} = 'EXISTS-ON';
+	$MON_FILES{ $MEDIA_PATH . '/DMX/cmd/GARAGE_CMD' } = 'EXISTS-VALUE-CLEAR';
 
 	# OS State
-	$MON_FILES{'COLOR'}       = 'STATUS_VALUE';
-	$MON_FILES{'AUDIO'}       = 'STATUS_VALUE';
-	$MON_FILES{'AUDIO_STATE'} = 'STATUS_VALUE';
+	$MON_FILES{'COLOR'}       = 'VALUE';
+	$MON_FILES{'AUDIO'}       = 'VALUE';
+	$MON_FILES{'AUDIO_STATE'} = 'VALUE';
 
 	# TV
 	$MON_FILES{'TV'}     = 'STATUS';
-	$MON_FILES{'TV_VOL'} = 'STATUS_VALUE';
+	$MON_FILES{'TV_VOL'} = 'VALUE';
 }
 my %EXTRAS = (
 	'PLAYING' => {
@@ -118,13 +118,12 @@ foreach my $file (keys(%EXTRAS)) {
 my %files = ();
 foreach my $file (keys(%MON_FILES)) {
 	my %tmp = (
-		'name'      => basename($file),
-		'type'      => $MON_FILES{$file},
-		'path'      => $DATA_DIR . $file,
-		'update'    => 0,
-		'status'    => 0,
-		'last'      => 0,
-		'available' => 1,
+		'name'   => basename($file),
+		'type'   => $MON_FILES{$file},
+		'path'   => $DATA_DIR . $file,
+		'update' => 0,
+		'value'  => 0,
+		'last'   => 0,
 	);
 
 	# Allow absolute paths to override the $DATA_DIR path
@@ -135,6 +134,50 @@ foreach my $file (keys(%MON_FILES)) {
 	# Record the directory name for folder monitoring
 	$tmp{'dir'} = dirname($tmp{'path'});
 
+	# Set the attribute bits, for use in later update handling logic
+	my %attr = (
+		'available' => 0,
+		'status'    => 0,
+		'exists'    => 0,
+		'value'     => 0,
+		'clear'     => 0,
+		'clear_off' => 0,
+		'mtime'     => 0,
+		'gui'       => 0,
+		'playing'   => 0,
+	);
+	if ($tmp{'type'} =~ /\bSTATUS\b/i) {
+		$attr{'status'} = 1;
+		$attr{'mtime'}  = 1;
+	}
+	if ($tmp{'type'} =~ /\bEXISTS\b/i) {
+		$attr{'exists'} = 1;
+		$attr{'mtime'}  = 1;
+	}
+	if ($tmp{'type'} =~ /\bVALUE\b/i) {
+		$attr{'status'} = 1;
+		$attr{'value'}  = 1;
+		$attr{'mtime'}  = 1;
+	}
+	if ($tmp{'type'} =~ /\bCLEAR\b/i) {
+		$attr{'clear'} = 1;
+	}
+	if ($tmp{'type'} =~ /\bON\b/i) {
+		$attr{'clear_off'} = 1;
+	}
+	if ($tmp{'type'} =~ /\bMTIME\b/i) {
+		$attr{'mtime'} = 1;
+	}
+	if ($tmp{'type'} =~ /\bGUI\b/i) {
+		$attr{'status'} = 1;
+		$attr{'gui'}    = 1;
+	}
+	if ($tmp{'type'} =~ /\bPLAYING\b/i) {
+		$attr{'status'}  = 1;
+		$attr{'playing'} = 1;
+	}
+	$tmp{'attr'} = \%attr;
+
 	# Push a hash ref
 	$files{$file} = \%tmp;
 }
@@ -231,31 +274,34 @@ while (1) {
 		}
 
 		# Always record the previous status and clear the new one
-		$file->{'last'}   = $file->{'status'};
-		$file->{'status'} = 0;
+		$file->{'last'}  = $file->{'value'};
+		$file->{'value'} = 0;
 
-		# Track available/unavailable files
-		if (!($file->{'type'} =~ /^EXISTS/)) {
-			my $wasAvailable = $file->{'available'};
-			$file->{'available'} = -r $file->{'path'} ? 1 : 0;
-			if ($wasAvailable != $file->{'available'}) {
+		# Track available/unavailable status in each cycle
+		{
+			my $wasAvailable = $file->{'attr'}->{'available'};
+			$file->{'attr'}->{'available'} = -r $file->{'path'} ? 1 : 0;
+			if ($wasAvailable != $file->{'attr'}->{'available'}) {
 
 				# Reset and extras for this file
 				if (exists($EXTRAS{ $file->{'name'} })) {
 					foreach my $extra (keys(%{ $EXTRAS{ $file->{'name'} } })) {
 						my $name = $file->{'name'} . '_' . $extra;
-						$files{$name}{'last'}   = $files{$name}{'status'};
-						$files{$name}{'status'} = 0;
+						$files{$name}{'last'}  = $files{$name}{'value'};
+						$files{$name}{'value'} = 0;
 					}
 				}
 
 				# Determine which display device we have (if any)
 				if (exists($DISPLAY_DEVS{ $file->{'name'} })) {
 					$DISPLAY = $file->{'name'};
+					if ($DEBUG) {
+						print STDERR 'Selecting display device: ' . $DISPLAY . "\n";
+					}
 				}
 
 				if ($DEBUG) {
-					if ($file->{'available'}) {
+					if ($file->{'attr'}->{'available'}) {
 						print STDERR 'Added file ' . $file->{'name'} . ': ' . $file->{'path'} . "\n";
 					} else {
 						print STDERR 'Dropped file ' . $file->{'name'} . ': ' . $file->{'path'} . "\n";
@@ -265,20 +311,33 @@ while (1) {
 		}
 
 		# Skip unavailable files
-		if (!$file->{'available'}) {
+		if (!$file->{'attr'}->{'available'}) {
 			next;
 		}
 
-		# Record the last update for STATUS and MTIME files
-		if ($file->{'type'} =~ /^STATUS/ || $file->{'type'} =~ /^MTIME/) {
-			$file->{'update'} = mtime($file->{'path'});
+		# Record the last update for MTIME files
+		if ($file->{'attr'}->{'mtime'}) {
+			my $mtime = mtime($file->{'path'});
+
+			# All EXISTS files have MTIME set, and mtime returns 0 if the file does not exist, so we can overload this check
+			if ($file->{'attr'}->{'exists'}) {
+				if ($mtime > 0) {
+					$file->{'update'} = $mtime;
+					$file->{'value'}  = 1;
+				} elsif ($file->{'last'}) {
+					$file->{'update'} = time();
+				}
+			} else {
+				$file->{'update'} = $mtime;
+			}
+
 			if ($DEBUG) {
-				print STDERR 'Last change: ' . $file->{'name'} . ': ' . localtime($file->{'update'}) . "\n";
+				print STDERR 'Mtime: ' . $file->{'name'} . ': ' . $file->{'value'} . ':' . $file->{'update'} . "\n";
 			}
 		}
 
 		# Grab the state from STATUS files
-		if ($file->{'type'} =~ /^STATUS/) {
+		if ($file->{'attr'}->{'status'}) {
 			my $text = '';
 			{
 				if (!open(my $fh, $file->{'path'})) {
@@ -290,25 +349,25 @@ while (1) {
 				}
 			}
 
-			# Record the main status for the file
-			if ($file->{'type'} eq 'STATUS_PLAYING') {
+			# Parse the value for the file
+			if ($file->{'attr'}->{'value'}) {
+				$text =~ s/\n$//;
+				$file->{'value'} = $text;
+			} elsif ($file->{'attr'}->{'playing'}) {
 				if ($text =~ /PlayStatus\:Playing/) {
-					$file->{'status'} = 1;
+					$file->{'value'} = 1;
 				}
-			} elsif ($file->{'type'} eq 'STATUS_GUI') {
+			} elsif ($file->{'attr'}->{'gui'}) {
 				if (!($text =~ /ActiveWindowName\:Fullscreen video/)) {
-					$file->{'status'} = 1;
+					$file->{'value'} = 1;
 				}
-			} elsif ($file->{'type'} eq 'STATUS_VALUE') {
-				$text =~ s/\n$//;
-				$file->{'status'} = $text;
 			} else {
 				if ($text =~ /1/) {
-					$file->{'status'} = 1;
+					$file->{'value'} = 1;
 				}
 			}
 			if ($DEBUG) {
-				print STDERR 'Status: ' . $file->{'name'} . ': ' . $file->{'status'} . "\n";
+				print STDERR 'Status: ' . $file->{'name'} . ': ' . $file->{'value'} . "\n";
 			}
 
 			# Handle extras, if any
@@ -316,34 +375,20 @@ while (1) {
 				foreach my $extra (keys(%{ $EXTRAS{ $file->{'name'} } })) {
 
 					my $name = $file->{'name'} . '_' . $extra;
-					$files{$name}{'last'}   = $files{$name}{'status'};
-					$files{$name}{'status'} = 0;
+					$files{$name}{'last'}   = $files{$name}{'value'};
+					$files{$name}{'value'}  = 0;
 					$files{$name}{'update'} = $file->{'update'};
 
 					if ($text =~ $EXTRAS{ $file->{'name'} }{$extra}) {
-						$files{$name}{'status'} = $1;
+						$files{$name}{'value'} = $1;
 					}
 
 					if ($DEBUG) {
-						print STDERR 'Status (extra): ' . $name . ': ' . $files{$name}{'status'} . "\n";
+						print STDERR 'Status (extra): ' . $name . ': ' . $files{$name}{'value'} . "\n";
 					}
 				}
 			}
 		}
-
-		# Check for the presence of EXISTS files and record their last change
-		if ($file->{'type'} =~ /^EXISTS/) {
-			my $mtime = mtime($file->{'path'});
-			if ($mtime > 0) {
-				$file->{'status'} = 1;
-				$file->{'update'} = $mtime;
-			} elsif ($file->{'last'}) {
-				$file->{'update'} = time();
-			}
-			if ($DEBUG) {
-				print STDERR 'Exists: ' . $file->{'name'} . ': ' . $file->{'status'} . ':' . $file->{'update'} . "\n";
-			}
-		}
 	}
 
 	# Set the global update timestamp
@@ -355,12 +400,12 @@ while (1) {
 
 	# Calculate the new state
 	$stateLast = $state;
-	if (exists($files{$DISPLAY}) && $files{$DISPLAY}->{'status'}) {
+	if ($DISPLAY && exists($files{$DISPLAY}) && $files{$DISPLAY}->{'value'}) {
 
 		# We are always either playing or paused if the display is on
 		# When we're playing "Audio" assume we are paused
-		if (   (exists($files{'PLAYING'}) && $files{'PLAYING'}->{'status'})
-			&& ((exists($files{'PLAYING_TYPE'}) && $files{'PLAYING_TYPE'}->{'status'} ne 'Audio') || !exists($files{'PLAYING_TYPE'})))
+		if (   (exists($files{'PLAYING'}) && $files{'PLAYING'}->{'value'})
+			&& ((exists($files{'PLAYING_TYPE'}) && $files{'PLAYING_TYPE'}->{'value'} ne 'Audio') || !exists($files{'PLAYING_TYPE'})))
 		{
 			$state = 'PLAY';
 		} else {
@@ -374,7 +419,7 @@ while (1) {
 		if ($timeSinceUpdate > $STATE_TIMEOUT) {
 			$state = 'OFF';
 		} elsif ($timeSinceUpdate < $STATE_TIMEOUT) {
-			if (exists($files{'NO_MOTION'}) && $files{'NO_MOTION'}->{'status'}) {
+			if (exists($files{'NO_MOTION'}) && $files{'NO_MOTION'}->{'value'}) {
 				$state = 'OFF';
 			} else {
 				$state = 'MOTION';
@@ -387,7 +432,7 @@ while (1) {
 
 	# Clear EXISTS_ON files when the main state is "OFF" (and before we append their status)
 	foreach my $file (values(%files)) {
-		if ($file->{'type'} eq 'EXISTS_ON' && $file->{'status'} && $state eq 'OFF') {
+		if ($file->{'attr'}->{'clear_off'} && $file->{'value'} && $state eq 'OFF') {
 			unlink($file->{'path'});
 			$file->{'stauts'} = 0;
 			if ($DEBUG) {
@@ -401,7 +446,7 @@ while (1) {
 	{
 		my @statTime = ();
 		foreach my $file (values(%files)) {
-			my $text = $file->{'status'};
+			my $text = $file->{'value'};
 			$text =~ s/\s/ /g;
 			$text =~ s/\|/-/g;
 			push(@statTime, $file->{'name'} . '|' . $text . '|' . $file->{'update'});
@@ -411,7 +456,7 @@ while (1) {
 
 	# Clear EXISTS_CLEAR files immediately (but after we append their status)
 	foreach my $file (values(%files)) {
-		if ($file->{'type'} eq 'EXISTS_CLEAR' && $file->{'status'}) {
+		if ($file->{'attr'}->{'clear'} && $file->{'value'}) {
 			unlink($file->{'path'});
 			$file->{'stauts'} = 0;
 			if ($DEBUG) {
@@ -427,7 +472,7 @@ while (1) {
 
 	# Update on any status change
 	foreach my $file (values(%files)) {
-		if ($file->{'status'} ne $file->{'last'}) {
+		if ($file->{'value'} ne $file->{'last'}) {
 			$update = 1;
 			last;
 		}