Logic.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Logic ->
  3. Contain all Configured Devices
  4. -> Retrieve Devices from Domotica API
  5. -> Retrieve Devices from Configuration
  6. WiFi -> Check for Devices
  7. Change to Offline
  8. -> Ping (Internal to WiFi)
  9. Fail
  10. -> Update Device to Offline
  11. Success
  12. -> NOOP
  13. Change to Online
  14. -> Ping (Internal to WiFi)
  15. Fail
  16. -> NOOP
  17. Success
  18. -> Update Device to Online
  19. Bluetooth -> Check for Devices
  20. Change to Offline
  21. Change to Online
  22. API -> Poll Devices
  23. - Who keeps track of the devices?
  24. Logic?
  25. Driver?
  26. //*/
  27. #include "Logic.h"
  28. #include <Logging.h>
  29. #include <json.hpp>
  30. #include <algorithm>
  31. #include <iostream>
  32. #include <sstream>
  33. namespace PresenceDetection {
  34. namespace Logic {
  35. Logic::Logic(const std::string& inventoryURL) :
  36. m_inventoryURL(inventoryURL)
  37. {
  38. }
  39. Logic::~Logic()
  40. {
  41. }
  42. void Logic::LoadDevicesFromConfiguration()
  43. {
  44. }
  45. void Logic::UpdateDevicesFromInventory()
  46. {
  47. try
  48. {
  49. Http::HttpRequest request(m_inventoryURL);
  50. std::string devices = m_httpClient.Open(request);
  51. nlohmann::json json = nlohmann::json::parse(devices);
  52. m_dynamicDevices.clear();
  53. for (auto& element : json)
  54. {
  55. int id = std::stoi(element["id"].get<std::string>());
  56. std::string wifi;
  57. std::string bluetooth;
  58. //if (element["wifi"] != "")
  59. if (element["macaddress"] != "")
  60. {
  61. //wifi = element["wifi"];
  62. wifi = element["macaddress"];
  63. std::transform(wifi.begin(), wifi.end(), wifi.begin(), ::tolower);
  64. }
  65. if (element["bluetooth"] != "")
  66. {
  67. bluetooth = element["bluetooth"];
  68. std::transform(bluetooth.begin(), bluetooth.end(), bluetooth.begin(), ::tolower);
  69. }
  70. m_dynamicDevices.push_back(Device::Device(id, wifi, bluetooth));
  71. }
  72. }
  73. catch (const std::exception& e)
  74. {
  75. std::stringstream ss;
  76. ss << "Logic::GetDevicesFromInventory() - Error: " << e.what() << std::endl;
  77. Logging::Log(Logging::Severity::Error, ss.str());
  78. }
  79. }
  80. void Logic::DumpDevices()
  81. {
  82. std::cout << "Static Devices:" << std::endl;
  83. for (auto& device : m_staticDevices)
  84. {
  85. std::cout << "ID: " << device.ID() << std::endl;
  86. std::cout << "Wifi: " << device.WifiMacAddress() << std::endl;
  87. std::cout << "Bluetooth: " << device.BluetoothMacAddress() << std::endl;
  88. std::cout << "Online: " << device.Online() << std::endl;
  89. std::cout << std::endl;
  90. }
  91. std::cout << "Dynamic Devices:" << std::endl;
  92. for (auto& device : m_dynamicDevices)
  93. {
  94. std::cout << "ID: " << device.ID() << std::endl;
  95. std::cout << "Wifi: " << device.WifiMacAddress() << std::endl;
  96. std::cout << "Bluetooth: " << device.BluetoothMacAddress() << std::endl;
  97. std::cout << "Online: " << device.Online() << std::endl;
  98. std::cout << std::endl;
  99. }
  100. }
  101. } // namespace Logic
  102. } // namespace PresenceDetection