Code style updates. Quote the word Function in SQL for newer mysql
This commit is contained in:
parent
285866a681
commit
0323925406
|
@ -93,12 +93,11 @@ if ( $version ) {
|
|||
exit(0);
|
||||
}
|
||||
|
||||
die( 'No command given' ) unless( $command );
|
||||
die( 'No unit code given' )
|
||||
die 'No command given' unless $command;
|
||||
die 'No unit code given'
|
||||
unless( $unit_code || ($command =~ /(?:start|status|shutdown)/) );
|
||||
|
||||
if ( $command eq 'start' )
|
||||
{
|
||||
if ( $command eq 'start' ) {
|
||||
X10Server::runServer();
|
||||
exit();
|
||||
}
|
||||
|
@ -108,14 +107,12 @@ socket( CLIENT, PF_UNIX, SOCK_STREAM, 0 )
|
|||
|
||||
my $saddr = sockaddr_un(SOCK_FILE);
|
||||
|
||||
if ( !connect( CLIENT, $saddr ) )
|
||||
{
|
||||
if ( !connect(CLIENT, $saddr) ) {
|
||||
# The server isn't there
|
||||
print("Unable to connect, starting server\n");
|
||||
close(CLIENT);
|
||||
|
||||
if ( my $cpid = fork() )
|
||||
{
|
||||
if ( my $cpid = fork() ) {
|
||||
# Parent process just sleep and fall through
|
||||
sleep(2);
|
||||
logReinit();
|
||||
|
@ -123,28 +120,23 @@ if ( !connect( CLIENT, $saddr ) )
|
|||
or Fatal("Can't open socket: $!");
|
||||
connect(CLIENT, $saddr)
|
||||
or Fatal("Can't connect: $!");
|
||||
}
|
||||
elsif ( defined($cpid) )
|
||||
{
|
||||
} elsif ( defined($cpid) ) {
|
||||
setpgrp();
|
||||
|
||||
logReinit();
|
||||
X10Server::runServer();
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
Fatal("Can't fork: $!");
|
||||
}
|
||||
}
|
||||
# The server is there, connect to it
|
||||
#print( "Writing commands\n" );
|
||||
CLIENT->autoflush();
|
||||
my $message = "$command";
|
||||
$message .= ";$unit_code" if ( $unit_code );
|
||||
my $message = $command;
|
||||
$message .= ';'.$unit_code if $unit_code;
|
||||
print(CLIENT $message);
|
||||
shutdown(CLIENT, 1);
|
||||
while ( my $line = <CLIENT> )
|
||||
{
|
||||
while ( my $line = <CLIENT> ) {
|
||||
chomp($line);
|
||||
print("$line\n");
|
||||
}
|
||||
|
@ -178,9 +170,8 @@ our %monitor_hash;
|
|||
our %device_hash;
|
||||
our %pending_tasks;
|
||||
|
||||
sub runServer
|
||||
{
|
||||
Info( "X10 server starting\n" );
|
||||
sub runServer {
|
||||
Info('X10 server starting');
|
||||
|
||||
socket(SERVER, PF_UNIX, SOCK_STREAM, 0)
|
||||
or Fatal("Can't open socket: $!");
|
||||
|
@ -191,7 +182,11 @@ sub runServer
|
|||
|
||||
$dbh = zmDbConnect();
|
||||
|
||||
$x10 = new X10::ActiveHome( port=>$Config{ZM_X10_DEVICE}, house_code=>$Config{ZM_X10_HOUSE_CODE}, debug=>0 );
|
||||
$x10 = new X10::ActiveHome(
|
||||
port=>$Config{ZM_X10_DEVICE},
|
||||
house_code=>$Config{ZM_X10_HOUSE_CODE},
|
||||
debug=>0
|
||||
);
|
||||
|
||||
loadTasks();
|
||||
|
||||
|
@ -205,46 +200,38 @@ sub runServer
|
|||
my $reload = undef;
|
||||
my $reload_count = 0;
|
||||
my $reload_limit = $Config{ZM_X10_DB_RELOAD_INTERVAL} / $timeout;
|
||||
while( 1 )
|
||||
{
|
||||
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) )
|
||||
{
|
||||
if ( $nfound > 0 ) {
|
||||
if ( vec($rout, fileno(SERVER),1) ) {
|
||||
my $paddr = accept(CLIENT, SERVER);
|
||||
my $message = <CLIENT>;
|
||||
|
||||
my ( $command, $unit_code ) = split( /;/, $message );
|
||||
my ($command, $unit_code) = split(';', $message);
|
||||
|
||||
my $device;
|
||||
if ( defined($unit_code) )
|
||||
{
|
||||
if ( $unit_code < 1 || $unit_code > 16 )
|
||||
{
|
||||
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 ),
|
||||
if ( !$device ) {
|
||||
$device = $device_hash{$unit_code} = {
|
||||
appliance=>$x10->Appliance(unit_code=>$unit_code),
|
||||
status=>'unknown'
|
||||
};
|
||||
}
|
||||
}
|
||||
} # end if defined($unit_code)
|
||||
|
||||
my $result;
|
||||
if ( $command eq 'on' )
|
||||
{
|
||||
if ( $command eq 'on' ) {
|
||||
$result = $device->{appliance}->on();
|
||||
}
|
||||
elsif ( $command eq 'off' )
|
||||
{
|
||||
} elsif ( $command eq 'off' ) {
|
||||
$result = $device->{appliance}->off();
|
||||
}
|
||||
#elsif ( $command eq 'dim' )
|
||||
|
@ -255,128 +242,97 @@ sub runServer
|
|||
#{
|
||||
#$result = $device->{appliance}->bright();
|
||||
#}
|
||||
elsif ( $command eq 'status' )
|
||||
{
|
||||
if ( $device )
|
||||
{
|
||||
elsif ( $command eq 'status' ) {
|
||||
if ( $device ) {
|
||||
dPrint(ZoneMinder::Logger::DEBUG, $unit_code.' '.$device->{status}."\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach my $unit_code ( sort( keys(%device_hash) ) )
|
||||
{
|
||||
} 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' )
|
||||
{
|
||||
} elsif ( $command eq 'shutdown' ) {
|
||||
last;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
dPrint(ZoneMinder::Logger::ERROR, "Invalid command '$command'\n");
|
||||
}
|
||||
if ( defined($result) )
|
||||
{
|
||||
if ( 1 || $result )
|
||||
{
|
||||
if ( defined($result) ) {
|
||||
# FIXME
|
||||
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
|
||||
{
|
||||
} else {
|
||||
dPrint(ZoneMinder::Logger::ERROR, $device->{appliance}->address()." $command, failed\n");
|
||||
}
|
||||
}
|
||||
} # end if defined result
|
||||
close(CLIENT);
|
||||
}
|
||||
elsif ( vec( $rout, $x10->select_fds(),1) )
|
||||
{
|
||||
} elsif ( vec($rout, $x10->select_fds(),1) ) {
|
||||
$x10->handle_input();
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
Fatal('Bogus descriptor');
|
||||
}
|
||||
}
|
||||
elsif ( $nfound < 0 )
|
||||
{
|
||||
if ( $! != EINTR )
|
||||
{
|
||||
} elsif ( $nfound < 0 ) {
|
||||
if ( $! != EINTR ) {
|
||||
Fatal("Can't select: $!");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
#print( "Select timed out\n" );
|
||||
# Check for state changes
|
||||
foreach my $monitor_id ( sort(keys(%monitor_hash) ) )
|
||||
{
|
||||
foreach my $monitor_id ( sort(keys(%monitor_hash) ) ) {
|
||||
my $monitor = $monitor_hash{$monitor_id};
|
||||
my $state = zmGetMonitorState($monitor);
|
||||
if ( !defined($state) )
|
||||
{
|
||||
if ( !defined($state) ) {
|
||||
$reload = !undef;
|
||||
next;
|
||||
}
|
||||
if ( defined( $monitor->{LastState} ) )
|
||||
{
|
||||
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)
|
||||
Debug("Applying ON_list for $monitor_id");
|
||||
$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'};
|
||||
Debug("Applying OFF_list for $monitor_id");
|
||||
$task_list = $monitor->{OFF_list};
|
||||
}
|
||||
if ( $task_list )
|
||||
{
|
||||
foreach my $task ( @$task_list )
|
||||
{
|
||||
if ( $task_list ) {
|
||||
foreach my $task ( @$task_list ) {
|
||||
processTask($task);
|
||||
}
|
||||
}
|
||||
}
|
||||
} # end if defined laststate
|
||||
$monitor->{LastState} = $state;
|
||||
}
|
||||
} # end foreach monitor
|
||||
|
||||
# Check for pending tasks
|
||||
my $now = time();
|
||||
foreach my $activation_time ( sort(keys(%pending_tasks) ) )
|
||||
{
|
||||
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 )
|
||||
{
|
||||
foreach my $task ( @$pending_list ) {
|
||||
processTask($task);
|
||||
}
|
||||
delete( $pending_tasks{$activation_time} );
|
||||
delete $pending_tasks{$activation_time};
|
||||
}
|
||||
if ( $reload || ++$reload_count >= $reload_limit )
|
||||
{
|
||||
if ( $reload or (++$reload_count >= $reload_limit) ) {
|
||||
loadTasks();
|
||||
$reload = undef;
|
||||
$reload_count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Info( "X10 server exiting\n" );
|
||||
Info("X10 server exiting");
|
||||
close(SERVER);
|
||||
exit();
|
||||
}
|
||||
|
||||
sub addToDeviceList
|
||||
{
|
||||
sub addToDeviceList {
|
||||
my $unit_code = shift;
|
||||
my $event = shift;
|
||||
my $monitor = shift;
|
||||
|
@ -384,36 +340,35 @@ sub addToDeviceList
|
|||
my $limit = shift;
|
||||
|
||||
Debug("Adding to device list, uc:$unit_code, ev:$event, mo:"
|
||||
.$monitor->{Id}.", fu:$function, li:$limit\n"
|
||||
.$monitor->{Id}.", fu:$function, li:$limit"
|
||||
);
|
||||
my $device = $device_hash{$unit_code};
|
||||
if ( !$device )
|
||||
{
|
||||
$device = $device_hash{$unit_code} = { appliance=>$x10->Appliance( unit_code=>$unit_code ),
|
||||
if ( !$device ) {
|
||||
$device = $device_hash{$unit_code} = {
|
||||
appliance=>$x10->Appliance(unit_code=>$unit_code),
|
||||
status=>'unknown'
|
||||
};
|
||||
}
|
||||
|
||||
my $task = { type=>'device',
|
||||
my $task = {
|
||||
type=>'device',
|
||||
monitor=>$monitor,
|
||||
address=>$device->{appliance}->address(),
|
||||
function=>$function
|
||||
};
|
||||
if ( $limit )
|
||||
{
|
||||
|
||||
if ( $limit ) {
|
||||
$task->{limit} = $limit
|
||||
}
|
||||
|
||||
my $task_list = $device->{$event.'_list'};
|
||||
if ( !$task_list )
|
||||
{
|
||||
if ( !$task_list ) {
|
||||
$task_list = $device->{$event.'_list'} = [];
|
||||
}
|
||||
push( @$task_list, $task );
|
||||
}
|
||||
push @$task_list, $task;
|
||||
} # end sub addToDeviceList
|
||||
|
||||
sub addToMonitorList
|
||||
{
|
||||
sub addToMonitorList {
|
||||
my $monitor = shift;
|
||||
my $event = shift;
|
||||
my $unit_code = shift;
|
||||
|
@ -421,59 +376,54 @@ sub addToMonitorList
|
|||
my $limit = shift;
|
||||
|
||||
Debug("Adding to monitor list, uc:$unit_code, ev:$event, mo:".$monitor->{Id}
|
||||
.", fu:$function, li:$limit\n"
|
||||
.", fu:$function, li:$limit"
|
||||
);
|
||||
my $device = $device_hash{$unit_code};
|
||||
if ( !$device )
|
||||
{
|
||||
$device = $device_hash{$unit_code} = { appliance=>$x10->Appliance( unit_code=>$unit_code ),
|
||||
if ( !$device ) {
|
||||
$device = $device_hash{$unit_code} = {
|
||||
appliance=>$x10->Appliance(unit_code=>$unit_code),
|
||||
status=>'unknown'
|
||||
};
|
||||
}
|
||||
|
||||
my $task = { type=>'monitor',
|
||||
my $task = {
|
||||
type=>'monitor',
|
||||
device=>$device,
|
||||
id=>$monitor->{Id},
|
||||
function=>$function
|
||||
};
|
||||
if ( $limit )
|
||||
{
|
||||
if ( $limit ) {
|
||||
$task->{limit} = $limit;
|
||||
}
|
||||
|
||||
my $task_list = $monitor->{$event.'_list'};
|
||||
if ( !$task_list )
|
||||
{
|
||||
if ( !$task_list ) {
|
||||
$task_list = $monitor->{$event.'_list'} = [];
|
||||
}
|
||||
push( @$task_list, $task );
|
||||
}
|
||||
push @$task_list, $task;
|
||||
} # end sub addToMonitorList
|
||||
|
||||
sub loadTasks
|
||||
{
|
||||
sub loadTasks {
|
||||
%monitor_hash = ();
|
||||
|
||||
Debug( "Loading tasks\n" );
|
||||
Debug('Loading tasks');
|
||||
# Clear out all old device task lists
|
||||
foreach my $unit_code ( sort( keys(%device_hash) ) )
|
||||
{
|
||||
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
|
||||
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 )"
|
||||
;
|
||||
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() )
|
||||
{
|
||||
while( my $monitor = $sth->fetchrow_hashref() ) {
|
||||
# Check shared memory ok
|
||||
if ( !zmMemVerify($monitor) ) {
|
||||
zmMemInvalidate($monitor);
|
||||
|
@ -482,301 +432,235 @@ sub loadTasks
|
|||
|
||||
$monitor_hash{$monitor->{Id}} = $monitor;
|
||||
|
||||
if ( $monitor->{Activation} )
|
||||
{
|
||||
Debug( "$monitor->{Name} has active string '$monitor->{Activation}'\n" );
|
||||
foreach my $code_string ( split( /,/, $monitor->{Activation} ) )
|
||||
{
|
||||
if ( $monitor->{Activation} ) {
|
||||
Debug("$monitor->{Name} has active string '$monitor->{Activation}'");
|
||||
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 '+' )
|
||||
{
|
||||
$limit = 0 if !$limit;
|
||||
if ( $unit_code ) {
|
||||
if ( !$modifier || $modifier eq '+' ) {
|
||||
addToDeviceList( $unit_code,
|
||||
'ON',
|
||||
$monitor,
|
||||
!$invert ? 'start_active'
|
||||
: 'stop_active',
|
||||
(!$invert ? 'start_active' : 'stop_active'),
|
||||
$limit
|
||||
);
|
||||
}
|
||||
if ( !$modifier || $modifier eq '-' )
|
||||
{
|
||||
if ( !$modifier || $modifier eq '-' ) {
|
||||
addToDeviceList( $unit_code,
|
||||
'OFF',
|
||||
$monitor,
|
||||
!$invert ? 'stop_active'
|
||||
: 'start_active',
|
||||
(!$invert ? 'stop_active' : 'start_active'),
|
||||
$limit
|
||||
);
|
||||
}
|
||||
} # end if unit_code
|
||||
} # end foreach code_string
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( $monitor->{AlarmInput} )
|
||||
{
|
||||
Debug( "$monitor->{Name} has alarm input string '$monitor->{AlarmInput}'\n" );
|
||||
foreach my $code_string ( split( /,/, $monitor->{AlarmInput} ) )
|
||||
{
|
||||
if ( $monitor->{AlarmInput} ) {
|
||||
Debug("$monitor->{Name} has alarm input string '$monitor->{AlarmInput}'");
|
||||
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 '+' )
|
||||
{
|
||||
$limit = 0 if !$limit;
|
||||
if ( $unit_code ) {
|
||||
if ( !$modifier || $modifier eq '+' ) {
|
||||
addToDeviceList( $unit_code,
|
||||
'ON',
|
||||
$monitor,
|
||||
!$invert ? 'start_alarm'
|
||||
: 'stop_alarm',
|
||||
(!$invert ? 'start_alarm' : 'stop_alarm'),
|
||||
$limit
|
||||
);
|
||||
}
|
||||
if ( !$modifier || $modifier eq '-' )
|
||||
{
|
||||
if ( !$modifier || $modifier eq '-' ) {
|
||||
addToDeviceList( $unit_code,
|
||||
'OFF',
|
||||
$monitor,
|
||||
!$invert ? 'stop_alarm'
|
||||
: 'start_alarm',
|
||||
(!$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} ) )
|
||||
{
|
||||
} # end if unit_code
|
||||
} # end foreach code_string
|
||||
} # end if AlarmInput
|
||||
if ( $monitor->{AlarmOutput} ) {
|
||||
Debug("$monitor->{Name} has alarm output string '$monitor->{AlarmOutput}'");
|
||||
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 '+' )
|
||||
{
|
||||
$limit = 0 if !$limit;
|
||||
if ( $unit_code ) {
|
||||
if ( !$modifier || $modifier eq '+' ) {
|
||||
addToMonitorList( $monitor,
|
||||
'ON',
|
||||
$unit_code,
|
||||
!$invert ? 'on'
|
||||
: 'off',
|
||||
(!$invert ? 'on' : 'off'),
|
||||
$limit
|
||||
);
|
||||
}
|
||||
if ( !$modifier || $modifier eq '-' )
|
||||
{
|
||||
if ( !$modifier || $modifier eq '-' ) {
|
||||
addToMonitorList( $monitor,
|
||||
'OFF',
|
||||
$unit_code,
|
||||
!$invert ? 'off'
|
||||
: 'on',
|
||||
(!$invert ? 'off' : 'on'),
|
||||
$limit
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} # end if unit_code
|
||||
} # end foreach code_string
|
||||
} # end if AlarmOutput
|
||||
zmMemInvalidate($monitor);
|
||||
}
|
||||
}
|
||||
} # end sub loadTasks
|
||||
|
||||
sub addPendingTask
|
||||
{
|
||||
sub addPendingTask {
|
||||
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) ) )
|
||||
{
|
||||
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} )
|
||||
{
|
||||
foreach my $pending_task ( @$pending_list ) {
|
||||
if ( $task->{type} ne $pending_task->{type} ) {
|
||||
push( @$new_pending_list, $pending_task )
|
||||
}
|
||||
elsif ( $task->{type} eq 'device' )
|
||||
{
|
||||
} 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 )
|
||||
push @$new_pending_list, $pending_task;
|
||||
}
|
||||
}
|
||||
elsif ( $task->{type} eq 'monitor' )
|
||||
{
|
||||
} 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 )
|
||||
) {
|
||||
push @$new_pending_list, $pending_task;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( @$new_pending_list )
|
||||
{
|
||||
} # end switch task->type
|
||||
} # end foreach pending_task
|
||||
|
||||
if ( @$new_pending_list ) {
|
||||
$pending_tasks{$activation_time} = $new_pending_list;
|
||||
} else {
|
||||
delete $pending_tasks{$activation_time};
|
||||
}
|
||||
else
|
||||
{
|
||||
delete( $pending_tasks{$activation_time} );
|
||||
}
|
||||
}
|
||||
} # end foreach activation_time
|
||||
|
||||
my $end_time = time() + $task->{limit};
|
||||
my $pending_list = $pending_tasks{$end_time};
|
||||
if ( !$pending_list )
|
||||
{
|
||||
if ( !$pending_list ) {
|
||||
$pending_list = $pending_tasks{$end_time} = [];
|
||||
}
|
||||
my $pending_task;
|
||||
if ( $task->{type} eq 'device' )
|
||||
{
|
||||
$pending_task = { type=>$task->{type},
|
||||
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},
|
||||
} 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 );
|
||||
}
|
||||
push @$pending_list, $pending_task;
|
||||
} # end sub addPendingTask
|
||||
|
||||
sub processTask
|
||||
{
|
||||
sub processTask {
|
||||
my $task = shift;
|
||||
|
||||
if ( $task->{type} eq 'device' )
|
||||
{
|
||||
if ( $task->{type} eq 'device' ) {
|
||||
my ( $instruction, $class ) = ( $task->{function} =~ /^(.+)_(.+)$/ );
|
||||
|
||||
if ( $class eq 'active' )
|
||||
{
|
||||
if ( $instruction eq 'start' )
|
||||
{
|
||||
if ( $class eq 'active' ) {
|
||||
if ( $instruction eq 'start' ) {
|
||||
zmMonitorEnable($task->{monitor});
|
||||
if ( $task->{limit} )
|
||||
{
|
||||
if ( $task->{limit} ) {
|
||||
addPendingTask($task);
|
||||
}
|
||||
}
|
||||
elsif( $instruction eq 'stop' )
|
||||
{
|
||||
} elsif( $instruction eq 'stop' ) {
|
||||
zmMonitorDisable($task->{monitor});
|
||||
}
|
||||
}
|
||||
elsif( $class eq 'alarm' )
|
||||
{
|
||||
if ( $instruction eq 'start' )
|
||||
{
|
||||
zmTriggerEventOn( $task->{monitor},
|
||||
} elsif( $class eq 'alarm' ) {
|
||||
if ( $instruction eq 'start' ) {
|
||||
zmTriggerEventOn(
|
||||
$task->{monitor},
|
||||
0,
|
||||
main::CAUSE_STRING,
|
||||
$task->{address}
|
||||
);
|
||||
if ( $task->{limit} )
|
||||
{
|
||||
if ( $task->{limit} ) {
|
||||
addPendingTask($task);
|
||||
}
|
||||
}
|
||||
elsif( $instruction eq 'stop' )
|
||||
{
|
||||
} elsif( $instruction eq 'stop' ) {
|
||||
zmTriggerEventCancel($task->{monitor});
|
||||
}
|
||||
}
|
||||
}
|
||||
elsif( $task->{type} eq 'monitor' )
|
||||
{
|
||||
if ( $task->{function} eq 'on' )
|
||||
{
|
||||
} # end switch class
|
||||
} elsif( $task->{type} eq 'monitor' ) {
|
||||
if ( $task->{function} eq 'on' ) {
|
||||
$task->{device}->{appliance}->on();
|
||||
if ( $task->{limit} )
|
||||
{
|
||||
if ( $task->{limit} ) {
|
||||
addPendingTask($task);
|
||||
}
|
||||
}
|
||||
elsif ( $task->{function} eq 'off' )
|
||||
{
|
||||
} elsif ( $task->{function} eq 'off' ) {
|
||||
$task->{device}->{appliance}->off();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sub dPrint
|
||||
{
|
||||
sub dPrint {
|
||||
my $dbg_level = shift;
|
||||
if ( fileno(CLIENT) )
|
||||
{
|
||||
if ( fileno(CLIENT) ) {
|
||||
print CLIENT @_
|
||||
}
|
||||
if ( $dbg_level == ZoneMinder::Logger::DEBUG )
|
||||
{
|
||||
if ( $dbg_level == ZoneMinder::Logger::DEBUG ) {
|
||||
Debug(@_);
|
||||
}
|
||||
elsif ( $dbg_level == ZoneMinder::Logger::INFO )
|
||||
{
|
||||
} elsif ( $dbg_level == ZoneMinder::Logger::INFO ) {
|
||||
Info(@_);
|
||||
}
|
||||
elsif ( $dbg_level == ZoneMinder::Logger::WARNING )
|
||||
{
|
||||
} elsif ( $dbg_level == ZoneMinder::Logger::WARNING ) {
|
||||
Warning(@_);
|
||||
}
|
||||
elsif ( $dbg_level == ZoneMinder::Logger::ERROR )
|
||||
{
|
||||
elsif ( $dbg_level == ZoneMinder::Logger::ERROR ) {
|
||||
Error( @_ );
|
||||
}
|
||||
elsif ( $dbg_level == ZoneMinder::Logger::FATAL )
|
||||
{
|
||||
} elsif ( $dbg_level == ZoneMinder::Logger::FATAL ) {
|
||||
Fatal( @_ );
|
||||
}
|
||||
}
|
||||
|
||||
sub x10listen
|
||||
{
|
||||
foreach my $event ( @_ )
|
||||
{
|
||||
sub x10listen {
|
||||
foreach my $event ( @_ ) {
|
||||
#print( Data::Dumper( $_ )."\n" );
|
||||
if ( $event->house_code() eq $Config{ZM_X10_HOUSE_CODE} )
|
||||
{
|
||||
if ( $event->house_code() eq $Config{ZM_X10_HOUSE_CODE} ) {
|
||||
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 ),
|
||||
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 )
|
||||
{
|
||||
if ( $task_list ) {
|
||||
foreach my $task ( @$task_list ) {
|
||||
processTask($task);
|
||||
}
|
||||
}
|
||||
} # end if correct house code
|
||||
Info('Got event - '.$event->as_string());
|
||||
}
|
||||
Info( "Got event - ".$event->as_string()."\n" );
|
||||
}
|
||||
}
|
||||
} # end sub x10listen
|
||||
|
||||
1;
|
||||
__END__
|
||||
|
|
Loading…
Reference in New Issue