SensESP 3.2.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
sensesp::SKWSClient Class Reference

The websocket connection to the Signal K server. More...

#include <sensesp/signalk/signalk_ws_client.h>

Inheritance diagram for sensesp::SKWSClient:
[legend]

Public Member Functions

 SKWSClient (const String &config_path, std::shared_ptr< SKDeltaQueue > sk_delta_queue, const String &server_address, uint16_t server_port, bool use_mdns=true)
const String get_server_address () const
uint16_t get_server_port () const
virtual bool to_json (JsonObject &root) override final
virtual bool from_json (const JsonObject &config) override final
ValueProducer< int > & get_delta_tx_count_producer ()
ValueProducer< int > & get_delta_rx_count_producer ()
 Get the delta rx count producer object.
String get_connection_status ()
 Get a String representation of the current connection state.
void on_disconnected ()
 Called when the websocket connection is disconnected.
void on_error ()
 Called when the websocket connection encounters an error.
void on_connected ()
 Called when the websocket connection is established.
void on_receive_delta (uint8_t *payload, size_t length)
 Called when the websocket receives a delta.
void on_receive_updates (JsonDocument &message)
 Called when a delta update is received.
void on_receive_put (JsonDocument &message)
 Called when a PUT event is received.
void connect ()
void loop ()
bool is_connected ()
void restart ()
void send_delta ()
void sendTXT (String &payload)
 Send some processed data to the websocket.
bool is_ssl_enabled () const
 Check if SSL/TLS is enabled.
void set_ssl_enabled (bool enabled)
 Enable or disable SSL/TLS manually.
bool is_tofu_enabled () const
 Check if TOFU certificate verification is enabled.
void set_tofu_enabled (bool enabled)
 Enable or disable TOFU certificate verification.
bool has_tofu_fingerprint () const
 Check if a TOFU fingerprint is stored.
const String & get_tofu_fingerprint () const
 Get the stored TOFU fingerprint.
void reset_tofu_fingerprint ()
 Reset the stored TOFU certificate fingerprint.
void set_tofu_fingerprint (const String &fingerprint)
 Set the TOFU fingerprint (called from verify callback).
Public Member Functions inherited from sensesp::FileSystemSaveable
 FileSystemSaveable (const String &config_path)
virtual bool load () override
 Load and populate the object from a persistent storage.
virtual bool save () override
 Save the object to a persistent storage.
virtual bool clear () override
 Delete the data from a persistent storage.
bool find_config_file (const String &config_path, String &filename)
Public Member Functions inherited from sensesp::Saveable
 Saveable (const String &config_path)
virtual bool refresh ()
 Refresh the object. This may or may not access the persistent storage but is not expected to overwrite the object's state.
const String & get_config_path () const
Public Member Functions inherited from sensesp::ValueProducer< SKWSConnectionState >
 ValueProducer ()
virtual const SKWSConnectionStateget () const
std::enable_if< std::is_base_of< ValueConsumer< typenameVConsumer::input_type >, VConsumer >::value &&std::is_convertible< SKWSConnectionState, typenameVConsumer::input_type >::value, std::shared_ptr< VConsumer > >::type connect_to (std::shared_ptr< VConsumer > consumer)
 Connect a producer to a transform with a different input type.
void emit (const SKWSConnectionState &new_value)
Public Member Functions inherited from sensesp::Observable
 Observable ()
 Observable (Observable &&other)
 Move constructor.
void notify ()
void attach (std::function< void()> observer)

Protected Member Functions

bool take_received_updates_semaphore (unsigned long int timeout_ms=0)
void release_received_updates_semaphore ()
void process_received_updates ()
 Loop through the received updates and process them.
void connect_loop ()
void test_token (const String host, const uint16_t port)
void send_access_request (const String host, const uint16_t port)
void poll_access_request (const String host, const uint16_t port, const String href)
void connect_ws (const String &host, const uint16_t port)
void subscribe_listeners ()
 Subscribes the SK delta paths to the websocket.
bool get_mdns_service (String &server_address, uint16_t &server_port)
bool detect_ssl ()
void set_connection_state (SKWSConnectionState state)
SKWSConnectionState get_connection_state ()

Protected Attributes

String server_address_ = ""
uint16_t server_port_ = 80
String conf_server_address_ = ""
uint16_t conf_server_port_ = 0
bool use_mdns_ = true
String client_id_ = ""
String polling_href_ = ""
String auth_token_ = NULL_AUTH_TOKEN
bool server_detected_ = false
bool token_test_success_ = false
bool ssl_enabled_ = false
bool tofu_enabled_ = true
String tofu_fingerprint_ = ""
TaskQueueProducer< SKWSConnectionStateconnection_state_
SKWSConnectionState task_connection_state_
esp_websocket_client_handle_t client_ = nullptr
std::shared_ptr< SKDeltaQueuesk_delta_queue_
TaskQueueProducer< int > delta_tx_tick_producer_ {0, event_loop(), 990}
 Emits the number of deltas sent since last report.
