10#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
14#ifdef USE_API_USER_DEFINED_ACTIONS
25#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
41 UserServiceBase(
const char *name,
const std::array<
const char *,
sizeof...(Ts)> &arg_names,
53 msg.
args.init(
sizeof...(Ts));
54 for (
size_t i = 0; i <
sizeof...(Ts); i++) {
55 auto &arg = msg.
args.emplace_back();
56 arg.type = arg_types[i];
63 if (req.
key != this->key_)
65 if (req.
args.size() !=
sizeof...(Ts))
67#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
70 this->
execute_(req.
args, 0,
false, std::make_index_sequence<
sizeof...(Ts)>{});
75#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
77 if (req.
key != this->key_)
79 if (req.
args.size() !=
sizeof...(Ts))
87 virtual void execute(uint32_t call_id,
bool return_response, Ts...
x) = 0;
88 template<
typename ArgsContainer,
size_t... S>
89 void execute_(
const ArgsContainer &args, uint32_t call_id,
bool return_response, std::index_sequence<S...> ) {
115 msg.
args.init(
sizeof...(Ts));
116 for (
size_t i = 0; i <
sizeof...(Ts); i++) {
117 auto &arg = msg.
args.emplace_back();
118 arg.type = arg_types[i];
125 if (req.
key != this->key_)
127 if (req.
args.size() !=
sizeof...(Ts))
129#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
132 this->
execute_(req.
args, 0,
false, std::make_index_sequence<
sizeof...(Ts)>{});
137#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
140 if (req.
key != this->key_)
142 if (req.
args.size() !=
sizeof...(Ts))
150 virtual void execute(uint32_t call_id,
bool return_response, Ts...
x) = 0;
151 template<
typename ArgsContainer,
size_t... S>
152 void execute_(
const ArgsContainer &args, uint32_t call_id,
bool return_response, std::index_sequence<S...> ) {
166template<
typename... Ts>
170 :
UserServiceBase<Ts...>(name, arg_names, enums::SUPPORTS_RESPONSE_NONE) {}
173 void execute(uint32_t ,
bool , Ts...
x)
override { this->trigger(
x...); }
177template<
typename... Ts>
179 public Trigger<uint32_t, bool, Ts...> {
182 :
UserServiceBase<Ts...>(name, arg_names, enums::SUPPORTS_RESPONSE_OPTIONAL) {}
185 void execute(uint32_t call_id,
bool return_response, Ts...
x)
override {
186 this->trigger(call_id, return_response,
x...);
191template<
typename... Ts>
193 public Trigger<uint32_t, Ts...> {
196 :
UserServiceBase<Ts...>(name, arg_names, enums::SUPPORTS_RESPONSE_ONLY) {}
199 void execute(uint32_t call_id,
bool , Ts...
x)
override { this->trigger(call_id,
x...); }
203template<
typename... Ts>
205 public Trigger<uint32_t, Ts...> {
208 :
UserServiceBase<Ts...>(name, arg_names, enums::SUPPORTS_RESPONSE_STATUS) {}
211 void execute(uint32_t call_id,
bool , Ts...
x)
override { this->trigger(call_id,
x...); }
217#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
232#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
233 void set_data(std::function<
void(Ts..., JsonObject)> func) {
239 void play(
const Ts &...
x)
override {
241 auto args = std::make_tuple(
x...);
242 uint32_t call_id = std::get<0>(args);
247#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
252 if constexpr (
sizeof...(Ts) >= 2) {
253 if constexpr (std::is_same_v<std::tuple_element_t<1, std::tuple<Ts...>>,
bool>) {
255 bool return_response = std::get<1>(args);
256 if (!return_response) {
267 std::string json_str = builder.
serialize();
269 reinterpret_cast<const uint8_t *
>(json_str.data()), json_str.size());
280#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
293 void play(
const Ts &...
x)
override {
295 auto args = std::make_tuple(
x...);
296 uint32_t call_id = std::get<0>(args);
StringRef is a reference to a string owned by something else.
void play(const Ts &...x) override
TemplatableValue< bool, Ts... > success_
void set_success(V success)
void set_data(std::function< void(Ts..., JsonObject)> func)
void set_error_message(V error)
void set_is_optional_mode(bool is_optional)
APIRespondAction(APIServer *parent)
std::function< void(Ts..., JsonObject)> json_builder_
TemplatableValue< std::string, Ts... > error_message_
void unregister_active_action_call(uint32_t action_call_id)
void send_action_response(uint32_t action_call_id, bool success, const std::string &error_message)
APIUnregisterServiceCallAction(APIServer *parent)
void play(const Ts &...x) override
FixedVector< ExecuteServiceArgument > args
enums::SupportsResponseType supports_response
FixedVector< ListEntitiesServicesArgument > args
void set_name(const StringRef &ref)
bool execute_service(const ExecuteServiceRequest &req, uint32_t action_call_id) override
void execute_(const ArgsContainer &args, uint32_t call_id, bool return_response, std::index_sequence< S... >)
enums::SupportsResponseType supports_response_
virtual void execute(uint32_t call_id, bool return_response, Ts... x)=0
std::array< const char *, sizeof...(Ts)> arg_names_
ListEntitiesServicesResponse encode_list_service_response() override
bool execute_service(const ExecuteServiceRequest &req) override
UserServiceBase(const char *name, const std::array< const char *, sizeof...(Ts)> &arg_names, enums::SupportsResponseType supports_response=enums::SUPPORTS_RESPONSE_NONE)
virtual ListEntitiesServicesResponse encode_list_service_response()=0
virtual bool execute_service(const ExecuteServiceRequest &req, uint32_t action_call_id)=0
virtual bool execute_service(const ExecuteServiceRequest &req)=0
UserServiceDynamic(std::string name, const std::array< std::string, sizeof...(Ts)> &arg_names)
bool execute_service(const ExecuteServiceRequest &req) override
void execute_(const ArgsContainer &args, uint32_t call_id, bool return_response, std::index_sequence< S... >)
bool execute_service(const ExecuteServiceRequest &req, uint32_t action_call_id) override
ListEntitiesServicesResponse encode_list_service_response() override
std::array< std::string, sizeof...(Ts)> arg_names_
virtual void execute(uint32_t call_id, bool return_response, Ts... x)=0
UserServiceTrigger(const char *name, const std::array< const char *, sizeof...(Ts)> &arg_names)
void execute(uint32_t, bool, Ts... x) override
void execute(uint32_t call_id, bool return_response, Ts... x) override
UserServiceTrigger(const char *name, const std::array< const char *, sizeof...(Ts)> &arg_names)
void execute(uint32_t call_id, bool, Ts... x) override
UserServiceTrigger(const char *name, const std::array< const char *, sizeof...(Ts)> &arg_names)
UserServiceTrigger(const char *name, const std::array< const char *, sizeof...(Ts)> &arg_names)
void execute(uint32_t call_id, bool, Ts... x) override
Builder class for creating JSON documents without lambdas.
enums::ServiceArgType to_service_arg_type()
T get_execute_arg_value(const ExecuteServiceArgument &arg)
uint32_t fnv1_hash(const char *str)
Calculate a FNV-1 hash of str.