Преглед изворни кода

Meant to delete the bash script, not perl

Zachary Kotlarek пре 14 година
родитељ
комит
9235c64aee
2 измењених фајлова са 148 додато и 121 уклоњено
  1. 148 0
      dmx/rope.pl
  2. 0 121
      dmx/rope.sh

+ 148 - 0
dmx/rope.pl

@@ -0,0 +1,148 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Time::HiRes qw( usleep );
+
+# Prototypes
+sub mtime($);
+
+# User config
+my %DIM = (
+	'OFF'   => { 'value' => 0,   'time' => 10000 },
+	'PLAY'  => { 'value' => 64,  'time' => 250 },
+	'PAUSE' => { 'value' => 192, 'time' => 1000 }
+);
+my $TIMEOUT = 600; # Seconds
+my $DELAY   = 0.5 * 1000000; # Microseconds
+
+# App config
+my $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
+chomp($TEMP_DIR);
+$TEMP_DIR = $TEMP_DIR . '/plexMonitor';
+my $EXEC_DIR = $ENV{'HOME'} . '/bin/video/dmx';
+
+# Ensure python can find its OLA imports
+my $PYTHON_PATH = "/opt/local/lib/python2.7/site-packages:/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages";
+if ($ENV{'PYTHONPATH'}) {
+	$PYTHON_PATH = $ENV{'PYTHONPATH'} . ':' . $PYTHON_PATH;
+}
+$ENV{'PYTHONPATH'} = $PYTHON_PATH;
+
+# Debug
+my $DEBUG = 0;
+if ($ENV{'DEBUG'}) {
+	$DEBUG = 1;
+}
+
+# Sanity check
+if (!-d $EXEC_DIR || !-d $TEMP_DIR) {
+	die("Bad config\n");
+}
+
+# Always force lights out at launch
+system($EXEC_DIR . '/setChannels.py', 0);
+
+# State
+my $state       = 'INIT';
+my $stateLast   = $state;
+my $playing     = 0;
+my $playingLast = $playing;
+my $valueLast   = $DIM{'OFF'}{'value'};
+my $updateLast  = 0;
+
+# Loop forever
+while (1) {
+
+	# Monitor the PLAY_STATUS file for changes and state
+	$playingLast = $playing;
+	{
+		my $mtime = mtime($TEMP_DIR . '/PLAY_STATUS');
+		if ($mtime > $updateLast) {
+			$updateLast = $mtime;
+
+			# Grab the PLAY_STATUS value
+			$playing = 0;
+			my $fh;
+			open($fh, $TEMP_DIR . '/PLAY_STATUS')
+			  or die("Unable to open PLAY_STATUS\n");
+			my $text = <$fh>;
+			close($fh);
+			if ($text =~ /1/) {
+				$playing = 1;
+			}
+		}
+	}
+
+	# Monitor the GUI and PLAYING files for changes only
+	{
+		my $mtime = mtime($TEMP_DIR . '/PLAYING');
+		if ($mtime > $updateLast) {
+			$updateLast = $mtime;
+		}
+		$mtime = mtime($TEMP_DIR . '/GUI');
+		if ($mtime > $updateLast) {
+			$updateLast = $mtime;
+		}
+	}
+
+	# Calculate the new state
+	$stateLast = $state;
+	if ($playing != $playingLast) {
+		if ($DEBUG) {
+			print STDERR "Play state changed\n";
+		}
+
+		# We always need to update when the play state changes
+		if ($playing) {
+			$state = 'PLAY';
+		} else {
+			$state = 'PAUSE';
+		}
+
+	} elsif (!$playing) {
+
+		# If we're not playing, check the timeout
+		my $timeSinceUpdate = time() - $updateLast;
+		if ($state ne 'OFF' && $timeSinceUpdate > $TIMEOUT) {
+			$state = 'OFF';
+		} elsif ($state eq 'OFF' && $timeSinceUpdate < $TIMEOUT) {
+			$state = 'PAUSE';
+		}
+	}
+
+	# Update the lighting state
+	if ($stateLast ne $state) {
+		if ($DEBUG) {
+			print STDERR 'New state: ' . $state . "\n";
+			print STDERR "\tTime: " . $DIM{$state}{'time'} . "\n";
+			print STDERR "\tFrom: " . $valueLast . "\n";
+			print STDERR "\tTo: " . $DIM{$state}{'value'} . "\n";
+		}
+		if ($valueLast != $DIM{$state}{'value'}) {
+			system($EXEC_DIR . '/dimChannels.py', $DIM{$state}{'time'}, $valueLast, $DIM{$state}{'value'});
+		} elsif ($DEBUG) {
+			print STDERR "Skipping noop dim request\n";
+		}
+		$valueLast = $DIM{$state}{'value'};
+
+		# Save the state and value to disk
+		my $fh;
+		open($fh, '>', $TEMP_DIR . '/ROPE')
+		  or die("Unable to open ROPE");
+		print $fh 'State: ' . $state . "\nValue: " . $valueLast . "\n";
+		close($fh);
+		open($fh, '>', $TEMP_DIR . '/ROPE.lastUpdate')
+		  or die("Unable to open ROPE");
+		print $fh time() . "\n";
+		close($fh);
+	}
+
+	# Wait and loop
+	usleep($DELAY);
+}
+
+sub mtime($) {
+	my ($file) = @_;
+	my (undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), $mtime, undef(), undef(), undef()) = stat($file);
+	return $mtime;
+}

