198 lines
5.5 KiB
C++
198 lines
5.5 KiB
C++
#include <iostream>
|
|
#include <string>
|
|
#include <signal.h>
|
|
#include <syslog.h>
|
|
#include <unistd.h>
|
|
#include <vector>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include "Util/clipp.h"
|
|
#include "Util/Device.h"
|
|
#include "Util/INIh.h"
|
|
#include "Util/Logger.h"
|
|
#include "UniFi/Device.h"
|
|
#include "Bluetooth/Device.h"
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
try
|
|
{
|
|
setlogmask(LOG_UPTO(LOG_DEBUG));
|
|
openlog("PresenceDetection", LOG_NDELAY | LOG_PID, LOG_USER);
|
|
|
|
bool daemon = false;
|
|
PresenceDetection::Util::clipp::group cli {
|
|
PresenceDetection::Util::clipp::option("-d", "--daemon").set(daemon).doc("Run the process as Daemon")
|
|
};
|
|
|
|
if (!PresenceDetection::Util::clipp::parse(argc, argv, cli))
|
|
{
|
|
std::cout << PresenceDetection::Util::clipp::make_man_page(cli, argv[0]) << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
PresenceDetection::Util::INIReader iniReader("PresenceDetection.ini");
|
|
if (iniReader.ParseError() != 0)
|
|
{
|
|
std::cerr << "Can't read PresenceDetection.ini" << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
if (daemon)
|
|
{
|
|
PresenceDetection::Util::Logger::Log(PresenceDetection::Util::Logger::Severity::Info, "Starting Daemon");
|
|
pid_t pid, sid;
|
|
pid = fork();
|
|
|
|
if (pid < 0)
|
|
exit(EXIT_FAILURE);
|
|
if (pid > 0)
|
|
exit(EXIT_SUCCESS);
|
|
|
|
umask(0);
|
|
sid = setsid();
|
|
if (sid < 0)
|
|
exit(EXIT_FAILURE);
|
|
|
|
if ((chdir("/")) < 0)
|
|
exit(EXIT_FAILURE);
|
|
|
|
close(STDIN_FILENO);
|
|
close(STDOUT_FILENO);
|
|
close(STDERR_FILENO);
|
|
}
|
|
|
|
std::string target = iniReader.Get("PresenceDetection", "Target", "");
|
|
|
|
bool unifi = iniReader.GetBoolean("PresenceDetection", "UniFi", false);
|
|
|
|
bool bluetooth = iniReader.GetBoolean("PresenceDetection", "Bluetooth", false);
|
|
|
|
std::vector<PresenceDetection::Util::Device> devices;
|
|
|
|
auto sections = iniReader.Sections();
|
|
for (auto section : sections)
|
|
{
|
|
if (section != "PresenceDetection" &&
|
|
section != "UniFi" &&
|
|
section != "Bluetooth")
|
|
{
|
|
std::string WifiMac = iniReader.Get(section, "WifiMac", "");
|
|
std::string BluetoothMac = iniReader.Get(section, "BluetoothMac", "");
|
|
|
|
if (WifiMac.empty() && BluetoothMac.empty())
|
|
{
|
|
std::stringstream ss;
|
|
ss << section << ": No Wifi or Bluetooth Mac address defined" << std::endl;
|
|
throw std::runtime_error(ss.str());
|
|
}
|
|
|
|
auto device = PresenceDetection::Util::Device(WifiMac, BluetoothMac);
|
|
|
|
std::string OnlineURL = iniReader.Get(section, "OnlineURL", "");
|
|
if (!OnlineURL.empty())
|
|
device.SetOnlineURL(OnlineURL);
|
|
|
|
std::string WifiOnlineURL = iniReader.Get(section, "WifiOnlineURL", "");
|
|
if (!WifiOnlineURL.empty())
|
|
device.SetWifiOnlineURL(WifiOnlineURL);
|
|
|
|
std::string BluetoothOnlineURL = iniReader.Get(section, "BluetoothOnlineURL", "");
|
|
if (!BluetoothOnlineURL.empty())
|
|
device.SetBluetoothOnlineURL(BluetoothOnlineURL);
|
|
|
|
std::string OfflineURL = iniReader.Get(section, "OfflineURL", "");
|
|
if (!OfflineURL.empty())
|
|
device.SetOfflineURL(OfflineURL);
|
|
|
|
std::string WifiOfflineURL = iniReader.Get(section, "WifiOfflineURL", "");
|
|
if (!WifiOfflineURL.empty())
|
|
device.SetWifiOfflineURL(WifiOfflineURL);
|
|
|
|
std::string BluetoothOfflineURL = iniReader.Get(section, "BluetoothOfflineURL", "");
|
|
if (!BluetoothOfflineURL.empty())
|
|
device.SetBluetoothOfflineURL(BluetoothOfflineURL);
|
|
|
|
devices.push_back(device);
|
|
}
|
|
}
|
|
|
|
std::shared_ptr<PresenceDetection::UniFi::Device> pUniFiDevice;
|
|
if (unifi)
|
|
{
|
|
std::string hostname = iniReader.Get("UniFi", "Hostname", "");
|
|
if (hostname.empty())
|
|
throw std::runtime_error("UniFi Hostname directive missing in ini file.");
|
|
|
|
int port = iniReader.GetInteger("UniFi", "Port", 8443);
|
|
|
|
std::string username = iniReader.Get("UniFi", "Username", "");
|
|
if (username.empty())
|
|
throw std::runtime_error("UniFi Username directive missing in ini file.");
|
|
|
|
std::string password = iniReader.Get("UniFi", "Password", "");
|
|
if (password.empty())
|
|
throw std::runtime_error("UniFi Password directive missing in ini file.");
|
|
|
|
std::string cookiefile = iniReader.Get("UniFi", "CookieFile", "");
|
|
if (cookiefile.empty())
|
|
throw std::runtime_error("UniFi CookieFile directive missing in ini file.");
|
|
|
|
int timeout = iniReader.GetInteger("UniFi", "Timeout", 150);
|
|
|
|
std::string inventoryURL = iniReader.Get("UniFi", "Inventory", "");
|
|
|
|
pUniFiDevice = std::make_shared<PresenceDetection::UniFi::Device>(hostname, port, username, password, cookiefile, timeout, inventoryURL, target, devices);
|
|
}
|
|
|
|
std::shared_ptr<PresenceDetection::Bluetooth::Device> pBluetoothDevice;
|
|
if (bluetooth)
|
|
{
|
|
int timeout = iniReader.GetInteger("Bluetooth", "Timeout", 150);
|
|
|
|
std::string inventoryURL = iniReader.Get("Bluetooth", "Inventory", "");
|
|
|
|
pBluetoothDevice = std::make_shared<PresenceDetection::Bluetooth::Device>(timeout, inventoryURL, target, devices);
|
|
}
|
|
|
|
sigset_t wset;
|
|
sigemptyset(&wset);
|
|
sigaddset(&wset,SIGHUP);
|
|
sigaddset(&wset,SIGINT);
|
|
sigaddset(&wset,SIGTERM);
|
|
int sig;
|
|
sigwait(&wset,&sig);
|
|
|
|
PresenceDetection::Util::Logger::Log(PresenceDetection::Util::Logger::Severity::Info, "Stopping...");
|
|
|
|
if (pUniFiDevice)
|
|
pUniFiDevice->Stop();
|
|
|
|
if (pBluetoothDevice)
|
|
pBluetoothDevice->Stop();
|
|
|
|
if (pUniFiDevice)
|
|
pUniFiDevice->Wait();
|
|
|
|
if (pBluetoothDevice)
|
|
pBluetoothDevice->Wait();
|
|
|
|
closelog();
|
|
}
|
|
catch (const std::exception& e)
|
|
{
|
|
std::stringstream ss;
|
|
ss << "ERROR: " << e.what() << std::endl;
|
|
|
|
std::cerr << ss.str() << std::endl;
|
|
PresenceDetection::Util::Logger::Log(PresenceDetection::Util::Logger::Severity::Error, ss.str());
|
|
|
|
closelog();
|
|
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|