SensESP 2.7.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
signalk_put_request.cpp
Go to the documentation of this file.
2
4#include "sensesp_app.h"
5
6namespace sensesp {
7
8extern ReactESP app;
9
10std::map<String, SKRequest::PendingRequest*> SKRequest::request_map;
11
14 std::function<void(DynamicJsonDocument&)> callback, uint32_t timeout) {
15 // Create a new PendingRequest object to track this request...
17
18 // Generate a uuid for the request...
19 pending_request->request_id = generate_uuid4();
20
21 // Save the callback for future processing...
22 pending_request->callback = callback;
23
24 // After 10 seconds, if we haven't already handled a response,
25 // assume its not coming.
26 pending_request->timeout_cleanup =
27 ReactESP::app->onDelay(timeout, [pending_request]() {
28 // Mark the delay reaction null as it will be cleaned up by the ReactESP
29 // framework if this executes...
30 debugW("No response from server for request Id %s",
31 pending_request->request_id.c_str());
32 pending_request->timeout_cleanup = nullptr;
34 });
35
37
38 // Now, send the actual request to the server...
39 request["requestId"] = pending_request->request_id;
40
43 debugD("Sending websocket request to server: %s", request_txt.c_str());
44
45 SensESPApp::get()->get_ws_client()->sendTXT(request_txt);
46
47 return pending_request->request_id;
48}
49
51 auto it = request_map.find(request_id);
52 if (it != request_map.end()) {
53 return (it->second);
54 } else {
55 return nullptr;
56 }
57}
58
60 String request_id = response["requestId"];
62 if (pending_request != nullptr) {
63 pending_request->callback(response);
64
65 // Now, are we done?
66 String state = response["state"];
67 if (!state.equalsIgnoreCase("PENDING")) {
68 remove_request(request_id);
69 }
70 } else {
71 debugW("Received request response for an untracked request: %s",
72 request_id.c_str());
73 }
74}
75
78 if (pending_request != nullptr) {
79 // First, stop any pending timeout handlers...
80 if (pending_request->timeout_cleanup != nullptr) {
81 // The timeout code was not called, so just
82 // remove it from the ReactESP execution queue...
83 pending_request->timeout_cleanup->remove();
84 }
85
86 // Now, remove the request from the map...
87 request_map.erase(request_id);
88
89 // Finally, discard the request tracker...
90 delete pending_request;
91 }
92}
93
95 uint32_t timeout)
96 : Configurable(config_path), sk_path{sk_path}, timeout{timeout} {
98}
99
103 JsonObject put_data = root.createNestedObject("put");
104 put_data["path"] = sk_path;
107 doc,
108 [this](DynamicJsonDocument& response) { this->on_response(response); },
109 timeout);
110}
111
113 return (get_request(this->pending_request_id_) != nullptr);
114}
115
117 String request_id = response["requestId"];
118 String state = response["state"];
119 debugD("Response %s received for PUT request: %s", state.c_str(),
120 request_id.c_str());
121}
122
126
127static const char SCHEMA[] PROGMEM = R"###({
128 "type": "object",
129 "properties": {
130 "sk_path": { "title": "Signal K Path", "type": "string" }
131 }
132 })###";
133
135
137 String expected[] = {"sk_path"};
138 for (auto str : expected) {
139 if (!config.containsKey(str)) {
140 return false;
141 }
142 }
143 this->sk_path = config["sk_path"].as<String>();
144 return true;
145}
146
147} // namespace sensesp
An object that is capable of having configuration data that can be set remotely using a RESTful API,...
virtual void load_configuration()
Construct a new transform based on a single function.
virtual void set_put_value(JsonObject &put_data)=0
virtual bool set_configuration(const JsonObject &config) override
SKPutRequestBase(String sk_path, String config_path="", uint32_t timeout=5000)
virtual String get_config_schema() override
virtual void get_configuration(JsonObject &doc) override
virtual void on_response(DynamicJsonDocument &response)
static void handle_response(DynamicJsonDocument &response)
static std::map< String, PendingRequest * > request_map
static String send_request(DynamicJsonDocument &request, std::function< void(DynamicJsonDocument &)> callback, uint32_t timeout=5000)
static PendingRequest * get_request(String request_id)
static void remove_request(String request_id)
static SensESPApp * get()
Get the singleton instance of the SensESPApp.
const uint8_t PAGE_css_bootstrap[] PROGMEM
#define debugD(fmt,...)
Definition local_debug.h:47
#define debugW(fmt,...)
Definition local_debug.h:49
String generate_uuid4()
Generate a random UUIDv4 string.
Definition uuid.cpp:5
ReactESP app