Add Static Devices, Timeout and UniFi Port Directive

This commit is contained in:
2020-03-05 16:56:12 +01:00
parent 2fbec833a2
commit 7b8fcc9722
8 changed files with 556 additions and 35 deletions
+120 -9
View File
@@ -9,11 +9,15 @@
namespace PresenceDetection {
namespace Bluetooth {
Device::Device(const std::string& inventoryURL, const std::string& target) :
Device::Device(int timeout, const std::string& inventoryURL, const std::string& target, const std::vector<Util::Device>& devices) :
m_timeout(timeout),
m_inventoryURL(inventoryURL),
m_target(target)
m_target(target),
m_staticDevices(devices)
{
UpdateDevicesFromInventory();
if (!m_inventoryURL.empty())
UpdateDevicesFromInventory();
ClearDevices();
Start();
}
@@ -35,6 +39,38 @@ void Device::Stop()
void Device::Wait()
{
m_deviceTimer.Wait();
void Device::ClearDevices()
{
for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it)
{
try
{
SendStateChange(false, *it);
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Bluetooth::Device::ClearDevices() - Error: " << e.what() << std::endl;
Util::Logger::Log(Util::Logger::Severity::Error, ss.str());
}
}
for (std::vector<Util::Device>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
{
if (it->HasBluetoothMac())
{
try
{
SendStateChange(false, it->BluetoothMac());
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Bluetooth::Device::ClearDevices() - Error: " << e.what() << std::endl;
Util::Logger::Log(Util::Logger::Severity::Error, ss.str());
}
}
}
}
void Device::UpdateDevicesFromInventory()
@@ -86,6 +122,36 @@ void Device::UpdatePresentDevices()
}
}
for (std::vector<Util::Device>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
{
if (it->HasBluetoothMac())
{
try
{
bool pollDevice = false;
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), it->BluetoothMac()) == m_presentDevices.end())
pollDevice = true;
else
pollDevice = updateOnlineDevices;
if (pollDevice && Functions::Ping(it->BluetoothMac()))
{
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), it->BluetoothMac()) == m_presentDevices.end())
SendStateChange(true, it->BluetoothMac());
presentDevices.push_back(it->BluetoothMac());
}
if (!pollDevice)
ignoredDevices.push_back(it->BluetoothMac());
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Bluetooth::Device::UpdatePresentDevices() - Error: " << e.what() << std::endl;
Util::Logger::Log(Util::Logger::Severity::Error, ss.str());
}
}
}
for (std::vector<std::string>::iterator it = m_presentDevices.begin(); it != m_presentDevices.end(); ++it)
if (std::find(presentDevices.begin(), presentDevices.end(), *it) == presentDevices.end())
SendStateChange(false, *it);
@@ -105,15 +171,60 @@ void Device::SendStateChange(bool present, const std::string& macAddress)
ss << "Bluetooth: " << sign << " " << macAddress;
Util::Logger::Log(Util::Logger::Severity::Info, ss.str());
std::stringstream url;
url << m_target << "/Bluetooth/" << sign << "/" << macAddress;
if (!m_target.empty())
{
std::stringstream url;
url << m_target << "/Bluetooth/" << sign << "/" << macAddress;
try
{
m_httpClient.GetUrlSilent(url.str());
try
{
m_httpClient.GetUrlSilent(url.str());
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Bluetooth::Device::SendStateChange() - Error: " << e.what() << std::endl;
Util::Logger::Log(Util::Logger::Severity::Error, ss.str());
}
}
catch (const std::exception& e)
for (std::vector<Util::Device>::iterator it = m_staticDevices.begin(); it != m_staticDevices.end(); ++it)
{
if (it->HasBluetoothMac() && it->BluetoothMac() == macAddress)
{
it->SetBluetoothState(present);
std::vector<std::string> urls;
if (present)
{
if (!it->GetWifiState() && it->HasOnlineURL())
urls.push_back(it->OnlineURL());
if (it->HasBluetoothOnlineURL())
urls.push_back(it->BluetoothOnlineURL());
}
else if (!present)
{
if (!it->GetWifiState() && it->HasOfflineURL())
urls.push_back(it->OfflineURL());
if (it->HasBluetoothOfflineURL())
urls.push_back(it->BluetoothOfflineURL());
}
for (auto& url : urls)
{
try
{
m_httpClient.GetUrlSilent(url);
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Bluetooth::Device::SendStateChange() - Error: " << e.what() << std::endl;
Util::Logger::Log(Util::Logger::Severity::Error, ss.str());
}
}
}
}
}