SensESP 3.0.1
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 SENSESP_BUTTON_PIN
5// Default button pin is 0 (GPIO0), normally connected to the BOOT button
6#define SENSESP_BUTTON_PIN 0
7#endif
8
9#include "sensesp.h"
10
11#include <memory>
12
13#include "esp_log.h"
16
17namespace sensesp {
18
19constexpr auto kDefaultHostname = "SensESP";
20
21inline void SetupLogging(esp_log_level_t default_level = ESP_LOG_VERBOSE) {
22 esp_log_level_set("*", default_level);
23}
24
25inline void SetupSerialDebug(uint32_t baudrate) {
27
28 if (baudrate != 115200) {
29 ESP_LOGW(__FILENAME__, "SetupSerialDebug baudrate parameter is ignored.");
30 }
31}
32
43 protected:
44 std::shared_ptr<reactesp::EventLoop> event_loop_;
45
46 public:
50 static const std::shared_ptr<SensESPBaseApp>& get() { return instance_; }
51
55 virtual bool destroy() {
56 bool outside_users = instance_.use_count() > 1;
57
58 if (outside_users) {
59 ESP_LOGW(__FILENAME__,
60 "SensESPBaseApp instance has active references and won't be "
61 "properly destroyed.");
62 }
63 instance_ = nullptr;
64 return !outside_users;
65 }
66
71 virtual void start() {
72 ESP_LOGW(__FILENAME__, "start() call is deprecated and can be removed.");
73 }
74
79 virtual void reset() {
80 ESP_LOGW(__FILENAME__,
81 "Resetting the device configuration to system defaults.");
83
84 this->event_loop_->onDelay(1000, []() {
85 ESP.restart();
86 delay(1000);
87 });
88 }
89
95 std::shared_ptr<ObservableValue<String>> get_hostname_observable() {
96 return std::static_pointer_cast<ObservableValue<String>>(hostname_);
97 }
98
104 static String get_hostname() {
105 return SensESPBaseApp::get()->get_hostname_observable().get()->get();
106 }
107
113 static std::shared_ptr<reactesp::EventLoop> get_event_loop() {
114 return SensESPBaseApp::get()->event_loop_;
115 }
116
117 protected:
126 : filesystem_{std::make_shared<Filesystem>()},
127 event_loop_{std::make_shared<reactesp::EventLoop>()} {
128 // Instance is now set by the builder
129 }
130
131 ~SensESPBaseApp() { instance_ = nullptr; }
132
134 hostname_ = std::make_shared<PersistingObservableValue<String>>(
135 kDefaultHostname, "/system/hostname");
136 ConfigItem(hostname_); // Make hostname configurable
137 }
138
146 virtual void setup() {
147 if (!hostname_) {
149 }
150 }
151
152 static std::shared_ptr<SensESPBaseApp> instance_;
153
154 void set_instance(const std::shared_ptr<SensESPBaseApp>& instance) {
155 instance_ = instance;
156 }
157
158 std::shared_ptr<PersistingObservableValue<String>> hostname_;
159
160 std::shared_ptr<Filesystem> filesystem_;
161
162 const SensESPBaseApp* set_hostname(String hostname) {
163 if (!hostname_) {
165 }
166 hostname_->set(hostname);
167 return this;
168 }
169};
170
171} // namespace sensesp
172
173#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 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_VERBOSE)