ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
requestor.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_ocm_serverctl
4  * @copyright ESO - European Southern Observatory
5  *
6  * @brief
7  */
8 #include <chrono>
9 #include <string>
10 
11 #include <daq/conversion.hpp>
12 #include <daqif/parsing.hpp>
13 
14 #include "requestor.hpp"
15 
16 using daq::ToString;
17 
18 std::ostream& operator<<(std::ostream& os, std::shared_ptr<daqif::DaqStatus> const& s) {
19  os << std::setprecision(std::numeric_limits<long double>::digits10 + 1);
20  os << "DaqStatus:\n"
21  << " id: " << s->getId() << "\n"
22  << " file_id: " << s->getFileId() << "\n"
23  << " state: \"" << ToString(s->getState()) << "\"\n"
24  << " substate: \"" << ToString(s->getSubState()) << "\"\n"
25  << " error: \"" << (s->getError() ? "true" : "false") << "\n"
26  << " message: " << s->getMessage() << "\n"
27  << " result: " << s->getResult() << "\n"
28  << " timestamp: " << s->getTimestamp();
29  os << "\n";
30 
31  return os;
32 }
33 
34 std::ostream& operator<<(std::ostream& os, std::shared_ptr<daqif::DaqReply> const& s) {
35  os << "DaqReply:\n"
36  << " id: " << s->getId() << "\n"
37  << " error: " << (s->getError() ? "true" : "false") << "\n";
38  return os;
39 }
40 
41 std::ostream& operator<<(std::ostream& os, std::shared_ptr<daqif::AwaitDaqReply> const& s) {
42  os << "AwaitDaqReply:\n"
43  << " timeout: " << (s->getTimeout() ? "true" : "false") << "\n"
44  << " status: " << s->getStatus() << "\n";
45  return os;
46 }
47 
48 std::ostream&
49 operator<<(std::ostream& os, std::vector<std::shared_ptr<daqif::DaqStatus>> const& vec) {
50  for (auto const& s : vec) {
51  os << JsonPrint(s);
52  os << "\n";
53  }
54  return os;
55 }
56 
57 std::ostream& operator<<(std::ostream& os, JsonPrint<std::shared_ptr<daqif::DaqStatus>> const& s) {
58  os << std::setprecision(std::numeric_limits<long double>::digits10 + 1);
59  os << "{\n"
60  " \"id\": \""
61  << s.t->getId()
62  << "\",\n"
63  " \"file_id\": \""
64  << s.t->getFileId()
65  << "\",\n"
66  " \"state\": \""
67  << ToString(s.t->getState())
68  << "\",\n"
69  " \"substate\": \""
70  << ToString(s.t->getSubState())
71  << "\",\n"
72  " \"error\": "
73  << (s.t->getError() ? "true" : "false")
74  << ",\n"
75  " \"result\": \""
76  << s.t->getResult()
77  << "\",\n"
78  " \"timestamp\": "
79  << s.t->getTimestamp() << "\n"
80  << "}";
81  return os;
82 }
83 
84 std::ostream&
85 operator<<(std::ostream& os, JsonPrint<std::vector<std::shared_ptr<daqif::DaqStatus>>> const& vec) {
86  os << "[\n";
87  size_t num = 0;
88  for (auto const& s : vec.t) {
89  num++;
90  os << JsonPrint(s);
91  if (num < vec.t.size()) {
92  os << ",\n";
93  }
94  }
95  os << "\n]";
96  return os;
97 }
98 
99 std::ostream& operator<<(std::ostream& os, JsonPrint<std::shared_ptr<daqif::DaqReply>> const& s) {
100  os << "{\n"
101  " \"id\": \""
102  << s.t->getId()
103  << "\",\n"
104  " \"error\": "
105  << (s.t->getError() ? "true" : "false") << "\n";
106  os << "}";
107 
108  return os;
109 }
110 
111 std::ostream&
112 operator<<(std::ostream& os, JsonPrint<std::shared_ptr<daqif::AwaitDaqReply>> const& s) {
113  os << "{\n"
114  " \"timeout\": "
115  << (s.t->getTimeout() ? "true" : "false")
116  << ",\n"
117  " \"status\": "
118  << JsonPrint(s.t->getStatus()) << "\n";
119  os << "}";
120 
121  return os;
122 }
123 
124 std::ostream& operator<<(std::ostream& os, JsonPrint<std::string> const& s) {
125  os << "\"" << s.t << "\"";
126  return os;
127 }
128 
129 Requestor::Requestor(CommonArgs& args) : m_args(args) {
130 }
131 
132 void Requestor::AddOptions(CLI::App* app) {
133 }
134 
135 void SetLogLevelRequestor::AddOptions(CLI::App* sub) {
136  sub->add_option("logger", m_logger, "Logger name");
137  sub->add_option("level", m_level, "Log level");
138 }
139 
140 void SetLogLevelRequestor::Handle(::stdif::StdCmdsSync& std_cmds,
141  ::daqif::OcmDaqControlSync& daq_cmds,
142  CLI::App* app) {
143  auto log_info = m_args.mal->createDataEntity<stdif::LogInfo>();
144  log_info->setLogger(m_logger);
145  log_info->setLevel(m_level);
146  auto reply = std_cmds.SetLogLevel(log_info);
147  if (m_args.json) {
148  std::cout << JsonPrint(reply);
149  } else {
150  std::cout << "Reply: " << reply << std::endl;
151  }
152 }
153 
154 void StartDaqRequestor::AddOptions(CLI::App* sub) {
155  sub->add_option("--id", m_id, "DAQ ID");
156  sub->add_option("--prefix", m_prefix, "File prefix");
157  sub->add_option("--properties", m_properties, "JSON properties");
158  sub->add_option("primary", m_primary_sources, "Primary data sources");
159  sub->add_option("metadata", m_metadata_sources, "Metadata sources");
160 }
161 
162 void StartDaqRequestor::Handle(::stdif::StdCmdsSync& std_cmds,
163  ::daqif::OcmDaqControlSync& daq_cmds,
164  CLI::App* app) {
165  auto reply =
167  if (m_args.json) {
168  std::cout << JsonPrint(reply);
169  } else {
170  std::cout << "Reply: " << reply << std::endl;
171  }
172 }
173 
175  sub->add_option("id", m_id, "DAQ ID");
176  sub->add_option("keywords", m_keywords, "JSON encoded list of keywords");
177 }
178 
179 void UpdateKeywordsRequestor::Handle(::stdif::StdCmdsSync& std_cmds,
180  ::daqif::OcmDaqControlSync& daq_cmds,
181  CLI::App* app) {
182  auto reply = daq_cmds.UpdateKeywords(m_id, m_keywords);
183  if (m_args.json) {
184  std::cout << JsonPrint(reply);
185  } else {
186  std::cout << "Reply: " << reply << std::endl;
187  }
188 }
189 
190 void AwaitStateRequestor::AddOptions(CLI::App* sub) {
191  sub->add_option("id", m_id, "DAQ ID");
192  sub->add_option("state", m_state, "State to await");
193  sub->add_option("substate", m_substate, "Substate to await");
194  sub->add_option("timeout", m_timeout, "timeout in [fractional] seconds");
195  sub->callback([&] {
196  auto min_timeout = m_timeout + 2;
197  if (m_args.timeout < min_timeout) {
198  std::cerr << "Note: Setting request timeout to " << min_timeout
199  << "s due to await timeout exceeding request timeout\n";
200  m_args.timeout = min_timeout;
201  }
202  });
203 }
204 
205 void AwaitStateRequestor::Handle(::stdif::StdCmdsSync& std_cmds,
206  ::daqif::OcmDaqControlSync& daq_cmds,
207  CLI::App* app) {
208  auto state = daqif::FromString<daqif::DaqState>(m_state);
209  auto substate = daqif::FromString<daqif::DaqSubState>(m_substate);
210  auto reply = daq_cmds.AwaitDaqState(m_id, state, substate, m_timeout);
211  if (m_args.json) {
212  std::cout << JsonPrint(reply);
213  } else {
214  std::cout << "Reply: " << reply << std::endl;
215  }
216 }
CommonArgs
Definition: requestor.hpp:44
parsing.hpp
Contains parse functions for daqif.
Requestor::Requestor
Requestor(CommonArgs &args)
Definition: requestor.cpp:129
JsonPrint
Definition: requestor.hpp:30
AwaitStateRequestor::Handle
void Handle(::stdif::StdCmdsSync &std_cmds, ::daqif::OcmDaqControlSync &daq_cmds, CLI::App *arg) override
Receives a parsed variables_map that include the options added with AddOptions.
Definition: requestor.cpp:205
Requestor::AddOptions
virtual void AddOptions(CLI::App *app)
Add arguments to command.
Definition: requestor.cpp:132
CommonArgs::json
bool json
Definition: requestor.hpp:45
UpdateKeywordsRequestor::AddOptions
void AddOptions(CLI::App *app) override
Add arguments to command.
Definition: requestor.cpp:174
StartDaqRequestor::m_primary_sources
std::string m_primary_sources
Definition: requestor.hpp:174
conversion.hpp
Contains support functions for daqif.
JsonPrint::t
T const & t
Definition: requestor.hpp:33
CommonArgs::mal
elt::mal::Mal * mal
Definition: requestor.hpp:50
StartDaqRequestor::Handle
void Handle(::stdif::StdCmdsSync &std_cmds, ::daqif::OcmDaqControlSync &daq_cmds, CLI::App *arg) override
Receives a parsed variables_map that include the options added with AddOptions.
Definition: requestor.cpp:162
UpdateKeywordsRequestor::Handle
void Handle(::stdif::StdCmdsSync &std_cmds, ::daqif::OcmDaqControlSync &daq_cmds, CLI::App *arg) override
Receives a parsed variables_map that include the options added with AddOptions.
Definition: requestor.cpp:179
CommonArgs::timeout
unsigned timeout
Definition: requestor.hpp:47
StartDaqRequestor::m_properties
std::string m_properties
Definition: requestor.hpp:176
AwaitStateRequestor::AddOptions
void AddOptions(CLI::App *app) override
Add arguments to command.
Definition: requestor.cpp:190
StartDaqRequestor::m_id
std::string m_id
Definition: requestor.hpp:172
daq::ToString
std::string_view ToString(daqif::DaqState state) noexcept
Definition: conversion.cpp:142
requestor.hpp
SetLogLevelRequestor::AddOptions
virtual void AddOptions(CLI::App *app) override
Add arguments to command.
Definition: requestor.cpp:135
StartDaqRequestor::m_prefix
std::string m_prefix
Definition: requestor.hpp:173
SetLogLevelRequestor::Handle
virtual void Handle(::stdif::StdCmdsSync &std_cmds, ::daqif::OcmDaqControlSync &daq_cmds, CLI::App *arg) override
Receives a parsed variables_map that include the options added with AddOptions.
Definition: requestor.cpp:140
StartDaqRequestor::AddOptions
void AddOptions(CLI::App *app) override
Add arguments to command.
Definition: requestor.cpp:154
Requestor::m_args
CommonArgs & m_args
Definition: requestor.hpp:73
StartDaqRequestor::m_metadata_sources
std::string m_metadata_sources
Definition: requestor.hpp:175
operator<<
std::ostream & operator<<(std::ostream &os, std::shared_ptr< daqif::DaqStatus > const &s)
Definition: requestor.cpp:18