Loading [MathJax]/extensions/tex2jax.js
SensESP 3.1.0
Universal Signal K sensor toolkit ESP32
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 <ArduinoJson.h>
7#include <WiFi.h>
8#include <esp_websocket_client.h>
9#include <functional>
10#include <list>
11#include <set>
12
18#include "sensesp_base_app.h"
19
20namespace sensesp {
21
22static const char* NULL_AUTH_TOKEN = "";
23
30
36 virtual public ValueProducer<SKWSConnectionState> {
37 public:
39 // main task methods
40
41 SKWSClient(const String& config_path,
42 std::shared_ptr<SKDeltaQueue> sk_delta_queue,
43 const String& server_address, uint16_t server_port,
44 bool use_mdns = true);
45
46 const String get_server_address() const { return server_address_; }
47 uint16_t get_server_port() const { return server_port_; }
48
49 virtual bool to_json(JsonObject& root) override final;
50 virtual bool from_json(const JsonObject& config) override final;
51
59
68
69 String get_connection_status();
70
72 // SKWSClient task methods
73
74 void on_disconnected();
75 void on_error();
76 void on_connected();
77 void on_receive_delta(uint8_t* payload, size_t length);
78 void on_receive_updates(JsonDocument& message);
79 void on_receive_put(JsonDocument& message);
80 void connect();
81 void loop();
82 bool is_connected();
83 void restart();
84 void send_delta();
85
91 void sendTXT(String& payload);
92
93 protected:
94 // these are the actually used values
95 String server_address_ = "";
96 uint16_t server_port_ = 80;
97 // these are the hardcoded and/or conf file values
99 uint16_t conf_server_port_ = 0;
100 bool use_mdns_ = true;
101
102 String client_id_ = "";
103 String polling_href_ = "";
104 String auth_token_ = NULL_AUTH_TOKEN;
105 bool server_detected_ = false;
107
110
115
116 WiFiClient wifi_client_{};
117 esp_websocket_client_handle_t client_{};
118 std::shared_ptr<SKDeltaQueue> sk_delta_queue_;
123
124 SemaphoreHandle_t received_updates_semaphore_ =
125 xSemaphoreCreateRecursiveMutex();
126 std::list<JsonDocument> received_updates_{};
127
129 // methods for all tasks
130
131 bool take_received_updates_semaphore(unsigned long int timeout_ms = 0) {
132 if (timeout_ms == 0) {
133 return xSemaphoreTake(received_updates_semaphore_, portMAX_DELAY) ==
134 pdTRUE;
135 } else {
136 return xSemaphoreTake(received_updates_semaphore_, timeout_ms) == pdTRUE;
137 }
138 }
142
144 // main task methods
145
147
149 // SKWSClient task methods
150
151 void connect_loop();
152 void test_token(const String host, const uint16_t port);
153 void send_access_request(const String host, const uint16_t port);
154 void poll_access_request(const String host, const uint16_t port,
155 const String href);
156 void connect_ws(const String& host, const uint16_t port);
157 void subscribe_listeners();
158 bool get_mdns_service(String& server_address, uint16_t& server_port);
159
165};
166
167inline const String ConfigSchema(const SKWSClient& obj) { return "null"; }
168
169inline bool ConfigRequiresRestart(const SKWSClient& obj) { return true; }
170
171} // namespace sensesp
172
173#endif
Integrator integrates (accumulates) the incoming values.
Definition integrator.h:19
The websocket connection to the Signal K server.
std::list< JsonDocument > received_updates_
void poll_access_request(const String host, const uint16_t port, const String href)
SKWSClient(const String &config_path, std::shared_ptr< SKDeltaQueue > sk_delta_queue, const String &server_address, uint16_t server_port, bool use_mdns=true)
void on_receive_delta(uint8_t *payload, size_t length)
Called when the websocket receives a delta.
SemaphoreHandle_t received_updates_semaphore_
void process_received_updates()
Loop through the received updates and process them.
void connect_ws(const String &host, const uint16_t port)
Integrator< int, int > delta_tx_count_producer_
TaskQueueProducer< SKWSConnectionState > connection_state_
SKWSConnectionState get_connection_state()
void on_receive_put(JsonDocument &message)
Called when a PUT event is received.
void release_received_updates_semaphore()
SKWSConnectionState task_connection_state_
void send_access_request(const String host, const uint16_t port)
Integrator< int, int > delta_rx_count_producer_
virtual bool to_json(JsonObject &root) override final
uint16_t get_server_port() const
esp_websocket_client_handle_t client_
void on_error()
Called when the websocket connection encounters an error.
void sendTXT(String &payload)
Send some processed data to the websocket.
bool get_mdns_service(String &server_address, uint16_t &server_port)
void on_disconnected()
Called when the websocket connection is disconnected.
ValueProducer< int > & get_delta_tx_count_producer()
void subscribe_listeners()
Subscribes the SK delta paths to the websocket.
void on_receive_updates(JsonDocument &message)
Called when a delta update is received.
void test_token(const String host, const uint16_t port)
void set_connection_state(SKWSConnectionState state)
ValueProducer< int > & get_delta_rx_count_producer()
Get the delta rx count producer object.
bool take_received_updates_semaphore(unsigned long int timeout_ms=0)
virtual bool from_json(const JsonObject &config) override final
String get_connection_status()
Get a String representation of the current connection state.
const String get_server_address() const
TaskQueueProducer< int > delta_tx_tick_producer_
Emits the number of deltas sent since last report.
std::shared_ptr< SKDeltaQueue > sk_delta_queue_
void on_connected()
Called when the websocket connection is established.
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.
const String ConfigSchema(const SmartSwitchController &obj)
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9
bool ConfigRequiresRestart(const HTTPServer &obj)