SensESP 3.0.1
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 <list>
10#include <set>
11
17#include "sensesp_base_app.h"
18
19namespace sensesp {
20
21static const char* NULL_AUTH_TOKEN = "";
22
29
35 virtual public ValueProducer<SKWSConnectionState> {
36 public:
38 // main task methods
39
40 SKWSClient(const String& config_path, std::shared_ptr<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 bool to_json(JsonObject& root) override final;
48 virtual bool from_json(const JsonObject& config) override final;
49
57
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 protected:
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
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;
105
108
113
114 WiFiClient wifi_client_{};
115 esp_websocket_client_handle_t client_{};
116 std::shared_ptr<SKDeltaQueue> sk_delta_queue_;
121
122 SemaphoreHandle_t received_updates_semaphore_ =
123 xSemaphoreCreateRecursiveMutex();
124 std::list<JsonDocument> received_updates_{};
125
127 // methods for all tasks
128
129 bool take_received_updates_semaphore(unsigned long int timeout_ms = 0) {
130 if (timeout_ms == 0) {
131 return xSemaphoreTake(received_updates_semaphore_, portMAX_DELAY) ==
132 pdTRUE;
133 } else {
134 return xSemaphoreTake(received_updates_semaphore_, timeout_ms) == pdTRUE;
135 }
136 }
140
142 // main task methods
143
145
147 // SKWSClient task methods
148
149 void connect_loop();
150 void test_token(const String host, const uint16_t port);
151 void send_access_request(const String host, const uint16_t port);
152 void poll_access_request(const String host, const uint16_t port,
153 const String href);
154 void connect_ws(const String& host, const uint16_t port);
155 void subscribe_listeners();
156 bool get_mdns_service(String& server_address, uint16_t& server_port);
157
163};
164
165inline const String ConfigSchema(const SKWSClient& obj) {
166 return "null";
167}
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)