11#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
15#ifdef USE_API_USER_DEFINED_ACTIONS
27#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
46#ifdef USE_API_USER_DEFINED_ACTION_METADATA
47static constexpr size_t USER_ACTION_HEADER_STRINGS = 2;
48static constexpr size_t USER_ACTION_ARG_BASE_STRINGS = 3;
50static constexpr size_t USER_ACTION_HEADER_STRINGS = 1;
51static constexpr size_t USER_ACTION_ARG_BASE_STRINGS = 1;
53#ifdef USE_API_USER_DEFINED_ACTION_OPTIONAL_ARGS
54static constexpr size_t USER_ACTION_ARG_STRINGS = USER_ACTION_ARG_BASE_STRINGS + 1;
56static constexpr size_t USER_ACTION_ARG_STRINGS = USER_ACTION_ARG_BASE_STRINGS;
84 if (req.
key != this->key_)
86 if (req.
args.size() !=
sizeof...(Ts))
88#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
91 this->
execute_(req.
args, 0,
false, std::make_index_sequence<
sizeof...(Ts)>{});
96#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
98 if (req.
key != this->key_)
100 if (req.
args.size() !=
sizeof...(Ts))
109 template<
typename ArgsContainer,
size_t... S>
110 void execute_(
const ArgsContainer &args,
uint32_t call_id,
bool return_response, std::index_sequence<S...> ) {
130 msg.
args.init(
sizeof...(Ts));
131 for (
size_t i = 0; i <
sizeof...(Ts); i++) {
132 auto &arg = msg.
args.emplace_back();
133 arg.type = arg_types[i];
140 if (req.
key != this->key_)
142 if (req.
args.size() !=
sizeof...(Ts))
144#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
147 this->
execute_(req.
args, 0,
false, std::make_index_sequence<
sizeof...(Ts)>{});
152#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
155 if (req.
key != this->key_)
157 if (req.
args.size() !=
sizeof...(Ts))
166 template<
typename ArgsContainer,
size_t... S>
167 void execute_(
const ArgsContainer &args,
uint32_t call_id,
bool return_response, std::index_sequence<S...> ) {
181template<
typename... Ts>
193template<
typename... Ts>
195 public Trigger<uint32_t, bool, Ts...> {
202 this->trigger(call_id, return_response,
x...);
207template<
typename... Ts>
209 public Trigger<uint32_t, Ts...> {
219template<
typename... Ts>
221 public Trigger<uint32_t, Ts...> {
233#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES
248#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
249 void set_data(std::function<
void(Ts..., JsonObject)> &&func) {
255 void play(
const Ts &...
x)
override {
257 auto args = std::make_tuple(
x...);
258 uint32_t call_id = std::get<0>(args);
263#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
268 if constexpr (
sizeof...(Ts) >= 2) {
269 if constexpr (std::is_same_v<std::tuple_element_t<1, std::tuple<Ts...>>,
bool>) {
271 bool return_response = std::get<1>(args);
272 if (!return_response) {
285 reinterpret_cast<const uint8_t *
>(json_buf.data()), json_buf.
size());
296#ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON
309 void play(
const Ts &...
x)
override {
311 auto args = std::make_tuple(
x...);
312 uint32_t call_id = std::get<0>(args);
A string that may live in flash (on ESP8266): pointer and length, with no byte-reading operations.
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
ListEntitiesServicesResponse encode_list_service_response() override
bool execute_service(const ExecuteServiceRequest &req) override
virtual ListEntitiesServicesResponse encode_list_service_response()=0
Build the list-entities message; its strings may point into flash (PROGMEM).
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
ListEntitiesServicesResponse encode_list_service_response_(std::span< const enums::ServiceArgType > arg_types) const
const char *const * strings_
ProgmemStringRef str_(size_t idx) const
Table entry idx as it sits in flash; nullptr gives an empty string.
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...
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.