blob: 9d426ceed990e2cf1ab04898b6157f247bd93a86 [file] [log] [blame]
/*
* Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/
*
* Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz>
*
*/
#include "DbusSemaphoreInput.h"
#include "utils/log.h"
namespace {
velia::health::State stateFromString(const std::string& str)
{
if (str == "WARNING")
return velia::health::State::WARNING;
if (str == "ERROR")
return velia::health::State::ERROR;
if (str == "OK")
return velia::health::State::OK;
throw std::invalid_argument("DbusSemaphoreInput received invalid state");
}
}
namespace velia::health {
DbusSemaphoreInput::DbusSemaphoreInput(std::shared_ptr<AbstractManager> manager, sdbus::IConnection& connection, const std::string& bus, const std::string& objectPath, const std::string& propertyName, const std::string& propertyInterface)
: AbstractInput(std::move(manager))
, m_dbusObjectProxy(sdbus::createProxy(connection, bus, objectPath))
, m_propertyName(propertyName)
, m_propertyInterface(propertyInterface)
, m_log(spdlog::get("main"))
{
m_dbusObjectProxy->uponSignal("PropertiesChanged").onInterface("org.freedesktop.DBus.Properties").call([&](const std::string& iface, const std::map<std::string, sdbus::Variant>& changed, [[maybe_unused]] const std::vector<std::string>& invalidated) {
if (iface != m_propertyInterface) {
return;
}
if (auto it = changed.find(m_propertyName); it != changed.end()) {
std::string newState = it->second.get<std::string>();
m_log->debug("Changed {} {}: {}", objectPath, propertyName, newState);
updateState(stateFromString(newState));
}
});
m_dbusObjectProxy->finishRegistration();
m_log->debug("Watching for property changes of {}, object {}, property {}.{}", bus, objectPath, propertyInterface, propertyName);
// we might update the state twice here (once from the callback, once from here).
// But better than querying the current state before the registration; we might miss a state change that could happen between querying and callback registration
std::string currentState = m_dbusObjectProxy->getProperty(m_propertyName).onInterface(propertyInterface).get<std::string>();
m_log->debug("Property initialized to {}", currentState);
updateState(stateFromString(currentState));
}
DbusSemaphoreInput::~DbusSemaphoreInput() = default;
}