~aleteoryx/muditaos

ref: 80dc483c5d132a4eac906cc7f4aa447bf0dbad2f muditaos/module-services/service-db/test/test-settings/test-service-db-settings-testservices.hpp -rw-r--r-- 9.5 KiB
80dc483c — Radoslaw Wicik [BH-422] Fix CMake in service GUI - post review update 01 4 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

namespace settings
{
    class MyService : public sys::Service
    {
      public:
        MyService(const std::string &name) : sys::Service(name)
        {
            mySettings = std::make_shared<settings::Settings>(this);
        }
        std::shared_ptr<settings::Settings> mySettings;
        std::vector<std::string> valChanged;
        std::string whoRequestedNotifyOnChange;
        void ValueChanged(std::string value)
        {
            valChanged.emplace_back(value);
        }
        void debug(const std::string &cmd, const std::string &k, const std::string &v)
        {
            std::cout << "[thr_id:" << std::this_thread::get_id() << ", thr_name:" << GetName() << "] " << cmd
                      << " [key, val] = (" << k << ", " << v << ")" << std::endl
                      << std::flush;
        }
        sys::MessagePointer DataReceivedHandler(sys::DataMessage *req, sys::ResponseMessage *resp) override
        {
            if (auto msg = dynamic_cast<settings::UTMsg::ReqRegValChg *>(req)) {
                debug("ReqRegValChg", msg->name, msg->value);
                whoRequestedNotifyOnChange = msg->sender;
                mySettings->registerValueChange(msg->name,
                                                ([this](std::string value) {
                                                    ValueChanged(value);
                                                    auto cnf = std::make_shared<settings::UTMsg::CnfValChg>("", value);
                                                    bus.sendUnicast(std::move(cnf), whoRequestedNotifyOnChange);
                                                }),
                                                settings::SettingsScope::Global);
                auto cnf = std::make_shared<settings::UTMsg::CnfRegValChg>(msg->name, msg->value);
                bus.sendUnicast(std::move(cnf), whoRequestedNotifyOnChange);
            }
            else if (auto msg = dynamic_cast<settings::UTMsg::ReqUnRegValChg *>(req)) {
                // unregister
                debug("ReqUnRegValChg", msg->name, msg->value);
                mySettings->unregisterValueChange(msg->name, settings::SettingsScope::Global);
                auto cnf = std::make_shared<settings::UTMsg::CnfUnRegValChg>(msg->name, msg->value);
                bus.sendUnicast(std::move(cnf), msg->sender);
            }
            else if (auto msg = dynamic_cast<settings::UTMsg::ReqSetVal *>(req)) {
                // set value
                debug("ReqSetVal", msg->name, msg->value);
                mySettings->setValue(msg->name, msg->value, settings::SettingsScope::Global);
                auto cnf = std::make_shared<settings::UTMsg::CnfReqSetVal>(msg->name, msg->value);
                bus.sendUnicast(std::move(cnf), msg->sender);
            }
            else if (dynamic_cast<settings::UTMsg::ReqGetVal *>(msg)) {
                debug("ReqGetValChg", msg->name, msg->value);
            }
            return std::make_shared<sys::ResponseMessage>();
        }
        sys::ReturnCodes InitHandler() override
        {
            std::cout << "inithandler thr_id: " << std::this_thread::get_id() << std::endl << std::flush;
            return sys::ReturnCodes::Success;
        }
        sys::ReturnCodes DeinitHandler() override
        {
            std::cout << "deinithandler thr_id: " << std::this_thread::get_id() << std::endl << std::flush;
            return sys::ReturnCodes::Success;
        }
        sys::ReturnCodes SwitchPowerModeHandler(const sys::ServicePowerMode mode) override
        {
            return sys::ReturnCodes::Success;
        }
    };