Integrator< int, int > delta_tx_count_producer_ {1, 0, ""}
Integrator< int, int > delta_rx_count_producer_ {1, 0, ""}
SemaphoreHandle_t received_updates_semaphore_
std::list< JsonDocument > received_updates_ {}
Protected Attributes inherited from sensesp::Saveable
const String config_path_
Protected Attributes inherited from sensesp::ValueProducer< SKWSConnectionState >
SKWSConnectionState output_

Detailed Description

The websocket connection to the Signal K server.

See also
SensESPApp

Definition at line 34 of file signalk_ws_client.h.

Constructor & Destructor Documentation

◆ SKWSClient()

sensesp::SKWSClient::SKWSClient ( const String & config_path,
std::shared_ptr< SKDeltaQueue > sk_delta_queue,
const String & server_address,
uint16_t server_port,
bool use_mdns = true )

Definition at line 155 of file signalk_ws_client.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ connect()

void sensesp::SKWSClient::connect ( )

Definition at line 538 of file signalk_ws_client.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ connect_loop()

void sensesp::SKWSClient::connect_loop ( )
protected

Definition at line 190 of file signalk_ws_client.cpp.

Here is the call graph for this function:

◆ connect_ws()

void sensesp::SKWSClient::connect_ws ( const String & host,
const uint16_t port )
protected

Definition at line 940 of file signalk_ws_client.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ detect_ssl()

bool sensesp::SKWSClient::detect_ssl ( )
protected

Definition at line 489 of file signalk_ws_client.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ from_json()

bool sensesp::SKWSClient::from_json ( const JsonObject & root)
finaloverridevirtual

Deserializes the current object data from a JsonObject.

Reimplemented from sensesp::Serializable.

Definition at line 1046 of file signalk_ws_client.cpp.

◆ get_connection_state()

SKWSConnectionState sensesp::SKWSClient::get_connection_state ( )
inlineprotected

Definition at line 229 of file signalk_ws_client.h.

Here is the caller graph for this function:

◆ get_connection_status()

String sensesp::SKWSClient::get_connection_status ( )

Get a String representation of the current connection state.

Returns
String

Definition at line 1086 of file signalk_ws_client.cpp.

Here is the call graph for this function:

◆ get_delta_rx_count_producer()

ValueProducer< int > & sensesp::SKWSClient::get_delta_rx_count_producer ( )
inline

Get the delta rx count producer object.

Returns
ValueProducer<int>&

Definition at line 64 of file signalk_ws_client.h.

Here is the call graph for this function:

◆ get_delta_tx_count_producer()

ValueProducer< int > & sensesp::SKWSClient::get_delta_tx_count_producer ( )
inline

Return a delta update ValueProducer that produces the number of sent deltas.

Definition at line 55 of file signalk_ws_client.h.

Here is the call graph for this function:

◆ get_mdns_service()

bool sensesp::SKWSClient::get_mdns_service ( String & server_address,
uint16_t & server_port )
protected

Definition at line 457 of file signalk_ws_client.cpp.

Here is the caller graph for this function:

◆ get_server_address()

const String sensesp::SKWSClient::get_server_address ( ) const
inline

Definition at line 45 of file signalk_ws_client.h.

◆ get_server_port()

uint16_t sensesp::SKWSClient::get_server_port ( ) const
inline

Definition at line 46 of file signalk_ws_client.h.

◆ get_tofu_fingerprint()

const String & sensesp::SKWSClient::get_tofu_fingerprint ( ) const
inline

Get the stored TOFU fingerprint.

Definition at line 132 of file signalk_ws_client.h.

◆ has_tofu_fingerprint()

bool sensesp::SKWSClient::has_tofu_fingerprint ( ) const
inline

Check if a TOFU fingerprint is stored.

Definition at line 127 of file signalk_ws_client.h.

◆ is_connected()

bool sensesp::SKWSClient::is_connected ( )

Definition at line 1005 of file signalk_ws_client.cpp.

Here is the call graph for this function:

◆ is_ssl_enabled()

bool sensesp::SKWSClient::is_ssl_enabled ( ) const
inline

Check if SSL/TLS is enabled.

Definition at line 95 of file signalk_ws_client.h.

◆ is_tofu_enabled()

bool sensesp::SKWSClient::is_tofu_enabled ( ) const
inline

Check if TOFU certificate verification is enabled.

Definition at line 111 of file signalk_ws_client.h.

