11 std::function<
void(JsonDocument&)> callback,
20 pending_request->callback = callback;
24 pending_request->timeout_cleanup =
25 event_loop()->onDelay(timeout, [pending_request]() {
28 ESP_LOGW(__FILENAME__,
"No response from server for request Id %s",
29 pending_request->request_id.c_str());
30 pending_request->timeout_cleanup =
nullptr;
34 request_map_[pending_request->request_id] = pending_request;
37 request[
"requestId"] = pending_request->request_id;
40 serializeJson(request, request_txt);
41 ESP_LOGD(__FILENAME__,
"Sending websocket request to server: %s",
46 return pending_request->request_id;
52 return (iterator->second);
58 String request_id = response[
"requestId"];
60 if (pending_request !=
nullptr) {
64 String state = response[
"state"];
65 if (!state.equalsIgnoreCase(
"PENDING")) {
69 ESP_LOGW(__FILENAME__,
70 "Received request response for an untracked request: %s",
77 if (pending_request !=
nullptr) {
90 delete pending_request;
95 const String& config_path, uint32_t timeout)
102 JsonObject root = doc.to<JsonObject>();
103 JsonObject put_data = root[
"put"].to<JsonObject>();
107 doc, [
this](JsonDocument& response) { this->
on_response(response); },
116 String request_id = response[
"requestId"];
117 String state = response[
"state"];
118 ESP_LOGD(__FILENAME__,
"Response %s received for PUT request: %s",
119 state.c_str(), request_id.c_str());
128 const String expected[] = {
"sk_path"};
129 for (
auto str : expected) {
130 if (!config[str].is<JsonVariant>()) {
134 this->
sk_path = config[
"sk_path"].as<String>();
virtual bool load() override
Load and populate the object from a persistent storage.
virtual void set_put_value(JsonObject &put_data)=0
SKPutRequestBase(const String &sk_path, const String &config_path="", uint32_t timeout=5000)
String pending_request_id_
virtual bool from_json(const JsonObject &config) override
virtual bool to_json(JsonObject &root) override
virtual void on_response(JsonDocument &response)
reactesp::DelayEvent * timeout_cleanup
std::function< void(JsonDocument &)> callback
static void handle_response(JsonDocument &response)
static String send_request(JsonDocument &request, std::function< void(JsonDocument &)> callback, uint32_t timeout=5000)
static PendingRequest * get_request(String request_id)
static std::map< String, PendingRequest * > request_map_
static void remove_request(String request_id)
static std::shared_ptr< SensESPApp > get()
Get the singleton instance of the SensESPApp.
std::shared_ptr< reactesp::EventLoop > event_loop()
String generate_uuid4()
Generate a random UUIDv4 string.