SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
time_counter.h
Go to the documentation of this file.
1#ifndef SENSESP_SRC_TRANSFORMS_TIME_COUNTER_H_
2#define SENSESP_SRC_TRANSFORMS_TIME_COUNTER_H_
3
6
7namespace sensesp {
8
9inline uint64_t ARDUINO_ISR_ATTR millis64() { return esp_timer_get_time() / 1000ULL; }
10
21template <typename T>
22class TimeCounter : public Transform<T, double> {
23 public:
24 TimeCounter(String config_path) : Transform<T, double>(config_path) {
25 this->load();
26 }
27
28 virtual void set(const T& input) override {
29 if (!initialized_) {
30 // Initialize the previous state
31 initialized_ = true;
32 previous_state_ = (bool)input;
35 }
36
37 // if previous_state_ is true, accumulate duration
38 if (previous_state_) {
40 }
41
42 if (input) {
43 if (previous_state_ == 0) {
44 // State change from false to true
48 this->save(); // Save configuration to flash, so that
49 // the duration is persistent
50 }
51 } else {
52 if (previous_state_ == 1) {
53 // State change from true to false
56 this->save(); // Save configuration to flash, so that
57 // the duration is persistent
58 }
59 }
60 this->emit((double)duration_ms_ / 1000.);
61 }
62
63 virtual bool to_json(JsonObject& root) override {
64 root["duration_s"] = duration_ms_ / 1000.; // convert to seconds
65 return true;
66 }
67
68 virtual bool from_json(const JsonObject& config) override {
69 ESP_LOGD(__FILENAME__, "Setting TimeCounter configuration");
70 if (config["duration_s"].is<double>()) {
72 config["duration_s"].as<double>() * 1000; // convert to milliseconds
73 } else if (!config["duration"].is<double>()) {
74 // If the duration is not in seconds, try to read it in milliseconds
75 duration_at_start_ms_ = config["duration"];
76 } else {
77 return false;
78 }
79
81 ESP_LOGD(__FILENAME__, "duration_at_start_ms_ = %ld",
83 return true;
84 }
85
86 protected:
87 bool initialized_ = false;
88 bool previous_state_ = false;
90 uint64_t duration_ms_ = 0;
92};
93
94template <typename U>
95const String ConfigSchema(TimeCounter<U>& obj) {
96 return R"({"type":"object","properties":{"duration_s":{"type":"number","displayMultiplier":0.0002777777777777778,"title":"Total Duration [hours]"}},"required":["duration_s"]})";
97}
98
99} // namespace sensesp
100
101#endif // SENSESP_TRANSFORMS_TIME_COUNTER_H_
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 transform that outputs the duration of the input value being true or non-null.
virtual bool from_json(const JsonObject &config) override
uint64_t duration_at_start_ms_
virtual bool to_json(JsonObject &root) override
TimeCounter(String config_path)
virtual void set(const T &input) override
The main Transform class. A transform is identified primarily by the type of value that is produces (...
Definition transform.h:53
void emit(const double &new_value)
const String ConfigSchema(const SmartSwitchController &obj)
uint64_t ARDUINO_ISR_ATTR millis64()
Definition time_counter.h:9