12 #include <unordered_map>
15 #include <CLI/CLI.hpp>
16 #include <boost/format.hpp>
18 #include <mal/Cii.hpp>
19 #include <mal/Mal.hpp>
20 #include <mal/rr/qos/ConnectionTime.hpp>
21 #include <mal/rr/qos/ReplyTime.hpp>
22 #include <mal/utility/LoadMal.hpp>
23 #include <rad/logger.hpp>
24 #include <log4cplus/hierarchy.h>
41 int main(
int argc,
char** argv) {
44 log4cplus::initialize();
46 log4cplus::Logger::getDefaultHierarchy().resetConfiguration();
49 log4cplus::BasicConfigurator(log4cplus::Logger::getDefaultHierarchy(),
true).configure();
50 RAD_LOG_TO_CONSOLE(
true);
51 RAD_LOG_SETLEVEL(
"TRACE");
58 CLI::App app(
"daqOcmCtl");
62 "request endpoint without service stem. e.g. zpb://127.0.0.1:4020, defaults to env. "
64 ->envname(
"OCM_REQUEST_EP");
66 app.add_option(
"--pep", args.
pub_addr,
"publish endpoint, defaults to env $OCM_PUBLISH_EP")
67 ->envname(
"OCM_PUBLISH_EP");
68 app.add_flag(
"--json", args.
json,
"output in JSON format");
69 app.add_flag(
"--status",
71 "subscribe to server topics (requires --pep or $OCM_PUBLISH_EP to be set)")
73 app.add_option(
"--timeout,-t", args.
timeout,
"request timeout in seconds",
true);
74 app.add_flag_callback(
77 std::cerr <<
"daqOcmCtl " << VERSION
82 throw CLI::Success(VERSION, 0);
84 "Print version and exit");
86 std::vector<std::string> subargs;
88 std::map<std::string, std::unique_ptr<Requestor>> requestors;
90 std::make_pair(
"std.getstatus",
91 std::make_unique<
SimpleRequestor<decltype(&stdif::StdCmds::GetStatus)>>(
92 &stdif::StdCmds::GetStatus, args)));
94 std::make_pair(
"std.getstate",
96 &stdif::StdCmds::GetState, args)));
98 std::make_pair(
"std.getversion",
99 std::make_unique<
SimpleRequestor<decltype(&stdif::StdCmds::GetVersion)>>(
100 &stdif::StdCmds::GetVersion, args)));
102 std::make_pair(
"std.setloglevel", std::make_unique<SetLogLevelRequestor>(args)));
104 std::make_pair(
"std.init",
106 &stdif::StdCmds::Init, args)));
108 std::make_pair(
"std.enable",
110 &stdif::StdCmds::Enable, args)));
112 std::make_pair(
"std.disable",
114 &stdif::StdCmds::Disable, args)));
116 std::make_pair(
"std.exit",
118 &stdif::StdCmds::Exit, args)));
120 requestors.emplace(std::make_pair(
123 &daqif::OcmDaqControl::StopDaq, args)));
124 requestors.emplace(std::make_pair(
127 &daqif::OcmDaqControl::ForceStopDaq, args)));
128 requestors.emplace(std::make_pair(
131 &daqif::OcmDaqControl::AbortDaq, args)));
132 requestors.emplace(std::make_pair(
135 &daqif::OcmDaqControl::ForceAbortDaq, args)));
137 requestors.emplace(std::make_pair(
140 &daqif::OcmDaqControl::GetStatus, args)));
141 requestors.emplace(std::make_pair(
143 std::make_unique<
NoArgRequestor<decltype(&daqif::OcmDaqControl::GetActiveList)>>(
144 &daqif::OcmDaqControl::GetActiveList, args)));
146 requestors.emplace(std::make_pair(
"daq.start", std::make_unique<StartDaqRequestor>(args)));
147 requestors.emplace(std::make_pair(
"daq.startv2", std::make_unique<StartDaqV2Requestor>(args)));
149 std::make_pair(
"daq.updatekeywords", std::make_unique<UpdateKeywordsRequestor>(args)));
151 std::make_pair(
"daq.awaitstate", std::make_unique<AwaitStateRequestor>(args)));
154 for (
auto& r : requestors) {
155 auto& command = r.first;
156 CLI::App* sub = app.add_subcommand(command);
157 r.second->AddOptions(sub);
161 CLI11_PARSE(app, argc, argv);
164 auto mal = elt::mal::loadMal(
"zpb", {});
166 elt::mal::CiiFactory::getInstance().registerMal(
"zpb",
mal);
168 std::optional<daqif::Subscription<stdif::Status>> std_status;
169 std::optional<daqif::Subscription<daqif::DaqStatus>> daq_status;
175 std_status.emplace(daqif::MakeSubscription<stdif::Status>(
178 [](
auto& sub,
auto const& event) {
179 auto const& sample = event.getData();
180 std::cerr <<
"status: " << sample->getStatus() << std::endl;
182 daq_status.emplace(daqif::MakeSubscription<daqif::DaqStatus>(
183 *
mal, Uri(args.
pub_addr +
"daq/status"), [](
auto& sub,
auto const& event) {
184 auto const& sample = event.getData();
185 std::cerr <<
"daq: id=" << sample->getId()
186 <<
", file_id=" << sample->getFileId()
187 <<
", state=" << daq::ToString(sample->getState())
188 <<
", substate=" << daq::ToString(sample->getSubState())
189 <<
", error=" << (sample->getError() ?
"true" :
"false")
190 <<
", message=" << sample->getMessage()
195 auto command = app.get_subcommands();
196 if (command.empty()) {
198 std::cerr <<
"no command provided -> will subscribe indefinitely\n";
200 std::this_thread::sleep_for(std::chrono::milliseconds(100));
207 auto std_cmds = elt::mal::CiiFactory::getInstance().getClient<stdif::StdCmdsSync>(
209 {std::make_shared<elt::mal::rr::qos::ReplyTime>(std::chrono::seconds(args.
timeout))},
211 auto daq_cmds = elt::mal::CiiFactory::getInstance().getClient<::daqif::OcmDaqControlSync>(
213 {std::make_shared<elt::mal::rr::qos::ReplyTime>(std::chrono::seconds(args.
timeout))},
217 auto req_it = requestors.find(command[0]->get_name());
218 if (req_it != requestors.end()) {
219 req_it->second->Handle(*std_cmds, *daq_cmds, command[0]);
220 std::cerr <<
"Done\n";
223 std::cerr <<
"error: Unknown command: '" << command[0]->get_name() <<
"'\n";
226 }
catch (daqif::DaqException
const& e) {
227 std::cerr <<
"DaqException: id: '" << e.getId() <<
"', message: " << e.getMessage()
230 }
catch (CLI::ParseError
const& e) {
231 std::cerr << e.what() << std::endl;
233 }
catch (std::exception
const& e) {
234 std::cerr <<
"error: " << e.what() << std::endl;
237 std::cerr <<
"unknown error" << std::endl;
Contains support functions for daqif.
int main(int argc, char **argv)
network::uri MakeServiceUri(std::string base_uri, std::string_view service_path)
Creates a service URI of the form <baseuri>/<service>.
void SignalHandler(int signal)
volatile std::sig_atomic_t g_signal_status
Simple Daq commands that accepts a single argument id and returns a shared_ptr type that can be forma...
Simple requestor for commands without argument.
Contains URI support functions for daqif.
Contains URI support functions for daqif.