diff --git a/scripts/zmtrigger.pl.in b/scripts/zmtrigger.pl.in index be38c5e91..acd4ce255 100644 --- a/scripts/zmtrigger.pl.in +++ b/scripts/zmtrigger.pl.in @@ -372,9 +372,10 @@ sub handleMessage { Warning("Can't find monitor '$id' for message '$message'"); return; } - Debug("Found monitor for id '$id'"); - - next if !zmMemVerify($monitor); + if ( !zmMemVerify($monitor) ) { + Warning("Can't verify monitor '$id' for message '$message'"); + return; + } Debug("Handling action '$action'"); if ( $action =~ /^(enable|disable)(?:[\+ ](\d+))?$/ ) { @@ -393,12 +394,19 @@ sub handleMessage { handleDelay($delay, $connection, $action_text); } } elsif ( $action =~ /^(on|off)(?:[ \+](\d+))?$/ ) { - next if !$monitor->{Enabled}; + if ( !$monitor->{Enabled} ) { + Info('Motion detection not enabled on monitor '.$id); + return; + } my $trigger = $1; my $delay = $2; my $trigger_data; if ( $trigger eq 'on' ) { + if ( $score <= 0 ) { + Warning('Triggering on with invalid score will have no result.'); + return; + } zmTriggerEventOn($monitor, $score, $cause, $text); zmTriggerShowtext($monitor, $showtext) if defined($showtext); Info("Trigger '$trigger' '$cause'"); diff --git a/src/zm_event.cpp b/src/zm_event.cpp index cd7ba1918..c858b1877 100644 --- a/src/zm_event.cpp +++ b/src/zm_event.cpp @@ -39,6 +39,7 @@ //#define USE_PREPARED_SQL 1 const char * Event::frame_type_names[3] = { "Normal", "Bulk", "Alarm" }; +#define MAX_DB_FRAMES 120 char frame_insert_sql[ZM_SQL_LGE_BUFSIZ] = "INSERT INTO `Frames` (`EventId`, `FrameId`, `Type`, `TimeStamp`, `Delta`, `Score`) VALUES "; int Event::pre_alarm_count = 0; @@ -255,7 +256,7 @@ Event::~Event() { } // endtime is set in AddFrame, so SHOULD be set to the value of the last frame timestamp. - if ( ! end_time.tv_sec ) { + if ( !end_time.tv_sec ) { Warning("Empty endtime for event. Should not happen. Setting to now."); gettimeofday(&end_time, nullptr); } @@ -503,7 +504,7 @@ void Event::updateNotes(const StringSetMap &newNoteSetMap) { } // void Event::updateNotes(const StringSetMap &newNoteSetMap) void Event::AddFrames(int n_frames, Image **images, struct timeval **timestamps) { - for (int i = 0; i < n_frames; i += ZM_SQL_BATCH_SIZE) { + for ( int i = 0; i < n_frames; i += ZM_SQL_BATCH_SIZE ) { AddFramesInternal(n_frames, i, images, timestamps); } } @@ -577,6 +578,9 @@ void Event::AddFramesInternal(int n_frames, int start_frame, Image **images, str void Event::WriteDbFrames() { char *frame_insert_values_ptr = (char *)&frame_insert_sql + 90; // 90 == strlen(frame_insert_sql); + + /* Each frame needs about 63 chars. So if we buffer too many frames, we will exceed the size of frame_insert_sql; + */ Debug(1, "Inserting %d frames", frame_data.size()); while ( frame_data.size() ) { Frame *frame = frame_data.front(); @@ -595,7 +599,6 @@ void Event::WriteDbFrames() { } *(frame_insert_values_ptr-1) = '\0'; // The -1 is for the extra , added for values above db_mutex.lock(); - Debug(1, "SQL: %s", frame_insert_sql); int rc = mysql_query(&dbconn, frame_insert_sql); db_mutex.unlock(); @@ -688,14 +691,23 @@ void Event::AddFrame(Image *image, struct timeval timestamp, int score, Image *a Debug(1, "Frame delta is %d.%d - %d.%d = %d.%d", start_time.tv_sec, start_time.tv_usec, timestamp.tv_sec, timestamp.tv_usec, delta_time.sec, delta_time.fsec); + double fps = monitor->get_fps(); + bool db_frame = ( frame_type != BULK ) || (frames==1) || ((frames%config.bulk_frame_interval)==0) ; if ( db_frame ) { // The idea is to write out 1/sec frame_data.push(new Frame(id, frames, frame_type, timestamp, delta_time, score)); - if ( write_to_db or ( monitor->get_fps() and (frame_data.size() > monitor->get_fps())) or frame_type==BULK ) { + if ( write_to_db + or + (frame_data.size() > MAX_DB_FRAMES) + or + (frame_type==BULK) + or + ( fps and (frame_data.size() > fps) ) + ) { Debug(1, "Adding %d frames to DB because write_to_db:%d or frames > analysis fps %f or BULK", - frame_data.size(), write_to_db, monitor->get_fps()); + frame_data.size(), write_to_db, fps); WriteDbFrames(); last_db_frame = frames; @@ -719,6 +731,9 @@ void Event::AddFrame(Image *image, struct timeval timestamp, int score, Image *a db_mutex.lock(); } db_mutex.unlock(); + } else { + Debug(1, "Not Adding %d frames to DB because write_to_db:%d or frames > analysis fps %f or BULK", + frame_data.size(), write_to_db, fps); } // end if frame_type == BULK } // end if db_frame diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index 0acfb3ded..e2ea20f88 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -1307,10 +1307,11 @@ bool Monitor::Analyse() { db_mutex.lock(); static char sql[ZM_SQL_SML_BUFSIZ]; snprintf(sql, sizeof(sql), "INSERT INTO Monitor_Status (MonitorId,AnalysisFPS) VALUES (%d, %.2lf) ON DUPLICATE KEY UPDATE AnalysisFPS = %.2lf", id, fps, fps); - if ( mysql_query(&dbconn, sql) ) { - Error("Can't run query: %s", mysql_error(&dbconn)); - } + int rc = mysql_query(&dbconn, sql); db_mutex.unlock(); + if ( rc ) { + Error("Can't run query %s: %s", sql, mysql_error(&dbconn)); + } } // end if fps != new_fps last_fps_time = now.tv_sec; @@ -2583,11 +2584,11 @@ int Monitor::Capture() { "VALUES (%d, %.2lf, %u, 'Connected') ON DUPLICATE KEY UPDATE " "CaptureFPS = %.2lf, CaptureBandwidth=%u, Status='Connected'", id, fps, new_capture_bandwidth, fps, new_capture_bandwidth); - if ( mysql_query(&dbconn, sql) ) { - Error("Can't run query: %s", mysql_error(&dbconn)); - } + rc = mysql_query(&dbconn, sql); db_mutex.unlock(); - Debug(4,sql); + if ( rc ) { + Error("Can't run query %s: %s", sql, mysql_error(&dbconn)); + } } // end if time has changed since last update } // end if it might be time to report the fps } // end if captureResult