◆ loop()

void sensesp::SKWSClient::loop ( )

Definition at line 604 of file signalk_ws_client.cpp.

◆ on_connected()

void sensesp::SKWSClient::on_connected ( )

Called when the websocket connection is established.

Called in the websocket task context.

Definition at line 232 of file signalk_ws_client.cpp.

Here is the call graph for this function:

◆ on_disconnected()

void sensesp::SKWSClient::on_disconnected ( )

Called when the websocket connection is disconnected.

This method is called in the websocket task context.

Definition at line 202 of file signalk_ws_client.cpp.

Here is the call graph for this function:

◆ on_error()

void sensesp::SKWSClient::on_error ( )

Called when the websocket connection encounters an error.

Called in the websocket task context.

Definition at line 222 of file signalk_ws_client.cpp.

Here is the call graph for this function:

◆ on_receive_delta()

void sensesp::SKWSClient::on_receive_delta ( uint8_t * payload,
size_t length )

Called when the websocket receives a delta.

Called in the websocket task context.

Parameters
payload

Definition at line 290 of file signalk_ws_client.cpp.

Here is the call graph for this function:

◆ on_receive_put()

void sensesp::SKWSClient::on_receive_put ( JsonDocument & message)

Called when a PUT event is received.

Called in the websocket task context.

Parameters
message

Definition at line 401 of file signalk_ws_client.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ on_receive_updates()

void sensesp::SKWSClient::on_receive_updates ( JsonDocument & message)

Called when a delta update is received.

Called in the websocket task context.

Parameters
message

Definition at line 330 of file signalk_ws_client.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ poll_access_request()

void sensesp::SKWSClient::poll_access_request ( const String host,
const uint16_t port,
const String href )
protected

Definition at line 822 of file signalk_ws_client.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_received_updates()

void sensesp::SKWSClient::process_received_updates ( )
protected

Loop through the received updates and process them.

This method is called in the main task context.

Definition at line 358 of file signalk_ws_client.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ release_received_updates_semaphore()

void sensesp::SKWSClient::release_received_updates_semaphore ( )
inlineprotected

Definition at line 203 of file signalk_ws_client.h.

Here is the caller graph for this function:

◆ reset_tofu_fingerprint()

void sensesp::SKWSClient::reset_tofu_fingerprint ( )
inline

Reset the stored TOFU certificate fingerprint.

Call this when the server certificate has changed legitimately and you want to trust the new certificate.

Definition at line 140 of file signalk_ws_client.h.

Here is the call graph for this function:

◆ restart()

void sensesp::SKWSClient::restart ( )

Definition at line 1009 of file signalk_ws_client.cpp.

Here is the call graph for this function:

◆ send_access_request()

void sensesp::SKWSClient::send_access_request ( const String host,
const uint16_t port )
protected

Definition at line 700 of file signalk_ws_client.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ send_delta()

void sensesp::SKWSClient::send_delta ( )

Definition at line 1018 of file signalk_ws_client.cpp.

Here is the call graph for this function:

◆ sendTXT()

void sensesp::SKWSClient::sendTXT ( String & payload)

Send some processed data to the websocket.

Sends the specified payload to the server over the websocket this client is connected to. If no connection currently exist, the call is safely ignored.

Called in the websocket task context.

Parameters
payload

Definition at line 450 of file signalk_ws_client.cpp.

Here is the call graph for this function:

◆ set_connection_state()

void sensesp::SKWSClient::set_connection_state ( SKWSConnectionState state)
inlineprotected

Definition at line 225 of file signalk_ws_client.h.

Here is the caller graph for this function:

◆ set_ssl_enabled()

void sensesp::SKWSClient::set_ssl_enabled ( bool enabled)
inline

Enable or disable SSL/TLS manually.

Note: SSL is normally auto-detected. Use this only if you need to override the auto-detection behavior.

Definition at line 103 of file signalk_ws_client.h.

Here is the call graph for this function:

◆ set_tofu_enabled()

void sensesp::SKWSClient::set_tofu_enabled ( bool enabled)
inline

Enable or disable TOFU certificate verification.

When enabled, the server certificate fingerprint is captured on first connection and verified on subsequent connections.

Definition at line 119 of file signalk_ws_client.h.

Here is the call graph for this function:

◆ set_tofu_fingerprint()

void sensesp::SKWSClient::set_tofu_fingerprint ( const String & fingerprint)
inline

Set the TOFU fingerprint (called from verify callback).

Definition at line 148 of file signalk_ws_client.h.

Here is the call graph for this function:

◆ subscribe_listeners()

void sensesp::SKWSClient::subscribe_listeners ( )
protected

Subscribes the SK delta paths to the websocket.

Called in the websocket task context.

