ode style and some more debug

This commit is contained in:
Isaac Connor 2018-09-28 11:50:34 -04:00
parent 7768b4eeef
commit d45406bce7
3 changed files with 97 additions and 101 deletions

View File

@ -247,11 +247,12 @@ sub zmMemInit {
sub zmMemVerify { sub zmMemVerify {
my $monitor = shift; 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 != $mem_data->{shared_data}->{size} ) {
if ( $sd_size ) { if ( $sd_size ) {
Error( "Shared data size conflict in shared_data for monitor " Error( "Shared data size conflict in shared_data for monitor "
@ -269,9 +270,9 @@ sub zmMemVerify {
.", got ".$sd_size .", 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 != $mem_data->{trigger_data}->{size} ) {
if ( $td_size ) { if ( $td_size ) {
Error( "Shared data size conflict in trigger_data for monitor " Error( "Shared data size conflict in trigger_data for monitor "
@ -290,14 +291,17 @@ sub zmMemVerify {
.$td_size .$td_size
); );
} }
return( undef ); return undef;
} }
if ( !zmMemRead($monitor, 'shared_data:valid',1) ) { my $valid = zmMemRead($monitor, 'shared_data:valid',1);
Error( "Shared data not valid for monitor $$monitor{Id}" ); if ( !$valid ) {
return( undef ); 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 { sub zmMemRead {

View File

@ -75,55 +75,50 @@ sub zmMemKey {
sub zmMemAttach { sub zmMemAttach {
my ( $monitor, $size ) = @_; 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; return undef;
} }
if ( !defined($monitor->{MMapAddr}) ) { if ( defined($monitor->{MMapAddr}) ) {
Debug("zmMemAttach already attached at $monitor->{MMapAddr}");
return !undef;
}
my $mmap_file = $Config{ZM_PATH_MAP}.'/zm.mmap.'.$monitor->{Id}; my $mmap_file = $Config{ZM_PATH_MAP}.'/zm.mmap.'.$monitor->{Id};
if ( ! -e $mmap_file ) { if ( ! -e $mmap_file ) {
Error( sprintf( "Memory map file '%s' does not exist. zmc might not be running." Error("Memory map file '$mmap_file' does not exist. zmc might not be running.");
, $mmap_file
)
);
return undef; return undef;
} }
my $mmap_file_size = -s $mmap_file; my $mmap_file_size = -s $mmap_file;
if ( $mmap_file_size < $size ) { if ( $mmap_file_size < $size ) {
Error( sprintf( "Memory map file '%s' should have been %d but was instead %d" Error("Memory map file '$mmap_file' should have been $size but was instead $mmap_file_size");
, $mmap_file
, $size
, $mmap_file_size
)
);
return undef; return undef;
} }
my $MMAP; my $MMAP;
if ( !open( $MMAP, '+<', $mmap_file ) ) { if ( !open($MMAP, '+<', $mmap_file) ) {
Error( sprintf( "Can't open memory map file '%s': $!", $mmap_file ) ); Error("Can't open memory map file '$mmap_file': $!");
return undef; return undef;
} }
my $mmap = undef; my $mmap = undef;
my $mmap_addr = mmap( $mmap, $size, PROT_READ|PROT_WRITE, MAP_SHARED, $MMAP ); my $mmap_addr = mmap($mmap, $size, PROT_READ|PROT_WRITE, MAP_SHARED, $MMAP);
if ( !$mmap_addr || !$mmap ) { if ( !$mmap_addr || !$mmap ) {
Error( sprintf( "Can't mmap to file '%s': $!\n", $mmap_file ) ); Error("Can't mmap to file '$mmap_file': $!");
close( $MMAP ); close($MMAP);
return undef; return undef;
} }
$monitor->{MMapHandle} = $MMAP; $monitor->{MMapHandle} = $MMAP;
$monitor->{MMapAddr} = $mmap_addr; $monitor->{MMapAddr} = $mmap_addr;
$monitor->{MMap} = \$mmap; $monitor->{MMap} = \$mmap;
}
return !undef; return !undef;
} } # end sub zmMemAttach
sub zmMemDetach { sub zmMemDetach {
my $monitor = shift; my $monitor = shift;
if ( $monitor->{MMap} ) { if ( $monitor->{MMap} ) {
if ( ! munmap( ${$monitor->{MMap}} ) ) { if ( ! munmap(${$monitor->{MMap}}) ) {
Warn( "Unable to munmap for monitor $$monitor{Id}\n"); Warn( "Unable to munmap for monitor $$monitor{Id}\n");
} }
delete $monitor->{MMap}; delete $monitor->{MMap};
@ -132,7 +127,7 @@ sub zmMemDetach {
delete $monitor->{MMapAddr}; delete $monitor->{MMapAddr};
} }
if ( $monitor->{MMapHandle} ) { if ( $monitor->{MMapHandle} ) {
close( $monitor->{MMapHandle} ); close($monitor->{MMapHandle});
delete $monitor->{MMapHandle}; delete $monitor->{MMapHandle};
} }
} }
@ -144,13 +139,10 @@ sub zmMemGet {
my $mmap = $monitor->{MMap}; my $mmap = $monitor->{MMap};
if ( !$mmap || !$$mmap ) { if ( !$mmap || !$$mmap ) {
Error( sprintf( "Can't read from mapped memory for monitor '%d', gone away?" Error("Can't read from mapped memory for monitor '$$monitor{Id}', gone away?");
, $monitor->{Id}
)
);
return undef; return undef;
} }
my $data = substr( $$mmap, $offset, $size ); my $data = substr($$mmap, $offset, $size);
return $data; return $data;
} }
@ -162,23 +154,20 @@ sub zmMemPut {
my $mmap = $monitor->{MMap}; my $mmap = $monitor->{MMap};
if ( !$mmap || !$$mmap ) { if ( !$mmap || !$$mmap ) {
Error( sprintf( "Can't write mapped memory for monitor '%d', gone away?" Error("Can't write mapped memory for monitor '$$monitor{Id}', gone away?");
, $monitor->{Id} return undef;
)
);
return( undef );
} }
substr( $$mmap, $offset, $size ) = $data; substr($$mmap, $offset, $size) = $data;
return( !undef ); return !undef;
} }
sub zmMemClean { sub zmMemClean {
Debug( "Removing memory map files\n" ); Debug("Removing memory map files");
my $mapPath = $Config{ZM_PATH_MAP}.'/zm.mmap.*'; my $mapPath = $Config{ZM_PATH_MAP}.'/zm.mmap.*';
foreach my $mapFile( glob( $mapPath ) ) { foreach my $mapFile( glob( $mapPath ) ) {
( $mapFile ) = $mapFile =~ /^(.+)$/; ( $mapFile ) = $mapFile =~ /^(.+)$/;
Debug( "Removing memory map file '$mapFile'\n" ); Debug("Removing memory map file '$mapFile'");
unlink( $mapFile ); unlink($mapFile);
} }
} }

View File

@ -305,11 +305,11 @@ while( 1 ) {
# zmDbConnect will ping and reconnect if neccessary # zmDbConnect will ping and reconnect if neccessary
$dbh = zmDbConnect(); $dbh = zmDbConnect();
} # end while ( 1 ) } # end while ( 1 )
Info( "Trigger daemon exiting\n" ); Info("Trigger daemon exiting");
exit; exit;
sub loadMonitors { sub loadMonitors {
Debug( "Loading monitors\n" ); Debug("Loading monitors");
$monitor_reload_time = time(); $monitor_reload_time = time();
my %new_monitors = (); my %new_monitors = ();
@ -324,13 +324,15 @@ sub loadMonitors {
or Fatal( "Can't execute: ".$sth->errstr() ); or Fatal( "Can't execute: ".$sth->errstr() );
while( my $monitor = $sth->fetchrow_hashref() ) { while( my $monitor = $sth->fetchrow_hashref() ) {
# Check shared memory ok # Check shared memory ok
if ( 0 ) {
if ( !zmMemVerify( $monitor ) ) { if ( !zmMemVerify( $monitor ) ) {
zmMemInvalidate( $monitor ); zmMemInvalidate( $monitor );
next; next;
} }
$monitor->{LastState} = zmGetMonitorState( $monitor ); $monitor->{LastState} = zmGetMonitorState($monitor);
$monitor->{LastEvent} = zmGetLastEvent( $monitor ); $monitor->{LastEvent} = zmGetLastEvent($monitor);
}
$new_monitors{$monitor->{Id}} = $monitor; $new_monitors{$monitor->{Id}} = $monitor;
} # end while fetchrow } # end while fetchrow
%monitors = %new_monitors; %monitors = %new_monitors;
@ -348,25 +350,25 @@ sub handleMessage {
my $monitor = $monitors{$id}; my $monitor = $monitors{$id};
if ( !$monitor ) { if ( !$monitor ) {
Warning( "Can't find monitor '$id' for message '$message'\n" ); Warning("Can't find monitor '$id' for message '$message'");
return; 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+))?$/ ) { if ( $action =~ /^(enable|disable)(?:\+(\d+))?$/ ) {
my $state = $1; my $state = $1;
my $delay = $2; my $delay = $2;
if ( $state eq 'enable' ) { if ( $state eq 'enable' ) {
zmMonitorEnable( $monitor ); zmMonitorEnable($monitor);
} else { } else {
zmMonitorDisable( $monitor ); zmMonitorDisable($monitor);
} }
# Force a reload # Force a reload
$monitor_reload_time = 0; $monitor_reload_time = 0;
Info( "Set monitor to $state\n" ); Info("Set monitor to $state");
if ( $delay ) { if ( $delay ) {
my $action_text = $id.'|'.( ($state eq 'enable') my $action_text = $id.'|'.( ($state eq 'enable')
? 'disable' ? 'disable'
@ -381,9 +383,9 @@ sub handleMessage {
my $delay = $2; my $delay = $2;
my $trigger_data; my $trigger_data;
if ( $trigger eq 'on' ) { if ( $trigger eq 'on' ) {
zmTriggerEventOn( $monitor, $score, $cause, $text ); zmTriggerEventOn($monitor, $score, $cause, $text);
zmTriggerShowtext( $monitor, $showtext ) if defined($showtext); zmTriggerShowtext($monitor, $showtext) if defined($showtext);
Info( "Trigger '$trigger' '$cause'\n" ); Info("Trigger '$trigger' '$cause'");
if ( $delay ) { if ( $delay ) {
my $action_text = $id.'|cancel'; my $action_text = $id.'|cancel';
handleDelay($delay, $connection, $action_text); handleDelay($delay, $connection, $action_text);
@ -393,29 +395,29 @@ sub handleMessage {
my $action_text = $id.'|off|0|'.$cause.'|'.$text; my $action_text = $id.'|off|0|'.$cause.'|'.$text;
handleDelay($delay, $connection, $action_text); handleDelay($delay, $connection, $action_text);
} else { } else {
my $last_event = zmGetLastEvent( $monitor ); my $last_event = zmGetLastEvent($monitor);
zmTriggerEventOff( $monitor ); zmTriggerEventOff($monitor);
zmTriggerShowtext( $monitor, $showtext ) if defined($showtext); zmTriggerShowtext($monitor, $showtext) if defined($showtext);
Info( "Trigger '$trigger'\n" ); Info("Trigger '$trigger'");
# Wait til it's finished # Wait til it's finished
while( zmInAlarm( $monitor ) while( zmInAlarm($monitor)
&& ($last_event == zmGetLastEvent( $monitor )) && ($last_event == zmGetLastEvent($monitor))
) { ) {
# Tenth of a second # Tenth of a second
usleep( 100000 ); usleep(100000);
} }
zmTriggerEventCancel( $monitor ); zmTriggerEventCancel($monitor);
} }
} # end if trigger is on or off } # end if trigger is on or off
} elsif( $action eq 'cancel' ) { } elsif( $action eq 'cancel' ) {
zmTriggerEventCancel( $monitor ); zmTriggerEventCancel($monitor);
zmTriggerShowtext( $monitor, $showtext ) if defined($showtext); zmTriggerShowtext($monitor, $showtext) if defined($showtext);
Info( "Cancelled event\n" ); Info("Cancelled event");
} elsif( $action eq 'show' ) { } elsif( $action eq 'show' ) {
zmTriggerShowtext( $monitor, $showtext ); zmTriggerShowtext( $monitor, $showtext );
Info( "Updated show text to '$showtext'\n" ); Info("Updated show text to '$showtext'");
} else { } else {
Error( "Unrecognised action '$action' in message '$message'\n" ); Error("Unrecognised action '$action' in message '$message'");
} }
} # end sub handleMessage } # end sub handleMessage
@ -430,8 +432,9 @@ sub handleDelay {
$action_array = $actions{$action_time} = []; $action_array = $actions{$action_time} = [];
} }
push( @$action_array, { connection=>$connection, message=>$action_text } ); 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; 1;
__END__ __END__
@ -506,13 +509,13 @@ B<id>|B<action>|B<score>|B<cause>|B<text>|B<showtext>
=back =back
Note that multiple messages can be sent at once and should be LF or CRLF 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 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 itself, but is intended to be used as 'glue' to help ZoneMinder interface
with other systems. It will almost certainly require some customisation 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 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 from external sources then using the ZoneMinder::SharedMem perl module is
likely to be easier. likely to be easier.
=head1 EXAMPLES =head1 EXAMPLES