mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-20 16:48:01 +02:00
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
This commit is contained in:

committed by
Dhanya Thattil

parent
975a5a4cab
commit
5c06549982
213
slsDetectorSoftware/src/CmdProxy.cpp
Normal file
213
slsDetectorSoftware/src/CmdProxy.cpp
Normal file
@ -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 <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
#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::ns>(time_str, unit); \
|
||||
det->SETFCN(t, {det_id}); \
|
||||
} else if (args.size() == 2) { \
|
||||
auto t = StringTo<time::ns>(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<std::string> &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<std::string> 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<std::string> CmdProxy::GetProxyCommands() {
|
||||
std::vector<std::string> 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 <typename V> std::string CmdProxy::OutString(const V &value) {
|
||||
if (value.equal())
|
||||
return ToString(value.front());
|
||||
return ToString(value);
|
||||
}
|
||||
template <typename V>
|
||||
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
|
@ -90,10 +90,9 @@ void multiSlsDetectorClient::runCommand() {
|
||||
if (detPtr != nullptr)
|
||||
multi_id = detPtr->getMultiId();
|
||||
sls::Detector d(multi_id);
|
||||
sls::CmdProxy<sls::Detector> proxy(&d);
|
||||
// sls::CmdProxy<multiSlsDetector> 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 {
|
||||
|
@ -534,27 +534,6 @@ slsDetectorCommand::slsDetectorCommand(multiSlsDetector *det) {
|
||||
descrToFuncMap[i].m_pFuncPtr = &slsDetectorCommand::cmdTiming;
|
||||
++i;
|
||||
|
||||
/*! \page timing
|
||||
- <b>exptime [i]</b> 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
|
||||
- <b>subexptime [i]</b> 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
|
||||
- <b>period [i]</b> 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
|
||||
- <b>subdeadtime [i]</b> 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
|
||||
- <b>rx_fifodepth [i]</b> 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
|
||||
- <b>rx_silent [i]</b> 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
|
||||
- <b>rx_framesperfile [i]</b> 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)) {
|
||||
|
Reference in New Issue
Block a user