Merge branch 'storageareas' of github.com:ConnorTechnology/ZoneMinder into storageareas

This commit is contained in:
Isaac Connor 2018-05-15 12:44:36 -04:00
commit 4ca77fdd69
7 changed files with 60 additions and 88 deletions

View File

@ -108,9 +108,18 @@ sub zmDbConnect {
, $Config{ZM_DB_PASS} , $Config{ZM_DB_PASS}
); );
}; };
Error("Error reconnecting to db: errstr:$DBI::errstr error val:$@") if (! $dbh) or $@; if ( !$dbh or $@ ) {
$dbh->trace(0) if $dbh; 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; return $dbh;
} # end sub zmDbConnect } # end sub zmDbConnect

View File

@ -429,54 +429,10 @@ sub databaseLevel {
if ( $this->{databaseLevel} != $databaseLevel ) { if ( $this->{databaseLevel} != $databaseLevel ) {
if ( $databaseLevel > NOLOG and $this->{databaseLevel} <= NOLOG ) { if ( $databaseLevel > NOLOG and $this->{databaseLevel} <= NOLOG ) {
if ( !$this->{dbh} ) { if ( !$this->{dbh} ) {
my $socket; $this->{dbh} = ZoneMinder::Database::zmDbConnect();
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 );
}
} }
} elsif ( $databaseLevel <= NOLOG && $this->{databaseLevel} > NOLOG ) { } elsif ( $databaseLevel <= NOLOG && $this->{databaseLevel} > NOLOG ) {
if ( $this->{dbh} ) { undef($this->{dbh});
$this->{dbh}->disconnect();
undef($this->{dbh});
}
} }
$this->{databaseLevel} = $databaseLevel; $this->{databaseLevel} = $databaseLevel;
} }

View File

