~aleteoryx/muditaos

ref: 2de71a003692ec1207cd4f0258379f82500f378f muditaos/module-services/service-db/agents/settings/SettingsProxy.cpp -rw-r--r-- 2.5 KiB
2de71a00 — Maciej-Mudita [MOS-74] Fix wrong tethering popup order 3 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <service-db/EntryPath.hpp>
#include <service-db/SettingsMessages.hpp>
#include <service-db/Settings.hpp>
#include <service-db/SettingsCache.hpp>
#include <service-db/DBServiceName.hpp>
#include <Service/ServiceProxy.hpp>
#include <Service/Service.hpp>
#include <utility>

namespace settings
{

    SettingsProxy::SettingsProxy(const service::ServiceProxy &interface) : service::ServiceProxy(interface)
    {}

    SettingsProxy::~SettingsProxy()
    {
        deinit();
    }

    std::string SettingsProxy::ownerName()
    {
        return getService()->GetName();
    }

    void SettingsProxy::init(std::function<void(EntryPath, std::string)> onChangeHandler)
    {
        this->onChangeHandler = std::move(onChangeHandler);

        getService()->bus.channels.push_back(sys::BusChannel::ServiceDBNotifications);
        getService()->connect(typeid(settings::Messages::VariableChanged), [this](sys::Message *req) {
            if (auto msg = dynamic_cast<settings::Messages::VariableChanged *>(req)) {
                onChange(msg->getPath(), msg->getValue().value_or(""));
            }
            return std::make_shared<sys::ResponseMessage>();
        });
    }

    void SettingsProxy::deinit()
    {
        if (isValid()) {
            getService()->disconnect(typeid(settings::Messages::VariableChanged));
        }
    }

    [[nodiscard]] bool SettingsProxy::isValid() const noexcept
    {
        return service::ServiceProxy::isValid();
    }

    void SettingsProxy::onChange(EntryPath path, std::string value)
    {
        if (onChangeHandler) {
            onChangeHandler(std::move(path), std::move(value));
        }
    }

    template <typename T, typename... Args> auto message(sys::BusProxy &bus, Args... args)
    {
        bus.sendUnicast(std::make_shared<T>(args...), service::name::db);
    }

    void SettingsProxy::registerValueChange(EntryPath path)
    {
        message<settings::Messages::RegisterOnVariableChange>(getService()->bus, std::move(path));
    }

    void SettingsProxy::unregisterValueChange(EntryPath path)
    {
        message<settings::Messages::UnregisterOnVariableChange>(getService()->bus, std::move(path));
    }

    void SettingsProxy::setValue(const EntryPath &path, const std::string &value)
    {
        message<settings::Messages::SetVariable>(getService()->bus, path, value);
    }

} // namespace settings