Zachary Kotlarek 11 лет назад
Родитель
Сommit
c8e9cf931c
5 измененных файлов с 286 добавлено и 53 удалено
  1. 129 0
      dmx/door.pl
  2. 46 51
      dmx/garage.pl
  3. 109 0
      dmx/oil.pl
  4. 1 1
      dmx/reset.sh
  5. 1 1
      dmx/state.pl

+ 129 - 0
dmx/door.pl

@@ -0,0 +1,129 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Time::HiRes qw( usleep sleep time );
+
+# Local modules
+use Cwd qw(abs_path);
+use File::Basename qw(dirname);
+use lib dirname(abs_path($0));
+use DMX;
+
+# User config
+my $RELAY_DELAY = 0.20;
+my %DIM         = (
+	'OFF'      => [ { 'channel' => 16, 'value' => 0,   'time' => 0 } ],
+	'ACTIVATE' => [ { 'channel' => 16, 'value' => 255, 'time' => 0 } ],
+);
+
+# App config
+my $DATA_DIR     = DMX::dataDir();
+my $STATE_SOCK   = 'DOOR';
+my $OUTPUT_FILE  = $DATA_DIR . $STATE_SOCK;
+my $PUSH_TIMEOUT = 20;
+my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
+my $DELAY        = $PULL_TIMEOUT / 2;
+
+# Debug
+my $DEBUG = 0;
+if ($ENV{'DEBUG'}) {
+	$DEBUG = 1;
+}
+
+# State
+my $state     = 'OFF';
+my $stateLast = $state;
+my %exists    = ();
+my $pushLast  = 0;
+my $pullLast  = time();
+my $update    = 0;
+
+# Always force lights into OFF at launch
+$state = 'OFF';
+DMX::applyDataset($DIM{$state}, $state, $OUTPUT_FILE);
+
+# Sockets
+DMX::stateSocket($STATE_SOCK);
+DMX::stateSubscribe($STATE_SOCK);
+
+# Loop forever
+while (1) {
+
+	# State is calculated; use newState to gather data
+	my $newState = 'OFF';
+
+	# Wait for state updates
+	my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
+	if (defined($cmdState)) {
+		$newState = $cmdState;
+		$pullLast = time();
+	}
+
+	# Die if we don't see regular updates
+	if (time() - $pullLast > $PULL_TIMEOUT) {
+		die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
+	}
+
+	# Calculate the new state
+	$stateLast = $state;
+	if ($newState eq 'ACTIVATE' || $exists{'DOOR_CMD'}) {
+		$state = 'ACTIVATE';
+	} else {
+		$state = 'OFF';
+	}
+
+	# Force updates on a periodic basis
+	if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
+		if ($DEBUG) {
+			print STDERR "Forcing periodic update\n";
+		}
+		$update = 1;
+	}
+
+	# Force updates on any state change
+	if (!$update && $stateLast ne $state) {
+		if ($DEBUG) {
+			print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
+		}
+		$update = 1;
+	}
+
+	# Special handling for toggle systems: only push if the state is "ACTIVATE"
+	if ($update && $state ne 'ACTIVATE') {
+		if ($DEBUG) {
+			print STDERR 'Skipping non-activate state: ' . $state . "\n";
+		}
+		$update = 0;
+	}
+
+	# Do not operate when LOCKed
+	if ($update && $exists{'LOCK'}) {
+		DMX::say('Door locked');
+		if ($exists{'DOOR_CMD'}) {
+			DMX::say('Access denied to: ' . $exists{'DOOR_CMD'});
+		}
+		$update = 0;
+	}
+
+	# Update the relay
+	if ($update) {
+
+		# Annouce the command
+		if ($exists{'DOOR_CMD'}) {
+			DMX::say('Door activated by ' . $exists{'DOOR_CMD'});
+		}
+
+		# Toggle
+		$state = 'ACTIVATE';
+		DMX::applyDataset($DIM{$state}, $state, $OUTPUT_FILE);
+		usleep($RELAY_DELAY * 1000000);
+		$state = 'OFF';
+		DMX::applyDataset($DIM{$state}, $state, $OUTPUT_FILE);
+
+		# Update the push time
+		$pushLast = time();
+
+		# Clear the update flag
+		$update = 0;
+	}
+}

+ 46 - 51
dmx/garage.pl

@@ -1,7 +1,6 @@
 #!/usr/bin/perl
 use strict;
 use warnings;
-use Time::HiRes qw( usleep sleep time );
 
 # Local modules
 use Cwd qw(abs_path);
@@ -10,10 +9,15 @@ use lib dirname(abs_path($0));
 use DMX;
 
 # User config
