11#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
15#ifdef USE_API_USER_DEFINED_ACTIONS
28#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
52#ifdef USE_API_USER_DEFINED_ACTION_METADATA
53static constexpr size_t USER_ACTION_HEADER_STRINGS = 2;
54static constexpr size_t USER_ACTION_ARG_STRINGS = 3;
56static constexpr size_t USER_ACTION_HEADER_STRINGS = 1;
57static constexpr size_t USER_ACTION_ARG_STRINGS = 1;
88 if (req.
key != this->key_)
90 if (req.
args.size() !=
sizeof...(Ts))
92#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
95 this->
execute_(req.
args, 0,
false, std::make_index_sequence<
sizeof...(Ts)>{});
100#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
102 if (req.
key != this->key_)
104 if (req.
args.size() !=
sizeof...(Ts))
113 template<
typename ArgsContainer,
size_t... S>
114 void execute_(
const ArgsContainer &args,
uint32_t call_id,
bool return_response, std::index_sequence<S...> ) {
134 msg.
args.init(
sizeof...(Ts));
135 for (
size_t i = 0; i <
sizeof...(Ts); i++) {
136 auto &arg = msg.
args.emplace_back();
137 arg.type = arg_types[i];
144 if (req.
key != this->key_)
146 if (req.
args.size() !=
sizeof...(Ts))
148#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
151 this->
execute_(req.
args, 0,
false, std::make_index_sequence<
sizeof...(Ts)>{});
156#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
159 if (req.
key != this->key_)
161 if (req.
args.size() !=
sizeof...(Ts))
170 template<
typename ArgsContainer,
size_t... S>
171 void execute_(
const ArgsContainer &args,
uint32_t call_id,
bool return_response, std::index_sequence<S...> ) {
185template<
typename... Ts>
190 :
UserServiceBase<Ts...>(strings, key, enums::SUPPORTS_RESPONSE_NONE) {}
197template<
typename... Ts>
199 public Trigger<uint32_t, bool, Ts...> {
202 :
UserServiceBase<Ts...>(strings, key, enums::SUPPORTS_RESPONSE_OPTIONAL) {}
206 this->trigger(call_id, return_response,
x...);
211template<
typename... Ts>
213 public Trigger<uint32_t, Ts...> {
216 :
UserServiceBase<Ts...>(strings, key, enums::SUPPORTS_RESPONSE_ONLY) {}
223template<
typename... Ts>
225 public Trigger<uint32_t, Ts...> {
228 :
UserServiceBase<Ts...>(strings, key, enums::SUPPORTS_RESPONSE_STATUS) {}
237#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
252#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
253 void set_data(std::function<
void(Ts..., JsonObject)> &&func) {
259 void play(
const Ts &...
x)
override {
261 auto args = std::make_tuple(
x...);
262 uint32_t call_id = std::get<0>(args);
267#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
272 if constexpr (
sizeof...(Ts) >= 2) {
273 if constexpr (std::is_same_v<std::tuple_element_t<1, std::tuple<Ts...>>,
bool>) {
275 bool return_response = std::get<1>(args);
276 if (!return_response) {
289 reinterpret_cast<const uint8_t *
>(json_buf.data()), json_buf.
size());
300#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
313 void play(
const Ts &...
x)
override {
315 auto args = std::make_tuple(
x...);
316 uint32_t call_id = std::get<0>(args);
StringRef is a reference to a string owned by something else.
constexpr size_type size() const
Function-pointer-only templatable storage (4 bytes on 32-bit).
Primary TemplatableValue: stores either a constant value or a function pointer.
void play(const Ts &...x) override
TemplatableFn< 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 send_action_response(uint32_t action_call_id, bool success, StringRef error_message)
void unregister_active_action_call(uint32_t action_call_id)
APIUnregisterServiceCallAction(APIServer *parent)
void play(const Ts &...x) override
FixedVector< ExecuteServiceArgument > args
enums::SupportsResponseType supports_response
FixedVector< ListEntitiesServicesArgument > args
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... >)
virtual void execute(uint32_t call_id, bool return_response, Ts... x)=0
bool execute_service(const ExecuteServiceRequest &req) override
ListEntitiesServicesResponse encode_list_service_response(std::span< char > scratch) override
virtual ListEntitiesServicesResponse encode_list_service_response(std::span< char > scratch)=0
Build the list-entities message.
virtual bool execute_service(const ExecuteServiceRequest &req, uint32_t action_call_id)=0
virtual bool execute_service(const ExecuteServiceRequest &req)=0
ListEntitiesServicesResponse encode_list_service_response(std::span< char >) override
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
std::array< std::string, sizeof...(Ts)> arg_names_
virtual void execute(uint32_t call_id, bool return_response, Ts... x)=0
const char *const * strings_
StringRef str_(size_t idx, std::span< char > &scratch) const
Reference table entry idx; nullptr gives an empty StringRef.
ListEntitiesServicesResponse encode_list_service_response_(std::span< const enums::ServiceArgType > arg_types, std::span< char > scratch) const
UserServiceStatic(const char *const *strings, uint32_t key, enums::SupportsResponseType supports_response=enums::SUPPORTS_RESPONSE_NONE)
enums::SupportsResponseType supports_response_
UserServiceTrigger(const char *const *strings, uint32_t key)
void execute(uint32_t, bool, Ts... x) override
void execute(uint32_t call_id, bool return_response, Ts... x) override
UserServiceTrigger(const char *const *strings, uint32_t key)
void execute(uint32_t call_id, bool, Ts... x) override
UserServiceTrigger(const char *const *strings, uint32_t key)
void execute(uint32_t call_id, bool, Ts... x) override
UserServiceTrigger(const char *const *strings, uint32_t key)
Builder class for creating JSON documents without lambdas.
SerializationBuffer serialize()
Serialize the JSON document to a SerializationBuffer (stack-first allocation) Uses 512-byte stack buf...
std::array< char, API_USER_ACTION_STRINGS_SCRATCH_SIZE > UserActionScratch
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.