13#ifndef RTCTK_RTCSUPERVISOR_SUPERVISORY_HPP
14#define RTCTK_RTCSUPERVISOR_SUPERVISORY_HPP
25using namespace rtctk::componentFramework;
46 explicit SetModesContext(
const rad::cii::Request<std::string, std::string>& request)
47 : m_logger(
GetLogger(
"rtctk")), m_request(request), m_arg() {
48 m_arg = JsonPayload::parse(m_request.GetRequestPayload());
65 }
catch (
const std::exception& 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 (
const std::exception& 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'");
346 Super::InputStage::Start();
371 this->m_engine.PostEvent(std::make_unique<events::SetModesDone>());
375 this->m_engine.PostEvent(std::make_unique<events::SetModesError>(std::move(eptr)));
379 this->m_engine.PostEvent(std::make_unique<events::WriteBackDone>());
383 this->m_engine.PostEvent(std::make_unique<events::WriteBackError>(std::move(eptr)));
388 this->m_no_disable_in_states.emplace_back(
"On:SetModes:Busy");
392 this->m_no_update_in_states.emplace_back(
"On:SetModes:Busy");
418 "GuardSettingModesAllowed", [
this](
const events::SetModes& ev) ->
bool {
427 auto act_state = this->m_engine.GetState();
429 if (act_state.find(s) != std::string::npos) {
435 .GuardSettingModesAllowed(ctx.
GetArg())) {
447 "GuardWritingBackAllowed", [
this](
const events::WriteBack& ev) ->
bool {
456 auto act_state = this->m_engine.GetState();
458 if (act_state.find(s) != std::string::npos) {
464 .GuardWritingBackAllowed(ctx.
GetArg())) {
478 "ActivitySettingModes",
488 "ActivityWritingBack",
498 "ActivityRecovering",
500 static_cast<BizLogicIf&
>(this->m_logic).ActivityRecovering(stop_token);
502 this->m_success_handler,
503 this->m_error_handler);
510 if (req ==
nullptr) {
514 auto status =
static_cast<BizLogicIf&
>(this->m_logic).ActionGetStatus();
515 req->SetReplyValue(status);
519 "ActionSetDeferredModes", [
this](
const events::SetDeferredModes& ev) {
521 if (req ==
nullptr) {
526 auto json_arg = JsonPayload::parse(req->GetRequestPayload());
527 static_cast<BizLogicIf&
>(this->m_logic).ActionSetDeferredModes(json_arg);
532 auto eptr = std::make_exception_ptr(nested);
540 "ActionGetAvailableModes", [
this](
const events::GetAvailableModes& ev) {
542 if (req ==
nullptr) {
548 static_cast<BizLogicIf&
>(this->m_logic).ActionGetAvailableModes();
549 req->SetReplyValue(result.dump());
553 auto eptr = std::make_exception_ptr(nested);
561 "ActionGetActiveModes", [
this](
const events::GetActiveModes& ev) {
563 if (req ==
nullptr) {
569 static_cast<BizLogicIf&
>(this->m_logic).ActionGetActiveModes();
570 req->SetReplyValue(result.dump());
574 auto eptr = std::make_exception_ptr(nested);
582 "ActionGetDeferredModes", [
this](
const events::GetDeferredModes& ev) {
584 if (req ==
nullptr) {
590 static_cast<BizLogicIf&
>(this->m_logic).ActionGetDeferredModes();
591 req->SetReplyValue(result.dump());
595 auto eptr = std::make_exception_ptr(nested);
603 [
this](
const events::SetModesDone&) {
610 "ActionSettingModesFailed", [
this](
const events::SetModesError& ev) {
616 "ActionWriteBackDone", [
this](
const events::WriteBackDone&) {
623 "ActionWriteBackFailed", [
this](
const events::WriteBackError& ev) {
629 "ActionWriteBackStopped", [
this](
const events::Stop& ev) {
631 if (req ==
nullptr) {
645 if (this->m_tmp_request) {
647 this->m_tmp_request =
nullptr;
653 if (this->m_tmp_request) {
654 this->m_tmp_request->SetException(
656 this->m_tmp_request =
nullptr;
680 this->mm.AddState(
Composite,
"On:SetModes",
"On");
681 this->mm.AddState(
Initial,
"On:SetModes:Initial",
"On:SetModes");
682 this->mm.AddState(
Simple,
"On:SetModes:Idle",
"On:SetModes");
683 this->mm.AddState(
Simple,
"On:SetModes:Busy",
"On:SetModes",
"ActivitySettingModes");
684 this->mm.AddState(
Initial,
"On::Operational::Initial",
"On::Operational");
685 this->mm.AddState(
Simple,
"On::Operational::Idle",
"On::Operational");
686 this->mm.AddState(
Simple,
"On::Operational::Recovering",
"On::Operational",
"ActivityRecovering",
"ActionRecoveringEntry");
687 this->mm.AddState(
Simple,
"On::Operational::Error",
"On::Operational");
689 this->mm.AddTrans(
"On",
"" , events::GetAvailableModes::ID,
"",
"ActionGetAvailableModes");
690 this->mm.AddTrans(
"On::NotOperational::NotReady",
"" , events::SetDeferredModes::ID,
"",
"ActionSetDeferredModes");
691 this->mm.AddTrans(
"On::NotOperational::NotReady",
"" , events::GetDeferredModes::ID,
"",
"ActionGetDeferredModes");
692 this->mm.AddTrans(
"On::NotOperational::Ready",
"" , events::SetDeferredModes::ID,
"",
"ActionSetDeferredModes");
693 this->mm.AddTrans(
"On::NotOperational::Ready",
"" , events::GetDeferredModes::ID,
"",
"ActionGetDeferredModes");
694 this->mm.AddTrans(
"On::NotOperational::Ready",
"" , events::GetActiveModes::ID,
"",
"ActionGetActiveModes");
695 this->mm.AddTrans(
"On::NotOperational::Enabling",
"" , events::GetActiveModes::ID,
"",
"ActionGetActiveModes");
696 this->mm.AddTrans(
"On::Operational",
"" , events::GetActiveModes::ID,
"",
"ActionGetActiveModes");
698 this->mm.AddTrans(
"On::Operational::Initial" ,
"On::Operational::Idle" );
699 this->mm.AddTrans(
"On::Operational::Idle" ,
"On::Operational::Recovering" ,
"events.Recover");
700 this->mm.AddTrans(
"On::Operational::Error" ,
"On::Operational::Recovering" ,
"events.Recover");
701 this->mm.AddTrans(
"On::Operational::Recovering" ,
"On::Operational::Idle" ,
"events.Done",
"" ,
"ActionRecoveringDone");
702 this->mm.AddTrans(
"On::Operational::Recovering" ,
"On::Operational::Error" ,
"events.Error",
"" ,
"ActionRecoveringFailed");
704 this->mm.AddTrans(
"On:SetModes:Initial",
"On:SetModes:Idle");
705 this->mm.AddTrans(
"On:SetModes:Idle",
"On:SetModes:Busy", events::SetModes::ID,
"GuardSettingModesAllowed");
706 this->mm.AddTrans(
"On:SetModes:Busy",
"On:SetModes:Idle", events::SetModesDone::ID,
"",
"ActionSettingModesDone");
707 this->mm.AddTrans(
"On:SetModes:Busy",
"On:SetModes:Idle", events::SetModesError::ID,
"",
"ActionSettingModesFailed");
709 this->mm.AddState(
Composite,
"On:WriteBack",
"On");
710 this->mm.AddState(
Initial,
"On:WriteBack:Initial",
"On:WriteBack");
711 this->mm.AddState(
Simple,
"On:WriteBack:Idle",
"On:WriteBack");
712 this->mm.AddState(
Simple,
"On:WriteBack:Busy",
"On:WriteBack",
"ActivityWritingBack");
714 this->mm.AddTrans(
"On:WriteBack:Initial",
"On:WriteBack:Idle");
715 this->mm.AddTrans(
"On:WriteBack:Idle",
"On:WriteBack:Busy", events::WriteBack::ID,
"GuardWritingBackAllowed");
716 this->mm.AddTrans(
"On:WriteBack:Busy",
"On:WriteBack:Idle", events::WriteBackDone::ID,
"",
"ActionWriteBackDone");
717 this->mm.AddTrans(
"On:WriteBack:Busy",
"On:WriteBack:Idle", events::WriteBackError::ID,
"",
"ActionWriteBackFailed");
718 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:161
std::string Str() const
Convenience function for constructing a std::string from the exception.
Definition exceptions.hpp:179
static void Register(CommandReplier &replier, StateMachineEngine &engine)
Definition rtcCmdsImpl.hpp:68
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:213
Definition stateMachineEngine.hpp:35
void RegisterGuardStatic(const std::string &id, std::function< bool(const Event &)> guard)
Register guard for statically known event type.
Definition stateMachineEngine.hpp:126
void RegisterAction(const std::string &id, ActionMethod action)
Register action.
Definition stateMachineEngine.cpp:86
void RegisterRejectHandler(const std::string &id, RejectMethod reject)
Register reject handler.
Definition stateMachineEngine.cpp:129
void RegisterActivity(const std::string &id, ActivityMethod activity, SuccessMethod on_success, FailureMethod on_failure)
Register activity.
Definition stateMachineEngine.cpp:121
void RegisterActionStatic(const std::string &id, std::function< void(const Event &)> action)
Register action for statically known event type.
Definition stateMachineEngine.hpp:93
Definition stdComponent.hpp:148
Definition stdComponent.hpp:420
Definition stdComponent.hpp:183
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()
Definition supervisory.hpp:286
virtual bool GuardSettingModesAllowed(const JsonPayload &arg)
Determines if set mode is possible at this time with the provided argument.
Definition supervisory.hpp:253
virtual void ActivitySettingModes(StopToken st, const JsonPayload &arg)
Definition supervisory.hpp:242
virtual JsonPayload ActionGetDeferredModes()
Definition supervisory.hpp:297
virtual std::string ActionGetStatus()
Definition supervisory.hpp:229
virtual bool GuardWritingBackAllowed(const JsonPayload &arg)
Definition supervisory.hpp:322
virtual void ActivityRecovering(StopToken st)
Definition supervisory.hpp:334
virtual JsonPayload ActionGetAvailableModes()
Definition supervisory.hpp:275
virtual void ActionSetDeferredModes(const JsonPayload &arg)
Definition supervisory.hpp:265
virtual void ActivityWritingBack(StopToken st, const JsonPayload &arg)
Definition supervisory.hpp:310
ModelBuilder(StateMachineEngine &engine)
Definition supervisory.hpp:678
std::list< std::string > m_no_writeback_in_states
Definition supervisory.hpp:670
std::function< void()> m_writeback_success_handler
Definition supervisory.hpp:667
std::function< void()> m_setmode_success_handler
Definition supervisory.hpp:665
std::function< void(std::exception_ptr)> m_setmode_error_handler
Definition supervisory.hpp:666
std::list< std::string > m_no_setmode_in_states
Definition supervisory.hpp:669
std::function< void(std::exception_ptr)> m_writeback_error_handler
Definition supervisory.hpp:668
OutputStage(StateMachineEngine &engine, BizLogicIf &bl)
Definition supervisory.hpp:359
std::optional< detail::SetModesContext > m_setmode_ctx
Definition supervisory.hpp:663
std::optional< detail::WriteBackContext > m_writeback_ctx
Definition supervisory.hpp:664
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
SetModesContext(const rad::cii::Request< std::string, std::string > &request)
Construct context with provided request.
Definition supervisory.hpp:46
void SendErrorReply(std::exception_ptr eptr) noexcept
Send exceptional reply.
Definition supervisory.hpp:80
SetModesContext(SetModesContext &&) noexcept=default
const JsonPayload & GetArg() const noexcept
Definition supervisory.hpp:55
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
const JsonPayload & GetArg() const noexcept
Definition supervisory.hpp:127
WriteBackContext(const rad::cii::Request< std::string, std::string > &request)
Construct context with provided request.
Definition supervisory.hpp:118
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 measurable.hpp:26
@ Simple
Definition model.hpp:23
@ Composite
Definition model.hpp:23
@ Initial
Definition model.hpp:23
rad::StopToken StopToken
Definition stopToken.hpp:20
std::shared_ptr< typename EVENT::payload_t > GetPayloadNothrow(scxml4cpp::Context *c)
Definition stateMachineEngine.hpp:256
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:97
log4cplus::Logger & GetLogger(const std::string &name="app")
Get handle to a specific logger.
Definition logger.cpp:192
nlohmann::json JsonPayload
Type requirements:
Definition jsonPayload.hpp:25
Definition alertAggregator.cpp:19
Lifecycle of a basic 'RtcComponent'.
RtcComponentSuper Super
Definition rtcComponent.hpp:75
Life cycle extension to make RtcComponent Supervisory.
Definition supervisory.hpp:213
Implementation of MAL commands for layer 'Supervisory'.