SensESP 3.3.0
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
60 static StaticSemaphore_t request_map_mutex_buffer_;
61 static SemaphoreHandle_t request_map_mutex_;
62
65 static void remove_request(String request_id);
66
69 static PendingRequest* get_request(String request_id);
70};
71
81 public FileSystemSaveable {
82 public:
90 SKPutRequestBase(const String& sk_path, const String& config_path = "",
91 uint32_t timeout = 5000);
92
93 // For reading and writing the configuration
94 virtual bool to_json(JsonObject& root) override;
95 virtual bool from_json(const JsonObject& config) override;
96
100 String get_sk_path() { return sk_path; }
101
107 bool request_pending();
108
109 protected:
113 void send_put_request();
114
119 virtual void set_put_value(JsonObject& put_data) = 0;
120
125 virtual void on_response(JsonDocument& response);
126
127 String sk_path{};
128 uint32_t timeout{};
130};
131
138template <typename T>
140 public ValueConsumer<T> {
141 public:
151 SKPutRequest(String sk_path, String config_path = "",
152 bool ignore_duplicates = true, uint32_t timeout = 5000)
153 : SKPutRequestBase(sk_path, config_path, timeout),
154 ignore_duplicates_{ignore_duplicates} {}
155
156 virtual void set(const T& new_value) override {
157 if (ignore_duplicates_ && new_value == value_) {
158 return;
159 }
160 if (!request_pending()) {
161 this->value_ = new_value;
163 } else {
164 ESP_LOGW(__FILENAME__,
165 "Ignoring PUT request (previous request still outstanding)");
166 }
167 };
168
169 virtual void set_put_value(JsonObject& put_data) override {
170 put_data["value"] = value_;
171 };
172
173 protected:
176};
177
178template <typename T>
179const String ConfigSchema(const SKPutRequest<T>& obj) {
180 static const char schema[] = R"###({"type":"object","properties":{"sk_path":{"title":"Signal K Path","type":"string"}} })###";
181 return schema;
182}
183
188
189} // namespace sensesp
190
191#endif
FileSystemSaveable(const String &config_path)
Definition saveable.h:63
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 SemaphoreHandle_t request_map_mutex_
static StaticSemaphore_t request_map_mutex_buffer_
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