diff --git a/src/zm_event.cpp b/src/zm_event.cpp index 005974bfc..c3fe6b578 100644 --- a/src/zm_event.cpp +++ b/src/zm_event.cpp @@ -28,21 +28,17 @@ Event::Event( Monitor *p_monitor, struct timeval p_start_time ) : monitor( p_mon static char start_time_str[32]; strftime( start_time_str, sizeof(start_time_str), "%Y-%m-%d %H:%M:%S", localtime( &start_time.tv_sec ) ); - sprintf( sql, "insert into Events set MonitorId=%d, Name='Event', StartTime='%s'", monitor->Id(), start_time_str ); + sprintf( sql, "insert into Events ( MonitorId, Name, StartTime ) values ( %d, 'Event', '%s' )", monitor->Id(), start_time_str ); if ( mysql_query( &dbconn, sql ) ) { Error(( "Can't insert event: %s", mysql_error( &dbconn ) )); exit( mysql_errno( &dbconn ) ); } id = mysql_insert_id( &dbconn ); - start_frame_id = 0; - end_frame_id = 0; - //end_time = 0; frames = 0; alarm_frames = 0; tot_score = 0; max_score = 0; - //sprintf( path, ZM_DIR_EVENTS "/%s/%04d", monitor->Name(), id ); sprintf( path, ZM_DIR_EVENTS "/%s/%d", monitor->Name(), id ); struct stat statbuf; @@ -75,10 +71,39 @@ Event::~Event() } } +void Event::AddFrames( int n_frames, struct timeval **timestamps, const Image **images ) +{ + static char sql[4096]; + strcpy( sql, "insert into Frames ( EventId, FrameId, ImagePath, Delta ) values " ); + for ( int i = 0; i < n_frames; i++ ) + { + frames++; + Debug( 1, ( "Writing pre-capture frame %d", frames )); + static char event_file[PATH_MAX]; + sprintf( event_file, "%s/capture-%03d.jpg", path, frames ); + images[i]->WriteJpeg( event_file ); + + struct DeltaTimeval delta_time; + DELTA_TIMEVAL( delta_time, *(timestamps[i]), start_time ); + + sprintf( sql+strlen(sql), "( %d, %d, '%s', %s%ld.%02ld ), ", id, frames, event_file, delta_time.positive?"":"-", delta_time.tv_sec, delta_time.tv_usec/10000 ); + } + + Debug( 1, ( "Adding %d frames to DB", n_frames )); + *(sql+strlen(sql)-2) = '\0'; + if ( mysql_query( &dbconn, sql ) ) + + { + Error(( "Can't insert frames: %s", mysql_error( &dbconn ) )); + exit( mysql_errno( &dbconn ) ); + } +} + void Event::AddFrame( struct timeval timestamp, const Image *image, const Image *alarm_image, unsigned int score ) { frames++; + Debug( 1, ( "Writing capture frame %d", frames )); static char event_file[PATH_MAX]; sprintf( event_file, "%s/capture-%03d.jpg", path, frames ); image->WriteJpeg( event_file ); @@ -86,18 +111,18 @@ void Event::AddFrame( struct timeval timestamp, const Image *image, const Image struct DeltaTimeval delta_time; DELTA_TIMEVAL( delta_time, timestamp, start_time ); + Debug( 1, ( "Adding frame %d to DB", frames )); static char sql[256]; - sprintf( sql, "insert into Frames set EventId=%d, FrameId=%d, AlarmFrame=%d, ImagePath='%s', Delta=%s%ld.%02ld, Score=%d", id, frames, alarm_image!=0, event_file, delta_time.positive?"":"-", delta_time.tv_sec, delta_time.tv_usec/10000, score ); + sprintf( sql, "insert into Frames ( EventId, FrameId, AlarmFrame, ImagePath, Delta, Score ) values ( %d, %d, %d, '%s', %s%ld.%02ld, %d )", id, frames, alarm_image!=0, event_file, delta_time.positive?"":"-", delta_time.tv_sec, delta_time.tv_usec/10000, score ); if ( mysql_query( &dbconn, sql ) ) { Error(( "Can't insert frame: %s", mysql_error( &dbconn ) )); exit( mysql_errno( &dbconn ) ); } - end_frame_id = mysql_insert_id( &dbconn ); - if ( !start_frame_id ) start_frame_id = end_frame_id; if ( alarm_image ) { + Debug( 1, ( "Writing analysis frame %d", frames )); end_time = timestamp; alarm_frames++; @@ -107,7 +132,6 @@ void Event::AddFrame( struct timeval timestamp, const Image *image, const Image if ( score > max_score ) max_score = score; } - //if ( !start_time ) start_time = end_time; } void Event::StreamEvent( const char *path, int event_id, unsigned long refresh, FILE *fd ) diff --git a/src/zm_event.h b/src/zm_event.h index 6cde3aff1..f18bf192b 100644 --- a/src/zm_event.h +++ b/src/zm_event.h @@ -52,8 +52,6 @@ protected: Monitor *monitor; struct timeval start_time; struct timeval end_time; - int start_frame_id; - int end_frame_id; int frames; int alarm_frames; unsigned int tot_score; @@ -68,6 +66,7 @@ public: int Frames() const { return( frames ); } int AlarmFrames() const { return( alarm_frames ); } + void AddFrames( int n_frames, struct timeval **timestamps, const Image **images ); void AddFrame( struct timeval timestamp, const Image *image, const Image *alarm_frame=NULL, unsigned int score=0 ); static void StreamEvent( const char *path, int event_id, unsigned long refresh=100, FILE *fd=stdout );