PresenceDetection.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <iostream>
  2. #include <string>
  3. #include <signal.h>
  4. #include <syslog.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include "Util/clipp.h"
  9. #include "Util/INIh.h"
  10. #include "Util/Logger.h"
  11. #include "UniFi/Device.h"
  12. #include "Bluetooth/Device.h"
  13. int main(int argc, char** argv)
  14. {
  15. try
  16. {
  17. setlogmask(LOG_UPTO(LOG_DEBUG));
  18. openlog("PresenceDetection", LOG_NDELAY | LOG_PID, LOG_USER);
  19. bool daemon = false;
  20. PresenceDetection::Util::group cli {
  21. PresenceDetection::Util::option("-d", "--daemon").set(daemon).doc("Run the process as Daemon")
  22. };
  23. if (!PresenceDetection::Util::parse(argc, argv, cli))
  24. {
  25. std::cout << PresenceDetection::Util::make_man_page(cli, argv[0]) << std::endl;
  26. return 1;
  27. }
  28. PresenceDetection::Util::INIReader iniReader("PresenceDetection.ini");
  29. if (iniReader.ParseError() != 0)
  30. {
  31. std::cerr << "Can't read PresenceDetection.ini" << std::endl;
  32. return 1;
  33. }
  34. if (daemon)
  35. {
  36. PresenceDetection::Util::Logger::Log(PresenceDetection::Util::Logger::Severity::Info, "Starting Daemon");
  37. pid_t pid, sid;
  38. pid = fork();
  39. if (pid < 0)
  40. exit(EXIT_FAILURE);
  41. if (pid > 0)
  42. exit(EXIT_SUCCESS);
  43. umask(0);
  44. sid = setsid();
  45. if (sid < 0)
  46. exit(EXIT_FAILURE);
  47. if ((chdir("/")) < 0)
  48. exit(EXIT_FAILURE);
  49. close(STDIN_FILENO);
  50. close(STDOUT_FILENO);
  51. close(STDERR_FILENO);
  52. }
  53. std::string target = iniReader.Get("PresenceDetection", "Target", "");
  54. if (target.empty())
  55. throw std::runtime_error("Target directive missing in ini file.");
  56. bool unifi = iniReader.GetBoolean("PresenceDetection", "UniFi", false);
  57. bool bluetooth = iniReader.GetBoolean("PresenceDetection", "Bluetooth", false);
  58. std::shared_ptr<PresenceDetection::UniFi::Device> pUniFiDevice;
  59. if (unifi)
  60. {
  61. std::string hostname = iniReader.Get("UniFi", "Hostname", "");
  62. if (hostname.empty())
  63. throw std::runtime_error("UniFi Hostname directive missing in ini file.");
  64. std::string username = iniReader.Get("UniFi", "Username", "");
  65. if (username.empty())
  66. throw std::runtime_error("UniFi Username directive missing in ini file.");
  67. std::string password = iniReader.Get("UniFi", "Password", "");
  68. if (password.empty())
  69. throw std::runtime_error("UniFi Password directive missing in ini file.");
  70. std::string cookiefile = iniReader.Get("UniFi", "CookieFile", "");
  71. if (cookiefile.empty())
  72. throw std::runtime_error("UniFi CookieFile directive missing in ini file.");
  73. std::string inventoryURL = iniReader.Get("UniFi", "Inventory", "");
  74. if (inventoryURL.empty())
  75. throw std::runtime_error("UniFi Inventory directive missing in ini file.");
  76. pUniFiDevice = std::make_shared<PresenceDetection::UniFi::Device>(hostname, username, password, cookiefile, inventoryURL, target);
  77. }
  78. std::shared_ptr<PresenceDetection::Bluetooth::Device> pBluetoothDevice;
  79. if (bluetooth)
  80. {
  81. std::string inventoryURL = iniReader.Get("Bluetooth", "Inventory", "");
  82. if (inventoryURL.empty())
  83. throw std::runtime_error("Bluetooth Inventory directive missing in ini file.");
  84. pBluetoothDevice = std::make_shared<PresenceDetection::Bluetooth::Device>(inventoryURL, target);
  85. }
  86. sigset_t wset;
  87. sigemptyset(&wset);
  88. sigaddset(&wset,SIGHUP);
  89. sigaddset(&wset,SIGINT);
  90. sigaddset(&wset,SIGTERM);
  91. int sig;
  92. sigwait(&wset,&sig);
  93. PresenceDetection::Util::Logger::Log(PresenceDetection::Util::Logger::Severity::Info, "Stopping...");
  94. if (pUniFiDevice)
  95. pUniFiDevice->Stop();
  96. if (pBluetoothDevice)
  97. pBluetoothDevice->Stop();
  98. if (pUniFiDevice)
  99. pUniFiDevice->Wait();
  100. if (pBluetoothDevice)
  101. pBluetoothDevice->Wait();
  102. closelog();
  103. }
  104. catch (const std::exception& e)
  105. {
  106. std::stringstream ss;
  107. ss << "ERROR: " << e.what() << std::endl;
  108. std::cerr << ss.str() << std::endl;
  109. PresenceDetection::Util::Logger::Log(PresenceDetection::Util::Logger::Severity::Error, ss.str());
  110. closelog();
  111. return -1;
  112. }
  113. return 0;
  114. }