SensESP 3.4.1-alpha
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
65 static void clear_registries();
66
70 virtual bool destroy() {
71 bool outside_users = instance_.use_count() > 1;
72
73 if (outside_users) {
74 ESP_LOGW(__FILENAME__,
75 "SensESPBaseApp instance has active references and won't be "
76 "properly destroyed.");
77 } else {
78 // Only clear the registries when the app is actually being torn down.
79 // If outside references survive, the registered objects survive too, and
80 // clearing would strand live objects (they register only in their
81 // constructors, so they would never re-appear).
83 }
84 instance_ = nullptr;
85 return !outside_users;
86 }
87
92 virtual void start() {
93 ESP_LOGW(__FILENAME__, "start() call is deprecated and can be removed.");
94 }
95
100 virtual void reset() {
101 ESP_LOGW(__FILENAME__,
102 "Resetting the device configuration to system defaults.");
104
105 this->event_loop_->onDelay(1000, []() {
106 ESP.restart();
107 delay(1000);
108 });
109 }
110
116 std::shared_ptr<ObservableValue<String>> get_hostname_observable() {
117 return std::static_pointer_cast<ObservableValue<String>>(hostname_);
118 }
119
125 static String get_hostname() {
126 return SensESPBaseApp::get()->get_hostname_observable().get()->get();
127 }
128
134 static std::shared_ptr<reactesp::EventLoop> get_event_loop() {
135 return SensESPBaseApp::get()->event_loop_;
136 }
137
138 protected:
147 : filesystem_{std::make_shared<Filesystem>()},
148 event_loop_{std::make_shared<reactesp::EventLoop>()} {
149 // Instance is now set by the builder
150 }
151
152 ~SensESPBaseApp() { instance_ = nullptr; }
153
154 void init_hostname(const String& default_hostname = kDefaultHostname) {
155 hostname_ = std::make_shared<PersistingObservableValue<String>>(
156 default_hostname, "/system/hostname");
158 }
159
167 virtual void setup() {
168 if (!hostname_) {
170 }
171 }
172
173 static std::shared_ptr<SensESPBaseApp> instance_;
174
175 void set_instance(const std::shared_ptr<SensESPBaseApp>& instance) {
176 instance_ = instance;
177 }
178
179 std::shared_ptr<PersistingObservableValue<String>> hostname_;
180
181 std::shared_ptr<Filesystem> filesystem_;
182
183 const SensESPBaseApp* set_hostname(String hostname) {
184 // Pass the builder's hostname as the default for the
185 // PersistingObservableValue constructor. If a user has
186 // configured a different hostname via the web UI, load()
187 // in the constructor will override this default — no
188 // special-case guard needed.
189 if (!hostname_) {
190 init_hostname(hostname);
191 }
192 return this;
193 }
194};
195
196} // namespace sensesp
197
198#endif
static void reset_all()
The base class for SensESP applications.
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 void clear_registries()
Clear all static object registries.
static std::shared_ptr< SensESPBaseApp > instance_
void init_hostname(const String &default_hostname=kDefaultHostname)
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)