-my $RELAY_DELAY = 0.20;
-my %DIM         = (
-	'OFF'      => [ { 'channel' => 16, 'value' => 0,   'time' => 0 } ],
-	'ACTIVATE' => [ { 'channel' => 16, 'value' => 255, 'time' => 0 } ],
+my $MOTION_TIMEOUT     = 30;
+my $POSTMOTION_TIMEOUT = 60;
+my %DIM                = (
+	'OFF'        => [ { 'channel' => 17, 'value' => 0,   'time' => 60000 }, ],
+	'PREMOTION'  => [ { 'channel' => 17, 'value' => 32,  'time' => 2500 }, ],
+	'MOTION'     => [ { 'channel' => 17, 'value' => 192, 'time' => 750 }, ],
+	'POSTMOTION' => [ { 'channel' => 17, 'value' => 32,  'time' => $POSTMOTION_TIMEOUT * 1000 }, ],
+	'BRIGHT'     => [ { 'channel' => 17, 'value' => 255, 'time' => 1000 }, ],
+	'ERROR'      => [ { 'channel' => 17, 'value' => 192, 'time' => 100 }, ],
 );
 
 # App config
@@ -31,15 +35,17 @@ if ($ENV{'DEBUG'}) {
 }
 
 # State
-my $state     = 'OFF';
-my $stateLast = $state;
-my %exists    = ();
-my $pushLast  = 0;
-my $pullLast  = time();
-my $update    = 0;
-
-# Always force lights into OFF at launch
-$state = 'OFF';
+my $state      = 'OFF';
+my $stateLast  = $state;
+my %exists     = ();
+my %mtime      = ();
+my $pushLast   = 0;
+my $pullLast   = time();
+my $update     = 0;
+my $lastMotion = 0;
+
+# Always force lights into ERROR at launch
+$state = 'ERROR';
 DMX::applyDataset($DIM{$state}, $state, $OUTPUT_FILE);
 
 # Sockets
@@ -50,30 +56,45 @@ DMX::stateSubscribe($STATE_SOCK);
 while (1) {
 
 	# State is calculated; use newState to gather data
-	my $newState = 'OFF';
+	my $newState = $state;
 
 	# Wait for state updates
-	my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
+	my $cmdState = DMX::readState($DELAY, \%exists, \%mtime, undef());
+
+	# Avoid repeated calls to time()
+	my $now = time();
+
+	# Record only valid states
 	if (defined($cmdState)) {
 		$newState = $cmdState;
-		$pullLast = time();
+		$pullLast = $now;
 	}
 
 	# Die if we don't see regular updates
-	if (time() - $pullLast > $PULL_TIMEOUT) {
+	if ($now - $pullLast > $PULL_TIMEOUT) {
 		die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
 	}
 
 	# Calculate the new state
 	$stateLast = $state;
-	if ($newState eq 'ACTIVATE' || $exists{'GARAGE_CMD'}) {
-		$state = 'ACTIVATE';
+	if ($exists{'BRIGHT'}) {
+		$newState = 'BRIGHT';
+	} elsif ($mtime{'MOTION_GARAGE'} > $now - $MOTION_TIMEOUT) {
+		$newState   = 'MOTION';
+		$lastMotion = $now;
+	} elsif ($newState eq 'PLAY' || $newState eq 'PAUSE' || $newState eq 'MOTION') {
+		if ($now > $lastMotion + $POSTMOTION_TIMEOUT) {
+			$newState = 'PREMOTION';
+		} else {
+			$newState = 'POSTMOTION';
+		}
 	} else {
-		$state = 'OFF';
+		$newState = 'OFF';
 	}
+	$state = $newState;
 
 	# Force updates on a periodic basis
-	if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
+	if (!$update && $now - $pushLast > $PUSH_TIMEOUT) {
 		if ($DEBUG) {
 			print STDERR "Forcing periodic update\n";
 		}
@@ -88,40 +109,14 @@ while (1) {
 		$update = 1;
 	}
 
-	# Special handling for toggle systems: only push if the state is "ACTIVATE"
-	if ($update && $state ne 'ACTIVATE') {
-		if ($DEBUG) {
-			print STDERR 'Skipping non-activate state: ' . $state . "\n";
-		}
-		$update = 0;
-	}
-
-	# Do not operate when LOCKed
-	if ($update && $exists{'LOCK'}) {
-		DMX::say('Garage door locked');
-		if ($exists{'GARAGE_CMD'}) {
-			DMX::say('Access denied to: ' . $exists{'GARAGE_CMD'});
-		}
-		$update = 0;
-	}
-
-	# Update the relay
+	# Update the lighting
 	if ($update) {
 
-		# Annouce the command
-		if ($exists{'GARAGE_CMD'}) {
-			DMX::say('Garage door activated by ' . $exists{'GARAGE_CMD'});
-		}
-
-		# Toggle
-		$state = 'ACTIVATE';
-		DMX::applyDataset($DIM{$state}, $state, $OUTPUT_FILE);
-		usleep($RELAY_DELAY * 1000000);
-		$state = 'OFF';
+		# Update
 		DMX::applyDataset($DIM{$state}, $state, $OUTPUT_FILE);
 
 		# Update the push time
-		$pushLast = time();
+		$pushLast = $now;
 
 		# Clear the update flag
 		$update = 0;

+ 109 - 0
dmx/oil.pl

@@ -0,0 +1,109 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+# Local modules
+use Cwd qw(abs_path);
+use File::Basename qw(dirname);
+use lib dirname(abs_path($0));
+use DMX;
+
+# User config
+my $MOTION_TIMEOUT = 120;
+my %DIM            = (
+	'OFF'        => [ { 'channel' => 19, 'value' => 0,   'time' => 0 }, ],
+	'ON'         => [ { 'channel' => 19, 'value' => 255, 'time' => 0 }, ],
+);
+
+# App config
+my $DATA_DIR     = DMX::dataDir();
+my $STATE_SOCK   = 'OIL';
+my $OUTPUT_FILE  = $DATA_DIR . $STATE_SOCK;
+my $PUSH_TIMEOUT = 20;
+my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
+my $DELAY        = $PULL_TIMEOUT / 2;
+
+# Debug
+my $DEBUG = 0;
+if ($ENV{'DEBUG'}) {
+	$DEBUG = 1;
+}
+
+# State
+my $state      = 'OFF';
+my $stateLast  = $state;
+my %exists     = ();
+my %mtime      = ();
+my $pushLast   = 0;
+my $pullLast   = time();
+my $update     = 0;
+
+# Always force the heater into OFF at launch
+$state = 'OFF';
+DMX::applyDataset($DIM{$state}, $state, $OUTPUT_FILE);
+
+# Sockets
+DMX::stateSocket($STATE_SOCK);
+DMX::stateSubscribe($STATE_SOCK);
+
+# Loop forever
+while (1) {
+
+	# State is calculated; use newState to gather data
+	my $newState = $state;
+
+	# Wait for state updates
+	my $cmdState = DMX::readState($DELAY, \%exists, \%mtime, undef());
+
+	# Avoid repeated calls to time()
+	my $now = time();
+
+	# Record only valid states
+	if (defined($cmdState)) {
+		$newState = $cmdState;
+		$pullLast = $now;
+	}
+
+	# Die if we don't see regular updates
+	if ($now - $pullLast > $PULL_TIMEOUT) {
+		die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
+	}
+
+	# Calculate the new state
+	$stateLast = $state;
+	if ($mtime{'MOTION_GARAGE'} > $now - $MOTION_TIMEOUT) {
+		$newState   = 'ON';
+	} else {
+		$newState = 'OFF';
+	}
+	$state = $newState;
+
+	# Force updates on a periodic basis
+	if (!$update && $now - $pushLast > $PUSH_TIMEOUT) {
+		if ($DEBUG) {
+			print STDERR "Forcing periodic update\n";
+		}
+		$update = 1;
+	}
+
+	# Force updates on any state change
+	if (!$update && $stateLast ne $state) {
+		if ($DEBUG) {
+			print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
+		}
+		$update = 1;
+	}
+
+	# Update the lighting
+	if ($update) {
+
+		# Update
+		DMX::applyDataset($DIM{$state}, $state, $OUTPUT_FILE);
+
+		# Update the push time
+		$pushLast = $now;
+
+		# Clear the update flag
+		$update = 0;
+	}
+}

+ 1 - 1
dmx/reset.sh

@@ -1,6 +1,6 @@
 #!/bin/bash
 
-PROCS="amplifierPower.pl audio.pl bias.pl color.pl debug.pl fan.pl garage.pl leds.pl overhead.pl projectorPower.pl rave.pl rifftrax.pl rope.pl frontApp stairs.pl announce.pl rumbleCtl.pl"
+PROCS="amplifierPower.pl audio.pl bias.pl color.pl debug.pl door.pl fan.pl garage.pl leds.pl oil.pl overhead.pl projectorPower.pl rave.pl rifftrax.pl rope.pl frontApp stairs.pl announce.pl rumbleCtl.pl"
 
 # Find the PID for each named process
 PIDS=""

+ 1 - 1
dmx/state.pl

@@ -40,7 +40,7 @@ if ($HOST =~ /loki/i) {
 	}
 
 	# Garage door opener
-	$MON_FILES{ $MEDIA_PATH . '/DMX/cmd/GARAGE_CMD' } = 'EXISTS-LINE-CLEAR';
+	$MON_FILES{ $MEDIA_PATH . '/DMX/cmd/DOOR_CMD' } = 'EXISTS-LINE-CLEAR';
 
 	# Rumble pack
 	$MON_FILES{'RUMBLE'}                                    = 'LINE-NOUPDATE';