SensESP 2.7.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
ws_client.h
Go to the documentation of this file.
1#ifndef _ws_client_H_
2#define _ws_client_H_
3#include <WebSocketsClient.h>
4
5#include <functional>
6#include <set>
7
8#include "sensesp.h"
15
16namespace sensesp {
17
18static const char* NULL_AUTH_TOKEN = "";
19
26
31class WSClient : public Configurable,
32 public Startable,
33 public ValueProducer<WSConnectionState> {
34 public:
36 // main task methods
37
40 virtual void start() override;
41
42 const String get_server_address() const { return server_address_; }
43 const uint16_t get_server_port() const { return server_port_; }
44
45 virtual void get_configuration(JsonObject& doc) override final;
46 virtual bool set_configuration(const JsonObject& config) override final;
47 virtual String get_config_schema() override;
48
54 return delta_count_producer_;
55 };
56
58
60 // WSClient task methods
61
62 void on_disconnected();
63 void on_error();
68 void connect();
69 void loop();
70 bool is_connected();
71 void restart();
72 void send_delta();
73
79 void sendTXT(String& payload);
80
81 private:
82 // these are the actually used values
83 String server_address_ = "";
84 uint16_t server_port_ = 80;
85 // these are the hardcoded and/or conf file values
86 String conf_server_address_ = "";
87 uint16_t conf_server_port_ = 0;
88
89 String client_id_ = "";
90 String polling_href_ = "";
91 String auth_token_ = NULL_AUTH_TOKEN;
92 bool server_detected_ = false;
93 bool token_test_success_ = false;
94
95 TaskQueueProducer<WSConnectionState> connection_state_ =
97
101
102 WiFiClient wifi_client_;
103 WebSocketsClient client_;
104 SKDeltaQueue* sk_delta_queue_;
105 TaskQueueProducer<int> delta_count_producer_ =
106 TaskQueueProducer<int>(0, ReactESP::app, 5, 990);
107
108 SemaphoreHandle_t received_updates_semaphore_ =
110 std::list<JsonObject> received_updates_;
111
113 // methods for all tasks
114
115 bool take_received_updates_semaphore(unsigned long int timeout_ms = 0) {
116 if (timeout_ms == 0) {
117 return xSemaphoreTake(received_updates_semaphore_, portMAX_DELAY) ==
118 pdTRUE;
119 } else {
120 return xSemaphoreTake(received_updates_semaphore_, timeout_ms) == pdTRUE;
121 }
122 }
123 void release_received_updates_semaphore() {
124 xSemaphoreGive(received_updates_semaphore_);
125 }
126
128 // main task methods
129
130 void process_received_updates();
131
133 // WSClient task methods
134
135 void connect_loop();
136 void test_token(const String host, const uint16_t port);
137 void send_access_request(const String host, const uint16_t port);
138 void poll_access_request(const String host, const uint16_t port,
139 const String href);
140 void connect_ws(const String host, const uint16_t port);
141 void subscribe_listeners();
142 bool get_mdns_service(String& server_address, uint16_t& server_port);
143
144 void set_connection_state(WSConnectionState state) {
145 task_connection_state_ = state;
146 connection_state_.set(state);
147 }
148 WSConnectionState get_connection_state() { return task_connection_state_; }
149
150};
151
152} // namespace sensesp
153
154#endif
An object that is capable of having configuration data that can be set remotely using a RESTful API,...
Construct a new transform based on a single function.
Signal K delta queue.
Automatic calling of the start() method at startup.
Definition startable.h:20
A base class for any sensor or piece of code that outputs a value for consumption elsewhere.
The websocket connection to the Signal K server.
Definition ws_client.h:33
void on_disconnected()
Called when the websocket connection is disconnected.
void on_receive_delta(uint8_t *payload)
Called when the websocket receives a delta.
virtual void get_configuration(JsonObject &doc) override final
void on_error()
Called when the websocket connection encounters an error.
virtual void start() override
void on_connected(uint8_t *payload)
Called when the websocket connection is established.
void on_receive_updates(DynamicJsonDocument &message)
Called when a delta update is received.
virtual String get_config_schema() override
String get_connection_status()
Get a String representation of the current connection state.
const String get_server_address() const
Definition ws_client.h:42
void on_receive_put(DynamicJsonDocument &message)
Called when a PUT event is received.
void sendTXT(String &payload)
Send some processed data to the websocket.
const uint16_t get_server_port() const
Definition ws_client.h:43
ValueProducer< int > & get_delta_count_producer()
Definition ws_client.h:53
virtual bool set_configuration(const JsonObject &config) override final
WSConnectionState
Definition ws_client.h:20