Remove Boost Link Dependency

This commit is contained in:
2019-04-26 11:56:54 +02:00
parent f1a134c8d6
commit f96a45e3a2
9 changed files with 157 additions and 296 deletions
+31 -63
View File
@@ -2,12 +2,11 @@
#include <string>
#include <signal.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <boost/lexical_cast.hpp>
#include <boost/program_options.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include "Util/clipp.h"
#include "Util/INIh.h"
#include "UniFi/Device.h"
#include "Bluetooth/Device.h"
@@ -19,27 +18,23 @@ int main(int argc, char** argv)
setlogmask(LOG_UPTO(LOG_INFO));
openlog("PresenceDetection", LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
boost::program_options::options_description description("Options");
description.add_options()
("help,h", "Provide Help Message")
("daemon,d", "Run the process as Daemon");
bool daemon = false;
PresenceDetection::Util::group cli {
PresenceDetection::Util::option("-d", "--daemon").set(daemon).doc("Run the process as Daemon")
};
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, description), vm);
boost::program_options::notify(vm);
if (vm.count("help"))
if (!PresenceDetection::Util::parse(argc, argv, cli))
{
std::cerr << description << std::endl;
std::cout << PresenceDetection::Util::make_man_page(cli, argv[0]) << std::endl;
return 1;
}
bool daemon = false;
if (vm.count("daemon"))
daemon = true;
boost::property_tree::ptree pt;
boost::property_tree::ini_parser::read_ini("PresenceDetection.ini", pt);
PresenceDetection::Util::INIReader iniReader("PresenceDetection.ini");
if (iniReader.ParseError() != 0)
{
std::cerr << "Can't read PresenceDetection.ini" << std::endl;
return 1;
}
if (daemon)
{
@@ -65,59 +60,35 @@ int main(int argc, char** argv)
close(STDERR_FILENO);
}
std::string target;
boost::optional<boost::property_tree::ptree&> presenceDetectionTarget = pt.get_child_optional("PresenceDetection.Target");
if (presenceDetectionTarget)
target = pt.get<std::string>("PresenceDetection.Target");
else
std::string target = iniReader.Get("PresenceDetection", "Target", "");
if (target.empty())
throw std::runtime_error("Target directive missing in ini file.");
bool unifi = false;
boost::optional<boost::property_tree::ptree&> presenceDetectionUniFi = pt.get_child_optional("PresenceDetection.UniFi");
if (presenceDetectionUniFi)
unifi = (pt.get<std::string>("PresenceDetection.UniFi") == "On");
bool unifi = iniReader.GetBoolean("PresenceDetection", "UniFi", false);
bool bluetooth = false;
boost::optional<boost::property_tree::ptree&> presenceDetectionBluetooth = pt.get_child_optional("PresenceDetection.Bluetooth");
if (presenceDetectionBluetooth)
bluetooth = (pt.get<std::string>("PresenceDetection.Bluetooth") == "On");
bool bluetooth = iniReader.GetBoolean("PresenceDetection", "Bluetooth", false);
std::shared_ptr<PresenceDetection::UniFi::Device> pUniFiDevice;
if (unifi)
{
std::string hostname;
boost::optional<boost::property_tree::ptree&> unifiHostname = pt.get_child_optional("UniFi.Hostname");
if (unifiHostname)
hostname = pt.get<std::string>("UniFi.Hostname");
else
std::string hostname = iniReader.Get("UniFi", "Hostname", "");
if (hostname.empty())
throw std::runtime_error("UniFi Hostname directive missing in ini file.");
std::string username;
boost::optional<boost::property_tree::ptree&> unifiUsername = pt.get_child_optional("UniFi.Username");
if (unifiUsername)
username = pt.get<std::string>("UniFi.Username");
else
std::string username = iniReader.Get("UniFi", "Username", "");
if (username.empty())
throw std::runtime_error("UniFi Username directive missing in ini file.");
std::string password;
boost::optional<boost::property_tree::ptree&> unifiPassword = pt.get_child_optional("UniFi.Password");
if (unifiPassword)
password = pt.get<std::string>("UniFi.Password");
else
std::string password = iniReader.Get("UniFi", "Password", "");
if (password.empty())
throw std::runtime_error("UniFi Password directive missing in ini file.");
std::string cookiefile;
boost::optional<boost::property_tree::ptree&> unifiCookieFile = pt.get_child_optional("UniFi.CookieFile");
if (unifiCookieFile)
cookiefile = pt.get<std::string>("UniFi.CookieFile");
else
std::string cookiefile = iniReader.Get("UniFi", "CookieFile", "");
if (cookiefile.empty())
throw std::runtime_error("UniFi CookieFile directive missing in ini file.");
std::string inventoryURL;
boost::optional<boost::property_tree::ptree&> bluetoothInventoryURL = pt.get_child_optional("UniFi.Inventory");
if (bluetoothInventoryURL)
inventoryURL = pt.get<std::string>("UniFi.Inventory");
else
std::string inventoryURL = iniReader.Get("UniFi", "Inventory", "");
if (inventoryURL.empty())
throw std::runtime_error("UniFi Inventory directive missing in ini file.");
pUniFiDevice = std::make_shared<PresenceDetection::UniFi::Device>(hostname, username, password, cookiefile, inventoryURL, target);
@@ -126,11 +97,8 @@ int main(int argc, char** argv)
std::shared_ptr<PresenceDetection::Bluetooth::Device> pBluetoothDevice;
if (bluetooth)
{
std::string inventoryURL;
boost::optional<boost::property_tree::ptree&> bluetoothInventoryURL = pt.get_child_optional("Bluetooth.Inventory");
if (bluetoothInventoryURL)
inventoryURL = pt.get<std::string>("Bluetooth.Inventory");
else
std::string inventoryURL = iniReader.Get("Bluetooth", "Inventory", "");
if (inventoryURL.empty())
throw std::runtime_error("Bluetooth Inventory directive missing in ini file.");
pBluetoothDevice = std::make_shared<PresenceDetection::Bluetooth::Device>(inventoryURL, target);