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