Merge branch 'plugin_support_new' into plugin_support

Conflicts:
	distros/debian8/changelog
This commit is contained in:
Emmanuel Papin 2015-01-27 19:28:42 +01:00
commit 7c0dfe9a5b
16 changed files with 236 additions and 6 deletions

View File

@ -104,8 +104,6 @@ void zmLoadConfig()
config.Assign(); config.Assign();
} }
StaticConfig staticConfig;
ConfigItem::ConfigItem( const char *p_name, const char *p_value, const char *const p_type ) ConfigItem::ConfigItem( const char *p_name, const char *p_value, const char *const p_type )
{ {
name = new char[strlen(p_name)+1]; name = new char[strlen(p_name)+1];

View File

@ -230,3 +230,17 @@ bool Detector::Detect(const Image &zmImage, Zone** zones, Event::StringSet &zone
return alarm; return alarm;
} }
void Detector::_onCreateEvent(Zone** zones, Event* event)
{
for(std::vector<unsigned int>::iterator it = m_vnPluginZones.begin(); it != m_vnPluginZones.end(); ++it)
onCreateEvent(zones[*it], *it, event);
}
void Detector::_onCloseEvent(Zone** zones, Event* event)
{
for(std::vector<unsigned int>::iterator it = m_vnPluginZones.begin(); it != m_vnPluginZones.end(); ++it)
onCloseEvent(zones[*it], *it, event);
}

View File

@ -78,6 +78,9 @@ public:
//! Detect (in an image later) //! Detect (in an image later)
bool Detect(const Image &image, Zone** zones, Event::StringSet &zoneSet, unsigned int &score); bool Detect(const Image &image, Zone** zones, Event::StringSet &zoneSet, unsigned int &score);
void _onCreateEvent(Zone** zones, Event *event);
void _onCloseEvent(Zone** zones, Event *event);
//! Load detector's parameters. //! Load detector's parameters.
virtual int loadConfig(string sConfigFileName, map<unsigned int,map<string,string> > mapPluginConf) = 0; virtual int loadConfig(string sConfigFileName, map<unsigned int,map<string,string> > mapPluginConf) = 0;
@ -98,6 +101,9 @@ protected:
//! Do detection inside one given zone. //! Do detection inside one given zone.
virtual bool checkZone(Zone *zone, unsigned int n_zone, const Image *zmImage) = 0; virtual bool checkZone(Zone *zone, unsigned int n_zone, const Image *zmImage) = 0;
virtual void onCreateEvent(Zone *zone, unsigned int n_zone, Event *event) = 0;
virtual void onCloseEvent(Zone *zone, unsigned int n_zone, Event *event) = 0;
//! Log messages to the SYSLOG. //! Log messages to the SYSLOG.
void log(int, string sLevel, string sMessage); void log(int, string sLevel, string sMessage);

View File

@ -35,6 +35,7 @@
#include "zm_signal.h" #include "zm_signal.h"
#include "zm_event.h" #include "zm_event.h"
#include "zm_monitor.h" #include "zm_monitor.h"
#include "zm_utils.h"
#include "zmf.h" #include "zmf.h"
@ -230,6 +231,106 @@ void Event::AddCause( const std::string new_cause )
} }
} }
// This is the transcription of JavaScript function DeleteEvent()
void Event::DeleteEvent()
{
static char sql[ZM_SQL_MED_BUFSIZ];
snprintf( sql, sizeof(sql), "DELETE FROM `Events` WHERE `Id` = %d;", id );
if ( mysql_query( &dbconn, sql) )
{
Error( "Can't run query: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
if ( !config.opt_fast_delete )
{
snprintf( sql, sizeof(sql), "DELETE FROM `Stats` WHERE `EventId` = %d;", id );
if ( mysql_query( &dbconn, sql ) )
{
Error( "Can't run query: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
snprintf( sql, sizeof(sql), "DELETE FROM `Frames` WHERE `EventId` = %d;", id );
if ( mysql_query( &dbconn, sql ) )
{
Error( "Can't run query: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
static char dir_events[PATH_MAX] = "";
if ( config.dir_events[0] == '/' )
snprintf( dir_events, sizeof(dir_events), "%s", config.dir_events );
else
snprintf( dir_events, sizeof(dir_events), "%s/%s", staticConfig.PATH_WEB.c_str(), config.dir_events );
if ( config.use_deep_storage )
{
static char event_glob[PATH_MAX] = "";
snprintf( event_glob, sizeof(event_glob), "%s/%d/*/*/*/.%d", dir_events, monitor->Id(), id );
glob_t pglob;
if( glob( event_glob, GLOB_ONLYDIR, 0, &pglob ) == 0 )
{
char *event_path = pglob.gl_pathv[0];
static char link[PATH_MAX] = "";
ssize_t len;
if ( ( len = readlink(event_path, link, sizeof(link)-1 ) ) )
{
link[len] = '\0';
int last_slash = strrchr( event_path, '/' ) - event_path;
char base_path[PATH_MAX] = "";
strncpy(base_path, event_path, last_slash);
static char link_path[PATH_MAX] = "";
snprintf( link_path, sizeof(link_path), "%s/%s", base_path, link );
// Delete linked folder (remove_dir is called with second
// argument = true to force deletion of folder content)
if (remove_dir(link_path, true, false) < 0)
return;
// Delete symlink
if (unlink(event_path) < 0)
return;
// Now we have successfully deleted all files we can do some
// cleaning on the storage directory
// The storage folders are scanned from deep to root and
// deleted if empty
for(size_t i=strlen(link_path)-1; i>strlen(dir_events); i--)
{
if(link_path[i] != '/')
continue;
char del_path[PATH_MAX] = "";
strncpy(del_path, link_path, i);
// Deletion is stopped at first non empty folder
// encountered
if(remove_dir(del_path, false, false) < 0)
break;
}
}
else
{
// Delete broken symlink
unlink(event_path);
}
}
}
else
{
static char event_path[PATH_MAX] = "";
snprintf(event_path, sizeof(event_path), "%s/%d/%d", dir_events, monitor->Id(), id);
remove_dir(event_path, true, false);
}
}
}
int Event::sd = -1; int Event::sd = -1;
bool Event::OpenFrameSocket( int monitor_id ) bool Event::OpenFrameSocket( int monitor_id )

View File

@ -133,6 +133,7 @@ public:
void AddFrames( int n_frames, Image **images, struct timeval **timestamps ); void AddFrames( int n_frames, Image **images, struct timeval **timestamps );
void AddFrame( Image *image, struct timeval timestamp, int score=0, Image *alarm_frame=NULL ); void AddFrame( Image *image, struct timeval timestamp, int score=0, Image *alarm_frame=NULL );
void AddCause( const std::string new_cause ); void AddCause( const std::string new_cause );
void DeleteEvent();
private: private:
void AddFramesInternal( int n_frames, int start_frame, Image **images, struct timeval **timestamps ); void AddFramesInternal( int n_frames, int start_frame, Image **images, struct timeval **timestamps );

View File

@ -41,7 +41,25 @@ ImageAnalyser::~ImageAnalyser()
delete *It; delete *It;
} }
void ImageAnalyser::onCreateEvent(Zone** zones, Event* event)
{
for ( DetectorsList::iterator It = m_Detectors.begin();
It != m_Detectors.end();
++It )
{
(*It)->_onCreateEvent(zones, event);
}
}
void ImageAnalyser::onCloseEvent(Zone** zones, Event* event)
{
for ( DetectorsList::iterator It = m_Detectors.begin();
It != m_Detectors.end();
++It )
{
(*It)->_onCloseEvent(zones, event);
}
}
/*!\fn ImageAnalyser::DoDetection(const Image &comp_image, Zone** zones, Event::StringSetMap noteSetMap, string& det_cause) /*!\fn ImageAnalyser::DoDetection(const Image &comp_image, Zone** zones, Event::StringSetMap noteSetMap, string& det_cause)
* \param comp_image is the image to analyse * \param comp_image is the image to analyse

View File

@ -75,6 +75,9 @@ class ImageAnalyser {
m_Detectors.push_back(Det.release()); m_Detectors.push_back(Det.release());
} }
void onCreateEvent(Zone** zones, Event* event);
void onCloseEvent(Zone** zones, Event* event);
//! Do detection in an image by calling all available detectors. //! Do detection in an image by calling all available detectors.
bool DoDetection(const Image &comp_image, Zone** zones, Event::StringSetMap& noteSetMap, std::string& det_cause, unsigned int& score); bool DoDetection(const Image &comp_image, Zone** zones, Event::StringSetMap& noteSetMap, std::string& det_cause, unsigned int& score);

View File

@ -1520,7 +1520,10 @@ bool Monitor::Analyse()
// Create event // Create event
event = new Event( this, *timestamp, "Continuous", noteSetMap ); event = new Event( this, *timestamp, "Continuous", noteSetMap );
shared_data->last_event = event->Id(); shared_data->last_event = event->Id();
#if ZM_PLUGINS_ON
if (config.load_plugins)
ThePluginManager.getImageAnalyser().onCreateEvent( zones, event );
#endif // ZM_PLUGINS_ON
Info( "%s: %03d - Opening new event %d, section start", name, image_count, event->Id() ); Info( "%s: %03d - Opening new event %d, section start", name, image_count, event->Id() );
/* To prevent cancelling out an existing alert\prealarm\alarm state */ /* To prevent cancelling out an existing alert\prealarm\alarm state */
@ -1578,7 +1581,10 @@ bool Monitor::Analyse()
} }
event = new Event( this, *(image_buffer[pre_index].timestamp), cause, noteSetMap ); event = new Event( this, *(image_buffer[pre_index].timestamp), cause, noteSetMap );
shared_data->last_event = event->Id(); shared_data->last_event = event->Id();
#if ZM_PLUGINS_ON
if (config.load_plugins)
ThePluginManager.getImageAnalyser().onCreateEvent( zones, event );
#endif // ZM_PLUGINS_ON
Info( "%s: %03d - Opening new event %d, alarm start", name, image_count, event->Id() ); Info( "%s: %03d - Opening new event %d, alarm start", name, image_count, event->Id() );
if ( pre_event_images ) if ( pre_event_images )
@ -3114,6 +3120,10 @@ bool Monitor::closeEvent()
{ {
gettimeofday( &(event->EndTime()), NULL ); gettimeofday( &(event->EndTime()), NULL );
} }
if ( config.load_plugins && ( purpose == ANALYSIS ) )
{
ThePluginManager.getImageAnalyser().onCloseEvent( zones, event );
}
delete event; delete event;
event = 0; event = 0;
return( true ); return( true );

View File

@ -17,13 +17,17 @@
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// //
//#include "zm_logger.h" #include "zm_logger.h"
#include "zm.h" #include "zm.h"
#include "zm_utils.h" #include "zm_utils.h"
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <stdlib.h>
unsigned int sseversion = 0; unsigned int sseversion = 0;
@ -345,3 +349,63 @@ void timespec_diff(struct timespec *start, struct timespec *end, struct timespec
} }
} }
int remove_dir(const char *path, bool force, bool verbose)
{
DIR *d = opendir(path);
size_t path_len = strlen(path);
int r = -1;
if (d)
{
struct dirent *p;
r = 0;
while (!r && (p=readdir(d)))
{
int r2 = -1;
char *buf;
size_t len;
if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
continue;
if (!force)
{
if (verbose)
Error("Can't remove directory '%s/%s': (not empty)", path, p->d_name);
return r2;
}
len = path_len + strlen(p->d_name) + 2;
buf = (char*)malloc(len);
if (buf)
{
struct stat statbuf;
snprintf(buf, len, "%s/%s", path, p->d_name);
if (!stat(buf, &statbuf))
{
if (S_ISDIR(statbuf.st_mode))
{
if (((r2 = remove_dir(buf, force)) < 0) && verbose)
Error("Can't remove directory '%s': %s", buf, strerror(errno));
}
else
{
if (((r2 = unlink(buf)) < 0) && verbose)
Error("Can't remove file '%s': %s", buf, strerror(errno));
}
}
free(buf);
}
r = r2;
}
closedir(d);
}
if ((!r) && ((r = rmdir(path)) < 0) && verbose)
Error("Can't remove directory '%s': %s", path, strerror(errno));
return r;
}

