From 5c06549982147f1d27157140b4000a7f84ed2c44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Fr=C3=B6jdh?= Date: Fri, 23 Aug 2019 14:32:44 +0200 Subject: [PATCH] Cleanup of the CmdProxy and migrated some commands (#52) * migrated rx_fifodepth * Moved and cleand CmdProxy * rx_slient * new commands * examples * fixed result string print --- CMakeLists.txt | 6 +- docs/src/gendoc.cpp | 2 +- sample/api.cpp | 2 +- sample/useResult.cpp | 9 + slsDetectorSoftware/CMakeLists.txt | 1 + slsDetectorSoftware/include/CmdProxy.h | 76 +++++++ slsDetectorSoftware/src/CmdProxy.cpp | 213 +++++++++++++++++ .../src/multiSlsDetectorClient.cpp | 5 +- .../src/slsDetectorCommand.cpp | 58 ----- slsDetectorSoftware/tests/test-Result.cpp | 16 ++ .../tests/test-multiSlsDetectorClient.cpp | 47 ++-- slsSupportLib/include/CmdLineParser.h | 1 + slsSupportLib/include/CmdProxy.h | 214 ------------------ slsSupportLib/include/ToString.h | 45 ++-- slsSupportLib/src/CmdLineParser.cpp | 9 + 15 files changed, 385 insertions(+), 319 deletions(-) create mode 100644 slsDetectorSoftware/include/CmdProxy.h create mode 100644 slsDetectorSoftware/src/CmdProxy.cpp delete mode 100644 slsSupportLib/include/CmdProxy.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 185dc448c..28a654b05 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,6 +44,7 @@ option(SLS_USE_SANITIZER "Sanitizers for debugging" OFF) option(SLS_USE_PYTHON "Python bindings" OFF) option(SLS_USE_CTBGUI "ctb GUI" OFF) option(SLS_BUILD_DOCS "docs" OFF) +option(SLS_BUILD_EXAMPLES "examples" OFF) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) @@ -185,8 +186,9 @@ configure_file( .clang-tidy ${CMAKE_BINARY_DIR}/.clang-tidy ) - -#add_subdirectory(sample) +if (SLS_BUILD_EXAMPLES) + add_subdirectory(sample) +endif(SLS_BUILD_EXAMPLES) if(SLS_BUILD_DOCS) add_subdirectory(docs) diff --git a/docs/src/gendoc.cpp b/docs/src/gendoc.cpp index 31dc13246..89d6e310a 100644 --- a/docs/src/gendoc.cpp +++ b/docs/src/gendoc.cpp @@ -36,7 +36,7 @@ int main() { std::cout << "Generating command line documentation!\n"; - sls::CmdProxy proxy(nullptr); + sls::CmdProxy proxy(nullptr); auto commands = proxy.GetProxyCommands(); std::ofstream fs("commands.rst"); diff --git a/sample/api.cpp b/sample/api.cpp index eff05049a..f353faf0a 100644 --- a/sample/api.cpp +++ b/sample/api.cpp @@ -47,7 +47,7 @@ using std::chrono::seconds; int main() { Detector d; - d.setConfig("/home/l_frojdh/virtual.config"); + // d.setConfig("/home/l_frojdh/virtual.config"); // d.setExptime(nanoseconds(500)); // set exptime of all modules // auto t0 = d.getExptime(); diff --git a/sample/useResult.cpp b/sample/useResult.cpp index cf7cf89c7..796075a52 100644 --- a/sample/useResult.cpp +++ b/sample/useResult.cpp @@ -47,4 +47,13 @@ auto main() -> int { // nres.push_back(sls::time::ns(i)); std::cout << "nres: " << sls::ToString(nres) << '\n'; // + + + /* Convert from Result to Result */ + Result int_result{0,1,0,3,-5}; + Result bool_result{int_result}; + std::cout << bool_result << '\n'; + + Result string_res{"ein", "zwei", "drei"}; + std::cout << string_res << '\n'; } \ No newline at end of file diff --git a/slsDetectorSoftware/CMakeLists.txt b/slsDetectorSoftware/CMakeLists.txt index 9e7136a8d..9a7e32b76 100755 --- a/slsDetectorSoftware/CMakeLists.txt +++ b/slsDetectorSoftware/CMakeLists.txt @@ -5,6 +5,7 @@ set(SOURCES src/slsDetectorCommand.cpp src/slsDetector.cpp src/Detector.cpp + src/CmdProxy.cpp ) set(HEADERS diff --git a/slsDetectorSoftware/include/CmdProxy.h b/slsDetectorSoftware/include/CmdProxy.h new file mode 100644 index 000000000..5cb19d19d --- /dev/null +++ b/slsDetectorSoftware/include/CmdProxy.h @@ -0,0 +1,76 @@ +#pragma once + +#include +#include +#include +#include + +namespace sls { +class Detector; + +class CmdProxy { + public: + explicit CmdProxy(Detector *ptr) : det(ptr) {} + + std::string Call(const std::string &command, + const std::vector &arguments, int detector_id, + int action = -1, std::ostream &os = std::cout); + + bool ReplaceIfDepreciated(std::string &command); + size_t GetFunctionMapSize() const noexcept { return functions.size(); }; + std::vector GetAllCommands(); + std::vector GetProxyCommands(); + + private: + Detector *det; + std::string cmd; + std::vector args; + int det_id{-1}; + + template std::string OutString(const V &value); + template + std::string OutString(const V &value, const std::string &unit); + + using FunctionMap = std::map; + using StringMap = std::map; + + // Initialize maps for translating name and function + FunctionMap functions{{"list", &CmdProxy::ListCommands}, + {"exptime", &CmdProxy::Exptime}, + {"period", &CmdProxy::Period}, + {"subexptime", &CmdProxy::SubExptime}, + {"rx_fifodepth", &CmdProxy::RxFifoDepth}, + {"rx_silent", &CmdProxy::RxSilent}}; + + StringMap depreciated_functions{{"r_readfreq", "rx_readfreq"}, + {"r_padding", "rx_padding"}, + {"r_silent", "rx_silent"}, + {"r_lastclient", "rx_lastclient"}, + {"r_lock", "rx_lock"}, + {"r_online", "rx_online"}, + {"r_checkonline", "rx_checkonline"}, + {"r_framesperfile", "rx_framesperfile"}, + {"r_discardpolicy", "rx_discardpolicy"}, + {"receiverversion", "rx_version"}, + {"receiver", "rx_status"}, + {"index", "findex"}, + {"exitreceiver", "rx_exit"}, + {"enablefwrite", "fwrite"}, + {"checkrecversion", "rx_checkversion"}, + {"masterfile", "fmaster"}, + {"outdir", "fpath"}, + {"fileformat", "fformat"}, + {"overwrite", "foverwrite"}}; + + void WrongNumberOfParameters(size_t expected); + + /* Commands */ + std::string ListCommands(int action); + std::string Period(int action); + std::string Exptime(int action); + std::string SubExptime(int action); + std::string RxFifoDepth(const int action); + std::string RxSilent(const int action); +}; + +} // namespace sls diff --git a/slsDetectorSoftware/src/CmdProxy.cpp b/slsDetectorSoftware/src/CmdProxy.cpp new file mode 100644 index 000000000..67a7f3221 --- /dev/null +++ b/slsDetectorSoftware/src/CmdProxy.cpp @@ -0,0 +1,213 @@ +#include "CmdProxy.h" +#include "Detector.h" +#include "Result.h" +#include "TimeHelper.h" +#include "ToString.h" +#include "logger.h" +#include "slsDetectorCommand.h" +#include "sls_detector_defs.h" +#include "sls_detector_exceptions.h" + +#include +#include + +#define TIME_COMMAND(GETFCN, SETFCN, HLPSTR) \ + std::ostringstream os; \ + os << cmd << ' '; \ + if (action == slsDetectorDefs::HELP_ACTION) \ + os << HLPSTR << '\n'; \ + else if (action == slsDetectorDefs::GET_ACTION) { \ + auto t = det->GETFCN({det_id}); \ + if (args.size() == 0) { \ + os << OutString(t) << '\n'; \ + } else if (args.size() == 1) { \ + os << OutString(t, args[0]) << '\n'; \ + } else { \ + WrongNumberOfParameters(2); \ + } \ + } else if (action == slsDetectorDefs::PUT_ACTION) { \ + if (args.size() == 1) { \ + std::string time_str(args[0]); \ + std::string unit = RemoveUnit(time_str); \ + auto t = StringTo(time_str, unit); \ + det->SETFCN(t, {det_id}); \ + } else if (args.size() == 2) { \ + auto t = StringTo(args[0], args[1]); \ + det->SETFCN(t, {det_id}); \ + } else { \ + WrongNumberOfParameters(2); \ + } \ + os << ToString(args) << '\n'; \ + } else { \ + throw sls::RuntimeError("Unknown action"); \ + } \ + return os.str(); + +#define INTEGER_COMMAND(GETFCN, SETFCN, HLPSTR) \ + std::ostringstream os; \ + os << cmd << ' '; \ + if (action == slsDetectorDefs::HELP_ACTION) \ + os << HLPSTR << '\n'; \ + else if (action == slsDetectorDefs::GET_ACTION) { \ + auto t = det->GETFCN({det_id}); \ + if (args.size() == 0) { \ + os << OutString(t) << '\n'; \ + } else { \ + WrongNumberOfParameters(2); \ + } \ + } else if (action == slsDetectorDefs::PUT_ACTION) { \ + if (args.size() == 1) { \ + auto val = std::stoi(args[0]); \ + det->SETFCN(val, {det_id}); \ + } else { \ + WrongNumberOfParameters(1); \ + } \ + os << ToString(args) << '\n'; \ + } else { \ + throw sls::RuntimeError("Unknown action"); \ + } \ + return os.str(); + +namespace sls { + +std::string CmdProxy::Call(const std::string &command, + const std::vector &arguments, + int detector_id, int action, std::ostream &os) { + cmd = command; + args = arguments; + det_id = detector_id; + + ReplaceIfDepreciated(cmd); + + auto it = functions.find(cmd); + if (it != functions.end()) { + os << ((*this).*(it->second))(action); + return {}; + } else { + return cmd; + } +} + +bool CmdProxy::ReplaceIfDepreciated(std::string &command) { + auto d_it = depreciated_functions.find(command); + if (d_it != depreciated_functions.end()) { + FILE_LOG(logWARNING) + << command + << " is depreciated and will be removed. Please migrate to: " + << d_it->second; + command = d_it->second; + return true; + } + return false; +} + +std::vector CmdProxy::GetAllCommands() { + auto commands = slsDetectorCommand(nullptr).getAllCommands(); + for (const auto &it : functions) + commands.emplace_back(it.first); + std::sort(begin(commands), end(commands)); + return commands; +} + +std::vector CmdProxy::GetProxyCommands() { + std::vector commands; + for (const auto &it : functions) + commands.emplace_back(it.first); + std::sort(begin(commands), end(commands)); + return commands; +} + +void CmdProxy::WrongNumberOfParameters(size_t expected) { + throw RuntimeError( + "Command " + cmd + " expected <=" + std::to_string(expected) + + " parameter/s but got " + std::to_string(args.size()) + "\n"); +} + +template std::string CmdProxy::OutString(const V &value) { + if (value.equal()) + return ToString(value.front()); + return ToString(value); +} +template +std::string CmdProxy::OutString(const V &value, const std::string &unit) { + if (value.equal()) + return ToString(value.front(), unit); + return ToString(value, unit); +} + +/************************************************ + * * + * COMMANDS * + * * + ************************************************/ + +std::string CmdProxy::Period(int action) { + TIME_COMMAND(getPeriod, setPeriod, + "[duration] [(optional unit) ns|us|ms|s]\n\tSet the period"); +} +std::string CmdProxy::Exptime(int action) { + TIME_COMMAND( + getExptime, setExptime, + "[duration] [(optional unit) ns|us|ms|s]\n\tSet the exposure time"); +} +std::string CmdProxy::SubExptime(int action) { + TIME_COMMAND(getSubExptime, setSubExptime, + "[duration] [(optional unit) ns|us|ms|s]\n\tSet the " + "exposure time of EIGER subframes"); +} + +std::string CmdProxy::RxFifoDepth(const int action) { + INTEGER_COMMAND( + getRxFifoDepth, setRxFifoDepth, + "[n_frames]\n\tSet the number of frames in the receiver fifo"); +} + +std::string CmdProxy::RxSilent(const int action){ + INTEGER_COMMAND( + getRxSilentMode, setRxSilentMode, + "[0, 1]\n\tSwitch on or off receiver text output"); +} + +std::string CmdProxy::ListCommands(int action) { + if (action == slsDetectorDefs::HELP_ACTION) + return "list\n\tlists all available commands, list deprecated - " + "list deprecated commands\n"; + + if (args.size() == 0) { + auto commands = slsDetectorCommand(nullptr).getAllCommands(); + for (const auto &it : functions) + commands.emplace_back(it.first); + std::sort(begin(commands), end(commands)); + + std::cout << "These " << commands.size() << " commands are available\n"; + for (auto &c : commands) + std::cout << c << '\n'; + return ""; + } else if (args.size() == 1) { + if (args[0] == "deprecated") { + std::cout << "The following " << depreciated_functions.size() + << " commands are deprecated\n"; + size_t field_width = 20; + for (const auto &it : depreciated_functions) { + std::cout << std::right << std::setw(field_width) << it.first + << " -> " << it.second << '\n'; + } + return ""; + } else if (args[0] == "migrated") { + std::cout << "The following " << functions.size() + << " commands have been migrated to the new API\n"; + for (const auto &it : functions) { + std::cout << it.first << '\n'; + } + return ""; + } else { + throw RuntimeError( + "Could not decode argument. Possible options: deprecated"); + } + } else { + WrongNumberOfParameters(1); + return ""; + } +} + +} // namespace sls \ No newline at end of file diff --git a/slsDetectorSoftware/src/multiSlsDetectorClient.cpp b/slsDetectorSoftware/src/multiSlsDetectorClient.cpp index 754dff440..9a1b7b455 100644 --- a/slsDetectorSoftware/src/multiSlsDetectorClient.cpp +++ b/slsDetectorSoftware/src/multiSlsDetectorClient.cpp @@ -90,10 +90,9 @@ void multiSlsDetectorClient::runCommand() { if (detPtr != nullptr) multi_id = detPtr->getMultiId(); sls::Detector d(multi_id); - sls::CmdProxy proxy(&d); - // sls::CmdProxy proxy(detPtr); + sls::CmdProxy proxy(&d); auto cmd = proxy.Call(parser.command(), parser.arguments(), - parser.detector_id(), action_); + parser.detector_id(), action_, os); if (cmd.empty()) { return; } else { diff --git a/slsDetectorSoftware/src/slsDetectorCommand.cpp b/slsDetectorSoftware/src/slsDetectorCommand.cpp index 68985ad85..3441a39dd 100755 --- a/slsDetectorSoftware/src/slsDetectorCommand.cpp +++ b/slsDetectorSoftware/src/slsDetectorCommand.cpp @@ -534,27 +534,6 @@ slsDetectorCommand::slsDetectorCommand(multiSlsDetector *det) { descrToFuncMap[i].m_pFuncPtr = &slsDetectorCommand::cmdTiming; ++i; - /*! \page timing - - exptime [i] sets/gets exposure time in s. \c Returns \c (double with 9 decimal digits) - */ - descrToFuncMap[i].m_pFuncName = "exptime"; - descrToFuncMap[i].m_pFuncPtr = &slsDetectorCommand::cmdTimer; - ++i; - - /*! \page timing - - subexptime [i] sets/gets sub exposure time in s. Used in EIGER only in 32 bit mode. \c Returns \c (double with 9 decimal digits) - */ - descrToFuncMap[i].m_pFuncName = "subexptime"; - descrToFuncMap[i].m_pFuncPtr = &slsDetectorCommand::cmdTimer; - ++i; - - /*! \page timing - - period [i] sets/gets frame period in s. \c Returns \c (double with 9 decimal digits) - */ - descrToFuncMap[i].m_pFuncName = "period"; - descrToFuncMap[i].m_pFuncPtr = &slsDetectorCommand::cmdTimer; - ++i; - /*! \page timing - subdeadtime [i] sets/gets sub frame dead time in s. Subperiod is set in the detector = subexptime + subdeadtime. This value is normally a constant in the config file. Used in EIGER only in 32 bit mode. \c Returns \c (double with 9 decimal digits) */ @@ -1805,19 +1784,6 @@ slsDetectorCommand::slsDetectorCommand(multiSlsDetector *det) { descrToFuncMap[i].m_pFuncPtr = &slsDetectorCommand::cmdReceiver; ++i; - /*! \page receiver - - rx_fifodepth [i] sets/gets receiver fifo (between Listener and Writer Threads) depth to i number of frames. Can improve listener packet loss (loss due to packet processing time in Listener threads), not if limited by writing. \c Returns \c (int) - */ - descrToFuncMap[i].m_pFuncName = "rx_fifodepth"; - descrToFuncMap[i].m_pFuncPtr = &slsDetectorCommand::cmdReceiver; - ++i; - - /*! \page receiver - - rx_silent [i] sets/gets receiver in silent mode, ie. it will not print anything during real time acquisition. 1 sets, 0 unsets. \c Returns \c (int) - */ - descrToFuncMap[i].m_pFuncName = "rx_silent"; - descrToFuncMap[i].m_pFuncPtr = &slsDetectorCommand::cmdReceiver; - ++i; /*! \page receiver - rx_framesperfile [i] sets/gets the frames per file in receiver to i. 0 means infinite or all frames in a single file. \c Returns \c (int) @@ -4918,30 +4884,6 @@ std::string slsDetectorCommand::cmdReceiver(int narg, const char * const args[], } - else if (cmd == "rx_fifodepth") { - if (action == PUT_ACTION) { - if (!sscanf(args[1], "%d", &ival)) - return std::string("Could not scan rx_fifodepth input ") + std::string(args[1]); - if (ival >= 0) - sprintf(answer, "%d", myDet->setReceiverFifoDepth(ival, detPos)); - } else - sprintf(answer, "%d", myDet->setReceiverFifoDepth(-1, detPos)); - return std::string(answer); - - } - - else if (cmd == "rx_silent") { - if (action == PUT_ACTION) { - if (!sscanf(args[1], "%d", &ival)) - return std::string("Could not scan rx_silent input ") + std::string(args[1]); - if (ival >= 0) - sprintf(answer, "%d", myDet->setReceiverSilentMode(ival, detPos)); - } else - sprintf(answer, "%d", myDet->setReceiverSilentMode(-1, detPos)); - return std::string(answer); - - } - else if (cmd == "rx_framesperfile") { if (action == PUT_ACTION) { if (sscanf(args[1], "%d", &ival)) { diff --git a/slsDetectorSoftware/tests/test-Result.cpp b/slsDetectorSoftware/tests/test-Result.cpp index 61a34efdc..493e8c428 100644 --- a/slsDetectorSoftware/tests/test-Result.cpp +++ b/slsDetectorSoftware/tests/test-Result.cpp @@ -155,4 +155,20 @@ TEST_CASE("Sorting a Result"){ REQUIRE(res[1] == 3); REQUIRE(res[2] == 4); REQUIRE(res[3] == 5); +} + +TEST_CASE("Printing Result"){ + Result res{"ein", "zwei", "drei"}; + std::ostringstream os; + os << res; + REQUIRE(os.str() == "[ein, zwei, drei]"); + +} + +TEST_CASE("Printing Result"){ + Result res{1, 2, 3}; + std::ostringstream os; + os << res; + REQUIRE(os.str() == "[1, 2, 3]"); + } \ No newline at end of file diff --git a/slsDetectorSoftware/tests/test-multiSlsDetectorClient.cpp b/slsDetectorSoftware/tests/test-multiSlsDetectorClient.cpp index 812d35c13..a4d744c91 100644 --- a/slsDetectorSoftware/tests/test-multiSlsDetectorClient.cpp +++ b/slsDetectorSoftware/tests/test-multiSlsDetectorClient.cpp @@ -384,12 +384,6 @@ TEST_CASE("rx_lastclient", "[.cmd]") { } -TEST_CASE("rx_checkonline", "[.cmd]") { - - std::ostringstream oss; - multiSlsDetectorClient("rx_checkonline", GET, nullptr, oss); - REQUIRE(oss.str() == "rx_checkonline All receiver online\n"); -} TEST_CASE("rx_checkversion", "[.cmd]") { @@ -402,34 +396,35 @@ TEST_CASE("exptime", "[.cmd]") { { std::ostringstream oss; multiSlsDetectorClient("exptime 0.05", PUT, nullptr, oss); - REQUIRE(oss.str() == "exptime 0.050000000\n"); + REQUIRE(oss.str() == "exptime 0.05\n"); } { std::ostringstream oss; multiSlsDetectorClient("exptime", GET, nullptr, oss); - REQUIRE(oss.str() == "exptime 0.050000000\n"); + REQUIRE(oss.str() == "exptime 50ms\n"); } { std::ostringstream oss; multiSlsDetectorClient("exptime 1", PUT, nullptr, oss); - REQUIRE(oss.str() == "exptime 1.000000000\n"); + REQUIRE(oss.str() == "exptime 1\n"); } } -// TEST_CASE("exptime2", "[.cmd]") { -// { -// std::ostringstream oss; -// multiSlsDetectorClient("exptime2 0.05", PUT, nullptr, oss); -// REQUIRE(oss.str() == "exptime2 0.05s\n"); -// } -// { -// std::ostringstream oss; -// multiSlsDetectorClient("exptime2", GET, nullptr, oss); -// REQUIRE(oss.str() == "exptime2 0.05s\n"); -// } -// { -// std::ostringstream oss; -// multiSlsDetectorClient("exptime2 1", PUT, nullptr, oss); -// REQUIRE(oss.str() == "exptime2 1s\n"); -// } -// } \ No newline at end of file + +TEST_CASE("period", "[.cmd]") { + { + std::ostringstream oss; + multiSlsDetectorClient("period 1.25s", PUT, nullptr, oss); + REQUIRE(oss.str() == "period 1.25s\n"); + } + { + std::ostringstream oss; + multiSlsDetectorClient("period", GET, nullptr, oss); + REQUIRE(oss.str() == "period 1.25s\n"); + } + { + std::ostringstream oss; + multiSlsDetectorClient("period 0", PUT, nullptr, oss); + REQUIRE(oss.str() == "period 0\n"); + } +} \ No newline at end of file diff --git a/slsSupportLib/include/CmdLineParser.h b/slsSupportLib/include/CmdLineParser.h index 3eed2fa8c..cbb41eaa1 100755 --- a/slsSupportLib/include/CmdLineParser.h +++ b/slsSupportLib/include/CmdLineParser.h @@ -21,6 +21,7 @@ class CmdLineParser { const std::string &executable() const { return executable_; } const std::vector &arguments() const { return arguments_; }; std::vector argv() const; + std::string cli_line() const; private: void DecodeIdAndPosition(const char *c); diff --git a/slsSupportLib/include/CmdProxy.h b/slsSupportLib/include/CmdProxy.h deleted file mode 100644 index 1b05a79c3..000000000 --- a/slsSupportLib/include/CmdProxy.h +++ /dev/null @@ -1,214 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include - -#include "Result.h" -#include "TimeHelper.h" -#include "ToString.h" -#include "container_utils.h" -#include "logger.h" -#include "slsDetectorCommand.h" -#include "sls_detector_defs.h" -#include "sls_detector_exceptions.h" -#include "string_utils.h" - -#define TIME_COMMAND(GETFCN, SETFCN, HLPSTR) \ - std::ostringstream os; \ - os << cmd << ' '; \ - if (action == slsDetectorDefs::HELP_ACTION) \ - os << HLPSTR << '\n'; \ - else if (action == slsDetectorDefs::GET_ACTION) { \ - auto t = det->GETFCN({det_id}); \ - if (args.size() == 0) { \ - os << OutString(t) << '\n'; \ - } else if (args.size() == 1) { \ - os << OutString(t, args[0]) << '\n'; \ - } else { \ - WrongNumberOfParameters(2); \ - } \ - } else if (action == slsDetectorDefs::PUT_ACTION) { \ - if (args.size() == 1) { \ - std::string time_str(args[0]); \ - std::string unit = RemoveUnit(time_str); \ - auto t = StringTo(time_str, unit); \ - det->SETFCN(t, {det_id}); \ - } else if (args.size() == 2) { \ - auto t = StringTo(args[0], args[1]); \ - det->SETFCN(t, {det_id}); \ - } else { \ - WrongNumberOfParameters(2); \ - } \ - os << ToString(args) << '\n'; \ - } else { \ - throw sls::RuntimeError("Unknown action"); \ - } \ - return os.str(); - -namespace sls { - -template class CmdProxy { - public: - explicit CmdProxy(T *detectorPtr) : det(detectorPtr) {} - - std::string Call(const std::string &command, - const std::vector &arguments, int detector_id, - int action = -1, std::ostream &os = std::cout) { - cmd = command; - args = arguments; - det_id = detector_id; - - ReplaceIfDepreciated(cmd); - - auto it = functions.find(cmd); - if (it != functions.end()) { - os << ((*this).*(it->second))(action); - return {}; - } else { - return cmd; - } - } - - bool ReplaceIfDepreciated(std::string &command) { - auto d_it = depreciated_functions.find(command); - if (d_it != depreciated_functions.end()) { - FILE_LOG(logWARNING) - << command - << " is depreciated and will be removed. Please migrate to: " - << d_it->second; - command = d_it->second; - return true; - } - return false; - } - - size_t GetFunctionMapSize() const noexcept { return functions.size(); }; - - std::vector GetAllCommands() { - auto commands = slsDetectorCommand(nullptr).getAllCommands(); - for (const auto &it : functions) - commands.emplace_back(it.first); - std::sort(begin(commands), end(commands)); - return commands; - } - std::vector GetProxyCommands() { - std::vector commands; - for (const auto &it : functions) - commands.emplace_back(it.first); - std::sort(begin(commands), end(commands)); - return commands; - } - - private: - T *det; - std::string cmd; - std::vector args; - int det_id{-1}; - - template std::string OutString(const V &value) { - if (value.equal()) - return ToString(value.front()); - return ToString(value); - } - template - std::string OutString(const V &value, const std::string &unit) { - if (value.equal()) - return ToString(value.front(), unit); - return ToString(value, unit); - } - - using FunctionMap = std::map; - using StringMap = std::map; - - // Initialize maps for translating name and function - FunctionMap functions{{"list", &CmdProxy::ListCommands}, - {"exptime2", &CmdProxy::Exptime}, - {"period2", &CmdProxy::Period}, - {"subexptime2", &CmdProxy::SubExptime}}; - - StringMap depreciated_functions{{"r_readfreq", "rx_readfreq"}, - {"r_padding", "rx_padding"}, - {"r_silent", "rx_silent"}, - {"r_lastclient", "rx_lastclient"}, - {"r_lock", "rx_lock"}, - {"r_online", "rx_online"}, - {"r_checkonline", "rx_checkonline"}, - {"r_framesperfile", "rx_framesperfile"}, - {"r_discardpolicy", "rx_discardpolicy"}, - {"receiverversion", "rx_version"}, - {"receiver", "rx_status"}, - {"index", "findex"}, - {"exitreceiver", "rx_exit"}, - {"enablefwrite", "fwrite"}, - {"checkrecversion", "rx_checkversion"}, - {"masterfile", "fmaster"}, - {"outdir", "fpath"}, - {"fileformat", "fformat"}, - {"overwrite", "foverwrite"}}; - - void WrongNumberOfParameters(size_t expected) { - throw RuntimeError( - "Command " + cmd + " expected <=" + std::to_string(expected) + - " parameter/s but got " + std::to_string(args.size()) + "\n"); - } - - // Mapped functions - std::string ListCommands(int action) { - if (action == slsDetectorDefs::HELP_ACTION) - return "list\n\tlists all available commands, list deprecated - " - "list deprecated commands\n"; - - if (args.size() == 0) { - auto commands = slsDetectorCommand(nullptr).getAllCommands(); - for (const auto &it : functions) - commands.emplace_back(it.first); - std::sort(begin(commands), end(commands)); - - std::cout << "These " << commands.size() - << " commands are available\n"; - for (auto &c : commands) - std::cout << c << '\n'; - return ""; - } else if (args.size() == 1) { - if (args[0] == "deprecated") { - std::cout << "The following " << depreciated_functions.size() - << " commands are deprecated\n"; - size_t field_width = 20; - for (const auto &it : depreciated_functions) { - std::cout << std::right << std::setw(field_width) - << it.first << " -> " << it.second << '\n'; - } - return ""; - } else { - throw RuntimeError( - "Could not decode argument. Possible options: deprecated"); - } - } else { - WrongNumberOfParameters(1); - return ""; - } - } - - std::string Period(int action) { - TIME_COMMAND( - getPeriod, setPeriod, - "[duration] [(optional unit) ns|us|ms|s]\n\tSet the period"); - } - std::string Exptime(int action) { - TIME_COMMAND( - getExptime, setExptime, - "[duration] [(optional unit) ns|us|ms|s]\n\tSet the exposure time"); - } - std::string SubExptime(int action) { - TIME_COMMAND(getSubExptime, setSubExptime, - "[duration] [(optional unit) ns|us|ms|s]\n\tSet the " - "exposure time of EIGER subframes"); - } -}; - -} // namespace sls diff --git a/slsSupportLib/include/ToString.h b/slsSupportLib/include/ToString.h index 750e6585a..b2f63138e 100644 --- a/slsSupportLib/include/ToString.h +++ b/slsSupportLib/include/ToString.h @@ -19,18 +19,6 @@ namespace sls { -inline std::string ToString(const std::vector &vec, - const char delimiter = ' ') { - std::ostringstream os; - if(!vec.empty()){ - auto it = vec.begin(); - os << *it++; - while(it != vec.end()) - os << delimiter << *it++; - } - return os.str(); -} - /** Convert std::chrono::duration with specified output unit */ template typename std::enable_if::value, std::string>::type @@ -84,9 +72,15 @@ ToString(const T &value) { return std::to_string(value); } -/** For a container loop over all elements and call ToString */ +/** + * For a container loop over all elements and call ToString on the element + * Container is excluded + */ template -typename std::enable_if::value, std::string>::type +typename std::enable_if< + is_container::value && + !std::is_same::value, + std::string>::type ToString(const T &container) { std::ostringstream os; os << '['; @@ -100,6 +94,29 @@ ToString(const T &container) { return os.str(); } +/** + * Special case when container holds a string, don't call ToString again but + * print directly to stream + */ + +template +typename std::enable_if< + is_container::value && + std::is_same::value, + std::string>::type +ToString(const T &vec) { + std::ostringstream os; + os << '['; + if (!vec.empty()) { + auto it = vec.begin(); + os << *it++; + while (it != vec.end()) + os << ", " << *it++; + } + os << ']'; + return os.str(); +} + /** Container and specified unit, call ToString(value, unit) */ template typename std::enable_if::value, std::string>::type diff --git a/slsSupportLib/src/CmdLineParser.cpp b/slsSupportLib/src/CmdLineParser.cpp index 0fd939bf7..2ddcdb253 100755 --- a/slsSupportLib/src/CmdLineParser.cpp +++ b/slsSupportLib/src/CmdLineParser.cpp @@ -100,4 +100,13 @@ std::vector CmdLineParser::argv() const { return vec; } + +std::string CmdLineParser::cli_line() const{ + std::ostringstream os; + os << command_; + for (const auto & arg : arguments_) + os << " " << arg; + return os.str(); +} + } // namespace sls \ No newline at end of file