SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
observablevalue.h
Go to the documentation of this file.
1#ifndef SENSESP_SYSTEM_OBSERVABLEVALUE_H
2#define SENSESP_SYSTEM_OBSERVABLEVALUE_H
3
4#include "saveable.h"
5#include "observable.h"
7#include "valueproducer.h"
8
9namespace sensesp {
10
11// forward declaration for the operator overloading functions
12template <class T>
13class ObservableValue;
14
15template <class T>
16bool operator==(ObservableValue<T> const& lhs, T const& rhs) {
17 return lhs.output_ == rhs;
18}
19
20template <class T>
21bool operator!=(ObservableValue<T> const& lhs, T const& rhs) {
22 return lhs.output_ != rhs;
23}
24
28template <class T>
29class ObservableValue : public ValueConsumer<T>, public ValueProducer<T> {
30 public:
31 ObservableValue() = default;
32
33 ObservableValue(const T& value)
34 : ValueConsumer<T>(), ValueProducer<T>(value) {}
35
36 // Delete copy constructor and assignment operator
39
40 void set(const T& value) override { this->ValueProducer<T>::emit(value); }
41
42 const T& operator=(const T& value) {
43 set(value);
44 return value;
45 }
46
48 set(this->output_ + 1);
49 return this->output_;
50 }
51
53 set(this->output_ - 1);
54 return this->output_;
55 }
56
57 T operator++(int) {
58 T old = this->output_;
59 set(this->output_ + 1);
60 return old;
61 }
62
63 T operator--(int) {
64 T old = this->output_;
65 set(this->output_ - 1);
66 return old;
67 }
68
69 const T& operator+=(const T& value) {
70 set(this->output_ + value);
71 return this->output_;
72 }
73
74 const T& operator-=(const T& value) {
75 set(this->output_ - value);
76 return this->output_;
77 }
78
79 protected:
80 template <class U>
81 friend bool operator==(ObservableValue<U> const& lhs, U const& rhs);
82 template <class U>
83 friend bool operator!=(ObservableValue<U> const& lhs, U const& rhs);
84};
85
94template <class T>
96 public FileSystemSaveable {
97 public:
98 PersistingObservableValue(const T& value, String config_path = "")
99 : ObservableValue<T>(value), FileSystemSaveable(config_path) {
100 load();
101
102 // Emit the current state as soon as the event loop starts
103 event_loop()->onDelay(0, [this]() { this->emit(this->output_); });
104 }
105
106 virtual void set(const T& value) override {
108 this->save();
109 }
110
111 virtual bool to_json(JsonObject& doc) override {
112 doc["value"] = this->output_;
113 return true;
114 }
115
116 virtual bool from_json(const JsonObject& config) override {
117 if (!config["value"].is<T>()) {
118 return false;
119 }
120 this->output_ = config["value"].as<T>();
121 return true;
122 }
123
124 protected:
125};
126
127template <class T>
129 String schema = R"({"type":"object","properties":{"value":{"title":"Value","type":"{{type_string}}"}}})";
130 const T value = obj.get();
131 schema.replace("{{type_string}}", get_schema_type_string(value));
132 return schema;
133}
134
135} // namespace sensesp
136
137#endif
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
virtual bool save() override
Save the object to a persistent storage.
Definition saveable.cpp:41
A value container that notifies its observers if it gets changed.
friend bool operator==(ObservableValue< U > const &lhs, U const &rhs)
ObservableValue(const ObservableValue &)=delete
const T & operator+=(const T &value)
void set(const T &value) override
friend bool operator!=(ObservableValue< U > const &lhs, U const &rhs)
ObservableValue(const T &value)
const T & operator=(const T &value)
const T & operator-=(const T &value)
ObservableValue & operator=(const ObservableValue &)=delete
An ObservableValue that saves its value to the configuration.
virtual bool from_json(const JsonObject &config) override
virtual void set(const T &value) override
PersistingObservableValue(const T &value, String config_path="")
virtual bool to_json(JsonObject &doc) override
A base class for piece of code (like a transform) that accepts data for input. ValueConsumers can acc...
A base class for any sensor or piece of code that outputs a value for consumption elsewhere.
virtual const T & get() const
void emit(const T &new_value)
const String ConfigSchema(const SmartSwitchController &obj)
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9
bool operator!=(ObservableValue< T > const &lhs, T const &rhs)
bool operator==(ObservableValue< T > const &lhs, T const &rhs)
const char * get_schema_type_string(const int)