+ 0 - 121
dmx/rope.sh

@@ -1,121 +0,0 @@
-#!/bin/bash
-
-# Config
-DIM_OFF=0
-DIM_OFF_TIME=10000
-DIM_PLAY=64
-DIM_PLAY_TIME=250
-DIM_PAUSE=192
-DIM_PAUSE_TIME=1000
-OFF_TIMEOUT=600
-DELAY=0.5
-
-# App Config
-TEMP_DIR="`getconf DARWIN_USER_TEMP_DIR`/plexMonitor"
-EXEC_DIR="${HOME}/bin/video/dmx"
-export PYTHONPATH="${PYTHONPATH}:/opt/local/lib/python2.7/site-packages:/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages"
-
-cleanup() {
-	exit 0
-}
-trap cleanup SIGINT
-
-checkForUpdates() {
-	UPDATE=0
-	if [ -r "${1}" ]; then
-		UPDATE="`cat "${1}"`"
-	fi
-	if [ -z "${UPDATE}" ]; then
-		UPDATE=0
-	fi
-	UPDATE=$(( $UPDATE + 0 ))
-	echo $UPDATE
-}
-
-# Sanity check
-if [ ! -d "${EXEC_DIR}" ] || [ ! -d "${TEMP_DIR}" ]; then
-	echo "Bad config" 1>&2
-	exit 1
-fi
-
-# Move into position
-cd "${TEMP_DIR}"
-
-# State
-STATE="OFF"
-LAST_STATE="OFF"
-DIM_LAST=$DIM_PLAY
-PLAYING=0
-LAST_UPDATE=0
-LAST_PLAYING=0
-
-# Always force lights out at launch
-"${EXEC_DIR}/setChannels.py" 0
-
-# Loop forever
-while [ 1 ]; do
-	# Monitor the PLAY_STATUS file for changes and state
-	LAST_PLAYING=$PLAYING
-	UPDATE=$(checkForUpdates PLAY_STATUS.lastUpdate)
-	if [ $UPDATE -gt $LAST_UPDATE ]; then
-		LAST_UPDATE=$UPDATE
-		PLAYING="`cat PLAY_STATUS`"
-		PLAYING=$(( $PLAYING + 0 ))
-	fi
-
-	# Monitor the GUI file for changes only
-	UPDATE=$(checkForUpdates GUI.lastUpdate)
-	if [ $UPDATE -gt $LAST_UPDATE ]; then
-		LAST_UPDATE=$UPDATE
-	fi
-	UPDATE=$(checkForUpdates PLAYING.lastUpdate)
-	if [ $UPDATE -gt $LAST_UPDATE ]; then
-		LAST_UPDATE=$UPDATE
-	fi
-
-	# Detect when we change states
-	LAST_STATE=$STATE
-
-	# If the play state has changed we must adjust
-	if [ $PLAYING -ne $LAST_PLAYING ]; then
-		if [ $PLAYING -gt 0 ]; then
-			STATE="PLAYING"
-		else
-			STATE="PAUSED"
-		fi
-
-	# If we're not playing, check for timeouts
-	elif [ $PLAYING -lt 1 ]; then
-		TIME_SINCE_CHANGE=$(( `date '+%s'` - $LAST_UPDATE ))
-
-		# Turn off when we reach the timeout
-		if [ "${STATE}" != "OFF" ] && [ $TIME_SINCE_CHANGE -gt $OFF_TIMEOUT ]; then
-			STATE="OFF"
-		# But fire back up if anything changes
-		elif [ "${STATE}" == "OFF" ] && [ $TIME_SINCE_CHANGE -lt $OFF_TIMEOUT ]; then
-			STATE="PAUSED"
-		fi
-	fi
-
-
-	# Set the lighting state
-	if [ "${STATE}" != "${LAST_STATE}" ]; then
-		if [ "${STATE}" == "PAUSED" ]; then
-			"${EXEC_DIR}/dimChannels.py" $DIM_PAUSE_TIME $DIM_LAST $DIM_PAUSE
-			DIM_LAST=$DIM_PAUSE
-		elif [ "${STATE}" == "PLAYING" ]; then
-			"${EXEC_DIR}/dimChannels.py" $DIM_PLAY_TIME $DIM_LAST $DIM_PLAY
-			DIM_LAST=$DIM_PLAY
-		else
-			"${EXEC_DIR}/dimChannels.py" $DIM_OFF_TIME $DIM_LAST $DIM_OFF
-			DIM_LAST=$DIM_OFF
-		fi
-
-		# Save the state to disk for external reference
-		echo -e "State: ${STATE}\nValue: ${DIM_LAST}" > ROPE
-		date '+%s' > ROPE.lastUpdate
-	fi
-
-	# Delay
-	sleep $DELAY
-done