ifw-daq  1.0.0
IFW Data Acquisition modules
actionsStd.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup server
4  * @copyright ESO - European Southern Observatory
5  * @author
6  *
7  * @brief ActionsStd class source file.
8  */
9 
10 #include "actionsStd.hpp"
11 #include <Ocmif.hpp>
12 #include <events.rad.hpp>
13 #include "dataContext.hpp"
14 #include "logger.hpp"
15 
16 #include <rad/exceptions.hpp>
17 #include <rad/getPayload.hpp>
18 #include <rad/mal/request.hpp>
19 #include <rad/smEvent.hpp>
20 
21 #include <scxml4cpp/Event.h>
22 #include <scxml4cpp/Helper.h>
23 #include <scxml4cpp/State.h>
24 
25 namespace server {
26 
27 
28 std::optional<std::string> MakeStatusString(std::set<scxml4cpp::State*>&& states) {
29  using scxml4cpp::State;
30  std::string status;
31  for (auto* s : states) {
32  if (!s->isAtomic()) {
33  continue;
34  }
35  // Found a leaf state. Assume it's the third state in hiearchy. If not it means
36  // we're in
37  State* substate = s;
38  if (substate->getId() == "Off") {
39  return "Off;";
40  }
41  State* state = s->getParent();
42  if (!state) {
43  return std::nullopt;
44  }
45  State* top = state->getParent();
46  if (!top) {
47  return std::nullopt;
48  }
49  if (top->getParent() != nullptr) {
50  // active state was nested to deeply.
51  return std::nullopt;
52  }
53  status = state->getId() + ";" + substate->getId();
54  return status;
55  }
56  return std::nullopt;
57 }
58 
59 
60 ActionsStd::ActionsStd(boost::asio::io_service & ios, rad::SMAdapter & sm, DataContext & data)
61  : rad::ActionGroup("ActionsStd"),
62  m_io_service(ios),
63  m_sm(sm),
64  m_signal(ios, sm, rad::UniqueEvent(new Events::CtrlC())),
65  m_data(data) {
66  RAD_TRACE(GetLogger());
67 
68  m_signal.Add(SIGINT);
69  m_signal.Add(SIGTERM);
70  m_signal.Install();
71 }
72 
73 void ActionsStd::Exit(scxml4cpp::Context * c) {
74  RAD_TRACE(GetLogger());
75 
76  auto req = rad::GetLastEventPayloadNothrow<Events::Exit>(c);
77  if (req == nullptr) {
78  LOG4CPLUS_ERROR(GetLogger(), "Exit event has no associated request!");
79  return;
80  }
81  req->SetReplyValue("OK");
82  m_io_service.stop();
83 }
84 
85 void ActionsStd::ExitNoReply(scxml4cpp::Context * c) {
86  RAD_TRACE(GetLogger());
87  m_io_service.stop();
88 }
89 
90 void ActionsStd::GetState(scxml4cpp::Context * c) {
91  RAD_TRACE(GetLogger());
92 
93  auto req = rad::GetLastEventPayloadNothrow<Events::GetState>(c);
94  if (req == nullptr) {
95  LOG4CPLUS_ERROR(GetLogger(), "Status event has no associated request!");
96  return;
97  }
98  req->SetReplyValue(m_sm.GetStatus());
99 }
100 
101 void ActionsStd::GetStatus(scxml4cpp::Context * c) {
102  RAD_TRACE(GetLogger());
103 
104  auto req = rad::GetLastEventPayloadNothrow<Events::GetStatus>(c);
105  if (req == nullptr) {
106  LOG4CPLUS_ERROR(GetLogger(), "Status event has no associated request!");
107  return;
108  }
109  auto status = MakeStatusString(m_sm.GetActiveStates()).value_or("NotOperational;Undefined");
110  req->SetReplyValue(status);
111 }
112 
113 
114 void ActionsStd::Stop(scxml4cpp::Context * c) {
115  RAD_TRACE(GetLogger());
116 
117  auto req = rad::GetLastEventPayloadNothrow<Events::Stop>(c);
118  if (req == nullptr) {
119  LOG4CPLUS_ERROR(GetLogger(), "Stop event has no associated request!");
120  return;
121  }
122  req->SetReplyValue("OK");
123 }
124 
125 void ActionsStd::Init(scxml4cpp::Context * c) {
126  RAD_TRACE(GetLogger());
127 
128  auto req = rad::GetLastEventPayloadNothrow<Events::Init>(c);
129  if (req == nullptr) {
130  LOG4CPLUS_ERROR(GetLogger(), "Init event has no associated request!");
131  return;
132  }
133  req->SetReplyValue("OK");
134 }
135 
136 void ActionsStd::Enable(scxml4cpp::Context * c) {
137  RAD_TRACE(GetLogger());
138 
139  auto req = rad::GetLastEventPayloadNothrow<Events::Enable>(c);
140  if (req == nullptr) {
141  LOG4CPLUS_ERROR(GetLogger(), "Enable event has no associated request!");
142  return;
143  }
144  req->SetReplyValue("OK");
145 }
146 
147 void ActionsStd::Disable(scxml4cpp::Context * c) {
148  RAD_TRACE(GetLogger());
149 
150  auto req = rad::GetLastEventPayloadNothrow<Events::Disable>(c);
151  if (req == nullptr) {
152  LOG4CPLUS_ERROR(GetLogger(), "Disable event has no associated request!");
153  return;
154  }
155  req->SetReplyValue("OK");
156 }
157 
158 void ActionsStd::Reset(scxml4cpp::Context * c) {
159  RAD_TRACE(GetLogger());
160 
161  auto req = rad::GetLastEventPayloadNothrow<Events::Reset>(c);
162  if (req == nullptr) {
163  LOG4CPLUS_ERROR(GetLogger(), "Reset event has no associated request!");
164  return;
165  }
166  req->SetReplyValue("OK");
167 }
168 
169 void ActionsStd::SetLogLevel(scxml4cpp::Context * c) {
170  RAD_TRACE(GetLogger());
171 
172  auto req = rad::GetLastEventPayloadNothrow<Events::SetLogLevel>(c);
173  if (req == nullptr) {
174  LOG4CPLUS_ERROR(GetLogger(), "SetLogLevel event has no associated request!");
175  return;
176  }
177 
178  auto req_params = req->GetRequestPayload();
179  std::string level = req_params->getLevel();
180  std::string logger_name = req_params->getLogger();
181 
182  log4cplus::LogLevelManager& log_mgr = log4cplus::getLogLevelManager();
183  // LOG4CPLUS_DEBUG(GetLogger(), "Log level" << level);
184  log4cplus::LogLevel ll = log_mgr.fromString(level);
185  if (ll == log4cplus::NOT_SET_LOG_LEVEL) {
186  req->SetReplyValue("ERR unknown logging level: " + level);
187  return;
188  }
189 
190  if (logger_name == "" || logger_name == LOGGER_NAME) {
191  GetLogger().setLogLevel(ll);
192  } else {
193  log4cplus::Logger::getInstance(logger_name).setLogLevel(ll);
194  }
195  LOG4CPLUS_DEBUG(GetLogger(), "Log level set to " << level << " for logger " << logger_name);
196 
197  req->SetReplyValue("OK");
198 }
199 
200 } // namespace server
dataContext.hpp
DataContext class header file.
daq::State
State
Observable states of the data acquisition process.
Definition: state.hpp:41
server::ActionsStd::ExitNoReply
void ExitNoReply(scxml4cpp::Context *c)
Implementation of the ExitNoReply action.
Definition: actionsStd.cpp:85
server::ActionsStd::Reset
void Reset(scxml4cpp::Context *c)
Implementation of the Reset action.
Definition: actionsStd.cpp:158
server::ActionsStd::Exit
void Exit(scxml4cpp::Context *c)
Implementation of the Exit action.
Definition: actionsStd.cpp:73
server::ActionsStd::Init
void Init(scxml4cpp::Context *c)
Implementation of the Init action.
Definition: actionsStd.cpp:125
server::MakeStatusString
std::optional< std::string > MakeStatusString(std::set< scxml4cpp::State * > &&states)
Make a status string from active states (as returned from scxml4cpp::Executor::getStatus()).
Definition: actionsStd.cpp:28
server::ActionsStd::Stop
void Stop(scxml4cpp::Context *c)
Implementation of the Stop action.
Definition: actionsStd.cpp:114
rad
Definition: ioExecutor.hpp:6
server::ActionsStd::Enable
void Enable(scxml4cpp::Context *c)
Implementation of the Enable action.
Definition: actionsStd.cpp:136
server::ActionsStd::Disable
void Disable(scxml4cpp::Context *c)
Implementation of the Disable action.
Definition: actionsStd.cpp:147
server::ActionsStd::SetLogLevel
void SetLogLevel(scxml4cpp::Context *c)
Implementation of the SetLogLevel action.
Definition: actionsStd.cpp:169
server::DataContext
This class provide access to the application run-time data including the in-memory DB.
Definition: dataContext.hpp:21
server::ActionsStd::GetStatus
void GetStatus(scxml4cpp::Context *c)
Implementation of the GetStatus action.
Definition: actionsStd.cpp:101
server
Definition: actionMgr.cpp:21
server::GetLogger
log4cplus::Logger & GetLogger()
Definition: logger.cpp:14
server::ActionsStd::GetState
void GetState(scxml4cpp::Context *c)
Implementation of the GetState action.
Definition: actionsStd.cpp:90
logger.hpp
Default logger name.
server::ActionsStd::ActionsStd
ActionsStd(boost::asio::io_service &ios, rad::SMAdapter &sm, DataContext &data)
Constructor.
Definition: actionsStd.cpp:60
actionsStd.hpp
ActionsStd class header file.
server::LOGGER_NAME
const std::string LOGGER_NAME
Definition: logger.hpp:17