Skip to content

Commit

Permalink
Merge pull request #660 from mestafin/signalk_listener
Browse files Browse the repository at this point in the history
added signalk_listener config option
  • Loading branch information
mairas authored Jun 22, 2023
2 parents 6e84c00 + d49c6da commit 9b0888a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
22 changes: 19 additions & 3 deletions src/sensesp/signalk/signalk_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace sensesp {

std::vector<SKListener*> SKListener::listeners;
std::vector<SKListener *> SKListener::listeners;

SemaphoreHandle_t SKListener::semaphore_ = xSemaphoreCreateRecursiveMutex();

SKListener::SKListener(String sk_path, int listen_delay)
: sk_path{sk_path}, listen_delay{listen_delay} {
SKListener::SKListener(String sk_path, int listen_delay, String config_path)
: Configurable(config_path), sk_path{sk_path}, listen_delay{listen_delay} {
listeners.push_back(this);
this->load_configuration();
}

bool SKListener::take_semaphore(unsigned long int timeout_ms) {
Expand All @@ -21,5 +22,20 @@ bool SKListener::take_semaphore(unsigned long int timeout_ms) {

void SKListener::release_semaphore() { xSemaphoreGiveRecursive(semaphore_); }

String SKListener::get_config_schema() { return FPSTR(SIGNALKINPUT_SCHEMA); }

void SKListener::get_configuration(JsonObject &root) {
root["sk_path"] = this->get_sk_path();
}

bool SKListener::set_configuration(const JsonObject &config) {
if (!config.containsKey("sk_path")) {
return false;
}
this->set_sk_path(config["sk_path"].as<String>());
return true;
}

void SKListener::set_sk_path(const String &path) { sk_path = path; }

} // namespace sensesp
19 changes: 17 additions & 2 deletions src/sensesp/signalk/signalk_listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
#include "sensesp/system/valueproducer.h"

namespace sensesp {
static const char SIGNALKINPUT_SCHEMA[] PROGMEM = R"({
"type": "object",
"properties": {
"sk_path": { "title": "Signal K Path", "type": "string" }
}
})";

/**
* @brief An Obervable class that listens for Signal K stream deltas
Expand All @@ -19,16 +25,19 @@ namespace sensesp {
* the most common descendant being `SKValueListener`
* @see SKValueListener
*/
class SKListener : virtual public Observable {
class SKListener : virtual public Observable, public Configurable {
public:
/**
* The constructor
* @param sk_path The Signal K path that identifies
* this particular subscription to value
* @param listen_delay How often you want the SK Server to send the
* data you're subscribing to
* @param config_path The optional configuration path that allows an end user
* to change the configuration of this object. See the Configurable class for
* more information.
*/
SKListener(String sk_path, int listen_delay);
SKListener(String sk_path, int listen_delay, String config_path="");

/**
* Returns the current Signal K path. An empty string
Expand All @@ -53,6 +62,12 @@ class SKListener : virtual public Observable {
static std::vector<SKListener*> listeners;
int listen_delay;
static SemaphoreHandle_t semaphore_;

virtual void get_configuration(JsonObject& doc) override;
virtual bool set_configuration(const JsonObject& config) override;
virtual String get_config_schema() override;

void set_sk_path(const String& path);
};

} // namespace sensesp
Expand Down
5 changes: 3 additions & 2 deletions src/sensesp/signalk/signalk_value_listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ class SKValueListener : public SKListener, public ValueProducer<T> {
* @param sk_path The Signal K path you want to listen to for value changes
* @param listen_delay The minimum interval between updates in ms
*/
SKValueListener(String sk_path, int listen_delay = 1000)
: SKListener(sk_path, listen_delay) {
SKValueListener(String sk_path, int listen_delay = 1000,
String config_path = "")
: SKListener(sk_path, listen_delay, config_path) {
if (sk_path == "") {
debugE("SKValueListener: User has provided no sk_path to listen to.");
}
Expand Down

0 comments on commit 9b0888a

Please sign in to comment.