blob: 35eb45f58896e158968d8e52400333eb1aacc483 [file] [log] [blame]
Tomáš Pecka0a2e8902020-06-09 21:11:20 +02001#include <sdbus-c++/sdbus-c++.h>
2#include <thread>
3#include "dbus_semaphore_server.h"
4#include "utils/log-init.h"
5
6/* Ask for current value:
7 * dbus-send --print-reply --system --dest=<bus> /cz/cesnet/led org.freedesktop.DBus.Properties.Get string:cz.cesnet.Led string:semaphore
8 */
9
10DbusSemaphoreServer::DbusSemaphoreServer(sdbus::IConnection& connection, const std::string& objectPath, const std::string& propertyName, const std::string& propertyInterface, const std::string& state)
11 : m_object(sdbus::createObject(connection, objectPath))
12 , m_propertyName(propertyName)
13 , m_propertyInterface(propertyInterface)
14 , m_semaphoreState(state)
15{
16 // Register D-Bus methods and signals on the object, and exports the object.
17 m_object->registerProperty(m_propertyName).onInterface(m_propertyInterface).withGetter([&]() {
18 std::lock_guard<std::mutex> lock(m_semaphoreStateMtx);
19 return m_semaphoreState;
20 });
21 m_object->finishRegistration();
22}
23
24void DbusSemaphoreServer::runStateChanges(const std::vector<std::pair<std::string, std::chrono::milliseconds>>& sequence)
25{
26 std::thread serverThr([&]() {
27 for (const auto& [state, sleepTime] : sequence) {
28 {
29 std::lock_guard<std::mutex> lock(m_semaphoreStateMtx);
30 m_semaphoreState = state;
31 }
32 m_object->emitPropertiesChangedSignal(m_propertyInterface, {m_propertyName});
33 std::this_thread::sleep_for(sleepTime);
34 }
35 });
36
37 serverThr.join();
38}