Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Json serialization #744

Merged
merged 7 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@

- Implement stream producers that emit characters or lines from a stream
(e.g., a serial port)
-
- SKEmitter::as_signalk() has been renamed to SKEmitter::as_signalk_json().
It now writes its output to a JsonDocument reference instead
of returning a String.

### Migrating Existing Projects

Expand Down
9 changes: 7 additions & 2 deletions src/sensesp/signalk/signalk_delta_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ void SKDeltaQueue::append(const String& val) {
void SKDeltaQueue::connect_emitters() {
for (auto const& sk_source : SKEmitter::get_sources()) {
if (sk_source->get_sk_path() != "") {
sk_source->attach(
[sk_source, this]() { this->append(sk_source->as_signalk()); });
sk_source->attach([sk_source, this]() {
String output;
JsonDocument doc;
sk_source->as_signalk_json(doc);
serializeJson(doc, output);
this->append(output);
});
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/sensesp/signalk/signalk_emitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ class SKEmitter : virtual public Observable {

/**
* Returns the data to be reported to the server as
* a Signal K json string.
* a Signal K json object.
*/
virtual String as_signalk() { return "not implemented"; }
virtual void as_signalk_json(JsonDocument& doc) {
ESP_LOGE("SKEmitter", "as_signalk_json() not implemented");
JsonVariant obj = doc.as<JsonVariant>();
obj.set("ERROR! Not implemented");
}

/**
* Returns a Metadata structure that describes the sk_path this SKEmitter
Expand Down
20 changes: 6 additions & 14 deletions src/sensesp/signalk/signalk_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,9 @@ class SKOutput : public SKEmitter, public SymmetricTransform<T> {
this->ValueProducer<T>::emit(new_value);
}

virtual String as_signalk() override {
JsonDocument json_doc;
String json;
json_doc["path"] = this->get_sk_path();
json_doc["value"] = ValueProducer<T>::output;
serializeJson(json_doc, json);
return json;
virtual void as_signalk_json(JsonDocument& doc) override {
doc["path"] = this->get_sk_path();
doc["value"] = ValueProducer<T>::output;
}

virtual void get_configuration(JsonObject& root) override {
Expand Down Expand Up @@ -92,13 +88,9 @@ class SKOutputRawJson : public SKOutput<String> {
SKMetadata* meta = NULL)
: SKOutput<String>(sk_path, config_path, meta) {}

virtual String as_signalk() override {
JsonDocument json_doc;
String json;
json_doc["path"] = this->get_sk_path();
json_doc["value"] = serialized(ValueProducer<String>::output);
serializeJson(json_doc, json);
return json;
virtual void as_signalk_json(JsonDocument& doc) override {
doc["path"] = this->get_sk_path();
doc["value"] = serialized(ValueProducer<String>::output);
}
};

Expand Down
30 changes: 0 additions & 30 deletions src/sensesp/signalk/signalk_position.cpp

This file was deleted.

18 changes: 0 additions & 18 deletions src/sensesp/signalk/signalk_position.h

This file was deleted.

10 changes: 3 additions & 7 deletions src/sensesp/signalk/signalk_time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ SKOutputTime::SKOutputTime(const String& sk_path, const String& config_path)
load_configuration();
}

String SKOutputTime::as_signalk() {
JsonDocument json_doc;
String json;
json_doc["path"] = this->sk_path_;
json_doc["value"] = output;
serializeJson(json_doc, json);
return json;
void SKOutputTime::as_signalk_json(JsonDocument& doc){
doc["path"] = this->sk_path_;
doc["value"] = output;
}

void SKOutputTime::get_configuration(JsonObject& doc) {
Expand Down
2 changes: 1 addition & 1 deletion src/sensesp/signalk/signalk_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace sensesp {
class SKOutputTime : public TimeString, public SKEmitter {
public:
SKOutputTime(const String& sk_path, const String& config_path = "");
virtual String as_signalk() override;
virtual void as_signalk_json(JsonDocument& doc) override;
virtual void get_configuration(JsonObject& doc) override;
virtual bool set_configuration(const JsonObject& config) override;
virtual String get_config_schema() override;
Expand Down
46 changes: 46 additions & 0 deletions src/sensesp/signalk/signalk_types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

#include "signalk_types.h"

namespace sensesp {

/**
* @brief Template specialization for SKOutputPosition::as_signalk_json()
*
* This specialization allows `Position` objects to be output as Signal K
* deltas.
*
* @tparam
* @return String
*/
template <>
void SKOutput<Position>::as_signalk_json(JsonDocument& doc) {
doc["path"] = this->get_sk_path();
JsonObject value = doc["value"].to<JsonObject>();
value["latitude"] = output.latitude;
value["longitude"] = output.longitude;
if (output.altitude != kPositionInvalidAltitude) {
value["altitude"] = output.altitude;
}
}

template <>
void SKOutput<ENUVector>::as_signalk_json(JsonDocument& doc) {
doc["path"] = this->get_sk_path();
JsonObject value = doc["value"].to<JsonObject>();
value["east"] = output.east;
value["north"] = output.north;
if (output.up != kPositionInvalidAltitude) {
value["up"] = output.up;
}
}

template <>
void SKOutput<AttitudeVector>::as_signalk_json(JsonDocument& doc) {
doc["path"] = this->get_sk_path();
JsonObject value = doc["value"].to<JsonObject>();
value["roll"] = output.roll;
value["pitch"] = output.pitch;
value["yaw"] = output.yaw;
}

} // namespace sensesp
26 changes: 26 additions & 0 deletions src/sensesp/signalk/signalk_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef SENSESP_SRC_SENSESP_SIGNALK_SIGNALK_TYPES_H_
#define SENSESP_SRC_SENSESP_SIGNALK_SIGNALK_TYPES_H_

#include <vector>

#include "sensesp/signalk/signalk_output.h"
#include "sensesp/types/position.h"

namespace sensesp {

template <>
void SKOutput<Position>::as_signalk_json(JsonDocument& doc);

template <>
void SKOutput<ENUVector>::as_signalk_json(JsonDocument& doc);

template <>
void SKOutput<AttitudeVector>::as_signalk_json(JsonDocument& doc);

typedef SKOutput<Position> SKOutputPosition;
typedef SKOutput<ENUVector> SKOutputENUVector;
typedef SKOutput<AttitudeVector> SKOutputAttitudeVector;

} // namespace sensesp

#endif
22 changes: 12 additions & 10 deletions src/sensesp/system/async_response_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <Arduino.h>
#include <elapsedMillis.h>

#include "sensesp_base_app.h"
#include "valueproducer.h"

namespace sensesp {
Expand Down Expand Up @@ -49,17 +50,18 @@ class AsyncResponseHandler : public ValueConsumer<bool>,
this->emit(status_);

if (timeout_event_ != nullptr) {
reactesp::EventLoop::app->remove(timeout_event_);
SensESPBaseApp::get_event_loop()->remove(timeout_event_);
timeout_event_ = nullptr;
}
timeout_event_ = reactesp::EventLoop::app->onDelay(timeout_, [this]() {
if (status_ == AsyncResponseStatus::kPending) {
ESP_LOGV("AsyncResponseHandler", "Timeout");
status_ = AsyncResponseStatus::kTimeout;
this->emit(status_);
}
this->timeout_event_ = nullptr;
});
timeout_event_ =
SensESPBaseApp::get_event_loop()->onDelay(timeout_, [this]() {
if (status_ == AsyncResponseStatus::kPending) {
ESP_LOGV("AsyncResponseHandler", "Timeout");
status_ = AsyncResponseStatus::kTimeout;
this->emit(status_);
}
this->timeout_event_ = nullptr;
});
}

void set(const bool& success) override {
Expand All @@ -72,7 +74,7 @@ class AsyncResponseHandler : public ValueConsumer<bool>,

// Clear the timeout event
if (timeout_event_ != nullptr) {
reactesp::EventLoop::app->remove(timeout_event_);
SensESPBaseApp::get_event_loop()->remove(timeout_event_);
timeout_event_ = nullptr;
}

Expand Down
47 changes: 25 additions & 22 deletions src/sensesp/system/stream_producer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "ReactESP.h"
#include "sensesp/system/valueproducer.h"
#include "sensesp_base_app.h"

namespace sensesp {

Expand All @@ -14,12 +15,13 @@ namespace sensesp {
class StreamCharProducer : public ValueProducer<char> {
public:
StreamCharProducer(Stream* stream) : stream_{stream} {
read_event_ = reactesp::EventLoop::app->onAvailable(*stream_, [this]() {
while (stream_->available()) {
char c = stream_->read();
this->emit(c);
}
});
read_event_ =
SensESPBaseApp::get_event_loop()->onAvailable(*stream_, [this]() {
while (stream_->available()) {
char c = stream_->read();
this->emit(c);
}
});
}

protected:
Expand All @@ -36,23 +38,24 @@ class StreamLineProducer : public ValueProducer<String> {
: stream_{stream}, max_line_length_{max_line_length} {
static int buf_pos = 0;
buf_ = new char[max_line_length_ + 1];
read_event_ = reactesp::EventLoop::app->onAvailable(*stream_, [this]() {
while (stream_->available()) {
char c = stream_->read();
if (c == '\n') {
// Include the newline character in the output
buf_[buf_pos++] = c;
buf_[buf_pos] = '\0';
this->emit(buf_);
buf_pos = 0;
} else {
buf_[buf_pos++] = c;
if (buf_pos >= max_line_length_ - 1) {
buf_pos = 0;
read_event_ =
SensESPBaseApp::get_event_loop()->onAvailable(*stream_, [this]() {
while (stream_->available()) {
char c = stream_->read();
if (c == '\n') {
// Include the newline character in the output
buf_[buf_pos++] = c;
buf_[buf_pos] = '\0';
this->emit(buf_);
buf_pos = 0;
} else {
buf_[buf_pos++] = c;
if (buf_pos >= max_line_length_ - 1) {
buf_pos = 0;
}
}
}
}
}
});
});
}

protected:
Expand Down
Loading
Loading