13#ifndef RTCTK_RTCSUPERVISOR_SUPERVISORY_HPP
14#define RTCTK_RTCSUPERVISOR_SUPERVISORY_HPP
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.push_back(
"On:SetModes:Busy");
392 this->m_no_update_in_states.push_back(
"On:SetModes:Busy");
418 "GuardSettingModesAllowed", [
this](
const events::SetModes&
ev) ->
bool {
427 auto act_state = this->m_engine.GetState();
435 .GuardSettingModesAllowed(
ctx.GetArg())) {
446 engine.RegisterGuardStatic<events::WriteBack>(
447 "GuardWritingBackAllowed", [
this](
const events::WriteBack&
ev) ->
bool {
456 auto act_state = this->m_engine.GetState();
464 .GuardWritingBackAllowed(
ctx.GetArg())) {
478 "ActivitySettingModes",
488 "ActivityWritingBack",
498 "ActivityRecovering",
502 this->m_success_handler,
503 this->m_error_handler);
508 engine.RegisterAction(
"ActionGetStatus", [
this](
auto c) {
510 if (req ==
nullptr) {
514 auto status =
static_cast<BizLogicIf&
>(this->m_logic).ActionGetStatus();
515 req->SetReplyValue(status);
518 engine.RegisterActionStatic<events::SetDeferredModes>(
519 "ActionSetDeferredModes", [
this](
const events::SetDeferredModes&
ev) {
521 if (req ==
nullptr) {
526 auto json_arg = JsonPayload::parse(req->GetRequestPayload());
539 engine.RegisterActionStatic<events::GetAvailableModes>(
540 "ActionGetAvailableModes", [
this](
const events::GetAvailableModes&
ev) {
542 if (req ==
nullptr) {
548 static_cast<BizLogicIf&
>(this->m_logic).ActionGetAvailableModes();
549 req->SetReplyValue(result.dump());
560 engine.RegisterActionStatic<events::GetActiveModes>(
561 "ActionGetActiveModes", [
this](
const events::GetActiveModes&
ev) {
563 if (req ==
nullptr) {
569 static_cast<BizLogicIf&
>(this->m_logic).ActionGetActiveModes();
570 req->SetReplyValue(result.dump());
581 engine.RegisterActionStatic<events::GetDeferredModes>(
582 "ActionGetDeferredModes", [
this](
const events::GetDeferredModes&
ev) {
584 if (req ==
nullptr) {
590 static_cast<BizLogicIf&
>(this->m_logic).ActionGetDeferredModes();
591 req->SetReplyValue(result.dump());
602 engine.RegisterActionStatic<events::SetModesDone>(
"ActionSettingModesDone",
603 [
this](
const events::SetModesDone&) {
609 engine.RegisterActionStatic<events::SetModesError>(
610 "ActionSettingModesFailed", [
this](
const events::SetModesError&
ev) {
615 engine.RegisterActionStatic<events::WriteBackDone>(
616 "ActionWriteBackDone", [
this](
const events::WriteBackDone&) {
622 engine.RegisterActionStatic<events::WriteBackError>(
623 "ActionWriteBackFailed", [
this](
const events::WriteBackError&
ev) {
628 engine.RegisterActionStatic<events::Stop>(
629 "ActionWriteBackStopped", [
this](
const events::Stop&
ev) {
631 if (req ==
nullptr) {
640 engine.RegisterAction(
"ActionRecoveringEntry", [
this](
auto c) {
644 engine.RegisterAction(
"ActionRecoveringDone", [
this](
auto c) {
645 if (this->m_tmp_request) {
647 this->m_tmp_request =
nullptr;
651 engine.RegisterAction(
"ActionRecoveringFailed", [
this](
auto c) {
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::GetActiveModes::ID,
"",
"ActionGetActiveModes");
693 this->mm.AddTrans(
"On::NotOperational::Enabling",
"" , events::GetActiveModes::ID,
"",
"ActionGetActiveModes");
694 this->mm.AddTrans(
"On::Operational",
"" , events::GetActiveModes::ID,
"",
"ActionGetActiveModes");
696 this->mm.AddTrans(
"On::Operational::Initial" ,
"On::Operational::Idle" );
697 this->mm.AddTrans(
"On::Operational::Idle" ,
"On::Operational::Recovering" ,
"events.Recover");
698 this->mm.AddTrans(
"On::Operational::Error" ,
"On::Operational::Recovering" ,
"events.Recover");
699 this->mm.AddTrans(
"On::Operational::Recovering" ,
"On::Operational::Idle" ,
"events.Done",
"" ,
"ActionRecoveringDone");
700 this->mm.AddTrans(
"On::Operational::Recovering" ,
"On::Operational::Error" ,
"events.Error",
"" ,
"ActionRecoveringFailed");
702 this->mm.AddTrans(
"On:SetModes:Initial",
"On:SetModes:Idle");
703 this->mm.AddTrans(
"On:SetModes:Idle",
"On:SetModes:Busy", events::SetModes::ID,
"GuardSettingModesAllowed");
704 this->mm.AddTrans(
"On:SetModes:Busy",
"On:SetModes:Idle", events::SetModesDone::ID,
"",
"ActionSettingModesDone");
705 this->mm.AddTrans(
"On:SetModes:Busy",
"On:SetModes:Idle", events::SetModesError::ID,
"",
"ActionSettingModesFailed");
707 this->mm.AddState(
Composite,
"On:WriteBack",
"On");
708 this->mm.AddState(
Initial,
"On:WriteBack:Initial",
"On:WriteBack");
709 this->mm.AddState(
Simple,
"On:WriteBack:Idle",
"On:WriteBack");
710 this->mm.AddState(
Simple,
"On:WriteBack:Busy",
"On:WriteBack",
"ActivityWritingBack");
712 this->mm.AddTrans(
"On:WriteBack:Initial",
"On:WriteBack:Idle");
713 this->mm.AddTrans(
"On:WriteBack:Idle",
"On:WriteBack:Busy", events::WriteBack::ID,
"GuardWritingBackAllowed");
714 this->mm.AddTrans(
"On:WriteBack:Busy",
"On:WriteBack:Idle", events::WriteBackDone::ID,
"",
"ActionWriteBackDone");
715 this->mm.AddTrans(
"On:WriteBack:Busy",
"On:WriteBack:Idle", events::WriteBackError::ID,
"",
"ActionWriteBackFailed");
716 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:159
std::string Str() const
Convenience function for constructing a std::string from the exception.
Definition exceptions.hpp:177
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:211
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
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 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)
Activity executed in its own thread that performs the mode setting.
Definition supervisory.hpp:242
virtual JsonPayload ActionGetDeferredModes()
Action is used for mode inspection and executed in the state machine thread.
Definition supervisory.hpp:297
virtual std::string ActionGetStatus()
Action is used for system status inspection and is executed in the state machine thread.
Definition supervisory.hpp:229
virtual bool GuardWritingBackAllowed(const JsonPayload &arg)
Determines if writeback is possible at this time with the provided argument.
Definition supervisory.hpp:322
virtual void ActivityRecovering(StopToken st)
Activity executed in its own thread that performs the error recovery.
Definition supervisory.hpp:334
virtual JsonPayload ActionGetAvailableModes()
Action is used for mode inspection and executed in the state machine thread.
Definition supervisory.hpp:275
virtual void ActionSetDeferredModes(const JsonPayload &arg)
Action is executed in the state machine thread and it performs deferred mode setting.
Definition supervisory.hpp:265
virtual void ActivityWritingBack(StopToken st, const JsonPayload &arg)
Activity executed in its own thread that performs the writeback.
Definition supervisory.hpp:310
Definition supervisory.hpp:676
ModelBuilder(StateMachineEngine &engine)
Definition supervisory.hpp:678
Definition supervisory.hpp:357
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 commandReplier.cpp:22
@ 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
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:95
nlohmann::json JsonPayload
Type requirements:
Definition jsonPayload.hpp:25
elt::mal::future< std::string > InjectReqRepEvent(StateMachineEngine &engine)
Definition malEventInjector.hpp:23
Definition rtcSupervisor.cpp:24
Lifecycle of a basic 'RtcComponent'.
Life cycle extension to make RtcComponent Supervisory.
Definition supervisory.hpp:213
Implementation of MAL commands for layer 'Supervisory'.