Definition at line 245 of file signalk_ws_client.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ take_received_updates_semaphore()

bool sensesp::SKWSClient::take_received_updates_semaphore ( unsigned long int timeout_ms = 0)
inlineprotected

Definition at line 195 of file signalk_ws_client.h.

Here is the caller graph for this function:

◆ test_token()

void sensesp::SKWSClient::test_token ( const String host,
const uint16_t port )
protected

Definition at line 608 of file signalk_ws_client.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_json()

bool sensesp::SKWSClient::to_json ( JsonObject & root)
finaloverridevirtual

Serializes the current object data into a JsonObject.

Reimplemented from sensesp::Serializable.

Definition at line 1031 of file signalk_ws_client.cpp.

Member Data Documentation

◆ auth_token_

String sensesp::SKWSClient::auth_token_ = NULL_AUTH_TOKEN
protected

Definition at line 164 of file signalk_ws_client.h.

◆ client_

esp_websocket_client_handle_t sensesp::SKWSClient::client_ = nullptr
protected

Definition at line 181 of file signalk_ws_client.h.

◆ client_id_

String sensesp::SKWSClient::client_id_ = ""
protected

Definition at line 162 of file signalk_ws_client.h.

◆ conf_server_address_

String sensesp::SKWSClient::conf_server_address_ = ""
protected

Definition at line 158 of file signalk_ws_client.h.

◆ conf_server_port_

uint16_t sensesp::SKWSClient::conf_server_port_ = 0
protected

Definition at line 159 of file signalk_ws_client.h.

◆ connection_state_

TaskQueueProducer<SKWSConnectionState> sensesp::SKWSClient::connection_state_
protected
Initial value:
{
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9

Definition at line 173 of file signalk_ws_client.h.

◆ delta_rx_count_producer_

Integrator<int, int> sensesp::SKWSClient::delta_rx_count_producer_ {1, 0, ""}
protected

Definition at line 186 of file signalk_ws_client.h.

◆ delta_tx_count_producer_

Integrator<int, int> sensesp::SKWSClient::delta_tx_count_producer_ {1, 0, ""}
protected

Definition at line 185 of file signalk_ws_client.h.

◆ delta_tx_tick_producer_

TaskQueueProducer<int> sensesp::SKWSClient::delta_tx_tick_producer_ {0, event_loop(), 990}
protected

Emits the number of deltas sent since last report.

Definition at line 184 of file signalk_ws_client.h.

◆ polling_href_

String sensesp::SKWSClient::polling_href_ = ""
protected

Definition at line 163 of file signalk_ws_client.h.

◆ received_updates_

std::list<JsonDocument> sensesp::SKWSClient::received_updates_ {}
protected

Definition at line 190 of file signalk_ws_client.h.

◆ received_updates_semaphore_

SemaphoreHandle_t sensesp::SKWSClient::received_updates_semaphore_
protected
Initial value:
=
xSemaphoreCreateRecursiveMutex()

Definition at line 188 of file signalk_ws_client.h.

◆ server_address_

String sensesp::SKWSClient::server_address_ = ""
protected

Definition at line 155 of file signalk_ws_client.h.

◆ server_detected_

bool sensesp::SKWSClient::server_detected_ = false
protected

Definition at line 165 of file signalk_ws_client.h.

◆ server_port_

uint16_t sensesp::SKWSClient::server_port_ = 80
protected

Definition at line 156 of file signalk_ws_client.h.

◆ sk_delta_queue_

std::shared_ptr<SKDeltaQueue> sensesp::SKWSClient::sk_delta_queue_
protected

Definition at line 182 of file signalk_ws_client.h.

◆ ssl_enabled_

bool sensesp::SKWSClient::ssl_enabled_ = false
protected

Definition at line 169 of file signalk_ws_client.h.

◆ task_connection_state_

SKWSConnectionState sensesp::SKWSClient::task_connection_state_
protected
Initial value:

task_connection_state is used to track the internal task state which might be out of sync with the published connection state.

Definition at line 178 of file signalk_ws_client.h.

◆ tofu_enabled_

bool sensesp::SKWSClient::tofu_enabled_ = true
protected

Definition at line 170 of file signalk_ws_client.h.

◆ tofu_fingerprint_

String sensesp::SKWSClient::tofu_fingerprint_ = ""
protected

Definition at line 171 of file signalk_ws_client.h.

◆ token_test_success_

bool sensesp::SKWSClient::token_test_success_ = false
protected

Definition at line 166 of file signalk_ws_client.h.

◆ use_mdns_

bool sensesp::SKWSClient::use_mdns_ = true
protected

Definition at line 160 of file signalk_ws_client.h.


The documentation for this class was generated from the following files: