Loading [MathJax]/extensions/tex2jax.js
SensESP 3.1.0
Universal Signal K sensor toolkit ESP32
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
sensesp_app.h
Go to the documentation of this file.
1#ifndef SENSESP_APP_H_
2#define SENSESP_APP_H_
3
8#include "sensesp/net/ota.h"
20#include "sensesp_base_app.h"
21
22namespace sensesp {
23
24class SensESPApp;
25// I'd rather not have this global variable here but many legacy examples
26// access it. Use SensESPApp::get() instead.
27extern std::shared_ptr<SensESPApp> sensesp_app;
28
34class SensESPApp : public SensESPBaseApp {
35 public:
39 SensESPApp(SensESPApp& other) = delete;
40
44 void operator=(const SensESPApp&) = delete;
45
49 static std::shared_ptr<SensESPApp> get() {
50 if (instance_ == nullptr) {
51 instance_ = std::shared_ptr<SensESPApp>(new SensESPApp());
52 }
53 return std::static_pointer_cast<SensESPApp>(instance_);
54 }
55
56 virtual bool destroy() override {
57 bool outside_users = instance_.use_count() > 2;
58 if (outside_users) {
59 ESP_LOGW(
60 __FILENAME__,
61 "SensESPApp instance has active references and won't be properly "
62 "destroyed.");
63 }
64 instance_ = nullptr;
65 // Also destroy the global pointer
66 sensesp_app = nullptr;
67 return !outside_users;
68 }
69
70 // getters for internal members
71 std::shared_ptr<SKDeltaQueue> get_sk_delta() { return this->sk_delta_queue_; }
72 std::shared_ptr<SystemStatusController> get_system_status_controller() {
73 return this->system_status_controller_;
74 }
75 std::shared_ptr<Networking>& get_networking() { return this->networking_; }
76 std::shared_ptr<SKWSClient> get_ws_client() { return this->ws_client_; }
77
78 protected:
87
88 // setters for all constructor arguments
89
90 const SensESPApp* set_hostname(String hostname) {
91 this->SensESPBaseApp::set_hostname(hostname);
92 return this;
93 }
94 const SensESPApp* set_ssid(String ssid) {
95 this->ssid_ = ssid;
96 return this;
97 }
98 const SensESPApp* set_wifi_password(String wifi_password) {
99 this->wifi_client_password_ = wifi_password;
100 return this;
101 }
102 const SensESPApp* set_ap_ssid(const String& ssid) {
103 this->ap_ssid_ = ssid;
104 return this;
105 }
106 const SensESPApp* set_ap_password(const String& password) {
107 this->ap_password_ = password;
108 return this;
109 }
110 const SensESPApp* set_sk_server_address(String sk_server_address) {
111 this->sk_server_address_ = sk_server_address;
112 return this;
113 }
114 const SensESPApp* set_sk_server_port(uint16_t sk_server_port) {
115 this->sk_server_port_ = sk_server_port;
116 return this;
117 }
119 std::shared_ptr<SystemStatusLed>& system_status_led) {
120 this->system_status_led_ = system_status_led;
121 return this;
122 }
123 const SensESPApp* set_admin_user(const char* username, const char* password) {
124 this->http_server_->set_auth_credentials(username, password, true);
125 return this;
126 }
127 const SensESPApp* enable_ota(const char* password) {
128 ota_password_ = password;
129 return this;
130 }
131 const SensESPApp* set_button_pin(int pin) {
132 button_gpio_pin_ = pin;
133 return this;
134 }
135
143 void setup() {
144 // call the parent setup()
146
148
149 // create the networking object
150 networking_ = std::make_shared<Networking>("/System/WiFi Settings", ssid_,
153
155
156 if (ota_password_ != nullptr) {
157 // create the OTA object
158 ota_ = std::make_shared<OTA>(ota_password_);
159 }
160
161 bool captive_portal_enabled = networking_->is_captive_portal_enabled();
162
163 // create the HTTP server
164 this->http_server_ = std::make_shared<HTTPServer>();
165 this->http_server_->set_captive_portal(captive_portal_enabled);
166
167 // Add the default HTTP server response handlers
172
174
175 // create the SK delta queue
176 sk_delta_queue_ = std::make_shared<SKDeltaQueue>();
177
178 // create the websocket client
179 bool const use_mdns = sk_server_address_ == "";
180 this->ws_client_ = std::make_shared<SKWSClient>(
181 "/System/Signal K Settings", sk_delta_queue_, sk_server_address_,
182 sk_server_port_, use_mdns);
183
184 ConfigItem(this->ws_client_);
185
186 // connect the system status controller
187 this->networking_->get_wifi_state_producer()->connect_to(
188 &system_status_controller_->get_wifi_state_consumer());
189 this->ws_client_->connect_to(
190 &system_status_controller_->get_ws_connection_state_consumer());
191
192 // create the MDNS discovery object
193 mdns_discovery_ = std::make_shared<MDNSDiscovery>();
194
195 // create a system status led and connect it
196
197 if (system_status_led_ == nullptr) {
198#ifdef PIN_RGB_LED
199 system_status_led_ = std::make_shared<RGBSystemStatusLed>(PIN_RGB_LED);
200#elif defined(LED_BUILTIN)
201 system_status_led_ = std::make_shared<SystemStatusLed>(LED_BUILTIN);
202#endif
203 }
204 if (system_status_led_ != nullptr) {
205 this->system_status_controller_->connect_to(
206 system_status_led_->system_status_consumer_);
207 this->ws_client_->get_delta_tx_count_producer().connect_to(
208 system_status_led_->get_delta_tx_count_consumer());
209 }
210
211 // create the button handler
212 if (button_gpio_pin_ != -1) {
213 button_handler_ = std::make_shared<ButtonHandler>(button_gpio_pin_);
214 }
215
216 // connect status page items
218 }
219
220 // Collect metrics for the status page
222 this->hostname_->connect_to(&this->hostname_ui_output_);
223 this->event_loop_->onRepeat(4999, [this]() {
224 wifi_ssid_ui_output_.set(WiFi.SSID());
225 free_memory_ui_output_.set(ESP.getFreeHeap());
226 wifi_rssi_ui_output_.set(WiFi.RSSI());
227
228 // Uptime
229 uptime_ui_output_.set(millis() / 1000);
230
231 // Event loop queue sizes
232 int event_loop_queue_size = event_loop_->getEventQueueSize();
233 int event_loop_timed_queue_size = event_loop_->getTimedEventQueueSize();
234 int event_loop_untimed_queue_size =
235 event_loop_->getUntimedEventQueueSize();
236
237 // Total tick count
238 uint64_t current_tick_count = event_loop_->getTickCount();
239 total_tick_count_ui_output_.set(current_tick_count);
240
241 // Event counts
242 uint64_t current_event_count = event_loop_->getEventCount();
243 uint64_t current_timed_event_count = event_loop_->getTimedEventCount();
244 uint64_t current_untimed_event_count =
245 event_loop_->getUntimedEventCount();
246 event_count_ui_output_.set(current_event_count);
247 timed_event_count_ui_output_.set(current_timed_event_count);
248 untimed_event_count_ui_output_.set(current_untimed_event_count);
249
250 // Ticks and events per second during last interval
251 static uint64_t last_tick_count = 0;
252 static uint64_t last_event_count = 0;
253 static uint64_t last_timed_event_count = 0;
254 static uint64_t last_untimed_event_count = 0;
255 static uint64_t last_millis = 0;
256 uint64_t current_millis = millis();
257 float interval_seconds = (current_millis - last_millis) / 1000.0;
258
259 uint64_t ticks_diff = current_tick_count - last_tick_count;
260 uint64_t events_diff = current_event_count - last_event_count;
261 uint64_t timed_events_diff =
262 current_timed_event_count - last_timed_event_count;
263 uint64_t untimed_events_diff =
264 current_untimed_event_count - last_untimed_event_count;
265
266 // Set outputs
267 event_loop_queue_size_ui_output_.set(event_loop_queue_size);
268 event_loop_timed_queue_ui_output_.set(event_loop_timed_queue_size);
269 event_loop_untimed_queue_ui_output_.set(event_loop_untimed_queue_size);
271 event_loop_->getISREventQueueSize());
272
273 ticks_per_second_ui_output_.set(int(ticks_diff / interval_seconds));
274 events_per_second_ui_output_.set(int(events_diff / interval_seconds));
276 int(timed_events_diff / interval_seconds));
278 int(untimed_events_diff / interval_seconds));
279
280 // Update last values
281 last_tick_count = current_tick_count;
282 last_event_count = current_event_count;
283 last_timed_event_count = current_timed_event_count;
284 last_untimed_event_count = current_untimed_event_count;
285 last_millis = current_millis;
286
287 sk_server_address_ui_output_.set(ws_client_->get_server_address());
288 sk_server_port_ui_output_.set(ws_client_->get_server_port());
289 sk_server_connection_ui_output_.set(ws_client_->get_connection_status());
290 });
291 ws_client_->get_delta_tx_count_producer().connect_to(
293 ws_client_->get_delta_rx_count_producer().connect_to(
295 }
296
297 String ssid_ = "";
300 uint16_t sk_server_port_ = 0;
301 String ap_ssid_ = "";
302 String ap_password_ = "thisisfine";
303 const char* ota_password_ = nullptr;
304
305 std::shared_ptr<MDNSDiscovery> mdns_discovery_;
306 std::shared_ptr<HTTPServer> http_server_;
307
308 std::shared_ptr<BaseSystemStatusLed> system_status_led_;
309 std::shared_ptr<SystemStatusController> system_status_controller_ =
310 std::make_shared<SystemStatusController>();
312 std::shared_ptr<ButtonHandler> button_handler_;
313
314 std::shared_ptr<Networking> networking_;
315
316 std::shared_ptr<OTA> ota_;
317 std::shared_ptr<SKDeltaQueue> sk_delta_queue_;
318 std::shared_ptr<SKWSClient> ws_client_;
319
320 StatusPageItem<int> free_memory_ui_output_{"Free memory (bytes)", 0, "System",
321 1000};
322 StatusPageItem<int> uptime_ui_output_{"Uptime (s)", 0, "System", 1100};
323
324 StatusPageItem<String> hostname_ui_output_{"Hostname", "", "Network", 1200};
326 "MAC Address", WiFi.macAddress(), "Network", 1300};
327 StatusPageItem<String> wifi_ssid_ui_output_{"SSID", "", "Network", 1400};
328
329 StatusPageItem<int8_t> wifi_rssi_ui_output_{"WiFi signal strength (dB)", -128,
330 "Network", 1500};
331
333 "", "Signal K", 1600};
335 "Signal K", 1700};
337 "", "Signal K", 1800};
339 "Signal K", 1900};
341 "Signal K", 2000};
342
344 "Event Loop queue size", 0, "Event Loop Queues", 2100};
346 "Event Loop timed queue size", 0, "Event Loop Queues", 2200};
348 "Event Loop untimed queue size", 0, "Event Loop Queues", 2300};
350 "Event Loop interrupt queue size", 0, "Event Loop Queues", 2400};
351
353 "Total ticks processed", 0, "Event Loop Lifetime", 2500};
355 "Event Loop Lifetime", 2600};
357 "Timed events processed", 0, "Event Loop Lifetime", 2700};
359 "Untimed events processed", 0, "Event Loop Lifetime", 2800};
361 "Ticks per second", 0, "Event Loop Performance", 2900};
363 "Events per second", 0, "Event Loop Performance", 3000};
365 "Timed events per second", 0, "Event Loop Performance", 3100};
367 "Untimed events per second", 0, "Event Loop Performance", 3200};
368
370 "SenseESP version", kSensESPVersion, "Software", 3300};
372 "Build date", __DATE__ " " __TIME__, "Software", 3400};
373
374 // Placeholders for system status sensors in case they are created
375 std::shared_ptr<ValueProducer<float>> system_hz_sensor_;
376 std::shared_ptr<ValueProducer<uint32_t>> free_mem_sensor_;
377 std::shared_ptr<ValueProducer<float>> uptime_sensor_;
378 std::shared_ptr<ValueProducer<String>> ip_address_sensor_;
379 std::shared_ptr<ValueProducer<int>> wifi_signal_sensor_;
380
381 friend class WebServer;
382 friend class SensESPAppBuilder;
383};
384
385} // namespace sensesp
386
387#endif
void set(const T &value) override
A class for quickly configuring a SensESP application object before wiring up your sensors.
StatusPageItem< int > event_loop_interrupt_queue_ui_output_
const SensESPApp * set_sk_server_port(uint16_t sk_server_port)
StatusPageItem< String > sensesp_version_ui_output_
static std::shared_ptr< SensESPApp > get()
Get the singleton instance of the SensESPApp.
Definition sensesp_app.h:49
SensESPApp()
SensESPApp constructor.
Definition sensesp_app.h:86
void connect_status_page_items()
void operator=(const SensESPApp &)=delete
std::shared_ptr< SKDeltaQueue > sk_delta_queue_
const SensESPApp * set_hostname(String hostname)
Definition sensesp_app.h:90
StatusPageItem< uint64_t > timed_event_count_ui_output_
std::shared_ptr< HTTPServer > http_server_
StatusPageItem< String > wifi_ssid_ui_output_
const SensESPApp * set_sk_server_address(String sk_server_address)
StatusPageItem< float > ticks_per_second_ui_output_
StatusPageItem< int8_t > wifi_rssi_ui_output_
virtual bool destroy() override
Destroy the SensESPBaseApp instance.
Definition sensesp_app.h:56
void setup()
Perform initialization of SensESPApp once builder configuration is done.
std::shared_ptr< ValueProducer< String > > ip_address_sensor_
StatusPageItem< uint64_t > untimed_event_count_ui_output_
std::shared_ptr< ValueProducer< float > > uptime_sensor_
StatusPageItem< String > hostname_ui_output_
const SensESPApp * set_wifi_password(String wifi_password)
Definition sensesp_app.h:98
StatusPageItem< uint16_t > sk_server_port_ui_output_
StatusPageItem< String > sk_server_address_ui_output_
StatusPageItem< int > event_loop_timed_queue_ui_output_
std::shared_ptr< ValueProducer< uint32_t > > free_mem_sensor_
StatusPageItem< String > mac_address_ui_output_
std::shared_ptr< SKWSClient > get_ws_client()
Definition sensesp_app.h:76
const char * ota_password_
StatusPageItem< uint64_t > total_tick_count_ui_output_
std::shared_ptr< BaseSystemStatusLed > system_status_led_
StatusPageItem< int > uptime_ui_output_
std::shared_ptr< ValueProducer< int > > wifi_signal_sensor_
StatusPageItem< int > delta_rx_count_ui_output_
StatusPageItem< int > event_loop_untimed_queue_ui_output_
StatusPageItem< int > free_memory_ui_output_
StatusPageItem< float > timed_events_per_second_ui_output_
StatusPageItem< int > event_loop_queue_size_ui_output_
const SensESPApp * enable_ota(const char *password)
const SensESPApp * set_ap_password(const String &password)
const SensESPApp * set_admin_user(const char *username, const char *password)
std::shared_ptr< Networking > & get_networking()
Definition sensesp_app.h:75
friend class WebServer
StatusPageItem< uint64_t > event_count_ui_output_
std::shared_ptr< SystemStatusController > get_system_status_controller()
Definition sensesp_app.h:72
std::shared_ptr< SKWSClient > ws_client_
StatusPageItem< String > build_info_ui_output_
StatusPageItem< float > untimed_events_per_second_ui_output_
const SensESPApp * set_system_status_led(std::shared_ptr< SystemStatusLed > &system_status_led)
StatusPageItem< String > sk_server_connection_ui_output_
std::shared_ptr< SystemStatusController > system_status_controller_
std::shared_ptr< ButtonHandler > button_handler_
std::shared_ptr< MDNSDiscovery > mdns_discovery_
const SensESPApp * set_ap_ssid(const String &ssid)
const SensESPApp * set_ssid(String ssid)
Definition sensesp_app.h:94
StatusPageItem< float > events_per_second_ui_output_
std::shared_ptr< OTA > ota_
SensESPApp(SensESPApp &other)=delete
std::shared_ptr< SKDeltaQueue > get_sk_delta()
Definition sensesp_app.h:71
std::shared_ptr< Networking > networking_
StatusPageItem< int > delta_tx_count_ui_output_
std::shared_ptr< ValueProducer< float > > system_hz_sensor_
const SensESPApp * set_button_pin(int pin)
The base class for SensESP applications.
static String get_hostname()
Get the current hostname.
static std::shared_ptr< SensESPBaseApp > instance_
std::shared_ptr< reactesp::EventLoop > event_loop_
const SensESPBaseApp * set_hostname(String hostname)
std::shared_ptr< PersistingObservableValue< String > > hostname_
virtual void setup()
Perform initialization of SensESPBaseApp once builder configuration is done.
Item that renders its own value on the web UI status page.
void add_static_file_handlers(std::shared_ptr< HTTPServer > server)
Provide handlers for static web content.
void add_config_handlers(std::shared_ptr< HTTPServer > &server)
Handle HTTP requests to /config.
const char *const kSensESPVersion
std::shared_ptr< SensESPApp > sensesp_app
void add_app_http_command_handlers(std::shared_ptr< HTTPServer > &server, std::shared_ptr< Networking > &networking)
void add_base_app_http_command_handlers(std::shared_ptr< HTTPServer > &server)
std::shared_ptr< ConfigItemT< T > > ConfigItem(std::shared_ptr< T >)
Register a ConfigItemT with the ConfigItemBase.
#define BUTTON_BUILTIN