ifw-sup 5.0.0
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1
8#ifndef SUP_SUBSIM_COMMON_UTILS_IPP_
9#define SUP_SUBSIM_COMMON_UTILS_IPP_
10
11#include <string>
12
13#include <ifw/core/utils/bat/logger.hpp>
14#include <rad/smAdapter.hpp>
15#include <rad/mal/request.hpp>
16#include <rad/activity.hpp>
18
19
21
22 template <class EVENT_DONE, class EVENT_ERR>
23 void EmulateEvent(const Config& config,
24 rad::SMAdapter& sm,
25 const std::string &cmd,
26 rad::ThreadActivity* activity) {
27
28 // In case data is not found, it takes the default value
29 auto delay = config.GetReplyDelay(cmd).value_or(0);
30 auto repl_ok = config.GetReplyOk(cmd).value_or(true);
31 const auto msg = config.GetReplyErrorMessage(cmd).value_or("Generic Error");
32
33 LOG4CPLUS_INFO(ifw::core::utils::bat::GetAppLogger(), "Reply msg: " << msg);
34 if (delay > 0) {
35 LOG4CPLUS_DEBUG(ifw::core::utils::bat::GetAppLogger(), "Reply delay: " << delay);
36 int total = 0;
37 int increment = int(delay/100);
38 LOG4CPLUS_DEBUG(ifw::core::utils::bat::GetAppLogger(), "Increment: " << increment);
39 while (total < delay) {
40 if (activity->IsStopRequested()) {
41 sm.PostEvent(rad::UniqueEvent(new EVENT_ERR(cmd + " has been stopped !" )));
42 return;
43 }
44 std::this_thread::sleep_for(std::chrono::milliseconds(increment));
45 total += increment;
46 LOG4CPLUS_DEBUG(ifw::core::utils::bat::GetAppLogger(), "Total Time: " << total << " [ms]");
47 }
48 }
49 if (repl_ok) {
50 sm.PostEvent(rad::UniqueEvent(new EVENT_DONE));
51 } else {
52 sm.PostEvent(rad::UniqueEvent(new EVENT_ERR(msg )));
53 }
54 }
55
56
57} // namespace
58
59#include "utils.ipp"
60#endif // SUP_SUBSIM_COMMON_UTILS_IPP_
Definition config.hpp:67
std::optional< int > GetReplyDelay(const std::string &name) const
Definition config.cpp:140
std::optional< std::string > GetReplyErrorMessage(const std::string &name) const
Definition config.cpp:159
std::optional< bool > GetReplyOk(const std::string &name) const
Definition config.cpp:115
ActionsInit class source file.
Definition actionMgr.cpp:30
void EmulateEvent(const Config &config, rad::SMAdapter &sm, const std::string &cmd, rad::ThreadActivity *activity)
Definition utils.hpp:23
Config class header file.