Explorar o código

Periodic updates/timeouts

Zachary Kotlarek %!s(int64=13) %!d(string=hai) anos
pai
achega
08e4bfa9c7
Modificáronse 4 ficheiros con 92 adicións e 31 borrados
  1. 25 12
      dmx/fan.pl
  2. 28 9
      dmx/leds.pl
  3. 25 6
      dmx/rope.pl
  4. 14 4
      dmx/state.pl

+ 25 - 12
dmx/fan.pl

@@ -17,14 +17,14 @@ my %DIM = (
 		{ 'channel' => 11, 'value' => 255, 'time' => 0 }
 		{ 'channel' => 11, 'value' => 255, 'time' => 0 }
 	],
 	],
 );
 );
-my $TIMEOUT = 180;
 
 
 # App config
 # App config
 my $SOCK_TIMEOUT = 5;
 my $SOCK_TIMEOUT = 5;
 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 $CMD_FILE = $DATA_DIR . 'DMX.socket';
+my $DATA_DIR     = $TEMP_DIR . 'plexMonitor/';
+my $CMD_FILE     = $DATA_DIR . 'DMX.socket';
+my $PUSH_TIMEOUT = 20;
 
 
 # Debug
 # Debug
 my $DEBUG = 0;
 my $DEBUG = 0;
@@ -52,17 +52,21 @@ my $sock = IO::Socket::UNIX->new(
 ) or die('Unable to open socket: ' . $CMD_FILE . ": ${@}\n");
 ) or die('Unable to open socket: ' . $CMD_FILE . ": ${@}\n");
 
 
 # State
 # State
-my $state      = 'INIT';
-my $stateLast  = $state;
-my $fan        = 0;
-my $projector  = 0;
+my $state     = 'INIT';
+my $stateLast = $state;
+my $fan       = 0;
+my $projector = 0;
+my $pushLast  = 0;
 
 
 # Always force lights out at launch
 # Always force lights out at launch
