This commit is contained in:
Timur A. Fatkhullin
2025-01-26 23:59:22 +03:00
parent 6e4e776ecf
commit e8dfb0de5a
3 changed files with 88 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
#pragma once
#include <functional>
#include <optional>
#include <string_view>
namespace BM700
@@ -42,33 +43,45 @@ static constexpr std::string_view CONTROL_PROTO_STR_TAG_MAXALT = "tagMAXALT";
// moving
static constexpr std::string_view CONTROL_PROTO_STR_SLEW_AZALT = "slewAZALT";
static constexpr std::string_view CONTROL_PROTO_STR_SLEW_RADEC = "slewRADEC";
static constexpr std::string_view CONTROL_PROTO_STR_SLEW_XVEL = "slewXVEL";
static constexpr std::string_view CONTROL_PROTO_STR_SLEW_YVEL = "slewYVEL";
static constexpr std::string_view CONTROL_PROTO_STR_SLEW_XVEL = "slewXVEL"; // velocity along Az/RA
static constexpr std::string_view CONTROL_PROTO_STR_SLEW_YVEL = "slewYVEL"; // velocity along ALT/DEC
static constexpr std::string_view CONTROL_PROTO_STR_TRACK_XVEL = "trackXVEL";
static constexpr std::string_view CONTROL_PROTO_STR_TRACK_YVEL = "trackYVEL";
static constexpr std::string_view CONTROL_PROTO_STR_STOP = "stop";
static constexpr std::array CONTROL_PROTO_VALID_COMMAND = {CONTROL_PROTO_STR_SLEW_AZALT, CONTROL_PROTO_STR_SLEW_RADEC};
class ControlProtoParser
{
public:
typedef std::function<void(std::string_view, std::vector<std::string_view>)> user_func_t;
// typedef std::pair<std::string_view, std::vector<std::string_view>> parse_result_t;
struct parse_result_t {
std::string_view cmd;
std::vector<std::string_view> args;
};
ControlProtoParser() {}
ControlProtoParser();
virtual ~ControlProtoParser() = default;
template <std::convertible_to<user_func_t> FT>
bool addUserFunc(std::string_view command, FT&& func)
{
auto res = _userFunc.try_emplace(command, std::forward<FT>(func));
return res.second;
// _userFunc[command] = std::forward<FT>(func);
if (auto res = _userFunc.find(command); res != _userFunc.end()) {
*res = std::forward<FT>(func);
return true;
} else {
return false;
}
}
bool parse(std::string_view command);
std::optional<parse_result_t> parse(std::string_view command);
bool exec(std::string_view command);
protected:
static std::unordered_map<std::string_view, user_func_t> _userFunc;
static void generateCommMap();
inline static std::unordered_map<std::string_view, user_func_t> _userFunc;
};