SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
signalk_put_request.h
Go to the documentation of this file.
1#ifndef SENSESP_SIGNALK_SIGNALK_PUT_REQUEST_H_
2#define SENSESP_SIGNALK_SIGNALK_PUT_REQUEST_H_
3
4#include <ArduinoJson.h>
5#include <functional>
6#include <map>
7
10
11namespace sensesp {
12
19class SKRequest {
20 public:
33 static String send_request(JsonDocument& request,
34 std::function<void(JsonDocument&)> callback,
35 uint32_t timeout = 5000);
36
41 static void handle_response(JsonDocument& response);
42
43 protected:
47 public:
48 String request_id;
49 std::function<void(JsonDocument&)> callback;
50 reactesp::DelayEvent* timeout_cleanup;
51 };
52
55 static std::map<String, PendingRequest*> request_map_;
56
59 static void remove_request(String request_id);
60
63 static PendingRequest* get_request(String request_id);
64};
65
75 public FileSystemSaveable {
76 public:
84 SKPutRequestBase(const String& sk_path, const String& config_path = "",
85 uint32_t timeout = 5000);
86
87 // For reading and writing the configuration
88 virtual bool to_json(JsonObject& root) override;
89 virtual bool from_json(const JsonObject& config) override;
90
94 String get_sk_path() { return sk_path; }
95
101 bool request_pending();
102
103 protected:
107 void send_put_request();
108
113 virtual void set_put_value(JsonObject& put_data) = 0;
114
119 virtual void on_response(JsonDocument& response);
120
121 String sk_path{};
122 uint32_t timeout{};
124};
125
132template <typename T>
134 public ValueConsumer<T> {
135 public:
145 SKPutRequest(String sk_path, String config_path = "",
146 bool ignore_duplicates = true, uint32_t timeout = 5000)
147 : SKPutRequestBase(sk_path, config_path, timeout),
148 ignore_duplicates_{ignore_duplicates} {}
149
150 virtual void set(const T& new_value) override {
151 if (ignore_duplicates_ && new_value == value_) {
152 return;
153 }
154 if (!request_pending()) {
155 this->value_ = new_value;
157 } else {
158 ESP_LOGW(__FILENAME__,
159 "Ignoring PUT request (previous request still outstanding)");
160 }
161 };
162
163 virtual void set_put_value(JsonObject& put_data) override {
164 put_data["value"] = value_;
165 };
166
167 protected:
170};
171
172template <typename T>
173const String ConfigSchema(const SKPutRequest<T>& obj) {
174 static const char schema[] = R"###({"type":"object","properties":{"sk_path":{"title":"Signal K Path","type":"string"}} })###";
175 return schema;
176}
177
182
183} // namespace sensesp
184
185#endif
A base class for all template variations of the PUT request class SKPutRequest. This base class keeps...
virtual void set_put_value(JsonObject &put_data)=0
SKPutRequestBase(const String &sk_path, const String &config_path="", uint32_t timeout=5000)
virtual bool from_json(const JsonObject &config) override
virtual bool to_json(JsonObject &root) override
virtual void on_response(JsonDocument &response)
Used to send requests to the server to change the value of the specified path to a specific value acc...
SKPutRequest(String sk_path, String config_path="", bool ignore_duplicates=true, uint32_t timeout=5000)
virtual void set_put_value(JsonObject &put_data) override
virtual void set(const T &new_value) override
std::function< void(JsonDocument &)> callback
A base class for all objects that are capable of sending "requests" to the SignalK server (and option...
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)
A base class for piece of code (like a transform) that accepts data for input. ValueConsumers can acc...
const String ConfigSchema(const SmartSwitchController &obj)
SKPutRequest< int > IntSKPutRequest
SKPutRequest< bool > BoolSKPutRequest
SKPutRequest< float > FloatSKPutRequest
SKPutRequest< String > StringSKPutRequest