Skip to content

Commit

Permalink
Breaking change: rename WSClient to SKWSClient
Browse files Browse the repository at this point in the history
  • Loading branch information
mairas committed Apr 16, 2024
1 parent aa8a343 commit ac602b5
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/sensesp/controllers/system_status_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define _SYSTEM_STATUS_CONTROLLER_H_

#include "sensesp/net/networking.h"
#include "sensesp/net/ws_client.h"
#include "sensesp/signalk/signalk_ws_client.h"
#include "sensesp/system/valueproducer.h"

namespace sensesp {
Expand Down Expand Up @@ -35,7 +35,7 @@ class SystemStatusController : public ValueConsumer<WiFiState>,
virtual void set(WiFiState new_value,
uint8_t input_channel = 0) override;
/// ValueConsumer interface for ValueConsumer<WSConnectionState>
/// (WSClient object state updates)
/// (SKWSClient object state updates)
virtual void set(WSConnectionState new_value,
uint8_t input_channel = 0) override;

Expand Down
2 changes: 1 addition & 1 deletion src/sensesp/signalk/signalk_delta_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace sensesp {
* @brief Signal K delta queue
*
* This class implements a Signal K delta queue. There should be a unique queue
* for each possible output channel (WSClient, NMEA 2000 messages,
* for each possible output channel (SKWSClient, NMEA 2000 messages,
* carrier pigeons).
*/
class SKDeltaQueue {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ws_client.h"
#include "signalk_ws_client.h"

#include <ArduinoJson.h>
#include <ESPmDNS.h>
Expand All @@ -18,7 +18,7 @@ namespace sensesp {

constexpr int ws_client_task_stack_size = 8192;

WSClient* ws_client;
SKWSClient* ws_client;

static const char* kRequestPermission = "readwrite";

Expand Down Expand Up @@ -74,17 +74,17 @@ static void websocket_event_handler(void* handler_args, esp_event_base_t base,
}
}

WSClient::WSClient(String config_path, SKDeltaQueue* sk_delta_queue,
SKWSClient::SKWSClient(String config_path, SKDeltaQueue* sk_delta_queue,
String server_address, uint16_t server_port, bool use_mdns)
: Configurable{config_path, "/System/Signal K Settings", 200},
sk_delta_queue_{sk_delta_queue},
conf_server_address_{server_address},
conf_server_port_{server_port},
use_mdns_{use_mdns} {
// a WSClient object observes its own connection_state_ member
// a SKWSClient object observes its own connection_state_ member
// and simply passes through any notification it emits. As a result,
// whenever the value of connection_state_ is updated, observers of the
// WSClient object get automatically notified.
// SKWSClient object get automatically notified.
this->connection_state_.attach(
[this]() { this->emit(this->connection_state_.get()); });

Expand All @@ -100,14 +100,14 @@ WSClient::WSClient(String config_path, SKDeltaQueue* sk_delta_queue,
delta_tx_tick_producer_.connect_to(&delta_tx_count_producer_);

ReactESP::app->onDelay(0, [this]() {
debugD("Starting WSClient");
xTaskCreate(ExecuteWebSocketTask, "WSClient", ws_client_task_stack_size,
debugD("Starting SKWSClient");
xTaskCreate(ExecuteWebSocketTask, "SKWSClient", ws_client_task_stack_size,
this, 1, NULL);
MDNS.addService("signalk-sensesp", "tcp", 80);
});
}

void WSClient::connect_loop() {
void SKWSClient::connect_loop() {
if (this->get_connection_state() == WSConnectionState::kWSDisconnected) {
this->connect();
}
Expand All @@ -119,7 +119,7 @@ void WSClient::connect_loop() {
* This method is called in the websocket task context.
*
*/
void WSClient::on_disconnected() {
void SKWSClient::on_disconnected() {
if (this->get_connection_state() == WSConnectionState::kWSConnecting &&
server_detected_ && !token_test_success_) {
// Going from connecting directly to disconnect when we
Expand All @@ -139,7 +139,7 @@ void WSClient::on_disconnected() {
* Called in the websocket task context.
*
*/
void WSClient::on_error() {
void SKWSClient::on_error() {
this->set_connection_state(WSConnectionState::kWSDisconnected);
debugW("Websocket client error.");
}
Expand All @@ -149,7 +149,7 @@ void WSClient::on_error() {
*
* Called in the websocket task context.
*/
void WSClient::on_connected() {
void SKWSClient::on_connected() {
this->set_connection_state(WSConnectionState::kWSConnected);
this->sk_delta_queue_->reset_meta_send();
debugI("Subscribing to Signal K listeners...");
Expand All @@ -162,7 +162,7 @@ void WSClient::on_connected() {
* Called in the websocket task context.
*
*/
void WSClient::subscribe_listeners() {
void SKWSClient::subscribe_listeners() {
bool output_available = false;
JsonDocument subscription;
subscription["context"] = "vessels.self";
Expand Down Expand Up @@ -206,7 +206,7 @@ void WSClient::subscribe_listeners() {
*
* @param payload
*/
void WSClient::on_receive_delta(uint8_t* payload) {
void SKWSClient::on_receive_delta(uint8_t* payload) {
#ifdef SIGNALK_PRINT_RCV_DELTA
debugD("Websocket payload received: %s", (char*)payload);
#endif
Expand Down Expand Up @@ -240,7 +240,7 @@ void WSClient::on_receive_delta(uint8_t* payload) {
*
* @param message
*/
void WSClient::on_receive_updates(JsonDocument& message) {
void SKWSClient::on_receive_updates(JsonDocument& message) {
// Process updates from subscriptions...
JsonArray updates = message["updates"];

Expand Down Expand Up @@ -269,7 +269,7 @@ void WSClient::on_receive_updates(JsonDocument& message) {
* This method is called in the main task context.
*
*/
void WSClient::process_received_updates() {
void SKWSClient::process_received_updates() {
SKListener::take_semaphore();

const std::vector<SKListener*>& listeners = SKListener::get_listeners();
Expand Down Expand Up @@ -310,7 +310,7 @@ void WSClient::process_received_updates() {
*
* @param message
*/
void WSClient::on_receive_put(JsonDocument& message) {
void SKWSClient::on_receive_put(JsonDocument& message) {
// Process PUT requests...
JsonArray puts = message["put"];
size_t response_count = 0;
Expand Down Expand Up @@ -359,14 +359,14 @@ void WSClient::on_receive_put(JsonDocument& message) {
*
* @param payload
*/
void WSClient::sendTXT(String& payload) {
void SKWSClient::sendTXT(String& payload) {
if (get_connection_state() == WSConnectionState::kWSConnected) {
esp_websocket_client_send_text(this->client_, payload.c_str(),
payload.length(), portMAX_DELAY);
}
}

bool WSClient::get_mdns_service(String& server_address, uint16_t& server_port) {
bool SKWSClient::get_mdns_service(String& server_address, uint16_t& server_port) {
// get IP address using an mDNS query
int n = MDNS.queryService("signalk-ws", "tcp");
if (n == 0) {
Expand All @@ -380,7 +380,7 @@ bool WSClient::get_mdns_service(String& server_address, uint16_t& server_port) {
}
}

void WSClient::connect() {
void SKWSClient::connect() {
if (get_connection_state() != WSConnectionState::kWSDisconnected) {
return;
}
Expand Down Expand Up @@ -437,7 +437,7 @@ void WSClient::connect() {
this->test_token(this->server_address_, this->server_port_);
}

void WSClient::test_token(const String server_address,
void SKWSClient::test_token(const String server_address,
const uint16_t server_port) {
// FIXME: implement async HTTP client!
HTTPClient http;
Expand Down Expand Up @@ -479,7 +479,7 @@ void WSClient::test_token(const String server_address,
}
}

void WSClient::send_access_request(const String server_address,
void SKWSClient::send_access_request(const String server_address,
const uint16_t server_port) {
debugD("Preparing a new access request");
if (client_id_ == "") {
Expand Down Expand Up @@ -539,7 +539,7 @@ void WSClient::send_access_request(const String server_address,
this->poll_access_request(server_address, server_port, this->polling_href_);
}

void WSClient::poll_access_request(const String server_address,
void SKWSClient::poll_access_request(const String server_address,
const uint16_t server_port,
const String href) {
debugD("Polling SK Server for authentication token");
Expand Down Expand Up @@ -606,7 +606,7 @@ void WSClient::poll_access_request(const String server_address,
}
}

void WSClient::connect_ws(const String host, const uint16_t port) {
void SKWSClient::connect_ws(const String host, const uint16_t port) {
String path = "/signalk/v1/stream?subscribe=none";
set_connection_state(WSConnectionState::kWSConnecting);

Expand Down Expand Up @@ -640,18 +640,18 @@ void WSClient::connect_ws(const String host, const uint16_t port) {
debugD("Websocket client started.");
}

bool WSClient::is_connected() {
bool SKWSClient::is_connected() {
return get_connection_state() == WSConnectionState::kWSConnected;
}

void WSClient::restart() {
void SKWSClient::restart() {
if (get_connection_state() == WSConnectionState::kWSConnected) {
esp_websocket_client_close(this->client_, portMAX_DELAY);
set_connection_state(WSConnectionState::kWSDisconnected);
}
}

void WSClient::send_delta() {
void SKWSClient::send_delta() {
String output;
if (get_connection_state() == WSConnectionState::kWSConnected) {
if (sk_delta_queue_->data_available()) {
Expand All @@ -664,7 +664,7 @@ void WSClient::send_delta() {
}
}

void WSClient::get_configuration(JsonObject& root) {
void SKWSClient::get_configuration(JsonObject& root) {
root["sk_address"] = this->conf_server_address_;
root["sk_port"] = this->conf_server_port_;
root["use_mdns"] = this->use_mdns_;
Expand All @@ -674,7 +674,7 @@ void WSClient::get_configuration(JsonObject& root) {
root["polling_href"] = this->polling_href_;
}

bool WSClient::set_configuration(const JsonObject& config) {
bool SKWSClient::set_configuration(const JsonObject& config) {
if (config.containsKey("sk_address")) {
this->conf_server_address_ = config["sk_address"].as<String>();
}
Expand Down Expand Up @@ -702,7 +702,7 @@ bool WSClient::set_configuration(const JsonObject& config) {
*
* @return String
*/
String WSClient::get_connection_status() {
String SKWSClient::get_connection_status() {
auto state = get_connection_state();
switch (state) {
case WSConnectionState::kWSAuthorizing:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _ws_client_H_
#define _ws_client_H_
#ifndef SENSESP_SRC_SENSESP_SIGNALK_SIGNALK_WS_CLIENT_H_
#define SENSESP_SRC_SENSESP_SIGNALK_SIGNALK_WS_CLIENT_H_

#include <WiFi.h>
#include <esp_websocket_client.h>
Expand Down Expand Up @@ -30,13 +30,13 @@ enum class WSConnectionState {
* @brief The websocket connection to the Signal K server.
* @see SensESPApp
*/
class WSClient : public Configurable,
class SKWSClient : public Configurable,
public ValueProducer<WSConnectionState> {
public:
/////////////////////////////////////////////////////////
// main task methods

WSClient(String config_path, SKDeltaQueue* sk_delta_queue,
SKWSClient(String config_path, SKDeltaQueue* sk_delta_queue,
String server_address, uint16_t server_port, bool use_mdns = true);

const String get_server_address() const { return server_address_; }
Expand Down Expand Up @@ -65,7 +65,7 @@ class WSClient : public Configurable,
String get_connection_status();

/////////////////////////////////////////////////////////
// WSClient task methods
// SKWSClient task methods

void on_disconnected();
void on_error();
Expand Down Expand Up @@ -143,7 +143,7 @@ class WSClient : public Configurable,
void process_received_updates();

/////////////////////////////////////////////////////////
// WSClient task methods
// SKWSClient task methods

void connect_loop();
void test_token(const String host, const uint16_t port);
Expand Down
6 changes: 3 additions & 3 deletions src/sensesp/system/led_blinker.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef _led_blinker_H_
#define _led_blinker_H_
#ifndef SENSESP_SRC_SENSESP_SYSTEM_LED_BLINKER_H_
#define SENSESP_SRC_SENSESP_SYSTEM_LED_BLINKER_H_

#include <ReactESP.h>

#include "sensesp/net/ws_client.h"
#include "sensesp/signalk/signalk_ws_client.h"

namespace sensesp {

Expand Down
4 changes: 2 additions & 2 deletions src/sensesp/system/system_status_led.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _LED_CONTROLLER_H_
#define _LED_CONTROLLER_H_
#ifndef SENSESP_SRC_SENSESP_SYSTEM_SYSTEM_STATUS_LED_H_
#define SENSESP_SRC_SENSESP_SYSTEM_SYSTEM_STATUS_LED_H_

#include "lambda_consumer.h"
#include "led_blinker.h"
Expand Down
2 changes: 1 addition & 1 deletion src/sensesp_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void SensESPApp::setup() {
// create the websocket client
bool use_mdns = sk_server_address_ == "";
this->ws_client_ =
new WSClient("/System/Signal K Settings", sk_delta_queue_,
new SKWSClient("/System/Signal K Settings", sk_delta_queue_,
sk_server_address_, sk_server_port_, use_mdns);

// connect the system status controller
Expand Down
6 changes: 3 additions & 3 deletions src/sensesp_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "sensesp/net/web/base_command_handler.h"
#include "sensesp/net/web/config_handler.h"
#include "sensesp/net/web/static_file_handler.h"
#include "sensesp/net/ws_client.h"
#include "sensesp/signalk/signalk_ws_client.h"
#include "sensesp/sensesp_version.h"
#include "sensesp/sensors/sensor.h"
#include "sensesp/signalk/signalk_delta_queue.h"
Expand Down Expand Up @@ -60,7 +60,7 @@ class SensESPApp : public SensESPBaseApp {
return &(this->system_status_controller_);
}
Networking* get_networking() { return this->networking_; }
WSClient* get_ws_client() { return this->ws_client_; }
SKWSClient* get_ws_client() { return this->ws_client_; }

protected:
/**
Expand Down Expand Up @@ -135,7 +135,7 @@ class SensESPApp : public SensESPBaseApp {
Networking* networking_ = NULL;
OTA* ota_;
SKDeltaQueue* sk_delta_queue_;
WSClient* ws_client_;
SKWSClient* ws_client_;

UIOutput<String>* sensesp_version_ui_output_ = new UIOutput<String>(
"SenseESP version", kSensESPVersion, "Software", 1900);
Expand Down

0 comments on commit ac602b5

Please sign in to comment.