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