~aleteoryx/muditaos

ref: 0823d82e5141f44812c54debf07245d0ca746124 muditaos/module-services/service-db/agents/settings/SettingsAgent.cpp -rw-r--r-- 2.6 KiB
0823d82e — Radoslaw Wicik [EGD-3743] Update copyrights in fies - add empty line after license 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "SettingsAgent.hpp"

#include <module-sys/Service/Bus.hpp>
#include <module-vfs/vfs.hpp>

#include <log/log.hpp>

#include <memory>

SettingsAgent::SettingsAgent(sys::Service *parentService) : DatabaseAgent(parentService)
{
    database = std::make_unique<Database>(getDbFilePath().c_str());
}

void SettingsAgent::initDb()
{
    // LOG_DEBUG("sql:\n--\n%s\n--", getDbInitString().c_str());
    database->execute(getDbInitString().c_str());
}

void SettingsAgent::deinitDb()
{
    database->deinitialize();
}

void SettingsAgent::registerMessages()
{
    // connect handler & message in parent service
    using std::placeholders::_1;
    using std::placeholders::_2;

    parentService->connect(Settings::Messages::GetVariable(),
                           std::bind(&SettingsAgent::handleGetVariable, this, _1, _2));
    parentService->connect(Settings::Messages::SetVariable(),
                           std::bind(&SettingsAgent::handleSetVariable, this, _1, _2));
}

auto SettingsAgent::getDbInitString() -> const std::string
{
    auto x          = 0;
    const char *sql = (
#include "settings.sql"
    );
    return sql;
}

auto SettingsAgent::getDbFilePath() -> const std::string
{

    return USER_PATH("settings_v2.db");
}
auto SettingsAgent::getAgentName() -> const std::string
{
    return std::string("settingsAgent");
}

auto SettingsAgent::getValue(Settings::EntryPath path) -> std::optional<std::string>
{
    // get value from db from specified path
    return "";
}

auto SettingsAgent::setValue(Settings::EntryPath path, std::string value) -> std::string
{
    // set value into db
    // return old value
    return "";
}

auto SettingsAgent::handleGetVariable(sys::DataMessage *req, sys::ResponseMessage * /*response*/) -> sys::Message_t
{
    if (auto msg = dynamic_cast<Settings::Messages::GetVariable *>(req)) {

        auto path  = msg->getPath();
        auto value = getValue(path);

        return std::make_shared<Settings::Messages::VariableResponse>(path, value);
    }
    return std::make_shared<sys::ResponseMessage>();
};

auto SettingsAgent::handleSetVariable(sys::DataMessage *req, sys::ResponseMessage * /*response*/) -> sys::Message_t
{
    if (auto msg = dynamic_cast<Settings::Messages::SetVariable *>(req)) {

        auto path     = msg->getPath();
        auto value    = msg->getValue().value_or("");
        auto oldValue = setValue(path, msg->getValue().value_or(""));

        auto updateMsg = std::make_shared<Settings::Messages::VariableChanged>(path, value, oldValue);
        sys::Bus::SendUnicast(std::move(updateMsg), "db-worker", parentService);
    }
    return std::make_shared<sys::ResponseMessage>();
};