From 6765d3088b7aecc63421f9cec3ce72f305095777 Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Sat, 1 Nov 2014 10:22:12 -0400 Subject: [PATCH] Daemon supports changing config file location from command line --- src/daemon.cpp | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/daemon.cpp b/src/daemon.cpp index 05b139f..632067b 100644 --- a/src/daemon.cpp +++ b/src/daemon.cpp @@ -30,8 +30,9 @@ bool uploadPost(std::string url, std::string data); void dataUploadThread(void* arg); // Constants +const std::string ALPRD_CONFIG_FILE_NAME="alprd.conf"; +const std::string OPENALPR_CONFIG_FILE_NAME="openalpr.conf"; const std::string DEFAULT_LOG_FILE_PATH="/var/log/alprd.log"; -const std::string DAEMON_CONFIG_FILE_PATH="/etc/openalpr/alprd.conf"; const std::string BEANSTALK_QUEUE_HOST="127.0.0.1"; const int BEANSTALK_PORT=11300; @@ -84,11 +85,11 @@ int main( int argc, const char** argv ) bool clockOn = false; std::string logFile; - std::string configFile; + std::string configDir; TCLAP::CmdLine cmd("OpenAlpr Daemon", ' ', Alpr::getVersion()); - TCLAP::ValueArg configFileArg("","config","Path to the openalpr.conf file.",false, "" ,"config_file"); + TCLAP::ValueArg configDirArg("","config","Path to the openalpr config directory that contains alprd.conf and openalpr.conf. (Default: /etc/openalpr/)",false, "/etc/openalpr/" ,"config_file"); TCLAP::ValueArg logFileArg("l","log","Log file to write to. Default=" + DEFAULT_LOG_FILE_PATH,false, DEFAULT_LOG_FILE_PATH ,"topN"); TCLAP::SwitchArg daemonOffSwitch("f","foreground","Set this flag for debugging. Disables forking the process as a daemon and runs in the foreground. Default=off", cmd, false); @@ -97,7 +98,7 @@ int main( int argc, const char** argv ) try { - cmd.add( configFileArg ); + cmd.add( configDirArg ); cmd.add( logFileArg ); @@ -107,7 +108,11 @@ int main( int argc, const char** argv ) return 1; } - configFile = configFileArg.getValue(); + // Make sure configDir ends in a slash + configDir = configDirArg.getValue(); + if (hasEnding(configDir, "/") == false) + configDir = configDir + "/"; + logFile = logFileArg.getValue(); noDaemon = daemonOffSwitch.getValue(); clockOn = clockSwitch.getValue(); @@ -118,6 +123,21 @@ int main( int argc, const char** argv ) return 1; } + std::string openAlprConfigFile = configDir + OPENALPR_CONFIG_FILE_NAME; + std::string daemonConfigFile = configDir + ALPRD_CONFIG_FILE_NAME; + + // Validate that the configuration files exist + if (fileExists(openAlprConfigFile.c_str()) == false) + { + std::cerr << "error, openalpr.conf file does not exist at: " << openAlprConfigFile << std::endl; + return 1; + } + if (fileExists(daemonConfigFile.c_str()) == false) + { + std::cerr << "error, alprd.conf file does not exist at: " << daemonConfigFile << std::endl; + return 1; + } + log4cplus::BasicConfigurator config; config.configure(); @@ -150,7 +170,7 @@ int main( int argc, const char** argv ) CSimpleIniA ini; ini.SetMultiKey(); - ini.LoadFile(DAEMON_CONFIG_FILE_PATH.c_str()); + ini.LoadFile(daemonConfigFile.c_str()); std::vector stream_urls; @@ -183,6 +203,7 @@ int main( int argc, const char** argv ) std::string upload_url = ini.GetValue("daemon", "upload_address", ""); std::string site_id = ini.GetValue("daemon", "site_id", ""); + LOG4CPLUS_INFO(logger, "Using: " << daemonConfigFile << " for daemon configuration"); LOG4CPLUS_INFO(logger, "Using: " << imageFolder << " for storing valid plate images"); pid_t pid; @@ -196,7 +217,7 @@ int main( int argc, const char** argv ) CaptureThreadData* tdata = new CaptureThreadData(); tdata->stream_url = stream_urls[i]; tdata->camera_id = i + 1; - tdata->config_file = configFile; + tdata->config_file = openAlprConfigFile; tdata->output_images = storePlates; tdata->output_image_folder = imageFolder; tdata->country_code = country;