Files
PresenceDetection/Logic/Logic.cpp
2021-07-28 16:23:27 +02:00

126 lines
2.8 KiB
C++

/*
Logic ->
Contain all Configured Devices
-> Retrieve Devices from Domotica API
-> Retrieve Devices from Configuration
WiFi -> Check for Devices
Change to Offline
-> Ping (Internal to WiFi)
Fail
-> Update Device to Offline
Success
-> NOOP
Change to Online
-> Ping (Internal to WiFi)
Fail
-> NOOP
Success
-> Update Device to Online
Bluetooth -> Check for Devices
Change to Offline
Change to Online
API -> Poll Devices
- Who keeps track of the devices?
Logic?
Driver?
//*/
#include "Logic.h"
#include <Logging.h>
#include <json.hpp>
#include <algorithm>
#include <iostream>
#include <sstream>
namespace PresenceDetection {
namespace Logic {
Logic::Logic(const std::string& inventoryURL) :
m_inventoryURL(inventoryURL)
{
}
Logic::~Logic()
{
}
void Logic::LoadDevicesFromConfiguration()
{
}
void Logic::UpdateDevicesFromInventory()
{
try
{
Http::HttpRequest request(m_inventoryURL);
std::string devices = m_httpClient.Open(request);
nlohmann::json json = nlohmann::json::parse(devices);
m_dynamicDevices.clear();
for (auto& element : json)
{
int id = std::stoi(element["id"].get<std::string>());
std::string wifi;
std::string bluetooth;
//if (element["wifi"] != "")
if (element["macaddress"] != "")
{
//wifi = element["wifi"];
wifi = element["macaddress"];
std::transform(wifi.begin(), wifi.end(), wifi.begin(), ::tolower);
}
if (element["bluetooth"] != "")
{
bluetooth = element["bluetooth"];
std::transform(bluetooth.begin(), bluetooth.end(), bluetooth.begin(), ::tolower);
}
m_dynamicDevices.push_back(Device::Device(id, wifi, bluetooth));
}
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Logic::GetDevicesFromInventory() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
}
}
void Logic::DumpDevices()
{
std::cout << "Static Devices:" << std::endl;
for (auto& device : m_staticDevices)
{
std::cout << "ID: " << device.ID() << std::endl;
std::cout << "Wifi: " << device.WifiMacAddress() << std::endl;
std::cout << "Bluetooth: " << device.BluetoothMacAddress() << std::endl;
std::cout << "Online: " << device.Online() << std::endl;
std::cout << std::endl;
}
std::cout << "Dynamic Devices:" << std::endl;
for (auto& device : m_dynamicDevices)
{
std::cout << "ID: " << device.ID() << std::endl;
std::cout << "Wifi: " << device.WifiMacAddress() << std::endl;
std::cout << "Bluetooth: " << device.BluetoothMacAddress() << std::endl;
std::cout << "Online: " << device.Online() << std::endl;
std::cout << std::endl;
}
}
} // namespace Logic
} // namespace PresenceDetection