@ -453,7 +453,7 @@ sub start {
$cmd_hash{$process->{command}} = $pid_hash{$cpid} = $process; $cmd_hash{$process->{command}} = $pid_hash{$cpid} = $process;
sigprocmask(SIG_SETMASK, $sigset) or Fatal("Can't restore SIGCHLD: $!"); sigprocmask(SIG_SETMASK, $sigset) or Fatal("Can't restore SIGCHLD: $!");
Debug("unblocko child"); Debug("unblocking child");
} elsif ( defined($cpid) ) { } elsif ( defined($cpid) ) {
# Force reconnection to the db. # Force reconnection to the db.
$dbh = zmDbConnect(1); $dbh = zmDbConnect(1);

View File

@ -24,7 +24,6 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
Group::Group() { Group::Group() {
Warning("Instantiating default Group Object. Should not happen."); Warning("Instantiating default Group Object. Should not happen.");
@ -33,30 +32,31 @@ Group::Group() {
strcpy(name, "Default"); 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; unsigned int index = 0;
id = atoi( dbrow[index++] ); id = atoi(dbrow[index++]);
parent_id = dbrow[index] ? atoi( dbrow[index] ): 0; index++; parent_id = dbrow[index] ? atoi(dbrow[index]): 0; index++;
strncpy( name, dbrow[index++], sizeof(name)-1 ); strncpy(name, dbrow[index++], sizeof(name)-1);
} }
/* If a zero or invalid p_id is passed, then the old default path will be assumed. */ /* 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; id = 0;
if ( p_id ) { if ( p_id ) {
char sql[ZM_SQL_SML_BUFSIZ]; char sql[ZM_SQL_SML_BUFSIZ];
snprintf( sql, sizeof(sql), "SELECT Id, ParentId, Name FROM Group WHERE Id=%d", p_id ); 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 ); Debug(2,"Loading Group for %d using %s", p_id, sql);
zmDbRow dbrow; zmDbRow dbrow;
if ( ! dbrow.fetch( sql ) ) { if ( !dbrow.fetch(sql) ) {
Error( "Unable to load group for id %d: %s", p_id, mysql_error( &dbconn ) ); Error("Unable to load group for id %d: %s", p_id, mysql_error(&dbconn));
} else { } else {
unsigned int index = 0; unsigned int index = 0;
id = atoi( dbrow[index++] ); id = atoi(dbrow[index++]);
parent_id = dbrow[index] ? atoi( dbrow[index] ): 0; index++; parent_id = dbrow[index] ? atoi(dbrow[index]): 0; index++;
strncpy( name, dbrow[index++], sizeof(name)-1 ); strncpy(name, dbrow[index++], sizeof(name)-1);
Debug( 1, "Loaded Group area %d '%s'", id, this->Name() ); Debug(1, "Loaded Group area %d '%s'", id, this->Name());
} }
} }
if ( ! id ) { if ( ! id ) {

View File

@ -2800,19 +2800,21 @@ Monitor::Snapshot *Monitor::getSnapshot() const {
return &image_buffer[ shared_data->last_write_index%image_buffer_count ]; return &image_buffer[ shared_data->last_write_index%image_buffer_count ];
} }
std::list<Group *> Monitor::Groups() { std::vector<Group *> Monitor::Groups() {
// At the moment, only load groups once. // At the moment, only load groups once.
if ( ! groups.size() ) { 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()); MYSQL_RES *result = zmDbFetch(sql.c_str());
if ( !result ) { if ( !result ) {
Error("Can't load groups: %s", mysql_error(&dbconn)); Error("Can't load groups: %s", mysql_error(&dbconn));
return groups; return groups;
} }
int n_groups = mysql_num_rows(result); 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) ) { while ( MYSQL_ROW dbrow = mysql_fetch_row(result) ) {
groups.push_back( new Group(dbrow) ); groups.push_back(new Group(dbrow));
} }
if ( mysql_errno(&dbconn) ) { if ( mysql_errno(&dbconn) ) {
Error("Can't fetch row: %s", mysql_error(&dbconn)); Error("Can't fetch row: %s", mysql_error(&dbconn));
@ -2821,3 +2823,13 @@ std::list<Group *> Monitor::Groups() {
} }
return 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;
}

View File

@ -344,7 +344,7 @@ protected:
int n_linked_monitors; int n_linked_monitors;
MonitorLink **linked_monitors; MonitorLink **linked_monitors;
std::list<Group *> groups; std::vector<Group *> groups;
public: public:
explicit Monitor( int p_id ); explicit Monitor( int p_id );
@ -506,7 +506,8 @@ public:
bool DumpSettings( char *output, bool verbose ); bool DumpSettings( char *output, bool verbose );
void DumpZoneImage( const char *zone_string=0 ); void DumpZoneImage( const char *zone_string=0 );
std::list<Group *> Groups(); std::vector<Group *> Groups();
StringVector GroupNames();
static int LoadMonitors(std::string sql, Monitor **&monitors, Purpose purpose); // Returns # of Monitors loaded, 0 on failure. static int LoadMonitors(std::string sql, Monitor **&monitors, Purpose purpose); // Returns # of Monitors loaded, 0 on failure.
#if ZM_HAS_V4L #if ZM_HAS_V4L

View File

@ -94,26 +94,22 @@ const std::string stringtf( const std::string format, ... )
return( tempString ); return( tempString );
} }
bool startsWith( const std::string &haystack, const std::string &needle ) bool startsWith(const std::string &haystack, const std::string &needle) {
{ return( haystack.substr(0, needle.length()) == 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; StringVector stringVector;
std::string tempString = string; std::string tempString = string;
std::string::size_type startIndex = 0; std::string::size_type startIndex = 0;
std::string::size_type endIndex = 0; std::string::size_type endIndex = 0;
//Info( "Looking for '%s' in '%s', limit %d", chars.c_str(), string.c_str(), limit ); //Info( "Looking for '%s' in '%s', limit %d", chars.c_str(), string.c_str(), limit );
do do {
{
// Find delimiters // Find delimiters
endIndex = string.find_first_of( chars, startIndex ); endIndex = string.find_first_of( chars, startIndex );
//Info( "Got endIndex at %d", endIndex ); //Info( "Got endIndex at %d", endIndex );
if ( endIndex > 0 ) if ( endIndex > 0 ) {
{
//Info( "Adding '%s'", string.substr( startIndex, endIndex-startIndex ).c_str() ); //Info( "Adding '%s'", string.substr( startIndex, endIndex-startIndex ).c_str() );
stringVector.push_back( string.substr( startIndex, endIndex-startIndex ) ); 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; break;
// Find non-delimiters // Find non-delimiters
startIndex = tempString.find_first_not_of( chars, endIndex ); 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 ) ); stringVector.push_back( string.substr( startIndex ) );
break; break;
} }
@ -130,22 +125,21 @@ StringVector split( const std::string &string, const std::string &chars, int lim
} while ( startIndex != std::string::npos ); } while ( startIndex != std::string::npos );
//Info( "Finished with %d strings", stringVector.size() ); //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; std::stringstream ss;
for(size_t i = 0; i < v.size(); ++i) { for (size_t i = 0; i < v.size(); ++i) {
if(i != 0) if ( i != 0 )
ss << ","; ss << delim;
ss << v[i]; ss << v[i];
} }
return ss.str(); 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' }; static char base64_table[64] = { '\0' };
if ( !base64_table[0] ) if ( !base64_table[0] )