13#ifndef RTCTK_RTCSUPERVISOR_SUPERVISORY_HPP
14#define RTCTK_RTCSUPERVISOR_SUPERVISORY_HPP
46 explicit SetModesContext(rad::cii::Request<std::string, std::string>
const& request)
47 : m_logger(
GetLogger(
"rtctk")), m_request(request), m_arg() {
48 m_arg = JsonPayload::parse(m_request.GetRequestPayload());
65 }
catch (std::exception
const& exception) {
67 m_logger,
"SetModesContext::SendReply: Failed to send reply: " << exception.what());
71 "SetModesContext::SendReply: Failed to send reply with unknown exception.");
86 "SetModesContext::SendErrorReply: Failed to send reply with unknown exception.");
91 log4cplus::Logger& m_logger;
94 rad::cii::Request<std::string, std::string> m_request;
119 : m_logger(
GetLogger(
"rtctk")), m_request(request), m_arg() {
120 m_arg = JsonPayload::parse(m_request.GetRequestPayload());
137 }
catch (std::exception
const& exception) {
140 "WriteBackContext::SendReply: Failed to send reply: " << exception.what());
144 "WriteBackContext::SendReply: Failed to send reply with unknown exception.");
159 "WriteBackContext::SendErrorReply: Failed to send reply with unknown exception.");
172 "SetModesContext::SendStopReply: Failed to send reply with unknown exception.");
177 log4cplus::Logger& m_logger;
180 rad::cii::Request<std::string, std::string> m_request;
212template <
typename Super>
214 static_assert(std::is_base_of_v<RtcComponent, Super>,
"'Supervisory' requires 'RtcComponent'");
335 Super::InputStage::Start();
358 this->m_engine.PostEvent(std::make_unique<events::SetModesDone>());
362 this->m_engine.PostEvent(std::make_unique<events::SetModesError>(std::move(eptr)));
366 this->m_engine.PostEvent(std::make_unique<events::WriteBackDone>());
370 this->m_engine.PostEvent(std::make_unique<events::WriteBackError>(std::move(eptr)));
375 this->m_no_disable_in_states.push_back(
"On:SetModes:Busy");
379 this->m_no_update_in_states.push_back(
"On:SetModes:Busy");
405 "GuardSettingModesAllowed", [
this](events::SetModes
const& ev) ->
bool {
414 auto act_state = this->m_engine.GetState();
416 if (act_state.find(s) != std::string::npos) {
422 .GuardSettingModesAllowed(ctx.
GetArg())) {
434 "GuardWritingBackAllowed", [
this](events::WriteBack
const& ev) ->
bool {
443 auto act_state = this->m_engine.GetState();
445 if (act_state.find(s) != std::string::npos) {
451 .GuardWritingBackAllowed(ctx.
GetArg())) {
465 "ActivitySettingModes",
475 "ActivityWritingBack",
488 auto req = GetPayloadNothrow<events::GetStatus>(c);
489 if (req ==
nullptr) {
493 auto status =
static_cast<BizLogicIf&
>(this->m_logic).ActionGetStatus();
494 req->SetReplyValue(status);
498 "ActionSetDeferredModes", [
this](events::SetDeferredModes
const& ev) {
499 auto req = GetPayloadNothrow<events::SetDeferredModes>(ev);
500 if (req ==
nullptr) {
505 auto json_arg = JsonPayload::parse(req->GetRequestPayload());
506 static_cast<BizLogicIf&
>(this->m_logic).ActionSetDeferredModes(json_arg);
511 auto eptr = std::make_exception_ptr(nested);
519 "ActionGetAvailableModes", [
this](events::GetAvailableModes
const& ev) {
520 auto req = GetPayloadNothrow<events::GetAvailableModes>(ev);
521 if (req ==
nullptr) {
527 static_cast<BizLogicIf&
>(this->m_logic).ActionGetAvailableModes();
528 req->SetReplyValue(result.dump());
532 auto eptr = std::make_exception_ptr(nested);
540 "ActionGetActiveModes", [
this](events::GetActiveModes
const& ev) {
541 auto req = GetPayloadNothrow<events::GetActiveModes>(ev);
542 if (req ==
nullptr) {
548 static_cast<BizLogicIf&
>(this->m_logic).ActionGetActiveModes();
549 req->SetReplyValue(result.dump());
553 auto eptr = std::make_exception_ptr(nested);
561 "ActionGetDeferredModes", [
this](events::GetDeferredModes
const& ev) {
562 auto req = GetPayloadNothrow<events::GetDeferredModes>(ev);
563 if (req ==
nullptr) {
569 static_cast<BizLogicIf&
>(this->m_logic).ActionGetDeferredModes();
570 req->SetReplyValue(result.dump());
574 auto eptr = std::make_exception_ptr(nested);
582 [
this](events::SetModesDone
const&) {
589 "ActionSettingModesFailed", [
this](events::SetModesError
const& ev) {
595 "ActionWriteBackDone", [
this](events::WriteBackDone
const&) {
602 "ActionWriteBackFailed", [
this](events::WriteBackError
const& ev) {
608 "ActionWriteBackStopped", [
this](events::Stop
const& ev) {
609 auto req = GetPayloadNothrow<events::Stop>(ev);
610 if (req ==
nullptr) {
638 this->mm.AddState(
Composite,
"On:SetModes",
"On");
639 this->mm.AddState(
Initial,
"On:SetModes:Initial",
"On:SetModes");
640 this->mm.AddState(
Simple,
"On:SetModes:Idle",
"On:SetModes");
641 this->mm.AddState(
Simple,
"On:SetModes:Busy",
"On:SetModes",
"ActivitySettingModes");
643 this->mm.AddTrans(
"On",
"" , events::GetAvailableModes::ID,
"",
"ActionGetAvailableModes");
644 this->mm.AddTrans(
"On::NotOperational::NotReady",
"" , events::SetDeferredModes::ID,
"",
"ActionSetDeferredModes");
645 this->mm.AddTrans(
"On::NotOperational::NotReady",
"" , events::GetDeferredModes::ID,
"",
"ActionGetDeferredModes");
646 this->mm.AddTrans(
"On::NotOperational::Ready",
"" , events::GetActiveModes::ID,
"",
"ActionGetActiveModes");
647 this->mm.AddTrans(
"On::NotOperational::Enabling",
"" , events::GetActiveModes::ID,
"",
"ActionGetActiveModes");
648 this->mm.AddTrans(
"On::Operational",
"" , events::GetActiveModes::ID,
"",
"ActionGetActiveModes");
650 this->mm.AddTrans(
"On:SetModes:Initial",
"On:SetModes:Idle");
651 this->mm.AddTrans(
"On:SetModes:Idle",
"On:SetModes:Busy", events::SetModes::ID,
"GuardSettingModesAllowed");
652 this->mm.AddTrans(
"On:SetModes:Busy",
"On:SetModes:Idle", events::SetModesDone::ID,
"",
"ActionSettingModesDone");
653 this->mm.AddTrans(
"On:SetModes:Busy",
"On:SetModes:Idle", events::SetModesError::ID,
"",
"ActionSettingModesFailed");
655 this->mm.AddState(
Composite,
"On:WriteBack",
"On");
656 this->mm.AddState(
Initial,
"On:WriteBack:Initial",
"On:WriteBack");
657 this->mm.AddState(
Simple,
"On:WriteBack:Idle",
"On:WriteBack");
658 this->mm.AddState(
Simple,
"On:WriteBack:Busy",
"On:WriteBack",
"ActivityWritingBack");
660 this->mm.AddTrans(
"On:WriteBack:Initial",
"On:WriteBack:Idle");
661 this->mm.AddTrans(
"On:WriteBack:Idle",
"On:WriteBack:Busy", events::WriteBack::ID,
"GuardWritingBackAllowed");
662 this->mm.AddTrans(
"On:WriteBack:Busy",
"On:WriteBack:Idle", events::WriteBackDone::ID,
"",
"ActionWriteBackDone");
663 this->mm.AddTrans(
"On:WriteBack:Busy",
"On:WriteBack:Idle", events::WriteBackError::ID,
"",
"ActionWriteBackFailed");
664 this->mm.AddTrans(
"On:WriteBack:Busy",
"On:WriteBack:Idle", events::Stop::ID,
"",
"ActionWriteBackStopped");
Adapter object intended to be used in contexts without direct access to the output-stream object.
Definition: exceptions.hpp:185
Thrown if the command was accepted but the task to run failed.
Definition: rtcComponent.hpp:53
Thrown if a command is not allowed in current state or guard.
Definition: rtcComponent.hpp:40
The RtctkException class is the base class for all Rtctk exceptions.
Definition: exceptions.hpp:237
Definition: stateMachineEngine.hpp:35
void RegisterRejectHandler(std::string const &id, RejectMethod reject)
Register reject handler.
Definition: stateMachineEngine.cpp:131
void RegisterActivity(std::string const &id, ActivityMethod activity, SuccessMethod on_success, FailureMethod on_failure)
Register activity.
Definition: stateMachineEngine.cpp:123
void RegisterGuardStatic(std::string const &id, std::function< bool(Event const &)> guard)
Register guard for statically known event type.
Definition: stateMachineEngine.hpp:126
void RegisterAction(std::string const &id, ActionMethod action)
Register action.
Definition: stateMachineEngine.cpp:86
void RegisterActionStatic(std::string const &id, std::function< void(Event const &)> action)
Register action for statically known event type.
Definition: stateMachineEngine.hpp:93
Thrown if somebody sent a stop or abort command.
Definition: stdComponent.hpp:96
static void Register(CommandReplier &replier, StateMachineEngine &engine)
Definition: supervisoryCmdsImpl.hpp:40
Business logic interface for Supervisory mixin.
Definition: supervisory.hpp:220
virtual JsonPayload ActionGetActiveModes()
Action is used for mode inspection and executed in the state machine thread.
Definition: supervisory.hpp:286
virtual void ActivityWritingBack(StopToken st, JsonPayload const &arg)
Activity executed in its own thread that performs the writeback.
Definition: supervisory.hpp:310
virtual void ActionSetDeferredModes(JsonPayload const &arg)
Action is executed in the state machine thread and it performs deferred mode setting.
Definition: supervisory.hpp:265
virtual JsonPayload ActionGetDeferredModes()
Action is used for mode inspection and executed in the state machine thread.
Definition: supervisory.hpp:297
virtual void ActivitySettingModes(StopToken st, JsonPayload const &arg)
Activity executed in its own thread that performs the mode setting.
Definition: supervisory.hpp:242
virtual std::string ActionGetStatus()
Action is used for system status inspection and is executed in the state machine thread.
Definition: supervisory.hpp:229
virtual JsonPayload ActionGetAvailableModes()
Action is used for mode inspection and executed in the state machine thread.
Definition: supervisory.hpp:275
virtual bool GuardSettingModesAllowed(JsonPayload const &arg)
Determines if set mode is possible at this time with the provided argument.
Definition: supervisory.hpp:253
virtual bool GuardWritingBackAllowed(JsonPayload const &arg)
Determines if writeback is possible at this time with the provided argument.
Definition: supervisory.hpp:322
Definition: supervisory.hpp:634
ModelBuilder(StateMachineEngine &engine)
Definition: supervisory.hpp:636
Definition: supervisory.hpp:345
std::list< std::string > m_no_writeback_in_states
Definition: supervisory.hpp:628
std::function< void()> m_writeback_success_handler
Definition: supervisory.hpp:625
std::function< void()> m_setmode_success_handler
Definition: supervisory.hpp:623
std::function< void(std::exception_ptr)> m_setmode_error_handler
Definition: supervisory.hpp:624
std::list< std::string > m_no_setmode_in_states
Definition: supervisory.hpp:627
std::function< void(std::exception_ptr)> m_writeback_error_handler
Definition: supervisory.hpp:626
OutputStage(StateMachineEngine &engine, BizLogicIf &bl)
Definition: supervisory.hpp:347
std::optional< detail::SetModesContext > m_setmode_ctx
Definition: supervisory.hpp:621
std::optional< detail::WriteBackContext > m_writeback_ctx
Definition: supervisory.hpp:622
static void Register(CommandReplier &replier, StateMachineEngine &engine)
Definition: supervisoryCmdsImpl.hpp:87
Holds context necessary for processing a SetModes request to completion.
Definition: supervisory.hpp:38
void SendErrorReply(std::exception_ptr eptr) noexcept
Send exceptional reply.
Definition: supervisory.hpp:80
SetModesContext(SetModesContext &&) noexcept=default
JsonPayload const & GetArg() const noexcept
Definition: supervisory.hpp:55
SetModesContext(rad::cii::Request< std::string, std::string > const &request)
Construct context with provided request.
Definition: supervisory.hpp:46
void SendReply() noexcept
Sends hardcoded string reply.
Definition: supervisory.hpp:62
Holds context necessary for processing a WriteBack request to completion.
Definition: supervisory.hpp:110
void SendReply() noexcept
Sends hardcoded string reply.
Definition: supervisory.hpp:134
WriteBackContext(rad::cii::Request< std::string, std::string > const &request)
Construct context with provided request.
Definition: supervisory.hpp:118
JsonPayload const & GetArg() const noexcept
Definition: supervisory.hpp:127
void SendStopReply() noexcept
Send stop reply.
Definition: supervisory.hpp:166
void SendErrorReply(std::exception_ptr eptr) noexcept
Send exceptional reply.
Definition: supervisory.hpp:153
WriteBackContext(WriteBackContext &&) noexcept=default
Provides macros and utilities for exception handling.
Defines the JSON payload type JsonPayload.
Definition: commandReplier.cpp:22
const std::string STD_OK_REPLY
Definition: stdComponent.hpp:86
auto WrapWithNested(E &&exception) noexcept(std::is_nothrow_constructible_v< detail::UnspecifiedNested< typename std::decay_t< E > >, E && >)
Constructs an unspecified exception that derives from both the provided object and std::nested_except...
Definition: exceptions.hpp:121
log4cplus::Logger & GetLogger(const std::string &name="app")
Get handle to a specific logger.
Definition: logger.cpp:180
nlohmann::json JsonPayload
Type requirements:
Definition: jsonPayload.hpp:25
rad::StopToken StopToken
Definition: stopToken.hpp:20
@ Simple
Definition: model.hpp:22
@ Composite
Definition: model.hpp:22
@ Initial
Definition: model.hpp:22
Definition: rtcSupervisor.cpp:21
Lifecycle of a basic 'RtcComponent'.
Life cycle extension to make RtcComponent Supervisory.
Definition: supervisory.hpp:213
Implementation of MAL commands for layer 'Supervisory'.