diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Database.pm b/scripts/ZoneMinder/lib/ZoneMinder/Database.pm index 6126ac761..c39a6a73f 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Database.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Database.pm @@ -108,9 +108,18 @@ sub zmDbConnect { , $Config{ZM_DB_PASS} ); }; - Error("Error reconnecting to db: errstr:$DBI::errstr error val:$@") if (! $dbh) or $@; - $dbh->trace(0) if $dbh; - } + if ( !$dbh or $@ ) { + Error("Error reconnecting to db: errstr:$DBI::errstr error val:$@"); + } else { + $dbh->{AutoCommit} = 1; + Fatal('Can\'t set AutoCommit on in database connection') + unless $dbh->{AutoCommit}; + $dbh->{mysql_auto_reconnect} = 1; + Fatal('Can\'t set mysql_auto_reconnect on in database connection') + unless $dbh->{mysql_auto_reconnect}; + $dbh->trace( 0 ); + } # end if success connecting + } # end if ! connected return $dbh; } # end sub zmDbConnect diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm b/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm index 8333eb75b..a455bf71c 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm @@ -429,54 +429,10 @@ sub databaseLevel { if ( $this->{databaseLevel} != $databaseLevel ) { if ( $databaseLevel > NOLOG and $this->{databaseLevel} <= NOLOG ) { if ( !$this->{dbh} ) { - my $socket; - my ( $host, $portOrSocket ) = ( $Config{ZM_DB_HOST} =~ /^([^:]+)(?::(.+))?$/ ); - - if ( defined($portOrSocket) ) { - if ( $portOrSocket =~ /^\// ) { - $socket = ';mysql_socket='.$portOrSocket; - } else { - $socket = ';host='.$host.';port='.$portOrSocket; - } - } else { - $socket = ';host='.$Config{ZM_DB_HOST}; - } - my $sslOptions = ''; - if ( $Config{ZM_DB_SSL_CA_CERT} ) { - $sslOptions = join(';','', - 'mysql_ssl=1', - 'mysql_ssl_ca_file='.$Config{ZM_DB_SSL_CA_CERT}, - 'mysql_ssl_client_key='.$Config{ZM_DB_SSL_CLIENT_KEY}, - 'mysql_ssl_client_cert='.$Config{ZM_DB_SSL_CLIENT_CERT} - ); - } - $this->{dbh} = DBI->connect( 'DBI:mysql:database='.$Config{ZM_DB_NAME} - .$socket.$sslOptions - , $Config{ZM_DB_USER} - , $Config{ZM_DB_PASS} - ); - if ( !$this->{dbh} ) { - $databaseLevel = NOLOG; - Error( 'Unable to write log entries to DB, can\'t connect to database ' - .$Config{ZM_DB_NAME} - .' on host ' - .$Config{ZM_DB_HOST} - ); - } else { - $this->{dbh}->{AutoCommit} = 1; - Fatal('Can\'t set AutoCommit on in database connection' ) - unless( $this->{dbh}->{AutoCommit} ); - $this->{dbh}->{mysql_auto_reconnect} = 1; - Fatal('Can\'t set mysql_auto_reconnect on in database connection' ) - unless( $this->{dbh}->{mysql_auto_reconnect} ); - $this->{dbh}->trace( 0 ); - } + $this->{dbh} = ZoneMinder::Database::zmDbConnect(); } } elsif ( $databaseLevel <= NOLOG && $this->{databaseLevel} > NOLOG ) { - if ( $this->{dbh} ) { - $this->{dbh}->disconnect(); - undef($this->{dbh}); - } + undef($this->{dbh}); } $this->{databaseLevel} = $databaseLevel; } diff --git a/scripts/zmdc.pl.in b/scripts/zmdc.pl.in index ecf6bf7fe..3ddf07e15 100644 --- a/scripts/zmdc.pl.in +++ b/scripts/zmdc.pl.in @@ -453,7 +453,7 @@ sub start { $cmd_hash{$process->{command}} = $pid_hash{$cpid} = $process; sigprocmask(SIG_SETMASK, $sigset) or Fatal("Can't restore SIGCHLD: $!"); - Debug("unblocko child"); + Debug("unblocking child"); } elsif ( defined($cpid) ) { # Force reconnection to the db. $dbh = zmDbConnect(1); diff --git a/src/zm_group.cpp b/src/zm_group.cpp index 734df1c94..7b96920b8 100644 --- a/src/zm_group.cpp +++ b/src/zm_group.cpp @@ -24,7 +24,6 @@ #include #include -#include Group::Group() { Warning("Instantiating default Group Object. Should not happen."); @@ -33,30 +32,31 @@ Group::Group() { strcpy(name, "Default"); } -Group::Group( MYSQL_ROW &dbrow ) { +// The order of columns is: Id, ParentId, Name +Group::Group(MYSQL_ROW &dbrow) { unsigned int index = 0; - id = atoi( dbrow[index++] ); - parent_id = dbrow[index] ? atoi( dbrow[index] ): 0; index++; - strncpy( name, dbrow[index++], sizeof(name)-1 ); + id = atoi(dbrow[index++]); + parent_id = dbrow[index] ? atoi(dbrow[index]): 0; index++; + strncpy(name, dbrow[index++], sizeof(name)-1); } /* If a zero or invalid p_id is passed, then the old default path will be assumed. */ -Group::Group( unsigned int p_id ) { +Group::Group(unsigned int p_id) { id = 0; if ( p_id ) { char sql[ZM_SQL_SML_BUFSIZ]; - snprintf( sql, sizeof(sql), "SELECT Id, ParentId, Name FROM Group WHERE Id=%d", p_id ); - Debug(2,"Loading Group for %d using %s", p_id, sql ); + snprintf(sql, sizeof(sql), "SELECT Id, ParentId, Name FROM Group WHERE Id=%d", p_id); + Debug(2,"Loading Group for %d using %s", p_id, sql); zmDbRow dbrow; - if ( ! dbrow.fetch( sql ) ) { - Error( "Unable to load group for id %d: %s", p_id, mysql_error( &dbconn ) ); + if ( !dbrow.fetch(sql) ) { + Error("Unable to load group for id %d: %s", p_id, mysql_error(&dbconn)); } else { unsigned int index = 0; - id = atoi( dbrow[index++] ); - parent_id = dbrow[index] ? atoi( dbrow[index] ): 0; index++; - strncpy( name, dbrow[index++], sizeof(name)-1 ); - Debug( 1, "Loaded Group area %d '%s'", id, this->Name() ); + id = atoi(dbrow[index++]); + parent_id = dbrow[index] ? atoi(dbrow[index]): 0; index++; + strncpy(name, dbrow[index++], sizeof(name)-1); + Debug(1, "Loaded Group area %d '%s'", id, this->Name()); } } if ( ! id ) { diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index 04c81492b..d9b07e40f 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -2800,19 +2800,21 @@ Monitor::Snapshot *Monitor::getSnapshot() const { return &image_buffer[ shared_data->last_write_index%image_buffer_count ]; } -std::list Monitor::Groups() { +std::vector Monitor::Groups() { // At the moment, only load groups once. if ( ! groups.size() ) { - std::string sql = stringtf("SELECT GroupId FROM Groups_Monitors WHERE MonitorId=%d",id); + std::string sql = stringtf( + "SELECT Id,ParentId,Name FROM Groups WHERE Groups.Id IN " + "(SELECT GroupId FROM Groups_Monitors WHERE MonitorId=%d)",id); MYSQL_RES *result = zmDbFetch(sql.c_str()); if ( !result ) { Error("Can't load groups: %s", mysql_error(&dbconn)); return groups; } int n_groups = mysql_num_rows(result); - Debug( 1, "Got %d groups", n_groups ); + Debug(1, "Got %d groups", n_groups); while ( MYSQL_ROW dbrow = mysql_fetch_row(result) ) { - groups.push_back( new Group(dbrow) ); + groups.push_back(new Group(dbrow)); } if ( mysql_errno(&dbconn) ) { Error("Can't fetch row: %s", mysql_error(&dbconn)); @@ -2821,3 +2823,13 @@ std::list Monitor::Groups() { } return groups; } + +StringVector Monitor::GroupNames() { + StringVector groupnames; + for(Group * g: Groups()) { + groupnames.push_back(std::string(g->Name())); +Debug(1,"Groups: %s", g->Name()); + } + return groupnames; +} + diff --git a/src/zm_monitor.h b/src/zm_monitor.h index 0e5f1d44e..6eb40a7a2 100644 --- a/src/zm_monitor.h +++ b/src/zm_monitor.h @@ -344,7 +344,7 @@ protected: int n_linked_monitors; MonitorLink **linked_monitors; - std::list groups; + std::vector groups; public: explicit Monitor( int p_id ); @@ -506,7 +506,8 @@ public: bool DumpSettings( char *output, bool verbose ); void DumpZoneImage( const char *zone_string=0 ); - std::list Groups(); + std::vector Groups(); + StringVector GroupNames(); static int LoadMonitors(std::string sql, Monitor **&monitors, Purpose purpose); // Returns # of Monitors loaded, 0 on failure. #if ZM_HAS_V4L diff --git a/src/zm_utils.cpp b/src/zm_utils.cpp index 111dd8f09..94c681dd5 100644 --- a/src/zm_utils.cpp +++ b/src/zm_utils.cpp @@ -94,26 +94,22 @@ const std::string stringtf( const std::string format, ... ) return( tempString ); } -bool startsWith( const std::string &haystack, const std::string &needle ) -{ - return( haystack.substr( 0, needle.length() ) == needle ); +bool startsWith(const std::string &haystack, const std::string &needle) { + return( haystack.substr(0, needle.length()) == needle ); } -StringVector split( const std::string &string, const std::string &chars, int limit ) -{ +StringVector split(const std::string &string, const std::string &chars, int limit) { StringVector stringVector; std::string tempString = string; std::string::size_type startIndex = 0; std::string::size_type endIndex = 0; //Info( "Looking for '%s' in '%s', limit %d", chars.c_str(), string.c_str(), limit ); - do - { + do { // Find delimiters endIndex = string.find_first_of( chars, startIndex ); //Info( "Got endIndex at %d", endIndex ); - if ( endIndex > 0 ) - { + if ( endIndex > 0 ) { //Info( "Adding '%s'", string.substr( startIndex, endIndex-startIndex ).c_str() ); stringVector.push_back( string.substr( startIndex, endIndex-startIndex ) ); } @@ -121,8 +117,7 @@ StringVector split( const std::string &string, const std::string &chars, int lim break; // Find non-delimiters startIndex = tempString.find_first_not_of( chars, endIndex ); - if ( limit && (stringVector.size() == (unsigned int)(limit-1)) ) - { + if ( limit && (stringVector.size() == (unsigned int)(limit-1)) ) { stringVector.push_back( string.substr( startIndex ) ); break; } @@ -130,22 +125,21 @@ StringVector split( const std::string &string, const std::string &chars, int lim } while ( startIndex != std::string::npos ); //Info( "Finished with %d strings", stringVector.size() ); - return( stringVector ); + return stringVector; } -const std::string join(const StringVector &v, const char * delim ) { +const std::string join(const StringVector &v, const char * delim=",") { std::stringstream ss; - for(size_t i = 0; i < v.size(); ++i) { - if(i != 0) - ss << ","; + for (size_t i = 0; i < v.size(); ++i) { + if ( i != 0 ) + ss << delim; ss << v[i]; } return ss.str(); } -const std::string base64Encode( const std::string &inString ) -{ +const std::string base64Encode(const std::string &inString) { static char base64_table[64] = { '\0' }; if ( !base64_table[0] )