2005-12-16 18:05:29 +08:00
|
|
|
#!/usr/bin/perl -wT
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# ZoneMinder X10 Control Script, $Date$, $Revision$
|
2008-07-25 17:48:16 +08:00
|
|
|
# Copyright (C) 2001-2008 Philip Coombes
|
2005-12-16 18:05:29 +08:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
2015-04-16 13:39:34 +08:00
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
zmx10.pl - ZoneMinder X10 Control Script
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
zmx10.pl -c <command>,--command=<command> [-u <unit code>,--unit-code=<unit code>]
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
This script controls the monitoring of the X10 interface and the consequent
|
|
|
|
management of the ZM daemons based on the receipt of X10 signals.
|
|
|
|
|
|
|
|
=head1 OPTIONS
|
|
|
|
|
|
|
|
-c <command>, --command=<command> - Command to issue, one of 'on','off','dim','bright','status','shutdown'
|
|
|
|
-u <unit code>, --unit-code=<unit code> - Unit code to act on required for all commands
|
|
|
|
except 'status' (optional) and 'shutdown'
|
|
|
|
-v, --verison - Pirnts the currently installed version of ZoneMinder
|
|
|
|
|
|
|
|
=cut
|
2005-12-16 18:05:29 +08:00
|
|
|
use strict;
|
|
|
|
use bytes;
|
|
|
|
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# These are the elements you can edit to suit your installation
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
|
2005-12-23 19:32:23 +08:00
|
|
|
use constant CAUSE_STRING => "X10"; # What gets written as the cause of any events
|
2005-12-16 18:05:29 +08:00
|
|
|
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# Don't change anything below here
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
|
2009-06-08 17:11:56 +08:00
|
|
|
@EXTRA_PERL_LIB@
|
2005-12-16 18:05:29 +08:00
|
|
|
use ZoneMinder;
|
|
|
|
use POSIX;
|
|
|
|
use Socket;
|
|
|
|
use Getopt::Long;
|
2015-04-16 13:39:34 +08:00
|
|
|
use autouse 'Pod::Usage'=>qw(pod2usage);
|
2015-04-16 13:42:56 +08:00
|
|
|
use autouse 'Data::Dumper'=>qw(Dumper);
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2014-01-03 22:33:49 +08:00
|
|
|
use constant SOCK_FILE => $Config{ZM_PATH_SOCKS}.'/zmx10.sock';
|
2005-12-16 18:36:22 +08:00
|
|
|
|
2005-12-16 18:05:29 +08:00
|
|
|
$| = 1;
|
|
|
|
|
|
|
|
$ENV{PATH} = '/bin:/usr/bin';
|
|
|
|
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
|
|
|
|
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
|
|
|
|
|
2011-06-21 17:19:10 +08:00
|
|
|
logInit();
|
|
|
|
logSetSignal();
|
2005-12-21 01:03:33 +08:00
|
|
|
|
2005-12-16 18:05:29 +08:00
|
|
|
my $command;
|
|
|
|
my $unit_code;
|
2015-01-07 22:14:00 +08:00
|
|
|
my $version;
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2015-04-16 13:39:34 +08:00
|
|
|
GetOptions(
|
|
|
|
'command=s' =>\$command,
|
|
|
|
'unit-code=i' =>\$unit_code,
|
|
|
|
'version' =>\$version
|
|
|
|
) or pod2usage(-exitstatus => -1);
|
|
|
|
|
2015-01-07 22:14:00 +08:00
|
|
|
if ( $version ) {
|
2015-04-16 13:39:34 +08:00
|
|
|
print ZoneMinder::Base::ZM_VERSION;
|
|
|
|
exit(0);
|
2015-01-07 22:14:00 +08:00
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
|
|
|
|
die( "No command given" ) unless( $command );
|
2015-04-16 13:39:34 +08:00
|
|
|
die( "No unit code given" )
|
|
|
|
unless( $unit_code || ($command =~ /(?:start|status|shutdown)/) );
|
2005-12-16 18:05:29 +08:00
|
|
|
|
|
|
|
if ( $command eq "start" )
|
|
|
|
{
|
2015-04-16 13:39:34 +08:00
|
|
|
X10Server::runServer();
|
|
|
|
exit();
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
2015-04-16 13:39:34 +08:00
|
|
|
socket( CLIENT, PF_UNIX, SOCK_STREAM, 0 )
|
|
|
|
or Fatal( "Can't open socket: $!" );
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2006-01-12 07:56:58 +08:00
|
|
|
my $saddr = sockaddr_un( SOCK_FILE );
|
2005-12-16 18:05:29 +08:00
|
|
|
|
|
|
|
if ( !connect( CLIENT, $saddr ) )
|
|
|
|
{
|
2015-04-16 13:39:34 +08:00
|
|
|
# The server isn't there
|
|
|
|
print( "Unable to connect, starting server\n" );
|
|
|
|
close( CLIENT );
|
|
|
|
|
|
|
|
if ( my $cpid = fork() )
|
|
|
|
{
|
|
|
|
# Parent process just sleep and fall through
|
|
|
|
sleep( 2 );
|
2011-06-25 00:04:32 +08:00
|
|
|
logReinit();
|
2015-04-16 13:39:34 +08:00
|
|
|
socket( CLIENT, PF_UNIX, SOCK_STREAM, 0 )
|
|
|
|
or Fatal( "Can't open socket: $!" );
|
|
|
|
connect( CLIENT, $saddr )
|
|
|
|
or Fatal( "Can't connect: $!" );
|
|
|
|
}
|
|
|
|
elsif ( defined($cpid) )
|
|
|
|
{
|
|
|
|
setpgrp();
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2011-06-25 00:04:32 +08:00
|
|
|
logReinit();
|
2015-04-16 13:39:34 +08:00
|
|
|
X10Server::runServer();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Fatal( "Can't fork: $!" );
|
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
# The server is there, connect to it
|
|
|
|
#print( "Writing commands\n" );
|
|
|
|
CLIENT->autoflush();
|
|
|
|
my $message = "$command";
|
|
|
|
$message .= ";$unit_code" if ( $unit_code );
|
|
|
|
print( CLIENT $message );
|
|
|
|
shutdown( CLIENT, 1 );
|
|
|
|
while ( my $line = <CLIENT> )
|
|
|
|
{
|
2015-04-16 13:39:34 +08:00
|
|
|
chomp( $line );
|
|
|
|
print( "$line\n" );
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
close( CLIENT );
|
|
|
|
#print( "Finished writing, bye\n" );
|
|
|
|
exit;
|
|
|
|
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# This is the X10 Server package
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
package X10Server;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use bytes;
|
|
|
|
|
2005-12-16 21:16:37 +08:00
|
|
|
use ZoneMinder;
|
2005-12-16 18:05:29 +08:00
|
|
|
use POSIX;
|
|
|
|
use DBI;
|
|
|
|
use Socket;
|
|
|
|
use X10::ActiveHome;
|
2015-04-16 13:42:56 +08:00
|
|
|
use autouse 'Data::Dumper'=>qw(Dumper);
|
2005-12-16 18:05:29 +08:00
|
|
|
|
|
|
|
our $dbh;
|
|
|
|
our $x10;
|
|
|
|
|
|
|
|
our %monitor_hash;
|
|
|
|
our %device_hash;
|
|
|
|
our %pending_tasks;
|
|
|
|
|
|
|
|
sub runServer
|
|
|
|
{
|
2015-04-16 13:39:34 +08:00
|
|
|
Info( "X10 server starting\n" );
|
|
|
|
|
|
|
|
socket( SERVER, PF_UNIX, SOCK_STREAM, 0 )
|
|
|
|
or Fatal( "Can't open socket: $!" );
|
|
|
|
unlink( main::SOCK_FILE );
|
|
|
|
my $saddr = sockaddr_un( main::SOCK_FILE );
|
|
|
|
bind( SERVER, $saddr ) or Fatal( "Can't bind: $!" );
|
|
|
|
listen( SERVER, SOMAXCONN ) or Fatal( "Can't listen: $!" );
|
|
|
|
|
|
|
|
$dbh = zmDbConnect();
|
|
|
|
|
2015-05-27 22:00:24 +08:00
|
|
|
$x10 = new X10::ActiveHome( port=>$Config{ZM_X10_DEVICE}, house_code=>$Config{ZM_X10_HOUSE_CODE}, debug=>0 );
|
2015-04-16 13:39:34 +08:00
|
|
|
|
|
|
|
loadTasks();
|
|
|
|
|
|
|
|
$x10->register_listener( \&x10listen );
|
|
|
|
|
|
|
|
my $rin = '';
|
|
|
|
vec( $rin, fileno(SERVER),1) = 1;
|
|
|
|
vec( $rin, $x10->select_fds(),1) = 1;
|
|
|
|
my $timeout = 0.2;
|
|
|
|
#print( "F:".fileno(SERVER)."\n" );
|
|
|
|
my $reload = undef;
|
|
|
|
my $reload_count = 0;
|
2015-05-27 22:00:24 +08:00
|
|
|
my $reload_limit = $Config{ZM_X10_DB_RELOAD_INTERVAL} / $timeout;
|
2015-04-16 13:39:34 +08:00
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
my $nfound = select( my $rout = $rin, undef, undef, $timeout );
|
|
|
|
#print( "Off select, NF:$nfound, ER:$!\n" );
|
|
|
|
#print( vec( $rout, fileno(SERVER),1)."\n" );
|
|
|
|
#print( vec( $rout, $x10->select_fds(),1)."\n" );
|
|
|
|
if ( $nfound > 0 )
|
|
|
|
{
|
|
|
|
if ( vec( $rout, fileno(SERVER),1) )
|
|
|
|
{
|
|
|
|
my $paddr = accept( CLIENT, SERVER );
|
|
|
|
my $message = <CLIENT>;
|
|
|
|
|
|
|
|
my ( $command, $unit_code ) = split( /;/, $message );
|
|
|
|
|
|
|
|
my $device;
|
|
|
|
if ( defined($unit_code) )
|
|
|
|
{
|
|
|
|
if ( $unit_code < 1 || $unit_code > 16 )
|
|
|
|
{
|
|
|
|
dPrint( ZoneMinder::Logger::ERROR, "Invalid unit code '$unit_code'\n" );
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
$device = $device_hash{$unit_code};
|
|
|
|
if ( !$device )
|
|
|
|
{
|
|
|
|
$device = $device_hash{$unit_code} = { appliance=>$x10->Appliance( unit_code=>$unit_code ),
|
|
|
|
status=>'unknown'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my $result;
|
|
|
|
if ( $command eq 'on' )
|
|
|
|
{
|
|
|
|
$result = $device->{appliance}->on();
|
|
|
|
}
|
|
|
|
elsif ( $command eq 'off' )
|
|
|
|
{
|
|
|
|
$result = $device->{appliance}->off();
|
|
|
|
}
|
|
|
|
#elsif ( $command eq 'dim' )
|
|
|
|
#{
|
|
|
|
#$result = $device->{appliance}->dim();
|
|
|
|
#}
|
|
|
|
#elsif ( $command eq 'bright' )
|
|
|
|
#{
|
|
|
|
#$result = $device->{appliance}->bright();
|
|
|
|
#}
|
|
|
|
elsif ( $command eq 'status' )
|
|
|
|
{
|
|
|
|
if ( $device )
|
|
|
|
{
|
|
|
|
dPrint( ZoneMinder::Logger::DEBUG, $unit_code." ".$device->{status}."\n" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach my $unit_code ( sort( keys(%device_hash) ) )
|
|
|
|
{
|
|
|
|
my $device = $device_hash{$unit_code};
|
|
|
|
dPrint( ZoneMinder::Logger::DEBUG, $unit_code." ".$device->{status}."\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ( $command eq 'shutdown' )
|
|
|
|
{
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dPrint( ZoneMinder::Logger::ERROR, "Invalid command '$command'\n" );
|
|
|
|
}
|
|
|
|
if ( defined($result) )
|
|
|
|
{
|
|
|
|
if ( 1 || $result )
|
|
|
|
{
|
|
|
|
$device->{status} = uc($command);
|
|
|
|
dPrint( ZoneMinder::Logger::DEBUG, $device->{appliance}->address()." $command, ok\n" );
|
|
|
|
#x10listen( new X10::Event( sprintf("%s %s", $device->{appliance}->address, uc($command) ) ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dPrint( ZoneMinder::Logger::ERROR, $device->{appliance}->address()." $command, failed\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close( CLIENT );
|
|
|
|
}
|
|
|
|
elsif ( vec( $rout, $x10->select_fds(),1) )
|
|
|
|
{
|
|
|
|
$x10->handle_input();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Fatal( "Bogus descriptor" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ( $nfound < 0 )
|
|
|
|
{
|
|
|
|
if ( $! != EINTR )
|
|
|
|
{
|
|
|
|
Fatal( "Can't select: $!" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#print( "Select timed out\n" );
|
|
|
|
# Check for state changes
|
|
|
|
foreach my $monitor_id ( sort(keys(%monitor_hash) ) )
|
|
|
|
{
|
|
|
|
my $monitor = $monitor_hash{$monitor_id};
|
|
|
|
my $state = zmGetMonitorState( $monitor );
|
|
|
|
if ( !defined($state) )
|
|
|
|
{
|
|
|
|
$reload = !undef;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if ( defined( $monitor->{LastState} ) )
|
|
|
|
{
|
|
|
|
my $task_list;
|
|
|
|
if ( ($state == STATE_ALARM || $state == STATE_ALERT)
|
|
|
|
&& ($monitor->{LastState} == STATE_IDLE || $monitor->{LastState} == STATE_TAPE)
|
|
|
|
) # Gone into alarm state
|
|
|
|
{
|
|
|
|
Debug( "Applying ON_list for $monitor_id\n" );
|
|
|
|
$task_list = $monitor->{"ON_list"};
|
|
|
|
}
|
|
|
|
elsif ( ($state == STATE_IDLE && $monitor->{LastState} != STATE_IDLE)
|
|
|
|
|| ($state == STATE_TAPE && $monitor->{LastState} != STATE_TAPE)
|
|
|
|
) # Come out of alarm state
|
|
|
|
{
|
|
|
|
Debug( "Applying OFF_list for $monitor_id\n" );
|
|
|
|
$task_list = $monitor->{"OFF_list"};
|
|
|
|
}
|
|
|
|
if ( $task_list )
|
|
|
|
{
|
|
|
|
foreach my $task ( @$task_list )
|
|
|
|
{
|
|
|
|
processTask( $task );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$monitor->{LastState} = $state;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check for pending tasks
|
|
|
|
my $now = time();
|
|
|
|
foreach my $activation_time ( sort(keys(%pending_tasks) ) )
|
|
|
|
{
|
|
|
|
last if ( $activation_time > $now );
|
|
|
|
my $pending_list = $pending_tasks{$activation_time};
|
|
|
|
foreach my $task ( @$pending_list )
|
|
|
|
{
|
|
|
|
processTask( $task );
|
|
|
|
}
|
|
|
|
delete( $pending_tasks{$activation_time} );
|
|
|
|
}
|
|
|
|
if ( $reload || ++$reload_count >= $reload_limit )
|
|
|
|
{
|
|
|
|
loadTasks();
|
|
|
|
$reload = undef;
|
|
|
|
$reload_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Info( "X10 server exiting\n" );
|
|
|
|
close( SERVER );
|
|
|
|
exit();
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sub addToDeviceList
|
|
|
|
{
|
2015-04-16 13:39:34 +08:00
|
|
|
my $unit_code = shift;
|
|
|
|
my $event = shift;
|
|
|
|
my $monitor = shift;
|
|
|
|
my $function = shift;
|
|
|
|
my $limit = shift;
|
|
|
|
|
|
|
|
Debug( "Adding to device list, uc:$unit_code, ev:$event, mo:"
|
|
|
|
.$monitor->{Id}.", fu:$function, li:$limit\n"
|
|
|
|
);
|
|
|
|
my $device = $device_hash{$unit_code};
|
|
|
|
if ( !$device )
|
|
|
|
{
|
|
|
|
$device = $device_hash{$unit_code} = { appliance=>$x10->Appliance( unit_code=>$unit_code ),
|
|
|
|
status=>'unknown'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
my $task = { type=>"device",
|
|
|
|
monitor=>$monitor,
|
|
|
|
address=>$device->{appliance}->address(),
|
|
|
|
function=>$function
|
|
|
|
};
|
|
|
|
if ( $limit )
|
|
|
|
{
|
|
|
|
$task->{limit} = $limit
|
|
|
|
}
|
|
|
|
|
|
|
|
my $task_list = $device->{$event."_list"};
|
|
|
|
if ( !$task_list )
|
|
|
|
{
|
|
|
|
$task_list = $device->{$event."_list"} = [];
|
|
|
|
}
|
|
|
|
push( @$task_list, $task );
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sub addToMonitorList
|
|
|
|
{
|
2015-04-16 13:39:34 +08:00
|
|
|
my $monitor = shift;
|
|
|
|
my $event = shift;
|
|
|
|
my $unit_code = shift;
|
|
|
|
my $function = shift;
|
|
|
|
my $limit = shift;
|
|
|
|
|
|
|
|
Debug( "Adding to monitor list, uc:$unit_code, ev:$event, mo:".$monitor->{Id}
|
|
|
|
.", fu:$function, li:$limit\n"
|
|
|
|
);
|
|
|
|
my $device = $device_hash{$unit_code};
|
|
|
|
if ( !$device )
|
|
|
|
{
|
|
|
|
$device = $device_hash{$unit_code} = { appliance=>$x10->Appliance( unit_code=>$unit_code ),
|
|
|
|
status=>'unknown'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
my $task = { type=>"monitor",
|
|
|
|
device=>$device,
|
|
|
|
id=>$monitor->{Id},
|
|
|
|
function=>$function
|
|
|
|
};
|
|
|
|
if ( $limit )
|
|
|
|
{
|
|
|
|
$task->{limit} = $limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $task_list = $monitor->{$event."_list"};
|
|
|
|
if ( !$task_list )
|
|
|
|
{
|
|
|
|
$task_list = $monitor->{$event."_list"} = [];
|
|
|
|
}
|
|
|
|
push( @$task_list, $task );
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sub loadTasks
|
|
|
|
{
|
2015-04-16 13:39:34 +08:00
|
|
|
%monitor_hash = ();
|
|
|
|
|
|
|
|
Debug( "Loading tasks\n" );
|
|
|
|
# Clear out all old device task lists
|
|
|
|
foreach my $unit_code ( sort( keys(%device_hash) ) )
|
|
|
|
{
|
|
|
|
my $device = $device_hash{$unit_code};
|
|
|
|
$device->{ON_list} = [];
|
|
|
|
$device->{OFF_list} = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
my $sql = "SELECT M.*,T.* from Monitors as M
|
|
|
|
INNER JOIN TriggersX10 as T on (M.Id = T.MonitorId)
|
|
|
|
WHERE find_in_set( M.Function, 'Modect,Record,Mocord,Nodect' )
|
|
|
|
AND M.Enabled = 1
|
|
|
|
AND find_IN_set( 'X10', M.Triggers )"
|
|
|
|
;
|
|
|
|
my $sth = $dbh->prepare_cached( $sql )
|
|
|
|
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
|
|
|
|
my $res = $sth->execute()
|
|
|
|
or Fatal( "Can't execute: ".$sth->errstr() );
|
|
|
|
while( my $monitor = $sth->fetchrow_hashref() )
|
|
|
|
{
|
|
|
|
next if ( !zmMemVerify( $monitor ) ); # Check shared memory ok
|
|
|
|
|
|
|
|
$monitor_hash{$monitor->{Id}} = $monitor;
|
|
|
|
|
|
|
|
if ( $monitor->{Activation} )
|
|
|
|
{
|
|
|
|
Debug( "$monitor->{Name} has active string '$monitor->{Activation}'\n" );
|
|
|
|
foreach my $code_string ( split( /,/, $monitor->{Activation} ) )
|
|
|
|
{
|
|
|
|
#Debug( "Code string: $code_string\n" );
|
|
|
|
my ( $invert, $unit_code, $modifier, $limit )
|
|
|
|
= ( $code_string =~ /^([!~])?(\d+)(?:([+-])(\d+)?)?$/ );
|
|
|
|
$limit = 0 if ( !$limit );
|
|
|
|
if ( $unit_code )
|
|
|
|
{
|
|
|
|
if ( !$modifier || $modifier eq '+' )
|
|
|
|
{
|
|
|
|
addToDeviceList( $unit_code,
|
|
|
|
"ON",
|
|
|
|
$monitor,
|
|
|
|
!$invert ? "start_active"
|
|
|
|
: "stop_active",
|
|
|
|
$limit
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( !$modifier || $modifier eq '-' )
|
|
|
|
{
|
|
|
|
addToDeviceList( $unit_code,
|
|
|
|
"OFF",
|
|
|
|
$monitor,
|
|
|
|
!$invert ? "stop_active"
|
|
|
|
: "start_active",
|
|
|
|
$limit
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $monitor->{AlarmInput} )
|
|
|
|
{
|
|
|
|
Debug( "$monitor->{Name} has alarm input string '$monitor->{AlarmInput}'\n" );
|
|
|
|
foreach my $code_string ( split( /,/, $monitor->{AlarmInput} ) )
|
|
|
|
{
|
|
|
|
#Debug( "Code string: $code_string\n" );
|
|
|
|
my ( $invert, $unit_code, $modifier, $limit )
|
|
|
|
= ( $code_string =~ /^([!~])?(\d+)(?:([+-])(\d+)?)?$/ );
|
|
|
|
$limit = 0 if ( !$limit );
|
|
|
|
if ( $unit_code )
|
|
|
|
{
|
|
|
|
if ( !$modifier || $modifier eq '+' )
|
|
|
|
{
|
|
|
|
addToDeviceList( $unit_code,
|
|
|
|
"ON",
|
|
|
|
$monitor,
|
|
|
|
!$invert ? "start_alarm"
|
|
|
|
: "stop_alarm",
|
|
|
|
$limit
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( !$modifier || $modifier eq '-' )
|
|
|
|
{
|
|
|
|
addToDeviceList( $unit_code,
|
|
|
|
"OFF",
|
|
|
|
$monitor,
|
|
|
|
!$invert ? "stop_alarm"
|
|
|
|
: "start_alarm",
|
|
|
|
$limit
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $monitor->{AlarmOutput} )
|
|
|
|
{
|
|
|
|
Debug( "$monitor->{Name} has alarm output string '$monitor->{AlarmOutput}'\n" );
|
|
|
|
foreach my $code_string ( split( /,/, $monitor->{AlarmOutput} ) )
|
|
|
|
{
|
|
|
|
#Debug( "Code string: $code_string\n" );
|
|
|
|
my ( $invert, $unit_code, $modifier, $limit )
|
|
|
|
= ( $code_string =~ /^([!~])?(\d+)(?:([+-])(\d+)?)?$/ );
|
|
|
|
$limit = 0 if ( !$limit );
|
|
|
|
if ( $unit_code )
|
|
|
|
{
|
|
|
|
if ( !$modifier || $modifier eq '+' )
|
|
|
|
{
|
|
|
|
addToMonitorList( $monitor,
|
|
|
|
"ON",
|
|
|
|
$unit_code,
|
|
|
|
!$invert ? "on"
|
|
|
|
: "off",
|
|
|
|
$limit
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( !$modifier || $modifier eq '-' )
|
|
|
|
{
|
|
|
|
addToMonitorList( $monitor,
|
|
|
|
"OFF",
|
|
|
|
$unit_code,
|
|
|
|
!$invert ? "off"
|
|
|
|
: "on",
|
|
|
|
$limit
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-25 16:27:46 +08:00
|
|
|
zmMemInvalidate( $monitor );
|
2015-04-16 13:39:34 +08:00
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sub addPendingTask
|
|
|
|
{
|
2015-04-16 13:39:34 +08:00
|
|
|
my $task = shift;
|
|
|
|
|
|
|
|
# Check whether we are just extending a previous pending task
|
|
|
|
# and remove it if it's there
|
|
|
|
foreach my $activation_time ( sort(keys(%pending_tasks) ) )
|
|
|
|
{
|
|
|
|
my $pending_list = $pending_tasks{$activation_time};
|
|
|
|
my $new_pending_list = [];
|
|
|
|
foreach my $pending_task ( @$pending_list )
|
|
|
|
{
|
|
|
|
if ( $task->{type} ne $pending_task->{type} )
|
|
|
|
{
|
|
|
|
push( @$new_pending_list, $pending_task )
|
|
|
|
}
|
|
|
|
elsif ( $task->{type} eq "device" )
|
|
|
|
{
|
|
|
|
if (( $task->{monitor}->{Id} != $pending_task->{monitor}->{Id} )
|
|
|
|
|| ( $task->{function} ne $pending_task->{function} ))
|
|
|
|
{
|
|
|
|
push( @$new_pending_list, $pending_task )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ( $task->{type} eq "monitor" )
|
|
|
|
{
|
|
|
|
if (( $task->{device}->{appliance}->unit_code()
|
|
|
|
!= $pending_task->{device}->{appliance}->unit_code()
|
|
|
|
)
|
|
|
|
|| ( $task->{function} ne $pending_task->{function} )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
push( @$new_pending_list, $pending_task )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( @$new_pending_list )
|
|
|
|
{
|
|
|
|
$pending_tasks{$activation_time} = $new_pending_list;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete( $pending_tasks{$activation_time} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my $end_time = time() + $task->{limit};
|
|
|
|
my $pending_list = $pending_tasks{$end_time};
|
|
|
|
if ( !$pending_list )
|
|
|
|
{
|
|
|
|
$pending_list = $pending_tasks{$end_time} = [];
|
|
|
|
}
|
|
|
|
my $pending_task;
|
|
|
|
if ( $task->{type} eq "device" )
|
|
|
|
{
|
|
|
|
$pending_task = { type=>$task->{type},
|
|
|
|
monitor=>$task->{monitor},
|
|
|
|
function=>$task->{function}
|
|
|
|
};
|
|
|
|
$pending_task->{function} =~ s/start/stop/;
|
|
|
|
}
|
|
|
|
elsif ( $task->{type} eq "monitor" )
|
|
|
|
{
|
|
|
|
$pending_task = { type=>$task->{type},
|
|
|
|
device=>$task->{device},
|
|
|
|
function=>$task->{function}
|
|
|
|
};
|
|
|
|
$pending_task->{function} =~ s/on/off/;
|
|
|
|
}
|
|
|
|
push( @$pending_list, $pending_task );
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sub processTask
|
|
|
|
{
|
2015-04-16 13:39:34 +08:00
|
|
|
my $task = shift;
|
|
|
|
|
|
|
|
if ( $task->{type} eq "device" )
|
|
|
|
{
|
|
|
|
my ( $instruction, $class ) = ( $task->{function} =~ /^(.+)_(.+)$/ );
|
|
|
|
|
|
|
|
if ( $class eq "active" )
|
|
|
|
{
|
|
|
|
if ( $instruction eq "start" )
|
|
|
|
{
|
|
|
|
zmMonitorEnable( $task->{monitor} );
|
|
|
|
if ( $task->{limit} )
|
|
|
|
{
|
|
|
|
addPendingTask( $task );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif( $instruction eq "stop" )
|
|
|
|
{
|
|
|
|
zmMonitorDisable( $task->{monitor} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif( $class eq "alarm" )
|
|
|
|
{
|
|
|
|
if ( $instruction eq "start" )
|
|
|
|
{
|
|
|
|
zmTriggerEventOn( $task->{monitor},
|
|
|
|
0,
|
|
|
|
main::CAUSE_STRING,
|
|
|
|
$task->{address}
|
|
|
|
);
|
|
|
|
if ( $task->{limit} )
|
|
|
|
{
|
|
|
|
addPendingTask( $task );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif( $instruction eq "stop" )
|
|
|
|
{
|
|
|
|
zmTriggerEventCancel( $task->{monitor} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif( $task->{type} eq "monitor" )
|
|
|
|
{
|
|
|
|
if ( $task->{function} eq "on" )
|
|
|
|
{
|
|
|
|
$task->{device}->{appliance}->on();
|
|
|
|
if ( $task->{limit} )
|
|
|
|
{
|
|
|
|
addPendingTask( $task );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ( $task->{function} eq "off" )
|
|
|
|
{
|
|
|
|
$task->{device}->{appliance}->off();
|
|
|
|
}
|
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
2005-12-23 19:32:23 +08:00
|
|
|
sub dPrint
|
2005-12-16 18:05:29 +08:00
|
|
|
{
|
2015-04-16 13:39:34 +08:00
|
|
|
my $dbg_level = shift;
|
|
|
|
if ( fileno(CLIENT) )
|
|
|
|
{
|
|
|
|
print CLIENT @_
|
|
|
|
}
|
|
|
|
if ( $dbg_level == ZoneMinder::Logger::DEBUG )
|
|
|
|
{
|
|
|
|
Debug( @_ );
|
|
|
|
}
|
|
|
|
elsif ( $dbg_level == ZoneMinder::Logger::INFO )
|
|
|
|
{
|
|
|
|
Info( @_ );
|
|
|
|
}
|
|
|
|
elsif ( $dbg_level == ZoneMinder::Logger::WARNING )
|
|
|
|
{
|
|
|
|
Warning( @_ );
|
|
|
|
}
|
|
|
|
elsif ( $dbg_level == ZoneMinder::Logger::ERROR )
|
|
|
|
{
|
|
|
|
Error( @_ );
|
|
|
|
}
|
|
|
|
elsif ( $dbg_level == ZoneMinder::Logger::FATAL )
|
|
|
|
{
|
|
|
|
Fatal( @_ );
|
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sub x10listen
|
|
|
|
{
|
2015-04-16 13:39:34 +08:00
|
|
|
foreach my $event ( @_ )
|
|
|
|
{
|
|
|
|
#print( Data::Dumper( $_ )."\n" );
|
2015-05-27 22:00:24 +08:00
|
|
|
if ( $event->house_code() eq $Config{ZM_X10_HOUSE_CODE} )
|
2015-04-16 13:39:34 +08:00
|
|
|
{
|
|
|
|
my $unit_code = $event->unit_code();
|
|
|
|
my $device = $device_hash{$unit_code};
|
|
|
|
if ( !$device )
|
|
|
|
{
|
|
|
|
$device = $device_hash{$unit_code} = { appliance=>$x10->Appliance( unit_code=>$unit_code ),
|
|
|
|
status=>'unknown'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
next if ( $event->func() !~ /(?:ON|OFF)/ );
|
|
|
|
$device->{status} = $event->func();
|
|
|
|
my $task_list = $device->{$event->func()."_list"};
|
|
|
|
if ( $task_list )
|
|
|
|
{
|
|
|
|
foreach my $task ( @$task_list )
|
|
|
|
{
|
|
|
|
processTask( $task );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Info( "Got event - ".$event->as_string()."\n" );
|
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|