|
|
@@ -10,14 +10,14 @@ use IPC::System::Simple qw( system capture );
|
|
|
# Local modules
|
|
|
use Cwd qw(abs_path);
|
|
|
use File::Basename qw(dirname);
|
|
|
-use lib dirname( abs_path($0) );
|
|
|
+use lib dirname(abs_path($0));
|
|
|
use DMX;
|
|
|
|
|
|
# Prototypes
|
|
|
sub mtime($);
|
|
|
|
|
|
# User config
|
|
|
-my %DISPLAY_DEVS = ( 'TV' => 1, 'PROJECTOR' => 1 );
|
|
|
+my %DISPLAY_DEVS = ('TV' => 1, 'PROJECTOR' => 1);
|
|
|
my $DISPLAY = undef();
|
|
|
my $STATE_TIMEOUT = 180;
|
|
|
my $EXISTS_TIMEOUT = 900;
|
|
|
@@ -27,7 +27,7 @@ my %MON_FILES = ();
|
|
|
# Host-specific config
|
|
|
# This is necessary to avoid contention on shared disks
|
|
|
my $HOST = Sys::Hostname::hostname();
|
|
|
-if ( $HOST =~ /loki/i ) {
|
|
|
+if ($HOST =~ /loki/i) {
|
|
|
|
|
|
# Ensure the media path is available before we get going
|
|
|
my $MEDIA_PATH = undef();
|
|
|
@@ -35,7 +35,7 @@ if ( $HOST =~ /loki/i ) {
|
|
|
my $mount = $ENV{'HOME'} . '/bin/video/mediaPath';
|
|
|
$MEDIA_PATH = capture($mount);
|
|
|
}
|
|
|
- if ( !$MEDIA_PATH || !-d $MEDIA_PATH ) {
|
|
|
+ if (!$MEDIA_PATH || !-d $MEDIA_PATH) {
|
|
|
die("Unable to access media path\n");
|
|
|
}
|
|
|
|
|
|
@@ -98,12 +98,12 @@ if ( $HOST =~ /loki/i ) {
|
|
|
$MON_FILES{'FAN_CMD'} = 'EXISTS-TIMEOUT';
|
|
|
|
|
|
# Garage
|
|
|
- $MON_FILES{'OIL'} = 'LINE-NOUPDATE';
|
|
|
- $MON_FILES{'DOOR'} = 'LINE-NOUPDATE';
|
|
|
- $MON_FILES{'LOCK'} = 'EXISTS-NOUPDATE';
|
|
|
+ $MON_FILES{'OIL'} = 'LINE-NOUPDATE';
|
|
|
+ $MON_FILES{'DOOR'} = 'LINE-NOUPDATE';
|
|
|
+ $MON_FILES{'LOCK'} = 'EXISTS-NOUPDATE';
|
|
|
|
|
|
# Alarm
|
|
|
- $MON_FILES{'ALARM'} = 'NONE';
|
|
|
+ $MON_FILES{'ALARM'} = 'NONE';
|
|
|
|
|
|
# OS State
|
|
|
$MON_FILES{'FRONT_APP'} = 'LINE-NOUPDATE';
|
|
|
@@ -162,13 +162,13 @@ my $PUSH_TIMEOUT = 20;
|
|
|
|
|
|
# Debug
|
|
|
my $DEBUG = 0;
|
|
|
-if ( $ENV{'DEBUG'} ) {
|
|
|
+if ($ENV{'DEBUG'}) {
|
|
|
$DEBUG = 1;
|
|
|
}
|
|
|
|
|
|
# Command-line arguments
|
|
|
my ($DELAY) = @ARGV;
|
|
|
-if ( !$DELAY ) {
|
|
|
+if (!$DELAY) {
|
|
|
$DELAY = 0.5;
|
|
|
}
|
|
|
|
|
|
@@ -176,9 +176,9 @@ if ( !$DELAY ) {
|
|
|
my $select = DMX::stateSocket($CMD_FILE);
|
|
|
|
|
|
# Add the extras to the main file list
|
|
|
-foreach my $file ( keys(%EXTRAS) ) {
|
|
|
- if ( exists( $MON_FILES{$file} ) ) {
|
|
|
- foreach my $extra ( keys( %{ $EXTRAS{$file} } ) ) {
|
|
|
+foreach my $file (keys(%EXTRAS)) {
|
|
|
+ if (exists($MON_FILES{$file})) {
|
|
|
+ foreach my $extra (keys(%{ $EXTRAS{$file} })) {
|
|
|
$MON_FILES{ $file . '_' . $extra } = 'EXTRA';
|
|
|
}
|
|
|
}
|
|
|
@@ -186,7 +186,7 @@ foreach my $file ( keys(%EXTRAS) ) {
|
|
|
|
|
|
# Init the file tracking structure
|
|
|
my %files = ();
|
|
|
-foreach my $name ( keys(%MON_FILES) ) {
|
|
|
+foreach my $name (keys(%MON_FILES)) {
|
|
|
my %file = (
|
|
|
'name' => basename($name),
|
|
|
'type' => $MON_FILES{$name},
|
|
|
@@ -197,12 +197,12 @@ foreach my $name ( keys(%MON_FILES) ) {
|
|
|
);
|
|
|
|
|
|
# Allow absolute paths to override the $DATA_DIR path
|
|
|
- if ( $name =~ /^\// ) {
|
|
|
+ if ($name =~ /^\//) {
|
|
|
$file{'path'} = $name;
|
|
|
}
|
|
|
|
|
|
# Record the directory name for folder monitoring
|
|
|
- $file{'dir'} = dirname( $file{'path'} );
|
|
|
+ $file{'dir'} = dirname($file{'path'});
|
|
|
|
|
|
# Set the attribute bits, for use in later update handling logic
|
|
|
my %attr = (
|
|
|
@@ -219,57 +219,57 @@ foreach my $name ( keys(%MON_FILES) ) {
|
|
|
'no_value' => 0,
|
|
|
'extra' => 0,
|
|
|
);
|
|
|
- if ( $file{'type'} =~ /\bSTATUS\b/i ) {
|
|
|
+ if ($file{'type'} =~ /\bSTATUS\b/i) {
|
|
|
$attr{'status'} = 1;
|
|
|
}
|
|
|
- if ( $file{'type'} =~ /\bEXISTS\b/i ) {
|
|
|
+ if ($file{'type'} =~ /\bEXISTS\b/i) {
|
|
|
$attr{'exists'} = 1;
|
|
|
}
|
|
|
- if ( $file{'type'} =~ /\bVALUE\b/i ) {
|
|
|
+ if ($file{'type'} =~ /\bVALUE\b/i) {
|
|
|
$attr{'value'} = 1;
|
|
|
}
|
|
|
- if ( $file{'type'} =~ /\bLINE\b/i ) {
|
|
|
+ if ($file{'type'} =~ /\bLINE\b/i) {
|
|
|
$attr{'line'} = 1;
|
|
|
}
|
|
|
- if ( $file{'type'} =~ /\bCLEAR\b/i ) {
|
|
|
+ if ($file{'type'} =~ /\bCLEAR\b/i) {
|
|
|
$attr{'clear'} = 1;
|
|
|
}
|
|
|
- if ( $file{'type'} =~ /\bON\b/i ) {
|
|
|
+ if ($file{'type'} =~ /\bON\b/i) {
|
|
|
$attr{'clear_off'} = 1;
|
|
|
}
|
|
|
- if ( $file{'type'} =~ /\bTIMEOUT\b/i ) {
|
|
|
+ if ($file{'type'} =~ /\bTIMEOUT\b/i) {
|
|
|
$attr{'clear_timeout'} = 1;
|
|
|
}
|
|
|
- if ( $file{'type'} =~ /\bMTIME\b/i ) {
|
|
|
+ if ($file{'type'} =~ /\bMTIME\b/i) {
|
|
|
$attr{'mtime'} = 1;
|
|
|
}
|
|
|
- if ( $file{'type'} =~ /\bPLAYING\b/i ) {
|
|
|
+ if ($file{'type'} =~ /\bPLAYING\b/i) {
|
|
|
$attr{'playing'} = 1;
|
|
|
}
|
|
|
- if ( $file{'type'} =~ /\bNOUPDATE\b/i ) {
|
|
|
+ if ($file{'type'} =~ /\bNOUPDATE\b/i) {
|
|
|
$attr{'no_update'} = 1;
|
|
|
}
|
|
|
- if ( $file{'type'} =~ /\bNONE\b/i ) {
|
|
|
+ if ($file{'type'} =~ /\bNONE\b/i) {
|
|
|
$attr{'no_value'} = 1;
|
|
|
}
|
|
|
- if ( $file{'type'} =~ /\bEXTRA\b/i ) {
|
|
|
+ if ($file{'type'} =~ /\bEXTRA\b/i) {
|
|
|
$attr{'extra'} = 1;
|
|
|
}
|
|
|
|
|
|
# Cross-match some data types for easy of use
|
|
|
- if ( $attr{'extra'} ) {
|
|
|
+ if ($attr{'extra'}) {
|
|
|
$attr{'no_value'} = 1;
|
|
|
}
|
|
|
- if ( $attr{'no_value'} ) {
|
|
|
+ if ($attr{'no_value'}) {
|
|
|
$attr{'no_update'} = 1;
|
|
|
}
|
|
|
- if ( $attr{'line'} ) {
|
|
|
+ if ($attr{'line'}) {
|
|
|
$attr{'value'} = 1;
|
|
|
}
|
|
|
- if ( $attr{'value'} || $attr{'playing'} ) {
|
|
|
+ if ($attr{'value'} || $attr{'playing'}) {
|
|
|
$attr{'status'} = 1;
|
|
|
}
|
|
|
- if ( $attr{'status'} || $attr{'exists'} ) {
|
|
|
+ if ($attr{'status'} || $attr{'exists'}) {
|
|
|
$attr{'mtime'} = 1;
|
|
|
}
|
|
|
|
|
|
@@ -281,14 +281,14 @@ foreach my $name ( keys(%MON_FILES) ) {
|
|
|
}
|
|
|
|
|
|
# Delete any existing input files, to ensure our state is reset
|
|
|
-foreach my $file ( values(%files) ) {
|
|
|
- if ( -e $file->{'path'} ) {
|
|
|
+foreach my $file (values(%files)) {
|
|
|
+ if (-e $file->{'path'}) {
|
|
|
if ($DEBUG) {
|
|
|
print STDERR 'Deleting '
|
|
|
. $file->{'name'} . ': '
|
|
|
. $file->{'path'} . "\n";
|
|
|
}
|
|
|
- unlink( $file->{'path'} );
|
|
|
+ unlink($file->{'path'});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -326,7 +326,7 @@ while (1) {
|
|
|
|
|
|
# Grab the inbound text
|
|
|
my $path = undef();
|
|
|
- $fh->recv( $path, $MAX_CMD_LEN );
|
|
|
+ $fh->recv($path, $MAX_CMD_LEN);
|
|
|
$path =~ s/^\s+//;
|
|
|
$path =~ s/\s+$//;
|
|
|
$path =~ s/\W/_/g;
|
|
|
@@ -336,7 +336,7 @@ while (1) {
|
|
|
|
|
|
# Open the new socket
|
|
|
my $sub = eval { DMX::clientSock($path); };
|
|
|
- if ( !$sub ) {
|
|
|
+ if (!$sub) {
|
|
|
print STDERR 'Unable to open socket: ' . $path . ": ${@}\n";
|
|
|
next;
|
|
|
}
|
|
|
@@ -346,10 +346,10 @@ while (1) {
|
|
|
'path' => $path,
|
|
|
'socket' => $sub,
|
|
|
);
|
|
|
- push( @subscribers, \%tmp );
|
|
|
+ push(@subscribers, \%tmp);
|
|
|
|
|
|
# Send immediately when someone subscribes
|
|
|
- if ( !$update ) {
|
|
|
+ if (!$update) {
|
|
|
if ($DEBUG) {
|
|
|
print STDERR "New subscriber\n";
|
|
|
}
|
|
|
@@ -359,11 +359,11 @@ while (1) {
|
|
|
|
|
|
# Use opendir/readdir in each folder to ensure fresh results
|
|
|
my %folders = ();
|
|
|
- foreach my $file ( values(%files) ) {
|
|
|
- if ( !$folders{ $file->{'dir'} } ) {
|
|
|
+ foreach my $file (values(%files)) {
|
|
|
+ if (!$folders{ $file->{'dir'} }) {
|
|
|
my $dh = undef();
|
|
|
- opendir( $dh, $file->{'dir'} )
|
|
|
- or die( 'Unable to open directory: ' . $file->{'dir'} );
|
|
|
+ opendir($dh, $file->{'dir'})
|
|
|
+ or die('Unable to open directory: ' . $file->{'dir'});
|
|
|
if ($DEBUG) {
|
|
|
print STDERR 'Refreshed folder: ' . $file->{'dir'} . "\n";
|
|
|
}
|
|
|
@@ -374,16 +374,16 @@ while (1) {
|
|
|
}
|
|
|
|
|
|
# Monitor files of all types
|
|
|
- foreach my $file ( values(%files) ) {
|
|
|
+ foreach my $file (values(%files)) {
|
|
|
|
|
|
-# Record the previous status and clear the new one, unless the file is type EXTRA
|
|
|
- if ( !$file->{'attr'}->{'extra'} ) {
|
|
|
+ # Record the previous status and clear the new one, unless the file is type EXTRA
|
|
|
+ if (!$file->{'attr'}->{'extra'}) {
|
|
|
$file->{'last'} = $file->{'value'};
|
|
|
$file->{'value'} = 0;
|
|
|
}
|
|
|
|
|
|
# Skip NO_VALUE files -- their values are abstracted from other data
|
|
|
- if ( $file->{'attr'}->{'no_value'} ) {
|
|
|
+ if ($file->{'attr'}->{'no_value'}) {
|
|
|
next;
|
|
|
}
|
|
|
|
|
|
@@ -391,13 +391,11 @@ while (1) {
|
|
|
{
|
|
|
my $wasAvailable = $file->{'attr'}->{'available'};
|
|
|
$file->{'attr'}->{'available'} = -r $file->{'path'} ? 1 : 0;
|
|
|
- if ( $wasAvailable != $file->{'attr'}->{'available'} ) {
|
|
|
+ if ($wasAvailable != $file->{'attr'}->{'available'}) {
|
|
|
|
|
|
# Reset and extras for this file
|
|
|
- if ( exists( $EXTRAS{ $file->{'name'} } ) ) {
|
|
|
- foreach
|
|
|
- my $extra ( keys( %{ $EXTRAS{ $file->{'name'} } } ) )
|
|
|
- {
|
|
|
+ if (exists($EXTRAS{ $file->{'name'} })) {
|
|
|
+ foreach my $extra (keys(%{ $EXTRAS{ $file->{'name'} } })) {
|
|
|
my $name = $file->{'name'} . '_' . $extra;
|
|
|
$files{$name}{'last'} = $files{$name}{'value'};
|
|
|
$files{$name}{'value'} = 0;
|
|
|
@@ -405,7 +403,7 @@ while (1) {
|
|
|
}
|
|
|
|
|
|
# Determine which display device we have (if any)
|
|
|
- if ( exists( $DISPLAY_DEVS{ $file->{'name'} } ) ) {
|
|
|
+ if (exists($DISPLAY_DEVS{ $file->{'name'} })) {
|
|
|
$DISPLAY = $file->{'name'};
|
|
|
if ($DEBUG) {
|
|
|
print STDERR 'Selecting display device: ' . $DISPLAY
|
|
|
@@ -414,12 +412,11 @@ while (1) {
|
|
|
}
|
|
|
|
|
|
if ($DEBUG) {
|
|
|
- if ( $file->{'attr'}->{'available'} ) {
|
|
|
+ if ($file->{'attr'}->{'available'}) {
|
|
|
print STDERR 'Added file '
|
|
|
. $file->{'name'} . ': '
|
|
|
. $file->{'path'} . "\n";
|
|
|
- }
|
|
|
- else {
|
|
|
+ } else {
|
|
|
print STDERR 'Dropped file '
|
|
|
. $file->{'name'} . ': '
|
|
|
. $file->{'path'} . "\n";
|
|
|
@@ -429,29 +426,27 @@ while (1) {
|
|
|
}
|
|
|
|
|
|
# Skip unavailable files
|
|
|
- if ( !$file->{'attr'}->{'available'} ) {
|
|
|
+ if (!$file->{'attr'}->{'available'}) {
|
|
|
next;
|
|
|
}
|
|
|
|
|
|
# Record the last update for MTIME files
|
|
|
- if ( $file->{'attr'}->{'mtime'} ) {
|
|
|
- my $mtime = mtime( $file->{'path'} );
|
|
|
+ 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 ) {
|
|
|
+ # 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'} ) {
|
|
|
+ } elsif ($file->{'last'}) {
|
|
|
$file->{'update'} = $now;
|
|
|
}
|
|
|
- }
|
|
|
- else {
|
|
|
+ } else {
|
|
|
$file->{'update'} = $mtime;
|
|
|
|
|
|
-# Set the value if the update is recent -- VALUE and STATUS will overwrite as needed
|
|
|
- if ( $mtime >= $now ) {
|
|
|
+ # Set the value if the update is recent -- VALUE and STATUS will overwrite as needed
|
|
|
+ if ($mtime >= $now) {
|
|
|
$file->{'value'} = 1;
|
|
|
}
|
|
|
}
|
|
|
@@ -465,17 +460,16 @@ while (1) {
|
|
|
}
|
|
|
|
|
|
# Grab the state from STATUS files (including VALUE files)
|
|
|
- if ( $file->{'attr'}->{'status'} ) {
|
|
|
+ if ($file->{'attr'}->{'status'}) {
|
|
|
|
|
|
# Overwrite the mtime-based value
|
|
|
$file->{'value'} = 0;
|
|
|
|
|
|
my $text = '';
|
|
|
{
|
|
|
- if ( !open( my $fh, $file->{'path'} ) ) {
|
|
|
- warn( 'Unable to open ' . $file->{'path'} . "\n" );
|
|
|
- }
|
|
|
- else {
|
|
|
+ if (!open(my $fh, $file->{'path'})) {
|
|
|
+ warn('Unable to open ' . $file->{'path'} . "\n");
|
|
|
+ } else {
|
|
|
local $/;
|
|
|
$text = <$fh>;
|
|
|
close($fh);
|
|
|
@@ -483,22 +477,19 @@ while (1) {
|
|
|
}
|
|
|
|
|
|
# Parse the value for the file
|
|
|
- if ( $file->{'attr'}->{'value'} ) {
|
|
|
- if ( $file->{'attr'}->{'line'} ) {
|
|
|
+ if ($file->{'attr'}->{'value'}) {
|
|
|
+ if ($file->{'attr'}->{'line'}) {
|
|
|
($text) = $text =~ /^([^\n]+)\s*\n/;
|
|
|
- }
|
|
|
- else {
|
|
|
+ } else {
|
|
|
$text =~ s/\s+$//;
|
|
|
}
|
|
|
$file->{'value'} = $text;
|
|
|
- }
|
|
|
- elsif ( $file->{'attr'}->{'playing'} ) {
|
|
|
- if ( $text =~ /^playing:1/m ) {
|
|
|
+ } elsif ($file->{'attr'}->{'playing'}) {
|
|
|
+ if ($text =~ /^playing:1/m) {
|
|
|
$file->{'value'} = 1;
|
|
|
}
|
|
|
- }
|
|
|
- else {
|
|
|
- if ( $text =~ /1/ ) {
|
|
|
+ } else {
|
|
|
+ if ($text =~ /1/) {
|
|
|
$file->{'value'} = 1;
|
|
|
}
|
|
|
}
|
|
|
@@ -509,15 +500,15 @@ while (1) {
|
|
|
}
|
|
|
|
|
|
# Handle extras, if any
|
|
|
- if ( exists( $EXTRAS{ $file->{'name'} } ) ) {
|
|
|
- foreach my $extra ( keys( %{ $EXTRAS{ $file->{'name'} } } ) ) {
|
|
|
+ if (exists($EXTRAS{ $file->{'name'} })) {
|
|
|
+ foreach my $extra (keys(%{ $EXTRAS{ $file->{'name'} } })) {
|
|
|
|
|
|
my $name = $file->{'name'} . '_' . $extra;
|
|
|
$files{$name}{'last'} = $files{$name}{'value'};
|
|
|
$files{$name}{'value'} = 0;
|
|
|
$files{$name}{'update'} = $file->{'update'};
|
|
|
|
|
|
- if ( $text =~ $EXTRAS{ $file->{'name'} }{$extra} ) {
|
|
|
+ if ($text =~ $EXTRAS{ $file->{'name'} }{$extra}) {
|
|
|
$files{$name}{'value'} = $1;
|
|
|
}
|
|
|
|
|
|
@@ -534,30 +525,30 @@ while (1) {
|
|
|
# Simulate the old GUI indicator
|
|
|
{
|
|
|
my $gui = 0;
|
|
|
- if ( exists( $files{'PLAYING_WINDOW'} )
|
|
|
- && $files{'PLAYING_WINDOW'}->{'value'} ne 'Fullscreen video' )
|
|
|
+ if (exists($files{'PLAYING_WINDOW'})
|
|
|
+ && $files{'PLAYING_WINDOW'}->{'value'} ne 'Fullscreen video')
|
|
|
{
|
|
|
$gui = 1;
|
|
|
}
|
|
|
$files{'GUI'}->{'value'} = $gui;
|
|
|
}
|
|
|
- if ( exists( $files{'PLAYING'} ) ) {
|
|
|
+ if (exists($files{'PLAYING'})) {
|
|
|
$files{'GUI'}->{'update'} = $files{'PLAYING'}->{'update'};
|
|
|
}
|
|
|
|
|
|
# Ignore the MOTION file if NO_MOTION is set
|
|
|
- if ( exists( $files{'NO_MOTION'} ) && $files{'NO_MOTION'}->{'value'} ) {
|
|
|
- if ( exists( $files{'MOTION'} ) ) {
|
|
|
+ if (exists($files{'NO_MOTION'}) && $files{'NO_MOTION'}->{'value'}) {
|
|
|
+ if (exists($files{'MOTION'})) {
|
|
|
$files{'MOTION'}->{'value'} = 0;
|
|
|
$files{'MOTION'}->{'update'} = 0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
# Set the global update timestamp, excluding files marked NOUPDATE
|
|
|
- foreach my $file ( values(%files) ) {
|
|
|
+ foreach my $file (values(%files)) {
|
|
|
if ( $file->{'update'} > $updateLast
|
|
|
&& !$file->{'attr'}->{'no_update'}
|
|
|
- && $file->{'update'} > $startTime )
|
|
|
+ && $file->{'update'} > $startTime)
|
|
|
{
|
|
|
$updateLast = $file->{'update'};
|
|
|
}
|
|
|
@@ -565,113 +556,106 @@ while (1) {
|
|
|
$timeSinceUpdate = $now - $updateLast;
|
|
|
|
|
|
# Calculate the PLEX state
|
|
|
- if ( exists( $files{'FRONT_APP'} ) ) {
|
|
|
+ if (exists($files{'FRONT_APP'})) {
|
|
|
if ( $files{'FRONT_APP'}->{'value'} =~ 'com.plexapp.plexhometheater'
|
|
|
|| $files{'FRONT_APP'}->{'value'} =~ 'tv.openpht.openpht'
|
|
|
- || $files{'FRONT_APP'}->{'value'} eq
|
|
|
- 'com.apple.ScreenSaver.Engine' )
|
|
|
+ || $files{'FRONT_APP'}->{'value'} eq 'com.apple.ScreenSaver.Engine')
|
|
|
{
|
|
|
- if ( !exists( $files{'GAME'} ) || !$files{'GAME'}->{'value'} ) {
|
|
|
+ if (!exists($files{'GAME'}) || !$files{'GAME'}->{'value'}) {
|
|
|
$files{'PLEX'}->{'value'} = 1;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- if ( $files{'PLEX'}->{'last'} != $files{'PLEX'}->{'value'} ) {
|
|
|
+ if ($files{'PLEX'}->{'last'} != $files{'PLEX'}->{'value'}) {
|
|
|
$files{'PLEX'}->{'update'} = $now;
|
|
|
}
|
|
|
|
|
|
# Determine some intermediate state data
|
|
|
my $playing = 0;
|
|
|
- if ( exists( $files{'PLAYING'} ) && $files{'PLAYING'}->{'value'} ) {
|
|
|
+ if (exists($files{'PLAYING'}) && $files{'PLAYING'}->{'value'}) {
|
|
|
$playing = 1;
|
|
|
}
|
|
|
my $video = 0;
|
|
|
- if ( exists( $files{'PLAYING_TYPE'} )
|
|
|
- && $files{'PLAYING_TYPE'}->{'value'} ne 'audio' )
|
|
|
+ if (exists($files{'PLAYING_TYPE'})
|
|
|
+ && $files{'PLAYING_TYPE'}->{'value'} ne 'audio')
|
|
|
{
|
|
|
$video = 1;
|
|
|
- }
|
|
|
- elsif ( !exists( $files{'PLAYING_TYPE'} ) ) {
|
|
|
+ } elsif (!exists($files{'PLAYING_TYPE'})) {
|
|
|
$video = 1;
|
|
|
}
|
|
|
|
|
|
# Non-plex apps are always "playing" and "video"
|
|
|
- if ( exists( $files{'PLEX'} ) && !$files{'PLEX'}->{'value'} ) {
|
|
|
+ if (exists($files{'PLEX'}) && !$files{'PLEX'}->{'value'}) {
|
|
|
$playing = 1;
|
|
|
$video = 1;
|
|
|
}
|
|
|
|
|
|
# Calculate the new state
|
|
|
$stateLast = $state;
|
|
|
- if ( !defined($DISPLAY) || !exists( $files{$DISPLAY} ) ) {
|
|
|
+ if (!defined($DISPLAY) || !exists($files{$DISPLAY})) {
|
|
|
|
|
|
-# If there's no display the state is always PLAY or PAUSE, as indicated by the master
|
|
|
+ # If there's no display the state is always PLAY or PAUSE, as indicated by the master
|
|
|
if ($playing) {
|
|
|
$state = 'PLAY';
|
|
|
- }
|
|
|
- else {
|
|
|
+ } else {
|
|
|
$state = 'PAUSE';
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
- elsif ( $files{$DISPLAY}->{'value'} ) {
|
|
|
+ } elsif ($files{$DISPLAY}->{'value'}) {
|
|
|
|
|
|
# If a display exists and is on:
|
|
|
# PLAY when video is active and playing
|
|
|
# PAUSE any other time (including when audio is active)
|
|
|
- if ( $playing && $video ) {
|
|
|
+ if ($playing && $video) {
|
|
|
$state = 'PLAY';
|
|
|
- }
|
|
|
- else {
|
|
|
+ } else {
|
|
|
$state = 'PAUSE';
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
- else {
|
|
|
+ } else {
|
|
|
|
|
|
-# If the display exists but is off, check the timeouts to determine MOTION vs. OFF
|
|
|
- if ( $timeSinceUpdate > $STATE_TIMEOUT ) {
|
|
|
+ # If the display exists but is off, check the timeouts to determine MOTION vs. OFF
|
|
|
+ if ($timeSinceUpdate > $STATE_TIMEOUT) {
|
|
|
$state = 'OFF';
|
|
|
- }
|
|
|
- elsif ( $timeSinceUpdate < $STATE_TIMEOUT ) {
|
|
|
+ } elsif ($timeSinceUpdate < $STATE_TIMEOUT) {
|
|
|
$state = 'MOTION';
|
|
|
}
|
|
|
}
|
|
|
- if ( $state eq 'INIT' ) {
|
|
|
+ if ($state eq 'INIT') {
|
|
|
$state = 'OFF';
|
|
|
}
|
|
|
|
|
|
# ALARM if LOCK exists and the master state is not OFF or any motion exists
|
|
|
{
|
|
|
my $alarm = 0;
|
|
|
- if ( exists( $files{'LOCK'} ) && $files{'LOCK'}->{'value'} ) {
|
|
|
+ if (exists($files{'LOCK'}) && $files{'LOCK'}->{'value'}) {
|
|
|
if (
|
|
|
$state ne 'OFF'
|
|
|
- || ( exists( $files{'MOTION'} ) && $files{'MOTION'}->{'value'} )
|
|
|
- || ( exists( $files{'MOTION_STAIRS'} )
|
|
|
- && $files{'MOTION_STAIRS'}->{'value'} )
|
|
|
- || ( exists( $files{'MOTION_GARAGE'} )
|
|
|
- && $files{'MOTION_GARAGE'}->{'value'} )
|
|
|
+ || (exists($files{'MOTION'}) && $files{'MOTION'}->{'value'})
|
|
|
+ || (exists($files{'MOTION_STAIRS'})
|
|
|
+ && $files{'MOTION_STAIRS'}->{'value'})
|
|
|
+ || (exists($files{'MOTION_GARAGE'})
|
|
|
+ && $files{'MOTION_GARAGE'}->{'value'})
|
|
|
)
|
|
|
{
|
|
|
$alarm = 1;
|
|
|
}
|
|
|
}
|
|
|
- if ( !exists( $files{'ALARM'}->{'value'} )
|
|
|
- || $files{'ALARM'}->{'value'} != $alarm )
|
|
|
+ if (!exists($files{'ALARM'}->{'value'})
|
|
|
+ || $files{'ALARM'}->{'value'} != $alarm)
|
|
|
{
|
|
|
$files{'ALARM'}->{'update'} = $now;
|
|
|
}
|
|
|
$files{'ALARM'}->{'value'} = $alarm;
|
|
|
}
|
|
|
|
|
|
-# Clear EXISTS_ON files when the main state is "OFF" (and before we append their status)
|
|
|
- foreach my $file ( values(%files) ) {
|
|
|
+ # Clear EXISTS_ON files when the main state is "OFF" (and before we append their status)
|
|
|
+ foreach my $file (values(%files)) {
|
|
|
if ( $file->{'attr'}->{'clear_off'}
|
|
|
&& $file->{'value'}
|
|
|
- && $state eq 'OFF' )
|
|
|
+ && $state eq 'OFF')
|
|
|
{
|
|
|
- unlink( $file->{'path'} );
|
|
|
+ unlink($file->{'path'});
|
|
|
$file->{'status'} = 0;
|
|
|
if ($DEBUG) {
|
|
|
print STDERR 'Clearing exists flag for: '
|
|
|
@@ -680,15 +664,15 @@ while (1) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-# Clear EXISTS_TIMEOUT files when the main state is "OFF" and EXISTS_TIMEOUT has expired (and before we append their status)
|
|
|
- foreach my $file ( values(%files) ) {
|
|
|
+ # Clear EXISTS_TIMEOUT files when the main state is "OFF" and EXISTS_TIMEOUT has expired (and before we append their status)
|
|
|
+ foreach my $file (values(%files)) {
|
|
|
if ( $file->{'attr'}->{'clear_timeout'}
|
|
|
&& $file->{'value'}
|
|
|
&& $state eq 'OFF'
|
|
|
- && $timeSinceUpdate > $EXISTS_TIMEOUT )
|
|
|
+ && $timeSinceUpdate > $EXISTS_TIMEOUT)
|
|
|
{
|
|
|
|
|
|
- unlink( $file->{'path'} );
|
|
|
+ unlink($file->{'path'});
|
|
|
$file->{'status'} = 0;
|
|
|
if ($DEBUG) {
|
|
|
print STDERR 'Clearing exists flag for: '
|
|
|
@@ -701,20 +685,20 @@ while (1) {
|
|
|
$statusLast = $status;
|
|
|
{
|
|
|
my @statTime = ();
|
|
|
- foreach my $file ( values(%files) ) {
|
|
|
+ foreach my $file (values(%files)) {
|
|
|
my $text = $file->{'value'};
|
|
|
$text =~ s/\s/ /g;
|
|
|
$text =~ s/\|/-/g;
|
|
|
- push( @statTime,
|
|
|
- $file->{'name'} . '|' . $text . '|' . $file->{'update'} );
|
|
|
+ push(@statTime,
|
|
|
+ $file->{'name'} . '|' . $text . '|' . $file->{'update'});
|
|
|
}
|
|
|
- $status = "\n" . join( "\n", @statTime );
|
|
|
+ $status = "\n" . join("\n", @statTime);
|
|
|
}
|
|
|
|
|
|
# Clear EXISTS_CLEAR files immediately (but after we append their status)
|
|
|
- foreach my $file ( values(%files) ) {
|
|
|
- if ( $file->{'attr'}->{'clear'} && $file->{'value'} ) {
|
|
|
- unlink( $file->{'path'} );
|
|
|
+ foreach my $file (values(%files)) {
|
|
|
+ if ($file->{'attr'}->{'clear'} && $file->{'value'}) {
|
|
|
+ unlink($file->{'path'});
|
|
|
$file->{'status'} = 0;
|
|
|
if ($DEBUG) {
|
|
|
print STDERR 'Clearing exists flag for: '
|
|
|
@@ -724,7 +708,7 @@ while (1) {
|
|
|
}
|
|
|
|
|
|
# Update on a periodic basis, so we don't timeout
|
|
|
- if ( !$update && $now - $pushLast > $PUSH_TIMEOUT ) {
|
|
|
+ if (!$update && $now - $pushLast > $PUSH_TIMEOUT) {
|
|
|
if ($DEBUG) {
|
|
|
print STDERR "Periodic update\n";
|
|
|
}
|
|
|
@@ -732,7 +716,7 @@ while (1) {
|
|
|
}
|
|
|
|
|
|
# Update on a master state change
|
|
|
- if ( !$update && $stateLast ne $state ) {
|
|
|
+ if (!$update && $stateLast ne $state) {
|
|
|
if ($DEBUG) {
|
|
|
print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
|
|
|
}
|
|
|
@@ -740,9 +724,9 @@ while (1) {
|
|
|
}
|
|
|
|
|
|
# Update on any mtime change
|
|
|
- if ( !$update ) {
|
|
|
- foreach my $file ( values(%files) ) {
|
|
|
- if ( $file->{'update'} > $pushLast ) {
|
|
|
+ if (!$update) {
|
|
|
+ foreach my $file (values(%files)) {
|
|
|
+ if ($file->{'update'} > $pushLast) {
|
|
|
if ($DEBUG) {
|
|
|
print STDERR 'Mtime change: '
|
|
|
. $file->{'name'} . ' ('
|
|
|
@@ -755,9 +739,9 @@ while (1) {
|
|
|
}
|
|
|
|
|
|
# Update on any value change
|
|
|
- if ( !$update ) {
|
|
|
- foreach my $file ( values(%files) ) {
|
|
|
- if ( $file->{'value'} ne $file->{'last'} ) {
|
|
|
+ if (!$update) {
|
|
|
+ foreach my $file (values(%files)) {
|
|
|
+ if ($file->{'value'} ne $file->{'last'}) {
|
|
|
if ($DEBUG) {
|
|
|
print STDERR 'Value change: '
|
|
|
. $file->{'name'} . ' ('
|
|
|
@@ -787,34 +771,33 @@ while (1) {
|
|
|
foreach my $sub (@subscribers) {
|
|
|
|
|
|
# Drop subscribers that are not available
|
|
|
- if ( !eval { $sub->{'socket'}->send( $state . $status ) } ) {
|
|
|
+ if (!eval { $sub->{'socket'}->send($state . $status) }) {
|
|
|
print STDERR 'Dropping bad socket from subscriber list: '
|
|
|
. $sub->{'path'} . "\n";
|
|
|
|
|
|
my @new_subscribers = ();
|
|
|
foreach my $new (@subscribers) {
|
|
|
- if ( $new->{'socket'} eq $sub->{'socket'} ) {
|
|
|
+ if ($new->{'socket'} eq $sub->{'socket'}) {
|
|
|
next;
|
|
|
}
|
|
|
- push( @new_subscribers, $new );
|
|
|
+ push(@new_subscribers, $new);
|
|
|
}
|
|
|
@subscribers = @new_subscribers;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
# Save the state and value to disk
|
|
|
- my ( $fh, $tmp ) =
|
|
|
- tempfile( $DATA_DIR . 'STATE.XXXXXXXX', 'UNLINK' => 0 );
|
|
|
+ my ($fh, $tmp) = tempfile($DATA_DIR . 'STATE.XXXXXXXX', 'UNLINK' => 0);
|
|
|
print $fh $state . "\n";
|
|
|
close($fh);
|
|
|
- rename( $tmp, $DATA_DIR . 'STATE' );
|
|
|
+ rename($tmp, $DATA_DIR . 'STATE');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
sub mtime($) {
|
|
|
my ($file) = @_;
|
|
|
my $mtime = 0;
|
|
|
- if ( -r $file ) {
|
|
|
+ if (-r $file) {
|
|
|
(
|
|
|
undef(), undef(), undef(), undef(), undef(), undef(), undef(),
|
|
|
undef(), undef(), $mtime, undef(), undef(), undef()
|