SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
signalk_output.h
Go to the documentation of this file.
1#ifndef SENSESP_SIGNALK_SIGNALK_OUTPUT_H_
2#define SENSESP_SIGNALK_SIGNALK_OUTPUT_H_
3
4#include <memory>
5
8#include "signalk_emitter.h"
9
10namespace sensesp {
11
16template <typename T>
17class SKOutput : public SKEmitter, public SymmetricTransform<T> {
18 public:
19 SKOutput() : SKOutput("") { this->load(); }
20
34 SKOutput(String sk_path, String config_path = "", SKMetadata* meta = nullptr)
35 : SKOutput(sk_path, config_path, std::shared_ptr<SKMetadata>(meta)) {}
36
37 SKOutput(String sk_path, String config_path, std::shared_ptr<SKMetadata> meta)
38 : SKEmitter(sk_path), SymmetricTransform<T>(config_path), meta_{meta} {
39 this->load();
40 }
41
42 SKOutput(String sk_path, SKMetadata* meta) : SKOutput(sk_path, "", meta) {}
43
44 virtual void set(const T& new_value) override {
45 this->ValueProducer<T>::emit(new_value);
46 }
47
48 virtual void as_signalk_json(JsonDocument& doc) override {
49 doc["path"] = this->get_sk_path();
50 doc["value"] = ValueProducer<T>::output_;
51 }
52
53 virtual bool to_json(JsonObject& root) override {
54 root["sk_path"] = this->get_sk_path();
55 return true;
56 }
57
58 virtual bool from_json(const JsonObject& config) override {
59 if (!config["sk_path"].is<String>()) {
60 return false;
61 }
62 this->set_sk_path(config["sk_path"].as<String>());
63 return true;
64 }
65
72 virtual void set_metadata(SKMetadata* meta) {
73 this->meta_ = std::make_shared<SKMetadata>(*meta);
74 }
75
76 virtual SKMetadata* get_metadata() override { return meta_.get(); }
77
78 protected:
79 std::shared_ptr<SKMetadata> meta_;
80};
81
82template <typename T>
83const String ConfigSchema(const SKOutput<T>& obj) {
84 return R"({"type":"object","properties":{"sk_path":{"title":"Signal K Path","type":"string"}} })";
85}
86
87template <typename T>
89 return true;
90}
91
95class SKOutputRawJson : public SKOutput<String> {
96 public:
97 SKOutputRawJson(String sk_path, String config_path = "",
98 SKMetadata* meta = NULL)
99 : SKOutput<String>(sk_path, config_path, meta) {}
100
101 virtual void as_signalk_json(JsonDocument& doc) override {
102 doc["path"] = this->get_sk_path();
103 doc["value"] = serialized(ValueProducer<String>::output_);
104 }
105};
106
111template <typename T>
112class SKOutputNumeric : public SKOutput<T> {
113 public:
114 SKOutputNumeric(String sk_path, String config_path = "",
115 SKMetadata* meta = nullptr);
116
117 SKOutputNumeric(String sk_path, SKMetadata* meta)
118 : SKOutputNumeric(sk_path, "", meta) {}
119
129 SKOutputNumeric(String sk_path, String config_path, String units)
130 : SKOutputNumeric(sk_path, config_path, new SKMetadata(units)) {}
131};
132
137
138} // namespace sensesp
139
140#endif
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
A class that produces Signal K output to be forwarded to the Signal K server (if the system is connec...
void set_sk_path(const String &path)
Holds Signal K meta data that is associated with the sk_path an SKEmitter class may optionally send t...
A specialized transform whose primary purpose is to output Signal K data on the Signal K network.
virtual void set_metadata(SKMetadata *meta)
virtual bool from_json(const JsonObject &config) override
virtual SKMetadata * get_metadata() override
std::shared_ptr< SKMetadata > meta_
virtual void as_signalk_json(JsonDocument &doc) override
SKOutput(String sk_path, String config_path, std::shared_ptr< SKMetadata > meta)
virtual bool to_json(JsonObject &root) override
SKOutput(String sk_path, String config_path="", SKMetadata *meta=nullptr)
virtual void set(const T &new_value) override
SKOutput(String sk_path, SKMetadata *meta)
A special class for sending numeric values to the Signal K server on a specific Signal K path.
SKOutputNumeric(String sk_path, String config_path, String units)
SKOutputNumeric(String sk_path, SKMetadata *meta)
SKOutputNumeric(String sk_path, String config_path="", SKMetadata *meta=nullptr)
Class for sending raw Json strings on a specific Signal K path.
virtual void as_signalk_json(JsonDocument &doc) override
SKOutputRawJson(String sk_path, String config_path="", SKMetadata *meta=NULL)
A common type of transform that consumes, transforms, then outputs values of the same data type.
Definition transform.h:94
A base class for any sensor or piece of code that outputs a value for consumption elsewhere.
void emit(const T &new_value)
const String ConfigSchema(const SmartSwitchController &obj)
SKOutput< String > SKOutputString
SKOutputNumeric< int > SKOutputInt
SKOutputNumeric< float > SKOutputFloat
bool ConfigRequiresRestart(const HTTPServer &obj)
SKOutput< bool > SKOutputBool