RTC Toolkit 6.0.0
Loading...
Searching...
No Matches
rtcComponent.hpp
Go to the documentation of this file.
1
11
12#ifndef RTCTK_COMPONENTFRAMEWORK_RTCCOMPONENT_HPP
13#define RTCTK_COMPONENTFRAMEWORK_RTCCOMPONENT_HPP
14
19
21
22// High level exception types returned via MAL
23
28class RequestAborted : public rtctkif::ExceptionErr {
29public:
30 static constexpr int32_t ERROR_CODE = 11;
31 RequestAborted() : rtctkif::ExceptionErr("Request aborted.", ERROR_CODE) {
32 }
33};
34
39class RequestRejected : public rtctkif::ExceptionErr {
40public:
41 static constexpr int32_t ERROR_CODE = 10;
42 RequestRejected(const std::string& request_id, const std::string& state_id)
43 : rtctkif::ExceptionErr("Request " + request_id + " rejected in state " + state_id,
44 ERROR_CODE) {
45 }
46};
47
52class RequestFailed : public rtctkif::ExceptionErr {
53public:
57 static constexpr int32_t ERROR_CODE = 501;
58 explicit RequestFailed(const std::string& message)
59 : rtctkif::ExceptionErr(message, ERROR_CODE) {
60 }
61};
62
64
66
75
80 public:
81 // NOLINTNEXTLINE(performance-unnecessary-value-param)
82 virtual void ActivityUpdating(StopToken st, const JsonPayload& args) {
83 }
84 // NOLINTNEXTLINE(performance-unnecessary-value-param)
85 virtual bool GuardUpdatingAllowed(const JsonPayload& args) {
86 return true;
87 }
88
89 virtual void ActionClearAlerts() {
90 }
91 };
92
97 public:
99
100 void Start() override {
101 Super::InputStage::Start();
102
105 }
106 };
107
112 public:
114 // Handlers ###################################################################
115
116 engine.RegisterRejectHandler<events::Update, RequestRejected>();
117 engine.RegisterRejectHandler<events::ClearAlerts, RequestRejected>();
118
119 m_update_success_handler = [this] {
120 this->m_engine.PostEvent(std::make_unique<events::UpdateDone>());
121 };
122
123 m_update_error_handler = [this](std::exception_ptr eptr) {
124 this->m_engine.PostEvent(std::make_unique<events::UpdateError>(std::move(eptr)));
125 };
126
127 // No Disable in states ###############################################################
128
129 m_no_disable_in_states.emplace_back("On:Update:Busy");
130
131 // No Update in states ################################################################
132
133 this->m_no_update_in_states.emplace_back("On::NotOperational::Starting");
134 this->m_no_update_in_states.emplace_back("On::NotOperational::NotReady");
135 this->m_no_update_in_states.emplace_back("On::NotOperational::Initialising");
136 this->m_no_update_in_states.emplace_back("On::NotOperational::Enabling");
137 this->m_no_update_in_states.emplace_back("On::NotOperational::Disabling");
138
139 // Actions ############################################################################
140
141 engine.RegisterAction("ActionClearAlerts", [this](auto c) {
143 if (req == nullptr) {
144 // no event or request
145 return;
146 }
147 static_cast<BizLogicIf&>(this->m_logic).ActionClearAlerts();
148 req->SetReplyValue(STD_OK_REPLY);
149 });
150
151 engine.RegisterAction("ActionUpdatingEntry", [this](auto c) {
153 });
154
155 engine.RegisterAction("ActionUpdatingDone", [this](auto c) {
157 m_tmp_update_request->SetReplyValue(STD_OK_REPLY);
158 m_tmp_update_request = nullptr;
159 }
160 });
161
162 engine.RegisterAction("ActionUpdatingFailed", [this](auto c) {
163 if (auto eptr = GetPayloadNothrow<events::UpdateError>(c); eptr) {
165 m_tmp_update_request->SetException(
167 m_tmp_update_request = nullptr;
168 }
169 }
170 });
171
172 // Guards ############################################################################
173
174 engine.RegisterGuard("GuardUpdatingAllowed", [this](auto c) {
176 if (req == nullptr) {
177 // no event or request
178 return false;
179 }
180
181 auto act_state = this->m_engine.GetState();
182 for (auto& s : m_no_update_in_states) {
183 if (act_state.find(s) != std::string::npos) {
184 return false;
185 }
186 }
187
188 std::string string_arg = req->GetRequestPayload();
189 JsonPayload json_arg = JsonPayload::parse(string_arg);
190 return static_cast<BizLogicIf&>(this->m_logic).GuardUpdatingAllowed(json_arg);
191 });
192
193 engine.RegisterGuard("GuardDisablingAllowed", [this](auto c) {
194 auto act_state = this->m_engine.GetState();
195 for (auto& s : m_no_disable_in_states) {
196 if (act_state.find(s) != std::string::npos) {
197 return false;
198 }
199 }
200 return true;
201 });
202
203 // Activities #####################################################################
204
205 engine.RegisterActivity(
206 "ActivityUpdating",
207 [this](StopToken stop_token) {
208 std::string string_arg = m_tmp_update_request->GetRequestPayload();
209 JsonPayload json_arg = JsonPayload::parse(string_arg);
210 static_cast<BizLogicIf&>(this->m_logic).ActivityUpdating(stop_token, json_arg);
211 },
214 }
215
216 protected:
217 std::shared_ptr<rad::cii::Request<std::string, std::string>> m_tmp_update_request;
218 std::function<void()> m_update_success_handler;
219 std::function<void(std::exception_ptr)> m_update_error_handler;
220 std::list<std::string> m_no_update_in_states;
221 std::list<std::string> m_no_disable_in_states;
222 };
223
228 public:
230 // clang-format off
231 this->mm.AddTrans("On::Operational" , "" , "events.ClearAlerts", "", "ActionClearAlerts");
232
233 const std::string update_region = "On:Update";
234 this->mm.AddState(Composite, update_region, "On");
235 this->mm.AddState(Initial, "On:Update:Initial", update_region);
236 this->mm.AddState(Simple, "On:Update:Idle", update_region);
237 this->mm.AddState(Simple, "On:Update:Busy", update_region , "ActivityUpdating" ,"ActionUpdatingEntry");
238
239 this->mm.AddTrans("On:Update:Initial" , "On:Update:Idle" );
240 this->mm.AddTrans("On:Update:Idle" , "On:Update:Busy" , "events.Update", "GuardUpdatingAllowed");
241 this->mm.AddTrans("On:Update:Busy" , "On:Update:Idle" , "events.UpdateDone", "" ,"ActionUpdatingDone" );
242 this->mm.AddTrans("On:Update:Busy" , "On:Update:Idle" , "events.UpdateError", "" ,"ActionUpdatingFailed" );
243
244 this->mm.ModTransGuard("On::Operational", "On::NotOperational::Disabling", "events.Disable", "", "GuardDisablingAllowed");
245 // clang-format on
246 }
247 };
248};
249
250} // namespace rtctk::componentFramework
251
252#endif // RTCTK_COMPONENTFRAMEWORK_RTCCOMPONENT_HPP
static void Register(CommandReplier &replier, StateMachineEngine &engine)
Definition rtcCmdsImpl.hpp:94
ModelManipulator mm
Definition modelBuilderBase.hpp:78
Adapter object intended to be used in contexts without direct access to the output-stream object.
Definition exceptions.hpp:168
std::string Str() const
Convenience function for constructing a std::string from the exception.
Definition exceptions.hpp:186
RequestAborted()
Definition rtcComponent.hpp:31
static constexpr int32_t ERROR_CODE
Definition rtcComponent.hpp:30
Thrown if the command was accepted but the task to run failed.
Definition rtcComponent.hpp:52
RequestFailed(const std::string &message)
Definition rtcComponent.hpp:58
static constexpr int32_t ERROR_CODE
Definition rtcComponent.hpp:57
Thrown if a command is not allowed in current state or guard.
Definition rtcComponent.hpp:39
RequestRejected(const std::string &request_id, const std::string &state_id)
Definition rtcComponent.hpp:42
static constexpr int32_t ERROR_CODE
Definition rtcComponent.hpp:41
virtual bool GuardUpdatingAllowed(const JsonPayload &args)
Definition rtcComponent.hpp:85
virtual void ActionClearAlerts()
Definition rtcComponent.hpp:89
virtual void ActivityUpdating(StopToken st, const JsonPayload &args)
Definition rtcComponent.hpp:82
void Start() override
Definition rtcComponent.hpp:100
InputStage(CommandReplier &replier, StateMachineEngine &engine)
Definition stdComponent.hpp:160
ModelBuilder(StateMachineEngine &engine)
Definition rtcComponent.hpp:229
std::function< void(std::exception_ptr)> m_update_error_handler
Definition rtcComponent.hpp:219
std::list< std::string > m_no_disable_in_states
Definition rtcComponent.hpp:221
std::list< std::string > m_no_update_in_states
Definition rtcComponent.hpp:220
OutputStage(StateMachineEngine &engine, BizLogicIf &bl)
Definition rtcComponent.hpp:113
std::function< void()> m_update_success_handler
Definition rtcComponent.hpp:218
std::shared_ptr< rad::cii::Request< std::string, std::string > > m_tmp_update_request
Definition rtcComponent.hpp:217
Definition stateMachineEngine.hpp:34
void RegisterAction(const std::string &id, ActionMethod action)
Register action.
Definition stateMachineEngine.cpp:85
void RegisterGuard(const std::string &id, GuardMethod guard)
Register guard.
Definition stateMachineEngine.cpp:107
void RegisterRejectHandler(const std::string &id, RejectMethod reject)
Register reject handler.
Definition stateMachineEngine.cpp:128
void RegisterActivity(const std::string &id, ActivityMethod activity, SuccessMethod on_success, FailureMethod on_failure)
Register activity.
Definition stateMachineEngine.cpp:120
CommandReplier & m_replier
Definition stdComponent.hpp:175
StateMachineEngine & m_engine
Definition stdComponent.hpp:176
StateMachineEngine & m_engine
Definition stdComponent.hpp:410
BizLogicIf & m_logic
Definition stdComponent.hpp:411
static void Register(CommandReplier &replier, StateMachineEngine &engine)
Definition rtcCmdsImpl.hpp:121
Defines the JSON payload type JsonPayload.
Definition commandReplier.cpp:21
StdComponent RtcComponentSuper
Definition rtcComponent.hpp:65
@ Simple
Definition model.hpp:22
@ Composite
Definition model.hpp:22
@ Initial
Definition model.hpp:22
rad::StopToken StopToken
Definition stopToken.hpp:19
std::shared_ptr< typename EVENT::payload_t > GetPayloadNothrow(scxml4cpp::Context *c)
Definition stateMachineEngine.hpp:255
const std::string STD_OK_REPLY
Definition stdComponent.hpp:85
nlohmann::json JsonPayload
Type requirements:
Definition jsonPayload.hpp:24
Implementation of MAL commands for layer 'RtcComponent'.
Lifecycle of the 'Standard Component'.
A simple Stop Token.
Basic life cycle for RtcComponent.
Definition rtcComponent.hpp:73
RtcComponentSuper Super
Definition rtcComponent.hpp:74
Basic life cycle for StdComponent.
Definition stdComponent.hpp:143