SensESP 3.0.0-beta.3
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
signalk_ws_client.h
Go to the documentation of this file.
1#ifndef SENSESP_SRC_SENSESP_SIGNALK_SIGNALK_WS_CLIENT_H_
2#define SENSESP_SRC_SENSESP_SIGNALK_SIGNALK_WS_CLIENT_H_
3
4#include "sensesp.h"
5
6#include <WiFi.h>
7#include <esp_websocket_client.h>
8#include <functional>
9#include <set>
10
17#include "sensesp_base_app.h"
18
19namespace sensesp {
20
21static const char* NULL_AUTH_TOKEN = "";
22
29
34class SKWSClient : public Configurable,
35 public ValueProducer<SKWSConnectionState> {
36 public:
38 // main task methods
39
40 SKWSClient(const String& config_path, SKDeltaQueue* sk_delta_queue,
41 const String& server_address, uint16_t server_port,
42 bool use_mdns = true);
43
44 const String get_server_address() const { return server_address_; }
45 uint16_t get_server_port() const { return server_port_; }
46
47 virtual void get_configuration(JsonObject& root) override final;
48 virtual bool set_configuration(const JsonObject& config) override final;
49
55 return delta_tx_count_producer_;
56 };
57
64 return delta_rx_count_producer_;
65 };
66
67 String get_connection_status();
68
70 // SKWSClient task methods
71
72 void on_disconnected();
73 void on_error();
74 void on_connected();
75 void on_receive_delta(uint8_t* payload, size_t length);
76 void on_receive_updates(JsonDocument& message);
77 void on_receive_put(JsonDocument& message);
78 void connect();
79 void loop();
80 bool is_connected();
81 void restart();
82 void send_delta();
83
89 void sendTXT(String& payload);
90
91 private:
92 // these are the actually used values
93 String server_address_ = "";
94 uint16_t server_port_ = 80;
95 // these are the hardcoded and/or conf file values
96 String conf_server_address_ = "";
97 uint16_t conf_server_port_ = 0;
98 bool use_mdns_ = true;
99
100 String client_id_ = "";
101 String polling_href_ = "";
102 String auth_token_ = NULL_AUTH_TOKEN;
103 bool server_detected_ = false;
104 bool token_test_success_ = false;
105
110
113 SKWSConnectionState task_connection_state_ =
115
116 WiFiClient wifi_client_{};
117 esp_websocket_client_handle_t client_{};
118 SKDeltaQueue* sk_delta_queue_;
120 TaskQueueProducer<int> delta_tx_tick_producer_ =
121 TaskQueueProducer<int>(0, SensESPBaseApp::get_event_loop(), 5, 990);
122 Integrator<int, int> delta_tx_count_producer_{1, 0, ""};
123 Integrator<int, int> delta_rx_count_producer_{1, 0, ""};
124
125 SemaphoreHandle_t received_updates_semaphore_ =
126 xSemaphoreCreateRecursiveMutex();
127 std::list<JsonDocument> received_updates_{};
128
130 // methods for all tasks
131
132 bool take_received_updates_semaphore(unsigned long int timeout_ms = 0) {
133 if (timeout_ms == 0) {
134 return xSemaphoreTake(received_updates_semaphore_, portMAX_DELAY) ==
135 pdTRUE;
136 } else {
137 return xSemaphoreTake(received_updates_semaphore_, timeout_ms) == pdTRUE;
138 }
139 }
140 void release_received_updates_semaphore() {
141 xSemaphoreGive(received_updates_semaphore_);
142 }
143
145 // main task methods
146
147 void process_received_updates();
148
150 // SKWSClient task methods
151
152 void connect_loop();
153 void test_token(const String host, const uint16_t port);
154 void send_access_request(const String host, const uint16_t port);
155 void poll_access_request(const String host, const uint16_t port,
156 const String href);
157 void connect_ws(const String& host, const uint16_t port);
158 void subscribe_listeners();
159 bool get_mdns_service(String& server_address, uint16_t& server_port);
160
161 void set_connection_state(SKWSConnectionState state) {
162 task_connection_state_ = state;
163 connection_state_.set(state);
164 }
165 SKWSConnectionState get_connection_state() { return task_connection_state_; }
166};
167
168} // namespace sensesp
169
170#endif
An object that is capable of having configuration data that can be set remotely using a RESTful API,...
Signal K delta queue.
The websocket connection to the Signal K server.
void on_receive_delta(uint8_t *payload, size_t length)
Called when the websocket receives a delta.
virtual void get_configuration(JsonObject &root) override final
void on_receive_put(JsonDocument &message)
Called when a PUT event is received.
uint16_t get_server_port() const
void on_error()
Called when the websocket connection encounters an error.
void sendTXT(String &payload)
Send some processed data to the websocket.
void on_disconnected()
Called when the websocket connection is disconnected.
ValueProducer< int > & get_delta_tx_count_producer()
void on_receive_updates(JsonDocument &message)
Called when a delta update is received.
SKWSClient(const String &config_path, SKDeltaQueue *sk_delta_queue, const String &server_address, uint16_t server_port, bool use_mdns=true)
ValueProducer< int > & get_delta_rx_count_producer()
Get the delta rx count producer object.
virtual bool set_configuration(const JsonObject &config) override final
String get_connection_status()
Get a String representation of the current connection state.
const String get_server_address() const
void on_connected()
Called when the websocket connection is established.
static reactesp::EventLoop * get_event_loop()
Get the event loop object from the singleton SensESPBaseApp instance.
Producer class that works across task boundaries.
virtual void set(const T &value) override
A base class for any sensor or piece of code that outputs a value for consumption elsewhere.