ode style and some more debug
This commit is contained in:
parent
7768b4eeef
commit
d45406bce7
|
@ -247,11 +247,12 @@ sub zmMemInit {
|
|||
|
||||
sub zmMemVerify {
|
||||
my $monitor = shift;
|
||||
if ( !zmMemAttach( $monitor, $mem_size ) ) {
|
||||
return( undef );
|
||||
|
||||
if ( !zmMemAttach($monitor, $mem_size) ) {
|
||||
return undef;
|
||||
}
|
||||
|
||||
my $sd_size = zmMemRead( $monitor, 'shared_data:size', 1 );
|
||||
my $sd_size = zmMemRead($monitor, 'shared_data:size', 1);
|
||||
if ( $sd_size != $mem_data->{shared_data}->{size} ) {
|
||||
if ( $sd_size ) {
|
||||
Error( "Shared data size conflict in shared_data for monitor "
|
||||
|
@ -269,9 +270,9 @@ sub zmMemVerify {
|
|||
.", got ".$sd_size
|
||||
);
|
||||
}
|
||||
return( undef );
|
||||
return undef;
|
||||
}
|
||||
my $td_size = zmMemRead( $monitor, 'trigger_data:size', 1 );
|
||||
my $td_size = zmMemRead($monitor, 'trigger_data:size', 1);
|
||||
if ( $td_size != $mem_data->{trigger_data}->{size} ) {
|
||||
if ( $td_size ) {
|
||||
Error( "Shared data size conflict in trigger_data for monitor "
|
||||
|
@ -290,14 +291,17 @@ sub zmMemVerify {
|
|||
.$td_size
|
||||
);
|
||||
}
|
||||
return( undef );
|
||||
return undef;
|
||||
}
|
||||
if ( !zmMemRead($monitor, 'shared_data:valid',1) ) {
|
||||
Error( "Shared data not valid for monitor $$monitor{Id}" );
|
||||
return( undef );
|
||||
my $valid = zmMemRead($monitor, 'shared_data:valid',1);
|
||||
if ( !$valid ) {
|
||||
Error("Shared data not valid for monitor $$monitor{Id}");
|
||||
return undef;
|
||||
} else {
|
||||
Debug("Shared data appears vaild for monitor $$monitor{Id}: $valid");
|
||||
}
|
||||
|
||||
return( !undef );
|
||||
return !undef;
|
||||
}
|
||||
|
||||
sub zmMemRead {
|
||||
|
|
|
@ -75,55 +75,50 @@ sub zmMemKey {
|
|||
|
||||
sub zmMemAttach {
|
||||
my ( $monitor, $size ) = @_;
|
||||
if ( ! $size ) {
|
||||
Error( "No size passed to zmMemAttach for monitor $$monitor{Id}\n" );
|
||||
|
||||
if ( !$size ) {
|
||||
Error("No size passed to zmMemAttach for monitor $$monitor{Id}");
|
||||
return undef;
|
||||
}
|
||||
if ( !defined($monitor->{MMapAddr}) ) {
|
||||
|
||||
my $mmap_file = $Config{ZM_PATH_MAP}.'/zm.mmap.'.$monitor->{Id};
|
||||
if ( ! -e $mmap_file ) {
|
||||
Error( sprintf( "Memory map file '%s' does not exist. zmc might not be running."
|
||||
, $mmap_file
|
||||
)
|
||||
);
|
||||
return undef;
|
||||
}
|
||||
my $mmap_file_size = -s $mmap_file;
|
||||
|
||||
if ( $mmap_file_size < $size ) {
|
||||
Error( sprintf( "Memory map file '%s' should have been %d but was instead %d"
|
||||
, $mmap_file
|
||||
, $size
|
||||
, $mmap_file_size
|
||||
)
|
||||
);
|
||||
return undef;
|
||||
}
|
||||
my $MMAP;
|
||||
if ( !open( $MMAP, '+<', $mmap_file ) ) {
|
||||
Error( sprintf( "Can't open memory map file '%s': $!", $mmap_file ) );
|
||||
return undef;
|
||||
}
|
||||
my $mmap = undef;
|
||||
my $mmap_addr = mmap( $mmap, $size, PROT_READ|PROT_WRITE, MAP_SHARED, $MMAP );
|
||||
if ( !$mmap_addr || !$mmap ) {
|
||||
Error( sprintf( "Can't mmap to file '%s': $!\n", $mmap_file ) );
|
||||
close( $MMAP );
|
||||
return undef;
|
||||
}
|
||||
$monitor->{MMapHandle} = $MMAP;
|
||||
$monitor->{MMapAddr} = $mmap_addr;
|
||||
$monitor->{MMap} = \$mmap;
|
||||
if ( defined($monitor->{MMapAddr}) ) {
|
||||
Debug("zmMemAttach already attached at $monitor->{MMapAddr}");
|
||||
return !undef;
|
||||
}
|
||||
|
||||
my $mmap_file = $Config{ZM_PATH_MAP}.'/zm.mmap.'.$monitor->{Id};
|
||||
if ( ! -e $mmap_file ) {
|
||||
Error("Memory map file '$mmap_file' does not exist. zmc might not be running.");
|
||||
return undef;
|
||||
}
|
||||
my $mmap_file_size = -s $mmap_file;
|
||||
|
||||
if ( $mmap_file_size < $size ) {
|
||||
Error("Memory map file '$mmap_file' should have been $size but was instead $mmap_file_size");
|
||||
return undef;
|
||||
}
|
||||
my $MMAP;
|
||||
if ( !open($MMAP, '+<', $mmap_file) ) {
|
||||
Error("Can't open memory map file '$mmap_file': $!");
|
||||
return undef;
|
||||
}
|
||||
my $mmap = undef;
|
||||
my $mmap_addr = mmap($mmap, $size, PROT_READ|PROT_WRITE, MAP_SHARED, $MMAP);
|
||||
if ( !$mmap_addr || !$mmap ) {
|
||||
Error("Can't mmap to file '$mmap_file': $!");
|
||||
close($MMAP);
|
||||
return undef;
|
||||
}
|
||||
$monitor->{MMapHandle} = $MMAP;
|
||||
$monitor->{MMapAddr} = $mmap_addr;
|
||||
$monitor->{MMap} = \$mmap;
|
||||
return !undef;
|
||||
}
|
||||
} # end sub zmMemAttach
|
||||
|
||||
sub zmMemDetach {
|
||||
my $monitor = shift;
|
||||
|
||||
if ( $monitor->{MMap} ) {
|
||||
if ( ! munmap( ${$monitor->{MMap}} ) ) {
|
||||
if ( ! munmap(${$monitor->{MMap}}) ) {
|
||||
Warn( "Unable to munmap for monitor $$monitor{Id}\n");
|
||||
}
|
||||
delete $monitor->{MMap};
|
||||
|
@ -132,7 +127,7 @@ sub zmMemDetach {
|
|||
delete $monitor->{MMapAddr};
|
||||
}
|
||||
if ( $monitor->{MMapHandle} ) {
|
||||
close( $monitor->{MMapHandle} );
|
||||
close($monitor->{MMapHandle});
|
||||
delete $monitor->{MMapHandle};
|
||||
}
|
||||
}
|
||||
|
@ -144,13 +139,10 @@ sub zmMemGet {
|
|||
|
||||
my $mmap = $monitor->{MMap};
|
||||
if ( !$mmap || !$$mmap ) {
|
||||
Error( sprintf( "Can't read from mapped memory for monitor '%d', gone away?"
|
||||
, $monitor->{Id}
|
||||
)
|
||||
);
|
||||
Error("Can't read from mapped memory for monitor '$$monitor{Id}', gone away?");
|
||||
return undef;
|
||||
}
|
||||
my $data = substr( $$mmap, $offset, $size );
|
||||
my $data = substr($$mmap, $offset, $size);
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
@ -162,23 +154,20 @@ sub zmMemPut {
|
|||
|
||||
my $mmap = $monitor->{MMap};
|
||||
if ( !$mmap || !$$mmap ) {
|
||||
Error( sprintf( "Can't write mapped memory for monitor '%d', gone away?"
|
||||
, $monitor->{Id}
|
||||
)
|
||||
);
|
||||
return( undef );
|
||||
Error("Can't write mapped memory for monitor '$$monitor{Id}', gone away?");
|
||||
return undef;
|
||||
}
|
||||
substr( $$mmap, $offset, $size ) = $data;
|
||||
return( !undef );
|
||||
substr($$mmap, $offset, $size) = $data;
|
||||
return !undef;
|
||||
}
|
||||
|
||||
sub zmMemClean {
|
||||
Debug( "Removing memory map files\n" );
|
||||
Debug("Removing memory map files");
|
||||
my $mapPath = $Config{ZM_PATH_MAP}.'/zm.mmap.*';
|
||||
foreach my $mapFile( glob( $mapPath ) ) {
|
||||
( $mapFile ) = $mapFile =~ /^(.+)$/;
|
||||
Debug( "Removing memory map file '$mapFile'\n" );
|
||||
unlink( $mapFile );
|
||||
Debug("Removing memory map file '$mapFile'");
|
||||
unlink($mapFile);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -305,11 +305,11 @@ while( 1 ) {
|
|||
# zmDbConnect will ping and reconnect if neccessary
|
||||
$dbh = zmDbConnect();
|
||||
} # end while ( 1 )
|
||||
Info( "Trigger daemon exiting\n" );
|
||||
Info("Trigger daemon exiting");
|
||||
exit;
|
||||
|
||||
sub loadMonitors {
|
||||
Debug( "Loading monitors\n" );
|
||||
Debug("Loading monitors");
|
||||
$monitor_reload_time = time();
|
||||
|
||||
my %new_monitors = ();
|
||||
|
@ -324,13 +324,15 @@ sub loadMonitors {
|
|||
or Fatal( "Can't execute: ".$sth->errstr() );
|
||||
while( my $monitor = $sth->fetchrow_hashref() ) {
|
||||
# Check shared memory ok
|
||||
if ( 0 ) {
|
||||
if ( !zmMemVerify( $monitor ) ) {
|
||||
zmMemInvalidate( $monitor );
|
||||
next;
|
||||
}
|
||||
|
||||
$monitor->{LastState} = zmGetMonitorState( $monitor );
|
||||
$monitor->{LastEvent} = zmGetLastEvent( $monitor );
|
||||
$monitor->{LastState} = zmGetMonitorState($monitor);
|
||||
$monitor->{LastEvent} = zmGetLastEvent($monitor);
|
||||
}
|
||||
$new_monitors{$monitor->{Id}} = $monitor;
|
||||
} # end while fetchrow
|
||||
%monitors = %new_monitors;
|
||||
|
@ -348,25 +350,25 @@ sub handleMessage {
|
|||
|
||||
my $monitor = $monitors{$id};
|
||||
if ( !$monitor ) {
|
||||
Warning( "Can't find monitor '$id' for message '$message'\n" );
|
||||
Warning("Can't find monitor '$id' for message '$message'");
|
||||
return;
|
||||
}
|
||||
Debug( "Found monitor for id '$id'\n" );
|
||||
Debug("Found monitor for id '$id'");
|
||||
|
||||
next if ( !zmMemVerify( $monitor ) );
|
||||
next if ( !zmMemVerify($monitor) );
|
||||
|
||||
Debug( "Handling action '$action'\n" );
|
||||
Debug("Handling action '$action'");
|
||||
if ( $action =~ /^(enable|disable)(?:\+(\d+))?$/ ) {
|
||||
my $state = $1;
|
||||
my $delay = $2;
|
||||
if ( $state eq 'enable' ) {
|
||||
zmMonitorEnable( $monitor );
|
||||
zmMonitorEnable($monitor);
|
||||
} else {
|
||||
zmMonitorDisable( $monitor );
|
||||
zmMonitorDisable($monitor);
|
||||
}
|
||||
# Force a reload
|
||||
$monitor_reload_time = 0;
|
||||
Info( "Set monitor to $state\n" );
|
||||
Info("Set monitor to $state");
|
||||
if ( $delay ) {
|
||||
my $action_text = $id.'|'.( ($state eq 'enable')
|
||||
? 'disable'
|
||||
|
@ -381,9 +383,9 @@ sub handleMessage {
|
|||
my $delay = $2;
|
||||
my $trigger_data;
|
||||
if ( $trigger eq 'on' ) {
|
||||
zmTriggerEventOn( $monitor, $score, $cause, $text );
|
||||
zmTriggerShowtext( $monitor, $showtext ) if defined($showtext);
|
||||
Info( "Trigger '$trigger' '$cause'\n" );
|
||||
zmTriggerEventOn($monitor, $score, $cause, $text);
|
||||
zmTriggerShowtext($monitor, $showtext) if defined($showtext);
|
||||
Info("Trigger '$trigger' '$cause'");
|
||||
if ( $delay ) {
|
||||
my $action_text = $id.'|cancel';
|
||||
handleDelay($delay, $connection, $action_text);
|
||||
|
@ -393,29 +395,29 @@ sub handleMessage {
|
|||
my $action_text = $id.'|off|0|'.$cause.'|'.$text;
|
||||
handleDelay($delay, $connection, $action_text);
|
||||
} else {
|
||||
my $last_event = zmGetLastEvent( $monitor );
|
||||
zmTriggerEventOff( $monitor );
|
||||
zmTriggerShowtext( $monitor, $showtext ) if defined($showtext);
|
||||
Info( "Trigger '$trigger'\n" );
|
||||
my $last_event = zmGetLastEvent($monitor);
|
||||
zmTriggerEventOff($monitor);
|
||||
zmTriggerShowtext($monitor, $showtext) if defined($showtext);
|
||||
Info("Trigger '$trigger'");
|
||||
# Wait til it's finished
|
||||
while( zmInAlarm( $monitor )
|
||||
&& ($last_event == zmGetLastEvent( $monitor ))
|
||||
while( zmInAlarm($monitor)
|
||||
&& ($last_event == zmGetLastEvent($monitor))
|
||||
) {
|
||||
# Tenth of a second
|
||||
usleep( 100000 );
|
||||
usleep(100000);
|
||||
}
|
||||
zmTriggerEventCancel( $monitor );
|
||||
zmTriggerEventCancel($monitor);
|
||||
}
|
||||
} # end if trigger is on or off
|
||||
} elsif( $action eq 'cancel' ) {
|
||||
zmTriggerEventCancel( $monitor );
|
||||
zmTriggerShowtext( $monitor, $showtext ) if defined($showtext);
|
||||
Info( "Cancelled event\n" );
|
||||
zmTriggerEventCancel($monitor);
|
||||
zmTriggerShowtext($monitor, $showtext) if defined($showtext);
|
||||
Info("Cancelled event");
|
||||
} elsif( $action eq 'show' ) {
|
||||
zmTriggerShowtext( $monitor, $showtext );
|
||||
Info( "Updated show text to '$showtext'\n" );
|
||||
Info("Updated show text to '$showtext'");
|
||||
} else {
|
||||
Error( "Unrecognised action '$action' in message '$message'\n" );
|
||||
Error("Unrecognised action '$action' in message '$message'");
|
||||
}
|
||||
} # end sub handleMessage
|
||||
|
||||
|
@ -430,8 +432,9 @@ sub handleDelay {
|
|||
$action_array = $actions{$action_time} = [];
|
||||
}
|
||||
push( @$action_array, { connection=>$connection, message=>$action_text } );
|
||||
Debug( "Added timed event '$action_text', expires at $action_time (+$delay secs)\n" );
|
||||
Debug("Added timed event '$action_text', expires at $action_time (+$delay secs)");
|
||||
}
|
||||
|
||||
1;
|
||||
__END__
|
||||
|
||||
|
@ -506,13 +509,13 @@ B<id>|B<action>|B<score>|B<cause>|B<text>|B<showtext>
|
|||
|
||||
=back
|
||||
|
||||
Note that multiple messages can be sent at once and should be LF or CRLF
|
||||
delimited. This script is not necessarily intended to be a solution in
|
||||
itself, but is intended to be used as 'glue' to help ZoneMinder interface
|
||||
with other systems. It will almost certainly require some customisation
|
||||
before you can make any use of it. If all you want to do is generate alarms
|
||||
from external sources then using the ZoneMinder::SharedMem perl module is
|
||||
likely to be easier.
|
||||
Note that multiple messages can be sent at once and should be LF or CRLF
|
||||
delimited. This script is not necessarily intended to be a solution in
|
||||
itself, but is intended to be used as 'glue' to help ZoneMinder interface
|
||||
with other systems. It will almost certainly require some customisation
|
||||
before you can make any use of it. If all you want to do is generate alarms
|
||||
from external sources then using the ZoneMinder::SharedMem perl module is
|
||||
likely to be easier.
|
||||
|
||||
=head1 EXAMPLES
|
||||
|
||||
|
|
Loading…
Reference in New Issue