ifw-daq  1.0.0
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 #include <ocmif/conversion.hpp>
11 #include <ocmif/parsing.hpp>
12 
13 #include "requestor.hpp"
14 
15 using ocmif::ToString;
16 
17 std::ostream& operator<<(std::ostream& os, std::shared_ptr<ocmif::DaqStatus> const& s) {
18  os << std::setprecision(std::numeric_limits<long double>::digits10 + 1);
19  os << "DaqStatus:\n"
20  << " id: " << s->getId() << "\n"
21  << " state: \"" << ToString(s->getState()) << "\"\n"
22  << " substate: \"" << ToString(s->getSubState()) << "\"\n"
23  << " error: \"" << (s->getError() ? "true" : "false") << "\n"
24  << " timestamp: " << s->getTimestamp();
25  os << "\n";
26 
27  return os;
28 }
29 
30 std::ostream& operator<<(std::ostream& os, std::shared_ptr<ocmif::DaqReply> const& s) {
31  os << "DaqReply:\n"
32  << " id: " << s->getId() << "\n"
33  << " error: " << (s->getError() ? "true" : "false") << "\n";
34  return os;
35 }
36 
37 std::ostream& operator<<(std::ostream& os, std::shared_ptr<ocmif::AwaitDaqReply> const& s) {
38  os << "AwaitDaqReply:\n"
39  << " timeout: " << (s->getTimeout() ? "true" : "false") << "\n"
40  << " status: " << s->getStatus() << "\n";
41  return os;
42 }
43 
44 std::ostream& operator<<(std::ostream& os, std::vector<std::shared_ptr<ocmif::DaqStatus>> const& vec) {
45  for (auto const& s: vec) {
46  os << JsonPrint(s);
47  os << "\n";
48  }
49  return os;
50 }
51 
52 
53 std::ostream& operator<<(std::ostream& os, JsonPrint<std::shared_ptr<ocmif::DaqStatus>> const& s) {
54  os << std::setprecision(std::numeric_limits<long double>::digits10 + 1);
55  os << "{\n"
56  " \"id\": \""
57  << s.t->getId()
58  << "\",\n"
59  " \"state\": \""
60  << ToString(s.t->getState())
61  << "\",\n"
62  " \"substate\": \""
63  << ToString(s.t->getSubState())
64  << "\",\n"
65  " \"error\": "
66  << (s.t->getError() ? "true" : "false")
67  << ",\n"
68  " \"timestamp\": "
69  << s.t->getTimestamp()
70  << "\n"
71  << "}";
72  return os;
73 }
74 
75 std::ostream&
76 operator<<(std::ostream& os, JsonPrint<std::vector<std::shared_ptr<ocmif::DaqStatus>>> const& vec) {
77  os << "[\n";
78  size_t num = 0;
79  for (auto const& s : vec.t) {
80  num++;
81  os << JsonPrint(s);
82  if (num < vec.t.size()) {
83  os << ",\n";
84  }
85  }
86  os << "\n]";
87  return os;
88 }
89 
90 std::ostream& operator<<(std::ostream& os, JsonPrint<std::shared_ptr<ocmif::DaqReply>> const& s) {
91  os << "{\n"
92  " \"id\": \""
93  << s.t->getId() << "\",\n"
94  " \"error\": "
95  << (s.t->getError() ? "true" : "false") << "\n";
96  os << "}";
97 
98  return os;
99 }
100 
101 std::ostream& operator<<(std::ostream& os, JsonPrint<std::shared_ptr<ocmif::AwaitDaqReply>> const& s) {
102  os << "{\n"
103  " \"timeout\": "
104  << (s.t->getTimeout() ? "true" : "false") << ",\n"
105  " \"status\": "
106  << JsonPrint(s.t->getStatus()) << "\n";
107  os << "}";
108 
109  return os;
110 }
111 
112 std::ostream& operator<<(std::ostream& os, JsonPrint<std::string> const& s) {
113  os << "\"" << s.t << "\"";
114  return os;
115 }
116 
117 Requestor::Requestor(CommonArgs& args) : m_args(args) {
118 }
119 
120 std::string Requestor::Synopsis() {
121  return std::string();
122 }
123 
124 void Requestor::AddOptions(boost::program_options::options_description& opt) {
125 }
127  return "[<options>] <logger> <level>";
128 }
129 
130 void SetLogLevelRequestor::Handle(::stdif::StdCmdsSync& std_cmds,
131  ::ocmif::OcmDaqSync& daq_cmds,
132  boost::program_options::variables_map& vm,
133  std::vector<std::string>& opts) {
134  // Note that the command daq.start is also counted.
135  if (opts.size() != 3) {
136  std::cerr << "setloglevel requires `<logger>` and <level>` arguments\n";
137  throw std::invalid_argument("Missing required arguments");
138  }
139  auto log_info = m_args.mal->createDataEntity<stdif::LogInfo>();
140  log_info->setLogger(opts[1]);
141  log_info->setLevel(opts[2]);
142  auto reply = std_cmds.SetLogLevel(log_info);
143  if (m_args.json) {
144  std::cout << JsonPrint(reply);
145  } else {
146  std::cout << "Reply: " << reply << std::endl;
147  }
148 }
149 
150 void StartDaqRequestor::AddOptions(boost::program_options::options_description& opt) {
151  namespace po = boost::program_options;
152  opt.add_options()("id", po::value<std::string>(&m_id), "data acquisition identifer")(
153  "prefix", po::value<std::string>(&m_prefix), "data product prefix")(
154  "properties", po::value<std::string>(&m_properties), "JSON properties");
155 
156 }
157 
159  return "[<options>] <primary-sources> <metadata-sources>";
160 }
161 
162 void StartDaqRequestor::Handle(::stdif::StdCmdsSync& std_cmds,
163  ::ocmif::OcmDaqSync& daq_cmds,
164  boost::program_options::variables_map& vm,
165  std::vector<std::string>& opts) {
166  // Note that the command daq.start is also counted.
167  if (opts.size() != 3) {
168  std::cerr << "start daq requires the `<primary-sources>` and <metadata-sources>` arguments\n";
169  throw std::invalid_argument("Missing required arguments");
170  }
171  std::cerr << "Properties: " << m_properties << "\n";
172  auto reply = daq_cmds.StartDaq(m_id, m_prefix, opts[1], opts[2], m_properties);
173  if (m_args.json) {
174  std::cout << JsonPrint(reply);
175  } else {
176  std::cout << "Reply: " << reply << std::endl;
177  }
178 }
179 
180 void UpdateKeywordsRequestor::AddOptions(boost::program_options::options_description& opt) {
181  namespace po = boost::program_options;
182 }
183 
185  return "[<options>] <id> <keywords>";
186 }
187 
188 void UpdateKeywordsRequestor::Handle(::stdif::StdCmdsSync& std_cmds,
189  ::ocmif::OcmDaqSync& daq_cmds,
190  boost::program_options::variables_map& vm,
191  std::vector<std::string>& opts) {
192  // Note that the command daq.updatekeywords is also counted.
193  if (opts.size() != 3) {
194  std::cerr << "UpdateKeywords requires the `<id>` and <keywords>` arguments\n";
195  throw std::invalid_argument("Missing required arguments");
196  }
197  auto reply = daq_cmds.UpdateKeywords(opts[1], opts[2]);
198  if (m_args.json) {
199  std::cout << JsonPrint(reply);
200  } else {
201  std::cout << "Reply: " << reply << std::endl;
202  }
203 }
204 
205 
206 void AwaitStateRequestor::AddOptions(boost::program_options::options_description& opt) {
207  namespace po = boost::program_options;
208 }
209 
211  return "[<options>] <id> <state> <substate> <timeout[s]>";
212 }
213 
214 void AwaitStateRequestor::Handle(::stdif::StdCmdsSync& std_cmds,
215  ::ocmif::OcmDaqSync& daq_cmds,
216  boost::program_options::variables_map& vm,
217  std::vector<std::string>& opts) {
218  // Note that the command is also counted.
219  if (opts.size() != 5) {
220  std::cerr << "AwaitDaqState requires the `<id>`, `<state>`, `<substate>` and <timeout>` "
221  "arguments\n";
222  throw std::invalid_argument("Missing required arguments");
223  }
224  auto state = ocmif::FromString<ocmif::DaqState>(opts[2]);
225  auto substate = ocmif::FromString<ocmif::DaqSubState>(opts[3]);
226  double timeout = std::stod(opts[4]);
227  auto reply = daq_cmds.AwaitDaqState(opts[1], state, substate, timeout);
228  if (m_args.json) {
229  std::cout << JsonPrint(reply);
230  } else {
231  std::cout << "Reply: " << reply << std::endl;
232  }
233 }
ocmif::ToString
std::string_view ToString(ocmif::DaqState state) noexcept
Definition: conversion.cpp:78
AwaitStateRequestor::Synopsis
std::string Synopsis() override
Definition: requestor.cpp:210
UpdateKeywordsRequestor::Synopsis
std::string Synopsis() override
Definition: requestor.cpp:184
CommonArgs
Definition: requestor.hpp:40
AwaitStateRequestor::Handle
void Handle(::stdif::StdCmdsSync &std_cmds, ::ocmif::OcmDaqSync &daq_cmds, boost::program_options::variables_map &vm, std::vector< std::string > &opts) override
Receives a parsed variables_map that include the options added with AddOptions.
Definition: requestor.cpp:214
parsing.hpp
Contains parse functions for ocmif.
operator<<
std::ostream & operator<<(std::ostream &os, std::shared_ptr< ocmif::DaqStatus > const &s)
Definition: requestor.cpp:17
Requestor::Requestor
Requestor(CommonArgs &args)
Definition: requestor.cpp:117
SetLogLevelRequestor::Synopsis
virtual std::string Synopsis() override
Definition: requestor.cpp:126
JsonPrint
Definition: requestor.hpp:29
StartDaqRequestor::AddOptions
void AddOptions(boost::program_options::options_description &descr) override
Add arguments to command.
Definition: requestor.cpp:150
Requestor::Synopsis
virtual std::string Synopsis()
Definition: requestor.cpp:120
CommonArgs::json
bool json
Definition: requestor.hpp:41
StartDaqRequestor::Synopsis
std::string Synopsis() override
Definition: requestor.cpp:158
conversion.hpp
Contains support functions for ocmif.
JsonPrint::t
T const & t
Definition: requestor.hpp:31
CommonArgs::mal
elt::mal::Mal * mal
Definition: requestor.hpp:45
StartDaqRequestor::m_properties
std::string m_properties
Definition: requestor.hpp:175
StartDaqRequestor::m_id
std::string m_id
Definition: requestor.hpp:171
requestor.hpp
StartDaqRequestor::m_prefix
std::string m_prefix
Definition: requestor.hpp:172
AwaitStateRequestor::AddOptions
void AddOptions(boost::program_options::options_description &descr) override
Add arguments to command.
Definition: requestor.cpp:206
Requestor::m_args
CommonArgs & m_args
Definition: requestor.hpp:70
Requestor::AddOptions
virtual void AddOptions(boost::program_options::options_description &descr)
Add arguments to command.
Definition: requestor.cpp:124
StartDaqRequestor::Handle
void Handle(::stdif::StdCmdsSync &std_cmds, ::ocmif::OcmDaqSync &daq_cmds, boost::program_options::variables_map &vm, std::vector< std::string > &opts) 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, ::ocmif::OcmDaqSync &daq_cmds, boost::program_options::variables_map &vm, std::vector< std::string > &opts) override
Receives a parsed variables_map that include the options added with AddOptions.
Definition: requestor.cpp:188
SetLogLevelRequestor::Handle
virtual void Handle(::stdif::StdCmdsSync &std_cmds, ::ocmif::OcmDaqSync &daq_cmds, boost::program_options::variables_map &vm, std::vector< std::string > &opts) override
Receives a parsed variables_map that include the options added with AddOptions.
Definition: requestor.cpp:130
UpdateKeywordsRequestor::AddOptions
void AddOptions(boost::program_options::options_description &descr) override
Add arguments to command.
Definition: requestor.cpp:180