PresenceDetection.cc 3.7 KB

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