-dim({'channel' => 11, 'value' => 0, 'time' => 0});
+dim({ 'channel' => 11, 'value' => 0, 'time' => 0 });
 
 
 # Loop forever
 # Loop forever
 while (1) {
 while (1) {
 
 
+	# Set anywhere to force an update this cycle
+	my $forceUpdate = 0;
+
 	# Monitor the PROJECTOR file for state
 	# Monitor the PROJECTOR file for state
 	{
 	{
 		$projector = 0;
 		$projector = 0;
@@ -81,6 +85,7 @@ while (1) {
 
 
 	# Monitor the FAN_CMD file for presence
 	# Monitor the FAN_CMD file for presence
 	{
 	{
+
 		# Clear the flag when the projector is off
 		# Clear the flag when the projector is off
 		if ($fan && !$projector) {
 		if ($fan && !$projector) {
 			unlink($DATA_DIR . 'FAN_CMD');
 			unlink($DATA_DIR . 'FAN_CMD');
@@ -110,8 +115,13 @@ while (1) {
 		$state = 'OFF';
 		$state = 'OFF';
 	}
 	}
 
 
+	# Force updates on a periodic basis
+	if (time() - $pushLast > $PUSH_TIMEOUT) {
+		$forceUpdate = 1;
+	}
+
 	# Update the fan state
 	# Update the fan state
-	if ($stateLast ne $state) {
+	if ($forceUpdate || $stateLast ne $state) {
 		if ($DEBUG) {
 		if ($DEBUG) {
 			print STDERR 'State: ' . $stateLast . ' => ' . $state . "\n";
 			print STDERR 'State: ' . $stateLast . ' => ' . $state . "\n";
 			foreach my $data (@{ $DIM{$state} }) {
 			foreach my $data (@{ $DIM{$state} }) {
@@ -131,6 +141,9 @@ while (1) {
 		print $fh 'State: ' . $state . "\n" . join("\n", @values) . "\n";
 		print $fh 'State: ' . $state . "\n" . join("\n", @values) . "\n";
 		close($fh);
 		close($fh);
 		rename($tmp, $DATA_DIR . 'FAN');
 		rename($tmp, $DATA_DIR . 'FAN');
+		
+		# Update the push time
+		$pushLast = time();
 	}
 	}
 
 
 	# Wait and loop
 	# Wait and loop
@@ -138,12 +151,12 @@ while (1) {
 }
 }
 
 
 # Send the command
 # Send the command
-sub dim(%) {
+sub dim($) {
 	my ($args) = @_;
 	my ($args) = @_;
-	if (! defined($args->{'delay'})) {
+	if (!defined($args->{'delay'})) {
 		$args->{'delay'} = 0;
 		$args->{'delay'} = 0;
 	}
 	}
-	if (! defined($args->{'channel'}) || ! defined($args->{'time'}) || ! defined($args->{'value'})) {
+	if (!defined($args->{'channel'}) || !defined($args->{'time'}) || !defined($args->{'value'})) {
 		die('Invalid command for socket: ' . join(', ', keys(%{$args})) . ': ' . join(', ', values(%{$args})) . "\n");
 		die('Invalid command for socket: ' . join(', ', keys(%{$args})) . ': ' . join(', ', values(%{$args})) . "\n");
 	}
 	}
 
 

+ 28 - 9
dmx/leds.pl

@@ -40,11 +40,13 @@ my %DIM = (
 my $SOCK_TIMEOUT = 5;
 my $SOCK_TIMEOUT = 5;
 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 $DMX_SOCK    = $DATA_DIR . 'DMX.socket';
-my $SUB_SOCK    = $DATA_DIR . 'STATE.socket';
-my $STATE_SOCK  = $DATA_DIR . 'LED.socket';
-my $MAX_CMD_LEN = 1024;
+my $DATA_DIR     = $TEMP_DIR . 'plexMonitor/';
+my $DMX_SOCK     = $DATA_DIR . 'DMX.socket';
+my $SUB_SOCK     = $DATA_DIR . 'STATE.socket';
+my $STATE_SOCK   = $DATA_DIR . 'LED.socket';
+my $MAX_CMD_LEN  = 1024;
+my $PUSH_TIMEOUT = 20;
+my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
 
 
 # Debug
 # Debug
 my $DEBUG = 0;
 my $DEBUG = 0;
@@ -97,14 +99,19 @@ my $state      = 'INIT';
 my $stateLast  = $state;
 my $stateLast  = $state;
 my $lights     = 0;
 my $lights     = 0;
 my $updateLast = 0;
 my $updateLast = 0;
+my $pushLast   = 0;
+my $pullLast   = 0;
 
 
 # Always force lights out at launch
 # Always force lights out at launch
-dim({'channel' => 13, 'value' => 0, 'time' => 0});
-dim({'channel' => 14, 'value' => 0, 'time' => 0});
-dim({'channel' => 15, 'value' => 0, 'time' => 0});
+dim({ 'channel' => 13, 'value' => 0, 'time' => 0 });
+dim({ 'channel' => 14, 'value' => 0, 'time' => 0 });
+dim({ 'channel' => 15, 'value' => 0, 'time' => 0 });
 
 
 # Loop forever
 # Loop forever
 while (1) {
 while (1) {
+	
+	# Set anywhere to force an update this cycle
+	my $forceUpdate = 0;
 
 
 	# State is calculated; use newState to gather data
 	# State is calculated; use newState to gather data
 	my $newState = $state;
 	my $newState = $state;
@@ -135,6 +142,7 @@ while (1) {
 
 
 		# Propogate the most recent command state
 		# Propogate the most recent command state
 		$newState = $cmdState;
 		$newState = $cmdState;
+		$pullLast = time();
 	}
 	}
 
 
 	# Monitor the LIGHTS file for presence
 	# Monitor the LIGHTS file for presence
@@ -165,9 +173,17 @@ while (1) {
 		}
 		}
 	}
 	}
 	$state = $newState;
 	$state = $newState;
+	
+	# Force updates on a periodic basis
+	if (time() - $pullLast > $PUSH_TIMEOUT) {
+		$forceUpdate = 1;
+	}
+	if (time() - $pullLast > $PULL_TIMEOUT) {
+		die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
+	}
 
 
 	# Update the lighting
 	# Update the lighting
-	if ($stateLast ne $state) {
+	if ($forceUpdate || $stateLast ne $state) {
 		if ($DEBUG) {
 		if ($DEBUG) {
 			print STDERR 'State: ' . $stateLast . ' => ' . $state . "\n";
 			print STDERR 'State: ' . $stateLast . ' => ' . $state . "\n";
 			foreach my $data (@{ $DIM{$state} }) {
 			foreach my $data (@{ $DIM{$state} }) {
@@ -187,6 +203,9 @@ while (1) {
 		print $fh 'State: ' . $state . "\n" . join("\n", @values) . "\n";
 		print $fh 'State: ' . $state . "\n" . join("\n", @values) . "\n";
 		close($fh);
 		close($fh);
 		rename($tmp, $DATA_DIR . 'LED');
 		rename($tmp, $DATA_DIR . 'LED');
+		
+		# Update the push time
+		$pushLast = time();
 	}
 	}
 }
 }
 
 

+ 25 - 6
dmx/rope.pl

@@ -68,11 +68,13 @@ my %DIM = (
 my $SOCK_TIMEOUT = 5;
 my $SOCK_TIMEOUT = 5;
 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 $DMX_SOCK    = $DATA_DIR . 'DMX.socket';
-my $SUB_SOCK    = $DATA_DIR . 'STATE.socket';
-my $STATE_SOCK  = $DATA_DIR . 'ROPE.socket';
-my $MAX_CMD_LEN = 1024;
+my $DATA_DIR     = $TEMP_DIR . 'plexMonitor/';
+my $DMX_SOCK     = $DATA_DIR . 'DMX.socket';
+my $SUB_SOCK     = $DATA_DIR . 'STATE.socket';
+my $STATE_SOCK   = $DATA_DIR . 'ROPE.socket';
+my $MAX_CMD_LEN  = 1024;
+my $PUSH_TIMEOUT = 20;
+my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
 
 
 # Debug
 # Debug
 my $DEBUG = 0;
 my $DEBUG = 0;
@@ -125,12 +127,17 @@ my $state      = 'INIT';
 my $stateLast  = $state;
 my $stateLast  = $state;
 my $lights     = 0;
 my $lights     = 0;
 my $updateLast = 0;
 my $updateLast = 0;
+my $pushLast   = 0;
+my $pullLast   = 0;
 
 
 # Always force lights out at launch
 # Always force lights out at launch
 dim({ 'channel' => 0, 'value' => 0, 'time' => 0 });
 dim({ 'channel' => 0, 'value' => 0, 'time' => 0 });
 
 
 # Loop forever
 # Loop forever
 while (1) {
 while (1) {
+	
+	# Set anywhere to force an update this cycle
+	my $forceUpdate = 0;
 
 
 	# State is calculated; use newState to gather data
 	# State is calculated; use newState to gather data
 	my $newState = $state;
 	my $newState = $state;
@@ -161,6 +168,7 @@ while (1) {
 
 
 		# Propogate the most recent command state
 		# Propogate the most recent command state
 		$newState = $cmdState;
 		$newState = $cmdState;
+		$pullLast = time();
 	}
 	}
 
 
 	# Monitor the LIGHTS file for presence
 	# Monitor the LIGHTS file for presence
@@ -192,8 +200,16 @@ while (1) {
 	}
 	}
 	$state = $newState;
 	$state = $newState;
 
 
+	# Force updates on a periodic basis
+	if (time() - $pushLast > $PUSH_TIMEOUT) {
+		$forceUpdate = 1;
+	}
+	if (time() - $pullLast > $PULL_TIMEOUT) {
+		die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
+	}
+
 	# Update the lighting
 	# Update the lighting
-	if ($stateLast ne $state) {
+	if ($forceUpdate || $stateLast ne $state) {
 		if ($DEBUG) {
 		if ($DEBUG) {
 			print STDERR 'State: ' . $stateLast . ' => ' . $state . "\n";
 			print STDERR 'State: ' . $stateLast . ' => ' . $state . "\n";
 			foreach my $data (@{ $DIM{$state} }) {
 			foreach my $data (@{ $DIM{$state} }) {
@@ -213,6 +229,9 @@ while (1) {
 		print $fh 'State: ' . $state . "\n" . join("\n", @values) . "\n";
 		print $fh 'State: ' . $state . "\n" . join("\n", @values) . "\n";
 		close($fh);
 		close($fh);
 		rename($tmp, $DATA_DIR . 'ROPE');
 		rename($tmp, $DATA_DIR . 'ROPE');
+		
+		# Update the push time
+		$pushLast = time();
 	}
 	}
 }
 }
 
 

+ 14 - 4
dmx/state.pl

@@ -15,10 +15,11 @@ my $STATE_TIMEOUT = 180;
 my $SOCK_TIMEOUT = 5;
 my $SOCK_TIMEOUT = 5;
 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 $CMD_FILE    = $DATA_DIR . 'STATE.socket';
-my $MAX_CMD_LEN = 4096;
-my $RESET_CMD   = $ENV{'HOME'} . '/bin/video/dmx/reset.sh';
+my $DATA_DIR     = $TEMP_DIR . 'plexMonitor/';
+my $CMD_FILE     = $DATA_DIR . 'STATE.socket';
+my $MAX_CMD_LEN  = 4096;
+my $RESET_CMD    = $ENV{'HOME'} . '/bin/video/dmx/reset.sh';
+my $PUSH_TIMEOUT = 20;
 
 
 # Debug
 # Debug
 my $DEBUG = 0;
 my $DEBUG = 0;
@@ -63,6 +64,7 @@ my $stateLast  = $state;
 my $playing    = 0;
 my $playing    = 0;
 my $projector  = 0;
 my $projector  = 0;
 my $updateLast = 0;
 my $updateLast = 0;
+my $pushLast   = 0;
 
 
 # Loop forever
 # Loop forever
 while (1) {
 while (1) {
@@ -196,12 +198,20 @@ while (1) {
 		$state = 'OFF';
 		$state = 'OFF';
 	}
 	}
 
 
+	# Force updates on a periodic basis
+	if (time() - $pushLast > $PUSH_TIMEOUT) {
+		$forceUpdate = 1;
+	}
+
 	# Update the state
 	# Update the state
 	if ($forceUpdate || $stateLast ne $state) {
 	if ($forceUpdate || $stateLast ne $state) {
 		if ($DEBUG) {
 		if ($DEBUG) {
 			print STDERR 'State: ' . $stateLast . ' => ' . $state . "\n";
 			print STDERR 'State: ' . $stateLast . ' => ' . $state . "\n";
 		}
 		}
 
 
+		# Note the push
+		$pushLast = time();
+
 		# Send notifications to all subscribers
 		# Send notifications to all subscribers
 		foreach my $sub (@subscribers) {
 		foreach my $sub (@subscribers) {