~aleteoryx/muditaos

ref: e4fcc8366e6020028706036092cc23a9f23c2d98 muditaos/module-services/service-db/agents/settings/Settings.cpp -rw-r--r-- 9.9 KiB
e4fcc836 — Marcin Smoczyński changelog: update changelog for v0.44.1 (#942) 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "Settings.hpp"

#include <Service/Service.hpp> // for Service
#include <Service/Bus.hpp>     // for Bus
#include <utility>             // for move, pair
#include <vector>              // for vector

#include "messages/SettingsMessages.hpp" // for EntryPath, VariableChanged, CurrentModeChanged, CurrentProfileChanged, AddProfile, GetCurrentMode, GetCurrentProfile, GetVariable, ListModes, ListProfiles, RegisterOnModeChange, RegisterOnProfileChange, RegisterOnVariableChange, SetCurrentMode, SetCurrentProfile, SetVariable, UnregisterOnModeChange, UnregisterOnProfileChange, UnregisterOnVariableChange, VariableResponse, ModeListResponse, ModeResponse, ProfileListResponse, ProfileResponse, SettingsMessage (ptr only)
#include "Service/Common.hpp" // for BusChannels, BusChannels::ServiceDBNotifications, ReturnCodes, ReturnCodes::Success
#include "Service/Message.hpp" // for ResponseMessage, DataMessage, Message_t
#include "log/log.hpp"         // for LOG_DEBUG, LOG_INFO, LOG_ERROR

namespace Settings
{
    Settings::Settings(sys::Service *app, const std::string &dbAgentName) : dbAgentName(dbAgentName)
    {
        this->app = std::shared_ptr<sys::Service>(app, [](sys::Service *) {}); /// with deleter that doesn't delete.
        this->app->busChannels.push_back(sys::BusChannels::ServiceDBNotifications);
        sys::Bus::Add(std::static_pointer_cast<sys::Service>(this->app));
        registerHandlers();
    }

    Settings::~Settings()
    {
        sys::Bus::Remove(std::static_pointer_cast<sys::Service>(app));
    }

    void Settings::sendMsg(std::shared_ptr<::Settings::Messages::SettingsMessage> &&msg)
    {
        sys::Bus::SendUnicast(std::move(msg), dbAgentName, app.get());
    }

    sys::Message_t Settings::sendMsgAndWaitForResponse(std::shared_ptr<::Settings::Messages::SettingsMessage> &&msg)
    {
        auto ret = sys::Bus::SendUnicast(std::move(msg), dbAgentName, app.get(), dbWaitTimeMs);
        if (ret.first == sys::ReturnCodes::Success) {
            return ret.second;
        }
        return sys::Message_t{};
    }

    void Settings::registerHandlers()
    {
        handleValueChgMsg();
        handleProfileChgMsg();
        handleModeChgMsg();
    }

    void Settings::handleValueChgMsg()
    {
        LOG_DEBUG("handle handleValueChgMsg");
        app->connect(::Settings::Messages::VariableChanged(), [&](sys::DataMessage *req, sys::ResponseMessage *res) {
            LOG_DEBUG("handleSettingsValueChgMsg");
            if (auto msg = dynamic_cast<::Settings::Messages::VariableChanged *>(req)) {
                auto key = msg->getPath().variable;
                auto val = msg->getValue();
                LOG_DEBUG("handleSettingsValueChgMsg: (k=v): (%s=%s)", key.c_str(), val.value_or("").c_str());
                ValueCb::iterator it_cb = cbValues.find(key);
                if (cbValues.end() != it_cb) {
                    it_cb->second(key, std::move(val));
                    return std::make_shared<sys::ResponseMessage>();
                }
            }
            return std::make_shared<sys::ResponseMessage>();
        });
    }

    void Settings::handleProfileChgMsg()
    {
        LOG_DEBUG("handle handleProfileChgMsg");
        app->connect(::Settings::Messages::CurrentProfileChanged(),
                     [&](sys::DataMessage *req, [[maybe_unused]] sys::ResponseMessage *res) {
                         LOG_DEBUG("handleProfileChgMsg");
                         if (auto msg = dynamic_cast<::Settings::Messages::CurrentProfileChanged *>(req)) {
                             auto profile = msg->getProfileName();
                             LOG_DEBUG("handleGetProfilesMsg: %s", profile.c_str());
                             cbProfile(profile);
                         }
                         return std::make_shared<sys::ResponseMessage>();
                     });
    }

    void Settings::handleModeChgMsg()
    {
        LOG_DEBUG("handle handleModeChgMsg");
        app->connect(::Settings::Messages::CurrentModeChanged(),
                     [&](sys::DataMessage *req, [[maybe_unused]] sys::ResponseMessage *res) {
                         LOG_DEBUG("handleModeChgMsg");
                         if (auto msg = dynamic_cast<::Settings::Messages::CurrentModeChanged *>(req)) {
                             auto mode = msg->getProfileName();
                             LOG_DEBUG("handleModeChgMsg: %s", mode.c_str());
                             cbMode(mode);
                         }
                         return std::make_shared<sys::ResponseMessage>();
                     });
    }

    void Settings::registerValueChange(const std::string &variableName, ValueChangedCallback cb)
    {
        ValueCb::iterator it_cb = cbValues.find(variableName);
        if (cbValues.end() != it_cb) {
            LOG_INFO("Callback function on value change (%s) already exists, rewriting", variableName.c_str());
        }
        cbValues[variableName] = cb;
        EntryPath path;
        path.variable = variableName;
        path.service  = app->GetName();
        auto msg      = std::make_shared<::Settings::Messages::RegisterOnVariableChange>(path);
        sendMsg(std::move(msg));
    }

    void Settings::unregisterValueChange(const std::string &variableName)
    {
        ValueCb::iterator it_cb = cbValues.find(variableName);
        if (cbValues.end() == it_cb) {
            LOG_INFO("Callback function on value change (%s) does not exist", variableName.c_str());
        }
        else {
            cbValues.erase(it_cb);
        }

        EntryPath path;
        path.variable = variableName;
        path.service  = app->GetName();
        auto msg      = std::make_shared<::Settings::Messages::UnregisterOnVariableChange>(path);
        sendMsg(std::move(msg));
    }

    std::optional<std::string> Settings::getValue(const std::string &variableName)
    {
        EntryPath path;
        path.variable = variableName;
        path.service  = app->GetName();
        auto msg      = std::make_shared<::Settings::Messages::GetVariable>(path);
        auto ret      = sendMsgAndWaitForResponse(msg);
        auto msgVar   = dynamic_cast<::Settings::Messages::VariableResponse *>(ret.get());
        if (nullptr != msgVar) {
            if (msgVar->getPath() == path.to_string()) {
                return msgVar->getValue();
            }
            else {
                LOG_ERROR("Settings::getValue asked for variable:%s received variable:%s",
                          variableName.c_str(),
                          msgVar->getPath().c_str());
            }
        }
        return std::string{};
    }

    void Settings::setValue(const std::string &variableName, const std::string &variableValue)
    {
        EntryPath path;
        path.variable = variableName;
        path.service  = app->GetName();
        auto msg      = std::make_shared<::Settings::Messages::SetVariable>(path, variableValue);
        sendMsg(std::move(msg));
    }

    Settings::ListOfProfiles Settings::getAllProfiles()
    {
        auto ret         = sendMsgAndWaitForResponse(std::make_shared<::Settings::Messages::ListProfiles>());
        auto msgProfiles = dynamic_cast<::Settings::Messages::ProfileListResponse *>(ret.get());
        if (nullptr != msgProfiles) {
            return msgProfiles->getValue();
        }
        return ListOfProfiles();
    }

    std::string Settings::getCurrentProfile()
    {
        auto ret     = sendMsgAndWaitForResponse(std::make_shared<::Settings::Messages::GetCurrentProfile>());
        auto msgProf = dynamic_cast<::Settings::Messages::ProfileResponse *>(ret.get());
        if (nullptr != msgProf) {
            return msgProf->getValue();
        }
        return std::string{};
    }

    void Settings::setCurrentProfile(const std::string &profile)
    {
        sendMsg(std::make_shared<::Settings::Messages::SetCurrentProfile>(profile));
    }

    void Settings::addProfile(const std::string &profile)
    {
        sendMsg(std::make_shared<::Settings::Messages::AddProfile>(profile));
    }

    void Settings::registerProfileChange(ProfileChangedCallback cb)
    {
        if (nullptr != cbProfile) {
            LOG_DEBUG("Profile change callback already exists, overwritting");
        }
        else {
            sendMsg(std::make_shared<::Settings::Messages::RegisterOnProfileChange>());
        }

        cbProfile = cb;
    }

    void Settings::unregisterProfileChange()
    {
        cbProfile = nullptr;
        sendMsg(std::make_shared<::Settings::Messages::UnregisterOnProfileChange>());
    }

    Settings::ListOfModes Settings::getAllModes()
    {
        auto ret      = sendMsgAndWaitForResponse(std::make_shared<::Settings::Messages::ListModes>());
        auto msgModes = dynamic_cast<::Settings::Messages::ModeListResponse *>(ret.get());
        if (nullptr != msgModes) {
            return msgModes->getValue();
        }
        return ListOfModes();
    }

    std::string Settings::getCurrentMode()
    {
        auto ret     = sendMsgAndWaitForResponse(std::make_shared<::Settings::Messages::GetCurrentMode>());
        auto msgMode = dynamic_cast<::Settings::Messages::ModeResponse *>(ret.get());
        if (nullptr != msgMode) {
            return msgMode->getValue();
        }
        return std::string{};
    }

    void Settings::setCurrentMode(const std::string &mode)
    {
        sendMsg(std::make_shared<::Settings::Messages::SetCurrentMode>(mode));
    }

    void Settings::registerModeChange(ModeChangedCallback cb)
    {
        if (nullptr == cbMode) {
            LOG_DEBUG("ModeChange callback allready set overwriting");
        }
        else {
            sendMsg(std::make_shared<::Settings::Messages::RegisterOnModeChange>());
        }

        cbMode = cb;
    }

    void Settings::unregisterModeChange()
    {
        cbMode = nullptr;
        sendMsg(std::make_shared<::Settings::Messages::UnregisterOnModeChange>());
    }
} // namespace Settings