SensESP 3.3.0
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
sensesp_base_app.h
Go to the documentation of this file.
1#ifndef SENSESP_BASE_APP_H_
2#define SENSESP_BASE_APP_H_
3
4#ifndef BUTTON_BUILTIN
5// Default button pin on ESP32-DevKitC devices is 0 (GPIO0),
6// normally connected to the BOOT button.
7#define BUTTON_BUILTIN 0
8#endif
9
10#include "sensesp.h"
11
12#include <memory>
13
14#include "esp_log.h"
17
18namespace sensesp {
19
20constexpr auto kDefaultHostname = "SensESP";
21
22inline void SetupLogging(esp_log_level_t default_level = ESP_LOG_DEBUG) {
23 esp_log_level_set("*", default_level);
24}
25
26inline void SetupSerialDebug(uint32_t baudrate) {
28
29 if (baudrate != 115200) {
30 ESP_LOGW(__FILENAME__, "SetupSerialDebug baudrate parameter is ignored.");
31 }
32}
33
43// WARNING: This class uses a shared_ptr singleton pattern. Only one instance
44// should exist at a time. Constructing multiple instances or calling get()
45// before the builder sets up the instance will produce unexpected behavior.
46// See GitHub issue #900 for planned improvements.
48 protected:
49 std::shared_ptr<reactesp::EventLoop> event_loop_;
50
51 public:
55 static const std::shared_ptr<SensESPBaseApp>& get() { return instance_; }
56
60 virtual bool destroy() {
61 bool outside_users = instance_.use_count() > 1;
62
63 if (outside_users) {
64 ESP_LOGW(__FILENAME__,
65 "SensESPBaseApp instance has active references and won't be "
66 "properly destroyed.");
67 }
68 instance_ = nullptr;
69 return !outside_users;
70 }
71
76 virtual void start() {
77 ESP_LOGW(__FILENAME__, "start() call is deprecated and can be removed.");
78 }
79
84 virtual void reset() {
85 ESP_LOGW(__FILENAME__,
86 "Resetting the device configuration to system defaults.");
88
89 this->event_loop_->onDelay(1000, []() {
90 ESP.restart();
91 delay(1000);
92 });
93 }
94
100 std::shared_ptr<ObservableValue<String>> get_hostname_observable() {
101 return std::static_pointer_cast<ObservableValue<String>>(hostname_);
102 }
103
109 static String get_hostname() {
110 return SensESPBaseApp::get()->get_hostname_observable().get()->get();
111 }
112
118 static std::shared_ptr<reactesp::EventLoop> get_event_loop() {
119 return SensESPBaseApp::get()->event_loop_;
120 }
121
122 protected:
131 : filesystem_{std::make_shared<Filesystem>()},
132 event_loop_{std::make_shared<reactesp::EventLoop>()} {
133 // Instance is now set by the builder
134 }
135
136 ~SensESPBaseApp() { instance_ = nullptr; }
137
139 hostname_ = std::make_shared<PersistingObservableValue<String>>(
140 kDefaultHostname, "/system/hostname");
141 ConfigItem(hostname_); // Make hostname configurable
142 }
143
151 virtual void setup() {
152 if (!hostname_) {
154 }
155 }
156
157 static std::shared_ptr<SensESPBaseApp> instance_;
158
159 void set_instance(const std::shared_ptr<SensESPBaseApp>& instance) {
160 instance_ = instance;
161 }
162
163 std::shared_ptr<PersistingObservableValue<String>> hostname_;
164
165 std::shared_ptr<Filesystem> filesystem_;
166
167 const SensESPBaseApp* set_hostname(String hostname) {
168 if (!hostname_) {
170 }
171 hostname_->set(hostname);
172 return this;
173 }
174};
175
176} // namespace sensesp
177
178#endif
static void reset_all()
static String get_hostname()
Get the current hostname.
static std::shared_ptr< reactesp::EventLoop > get_event_loop()
Get the event loop object from the singleton SensESPBaseApp instance.
static std::shared_ptr< SensESPBaseApp > instance_
std::shared_ptr< reactesp::EventLoop > event_loop_
void set_instance(const std::shared_ptr< SensESPBaseApp > &instance)
const SensESPBaseApp * set_hostname(String hostname)
std::shared_ptr< PersistingObservableValue< String > > hostname_
virtual void start()
Start the app (activate all the subcomponents).
std::shared_ptr< ObservableValue< String > > get_hostname_observable()
Get the hostname observable object.
virtual void setup()
Perform initialization of SensESPBaseApp once builder configuration is done.
SensESPBaseApp()
Construct a new SensESP Base App object.
virtual void reset()
Reset the device to factory defaults.
virtual bool destroy()
Destroy the SensESPBaseApp instance.
std::shared_ptr< Filesystem > filesystem_
static const std::shared_ptr< SensESPBaseApp > & get()
Get the singleton instance of the SensESPBaseApp.
constexpr auto kDefaultHostname
void SetupSerialDebug(uint32_t baudrate)
std::shared_ptr< ConfigItemT< T > > ConfigItem(std::shared_ptr< T >)
Register a ConfigItemT with the ConfigItemBase.
void SetupLogging(esp_log_level_t default_level=ESP_LOG_DEBUG)