Rework to use ZoneMinder::Monitor class. Simplify loadMonitors and get rid of loadMonitor. Add in ServerId change handling.

This commit is contained in:
Isaac Connor 2022-01-12 23:07:48 -05:00
parent c9db5f44a4
commit 8d06175024
1 changed files with 24 additions and 38 deletions

View File

@ -42,6 +42,7 @@ use constant SELECT_TIMEOUT => 0.25;
@EXTRA_PERL_LIB@ @EXTRA_PERL_LIB@
use ZoneMinder; use ZoneMinder;
use ZoneMinder::Monitor;
use ZoneMinder::Trigger::Channel::Inet; use ZoneMinder::Trigger::Channel::Inet;
use ZoneMinder::Trigger::Channel::Unix; use ZoneMinder::Trigger::Channel::Unix;
use ZoneMinder::Trigger::Channel::Serial; use ZoneMinder::Trigger::Channel::Serial;
@ -203,14 +204,10 @@ while (!$zm_terminate) {
# Check for alarms that might have happened # Check for alarms that might have happened
my @out_messages; my @out_messages;
foreach my $monitor ( values %monitors ) { foreach my $monitor ( values %monitors ) {
if ($$monitor{Function} eq 'None') { if (!$monitor->connect()) {
$monitor_reload_time = 0;
next;
}
if (!zmMemVerify($monitor)) {
# Our attempt to verify the memory handle failed. We should reload the monitors. # Our attempt to verify the memory handle failed. We should reload the monitors.
# Don't need to zmMemInvalidate because the monitor reload will do it. # Don't need to zmMemInvalidate because the monitor reload will do it.
Debug("Failed connect, putting on reloads");
push @needsReload, $monitor; push @needsReload, $monitor;
next; next;
} }
@ -249,6 +246,7 @@ while (!$zm_terminate) {
} }
$monitor->{LastState} = $state; $monitor->{LastState} = $state;
$monitor->{LastEvent} = $last_event; $monitor->{LastEvent} = $last_event;
$monitor->disconnect();
} # end foreach monitor } # end foreach monitor
foreach my $connection ( @out_connections ) { foreach my $connection ( @out_connections ) {
@ -298,15 +296,22 @@ while (!$zm_terminate) {
# Reload all monitors from the dB every MONITOR_RELOAD_INTERVAL # Reload all monitors from the dB every MONITOR_RELOAD_INTERVAL
if ( (time() - $monitor_reload_time) > MONITOR_RELOAD_INTERVAL ) { if ( (time() - $monitor_reload_time) > MONITOR_RELOAD_INTERVAL ) {
foreach my $monitor ( values(%monitors) ) {
zmMemInvalidate( $monitor ); # Free up any used memory handle
}
loadMonitors(); loadMonitors();
@needsReload = (); # We just reloaded all monitors so no need reload a specific monitor @needsReload = (); # We just reloaded all monitors so no need reload a specific monitor
# If we have NOT just reloaded all monitors, reload a specific monitor if its shared mem changed # If we have NOT just reloaded all monitors, reload a specific monitor if its shared mem changed
} elsif (@needsReload) { } elsif (@needsReload) {
foreach my $monitor (@needsReload) { foreach my $monitor (@needsReload) {
loadMonitor($monitor); $monitor = $monitors{$monitor->Id()} = ZoneMinder::Monitor->find_one(Id=>$monitor->Id());
if ( $$monitor{Function} eq 'None' ) {
delete $monitors{$monitor->Id()};
} elsif ( $Config{ZM_SERVER_ID} and ($$monitor{ServerId} != $Config{ZM_SERVER_ID})) {
delete $monitors{$monitor->Id()};
} else {
if ($monitor->connect()) {
$monitor->{LastState} = zmGetMonitorState($monitor);
$monitor->{LastEvent} = zmGetLastEvent($monitor);
}
}
} }
@needsReload = (); @needsReload = ();
} }
@ -317,40 +322,21 @@ while (!$zm_terminate) {
Info('Trigger daemon exiting'); Info('Trigger daemon exiting');
exit; exit;
sub loadMonitor {
my $monitor = shift;
Debug('Loading monitor '.$monitor);
zmMemInvalidate($monitor);
if ( zmMemVerify($monitor) ) { # This will re-init shared memory
$monitor->{LastState} = zmGetMonitorState($monitor);
$monitor->{LastEvent} = zmGetLastEvent($monitor);
}
} # end sub loadMonitor
sub loadMonitors { sub loadMonitors {
$monitor_reload_time = time(); $monitor_reload_time = time();
my %new_monitors = (); %monitors = ();
my $sql = 'SELECT * FROM `Monitors` foreach my $monitor ( ZoneMinder::Monitor->find(
WHERE find_in_set( `Function`, \'Modect,Mocord,Nodect,Record\' )'. Function=>['Modect','Mocord','Nodect','Record'],
( $Config{ZM_SERVER_ID} ? ' AND `ServerId`=?' : '' ) ($Config{ZM_SERVER_ID} ? (ServerId=>$Config{ZM_SERVER_ID}) : ()),
; )) {
my $sth = $dbh->prepare_cached( $sql ) if ($monitor->connect()) { # This will re-init shared memory
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $Config{ZM_SERVER_ID} ? $Config{ZM_SERVER_ID} : () )
or Fatal( "Can't execute: ".$sth->errstr() );
while ( my $monitor = $sth->fetchrow_hashref() ) {
if ( zmMemVerify($monitor) ) { # This will re-init shared memory
$monitor->{LastState} = zmGetMonitorState($monitor); $monitor->{LastState} = zmGetMonitorState($monitor);
$monitor->{LastEvent} = zmGetLastEvent($monitor); $monitor->{LastEvent} = zmGetLastEvent($monitor);
} }
$new_monitors{$monitor->{Id}} = $monitor; $monitors{$monitor->{Id}} = $monitor;
} # end while fetchrow } # end while fetchrow
%monitors = %new_monitors;
} # end sub loadMonitors } # end sub loadMonitors
sub handleMessage { sub handleMessage {