    class ServiceProfile : public MyService
    {
      public:
        ServiceProfile(const std::string &name) : MyService(name)
        {}
        settings::Settings::ListOfProfiles profiles;
        std::string profile;
        sys::MessagePointer DataReceivedHandler(sys::DataMessage *req, sys::ResponseMessage *resp) override
        {
            if (auto msg = dynamic_cast<settings::UTMsg::ReqRegProfileChg *>(req)) {
                debug("ReqRegProfileChg", msg->name, msg->value);
                whoRequestedNotifyOnChange = msg->sender;
                mySettings->registerProfileChange(([this](const std::string &profile) {
                    this->profile = profile;
                    auto cnf      = std::make_shared<settings::UTMsg::ProfileChg>(profile);
                    bus.sendUnicast(std::move(cnf), whoRequestedNotifyOnChange);
                }));
                auto cnf = std::make_shared<settings::UTMsg::CnfRegProfileChg>();
                bus.sendUnicast(std::move(cnf), whoRequestedNotifyOnChange);
            }
            else if (auto msg = dynamic_cast<settings::UTMsg::ReqUnRegProfileChg *>(req)) {
                // unregister
                debug("ReqUnRegProfileChg", msg->name, msg->value);
                mySettings->unregisterProfileChange();
                auto cnf = std::make_shared<settings::UTMsg::CnfUnRegProfileChg>();
                bus.sendUnicast(std::move(cnf), msg->sender);
            }
            else if (auto msg = dynamic_cast<settings::UTMsg::ReqSetCurrentProfile *>(req)) {
                // set value
                debug("ReqSetCurrentProfile", msg->name, msg->value);
                mySettings->setCurrentProfile(msg->name);
                auto cnf = std::make_shared<settings::UTMsg::CnfSetCurrentProfile>(msg->name);
                bus.sendUnicast(std::move(cnf), msg->sender);
            }
            else if (auto msg = dynamic_cast<settings::UTMsg::ReqGetAllProfiles *>(req)) {
                debug("ReqGetAllProfiles", msg->name, msg->value);
                mySettings->getAllProfiles(([this](const settings::Settings::ListOfProfiles &profiles) {
                    this->profiles = profiles;
                    auto cnf       = std::make_shared<settings::UTMsg::ProfilesChg>(profiles);
                    bus.sendUnicast(std::move(cnf), whoRequestedNotifyOnChange);
                }));
                auto cnf = std::make_shared<settings::UTMsg::CnfGetAllProfiles>();
                bus.sendUnicast(std::move(cnf), msg->sender);
            }
            else if (auto msg = dynamic_cast<settings::UTMsg::ReqAddProfile *>(req)) {
                debug("ReqAddProfile", msg->name, msg->value);
                mySettings->addProfile(msg->name);
                auto cnf = std::make_shared<settings::UTMsg::CnfAddProfile>(msg->name);
                bus.sendUnicast(std::move(cnf), msg->sender);
            }

            return std::make_shared<sys::ResponseMessage>();
        }
    };

    class ServiceMode : public MyService
    {
      public:
        ServiceMode(const std::string &name) : MyService(name)
        {}
        settings::Settings::ListOfModes modes;
        std::string mode;
        sys::MessagePointer DataReceivedHandler(sys::DataMessage *req, sys::ResponseMessage *resp) override
        {
            if (auto msg = dynamic_cast<settings::UTMsg::ReqRegProfileChg *>(req)) {
                debug("ReqRegProfileChg", msg->name, msg->value);
                whoRequestedNotifyOnChange = msg->sender;
                mySettings->registerModeChange(([this](const std::string &mode) {
                    this->mode = mode;
                    auto cnf   = std::make_shared<settings::UTMsg::ProfileChg>(mode);
                    bus.sendUnicast(std::move(cnf), whoRequestedNotifyOnChange);
                }));
                auto cnf = std::make_shared<settings::UTMsg::CnfRegProfileChg>();
                bus.sendUnicast(std::move(cnf), whoRequestedNotifyOnChange);
            }
            else if (auto msg = dynamic_cast<settings::UTMsg::ReqUnRegProfileChg *>(req)) {
                // unregister
                debug("ReqUnRegProfileChg", msg->name, msg->value);
                mySettings->unregisterModeChange();
                auto cnf = std::make_shared<settings::UTMsg::CnfUnRegProfileChg>();
                bus.sendUnicast(std::move(cnf), msg->sender);
            }
            else if (auto msg = dynamic_cast<settings::UTMsg::ReqSetCurrentProfile *>(req)) {
                // set value
                debug("ReqSetCurrentProfile", msg->name, msg->value);
                mySettings->setCurrentMode(msg->name);
                auto cnf = std::make_shared<settings::UTMsg::CnfSetCurrentProfile>(msg->name);
                bus.sendUnicast(std::move(cnf), msg->sender);
            }
            else if (auto msg = dynamic_cast<settings::UTMsg::ReqGetAllProfiles *>(req)) {
                debug("ReqGetAllProfiles", msg->name, msg->value);
                mySettings->getAllModes(([this](const settings::Settings::ListOfModes &modes) {
                    this->modes = modes;
                    auto cnf    = std::make_shared<settings::UTMsg::ProfilesChg>(modes);
                    bus.sendUnicast(std::move(cnf), whoRequestedNotifyOnChange);
                }));
                auto cnf = std::make_shared<settings::UTMsg::CnfGetAllProfiles>();
                bus.sendUnicast(std::move(cnf), msg->sender);
            }
            else if (auto msg = dynamic_cast<settings::UTMsg::ReqAddProfile *>(req)) {
                debug("ReqAddProfile", msg->name, msg->value);
                mySettings->addMode(msg->name);
                auto cnf = std::make_shared<settings::UTMsg::CnfAddProfile>(msg->name);
                bus.sendUnicast(std::move(cnf), msg->sender);
            }

            return std::make_shared<sys::ResponseMessage>();
        }
    };
} // namespace settings