View File

@ -60,4 +60,6 @@ void timespec_diff(struct timespec *start, struct timespec *end, struct timespec
extern unsigned int sseversion; extern unsigned int sseversion;
int remove_dir(const char *path, bool force = false, bool verbose = false);
#endif // ZM_UTILS_H #endif // ZM_UTILS_H

View File

@ -25,6 +25,8 @@
#include "zm_signal.h" #include "zm_signal.h"
#include "zm_monitor.h" #include "zm_monitor.h"
StaticConfig staticConfig;
void Usage() void Usage()
{ {
fprintf( stderr, "zma -m <monitor_id>\n" ); fprintf( stderr, "zma -m <monitor_id>\n" );

View File

@ -31,6 +31,8 @@
#include "zm_signal.h" #include "zm_signal.h"
#include "zm_monitor.h" #include "zm_monitor.h"
StaticConfig staticConfig;
void Usage() void Usage()
{ {
fprintf( stderr, "zmc -d <device_path> or -r <proto> -H <host> -P <port> -p <path> or -f <file_path> or -m <monitor_id>\n" ); fprintf( stderr, "zmc -d <device_path> or -r <proto> -H <host> -P <port> -p <path> or -f <file_path> or -m <monitor_id>\n" );

View File

@ -38,6 +38,8 @@
#include "zmf.h" #include "zmf.h"
StaticConfig staticConfig;
int OpenSocket( int monitor_id ) int OpenSocket( int monitor_id )
{ {
int sd = socket( AF_UNIX, SOCK_STREAM, 0); int sd = socket( AF_UNIX, SOCK_STREAM, 0);
@ -165,6 +167,7 @@ int main( int argc, char *argv[] )
char log_id_string[16]; char log_id_string[16];
snprintf( log_id_string, sizeof(log_id_string), "m%d", id ); snprintf( log_id_string, sizeof(log_id_string), "m%d", id );
StaticConfig staticConfig;
zmLoadConfig(); zmLoadConfig();
logInit( "zmf" ); logInit( "zmf" );

View File

@ -26,6 +26,8 @@
#include "zm_signal.h" #include "zm_signal.h"
#include "zm_monitor.h" #include "zm_monitor.h"
StaticConfig staticConfig;
bool ValidateAccess( User *user, int mon_id ) bool ValidateAccess( User *user, int mon_id )
{ {
bool allowed = true; bool allowed = true;
@ -86,7 +88,7 @@ int main( int argc, const char *argv[] )
{ {
nph = true; nph = true;
} }
zmLoadConfig(); zmLoadConfig();
logInit( "zms" ); logInit( "zms" );

View File

@ -45,6 +45,8 @@
#define ZMS_DEFAULT_FPS 25.0 #define ZMS_DEFAULT_FPS 25.0
#define ZMS_DEFAULT_BUFFER 1000 #define ZMS_DEFAULT_BUFFER 1000
StaticConfig staticConfig;
int main(int argc, char** argv) { int main(int argc, char** argv) {
self = argv[0]; self = argv[0];
// Set initial values to the default values // Set initial values to the default values

View File

@ -26,6 +26,8 @@
#include "zm_monitor.h" #include "zm_monitor.h"
#include "zm_local_camera.h" #include "zm_local_camera.h"
StaticConfig staticConfig;
void Usage( int status=-1 ) void Usage( int status=-1 )
{ {
fprintf( stderr, "zmu <-d device_path> [-v] [function] [-U<username> -P<password>]\n" ); fprintf( stderr, "zmu <-d device_path> [-v] [function] [-U<username> -P<password>]\n" );