SensESP 3.6.1-alpha
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
sensesp_app_builder.h
Go to the documentation of this file.
1#ifndef SENSESP_APP_BUILDER_H
2#define SENSESP_APP_BUILDER_H
3
4#include <memory>
5
6#include "Arduino.h"
11#include "sensesp_app.h"
13
14namespace sensesp {
15
16constexpr const char* kDefaultSystemInfoSensorPrefix = "sensorDevice.";
17
23 private:
24 String hostname_ = "SensESP";
25 String ssid_ = "";
26 String password_ = "";
27 String sk_server_address_ = "";
28 uint16_t sk_server_port_ = 0;
29
30 protected:
31 std::shared_ptr<SensESPApp> app_;
32
33 public:
41 app_ = std::shared_ptr<SensESPApp>(new SensESPApp());
42 app_->set_instance(app_);
43 }
54 SensESPAppBuilder* set_wifi_client(String ssid, String password) {
55 app_->set_ssid(ssid);
56 app_->set_wifi_password(password);
57 return this;
58 }
59
60 [[deprecated("Use set_wifi_client instead")]]
61 SensESPAppBuilder* set_wifi(String ssid, String password) {
62 return set_wifi_client(ssid, password);
63 }
64
83 const std::vector<ClientSSIDConfig>& wifi_client_configs) {
84 app_->set_wifi_clients(wifi_client_configs);
85 return this;
86 }
87
98 const String& password) {
99 app_->set_ap_ssid(ssid);
100 app_->set_ap_password(password);
101 return this;
102 }
103
117 app_->set_ssid("");
118 app_->set_wifi_password("");
119 app_->set_ap_ssid("");
120 app_->set_ap_password("");
121 return this;
122 }
123
150 template <typename ProvisionerConfigT>
151 SensESPAppBuilder* set_ethernet(ProvisionerConfigT config) {
152 disable_wifi();
153 app_->set_network_provisioner_factory(
154 [config]() -> std::shared_ptr<NetworkProvisioner> {
155 return std::make_shared<typename ProvisionerConfigT::ProvisionerType>(
156 config);
157 });
158 return this;
159 }
160
170 SensESPAppBuilder* set_sk_server(String address, uint16_t port) {
171 app_->set_sk_server_address(address);
172 app_->set_sk_server_port(port);
173 return this;
174 }
183 SensESPAppBuilder* set_hostname(String hostname) override final {
184 app_->set_hostname(hostname);
185 return this;
186 }
191 SensESPAppBuilder* set_admin_user(const char* username,
192 const char* password) {
193 app_->set_admin_user(username, password);
194 return this;
195 }
196
206 std::shared_ptr<SystemStatusLed> system_status_led) {
207 app_->set_system_status_led(system_status_led);
208 return this;
209 }
220 String prefix = kDefaultSystemInfoSensorPrefix) {
221 auto sensor = std::make_shared<SystemHz>();
222 auto vproducer = std::static_pointer_cast<ValueProducer<float>>(sensor);
223 // We need to store the sensor in the app so it doesn't get garbage
224 // collected.
225 app_->system_hz_sensor_ = vproducer;
226 connect_system_info_sensor(vproducer, prefix, "systemHz");
227 return this;
228 }
236 String prefix = kDefaultSystemInfoSensorPrefix) {
237 auto sensor = std::make_shared<FreeMem>();
238 auto vproducer = std::static_pointer_cast<ValueProducer<uint32_t>>(sensor);
239 // We need to store the sensor in the app so it doesn't get garbage
240 // collected.
241 app_->free_mem_sensor_ = vproducer;
242 connect_system_info_sensor(vproducer, prefix, "freeMemory");
243 return this;
244 }
252 String prefix = kDefaultSystemInfoSensorPrefix) {
253 auto sensor = std::make_shared<Uptime>();
254 auto vproducer = std::static_pointer_cast<ValueProducer<float>>(sensor);
255 // We need to store the sensor in the app so it doesn't get garbage
256 // collected.
257 app_->uptime_sensor_ = vproducer;
258 connect_system_info_sensor(vproducer, prefix, "uptime");
259 return this;
260 }
268 String prefix = kDefaultSystemInfoSensorPrefix) {
269 auto sensor = std::make_shared<IPAddrDev>();
270 auto vproducer = std::static_pointer_cast<ValueProducer<String>>(sensor);
271 // We need to store the sensor in the app so it doesn't get garbage
272 // collected.
273 app_->ip_address_sensor_ = vproducer;
274 connect_system_info_sensor(vproducer, prefix, "ipAddress");
275 return this;
276 }
284 String prefix = kDefaultSystemInfoSensorPrefix) {
285 auto sensor = std::make_shared<WiFiSignal>();
286 auto vproducer = std::static_pointer_cast<ValueProducer<int>>(sensor);
287 // We need to store the sensor in the app so it doesn't get garbage
288 // collected.
289 app_->wifi_signal_sensor_ = vproducer;
290 connect_system_info_sensor(vproducer, prefix, "wifiSignalLevel");
291 return this;
292 }
293
300 app_->set_button_pin(pin);
301 return this;
302 }
303
311 String prefix = kDefaultSystemInfoSensorPrefix) {
312 this->enable_system_hz_sensor(prefix);
313 this->enable_free_mem_sensor(prefix);
314 this->enable_uptime_sensor(prefix);
315 this->enable_ip_address_sensor(prefix);
316 this->enable_wifi_signal_sensor(prefix);
317 return this;
318 }
319
326 SensESPAppBuilder* enable_ota(const char* password) {
327 app_->enable_ota(password);
328 return this;
329 }
330
341 [[deprecated("Use set_wifi_access_point instead")]]
343 app_->set_ap_password(password);
344 return this;
345 }
346
365 if (!WiFiWatchdog::is_valid_timeout_s(timeout_s)) {
366 ESP_LOGE(__FILENAME__,
367 "WiFi watchdog timeout %d s is out of range [1, %d]; watchdog "
368 "not enabled.",
369 timeout_s, WiFiWatchdog::kMaxTimeoutS);
370 return this;
371 }
372 if (app_->network_state_producer_) {
373 ESP_LOGE(__FILENAME__,
374 "enable_wifi_watchdog() must be called before get_app(); "
375 "watchdog not enabled.");
376 return this;
377 }
378 app_->wifi_watchdog_timeout_s_ = timeout_s;
379 return this;
380 }
381
389 std::shared_ptr<SensESPApp> get_app() {
391 app_->setup();
392 return app_;
393 }
394};
395
396} // namespace sensesp
397
398#endif
A class for quickly configuring a SensESP application object before wiring up your sensors.
SensESPAppBuilder * set_wifi_clients(const std::vector< ClientSSIDConfig > &wifi_client_configs)
Set multiple Wi-Fi client (STA) networks for failover.
SensESPAppBuilder()
Construct a new SensESPApp Builder object.
SensESPAppBuilder * set_admin_user(const char *username, const char *password)
Set admin username and password for the web interface.
SensESPAppBuilder * set_hostname(String hostname) override final
Set the device hostname.
SensESPAppBuilder * disable_wifi()
Clear the default WiFi STA/AP settings.
SensESPAppBuilder * enable_wifi_watchdog(int timeout_s=180)
Restart the device after a prolonged network outage.
SensESPAppBuilder * set_ethernet(ProvisionerConfigT config)
Configure the app to use a non-WiFi NetworkProvisioner.
SensESPAppBuilder * set_system_status_led(std::shared_ptr< SystemStatusLed > system_status_led)
Set the system status led object.
SensESPAppBuilder * set_sk_server(String address, uint16_t port)
Set the Signal K server address and port.
SensESPAppBuilder * set_wifi(String ssid, String password)
std::shared_ptr< SensESPApp > app_
SensESPAppBuilder * set_wifi_client(String ssid, String password)
Set the Wi-Fi network SSID and password.
SensESPAppBuilder * set_wifi_manager_password(const char *password)
Set the wifi manager password.
SensESPAppBuilder * enable_ip_address_sensor(String prefix=kDefaultSystemInfoSensorPrefix)
Report the IP address of the device.
SensESPAppBuilder * enable_free_mem_sensor(String prefix=kDefaultSystemInfoSensorPrefix)
Enable the free memory sensor.
SensESPAppBuilder * set_wifi_access_point(const String &ssid, const String &password)
Set the wifi access point object SSID and password.
SensESPAppBuilder * set_button_pin(int pin)
Set the button GPIO pin.
std::shared_ptr< SensESPApp > get_app()
Get the SensESPApp object.
SensESPAppBuilder * enable_system_hz_sensor(String prefix=kDefaultSystemInfoSensorPrefix)
Enable the System Hz sensor.
SensESPAppBuilder * enable_system_info_sensors(String prefix=kDefaultSystemInfoSensorPrefix)
Enable all built-in system info sensors.
SensESPAppBuilder * enable_uptime_sensor(String prefix=kDefaultSystemInfoSensorPrefix)
Report the system uptime in seconds since the last reboot.
SensESPAppBuilder * enable_wifi_signal_sensor(String prefix=kDefaultSystemInfoSensorPrefix)
Report the Wi-Fi signal strength.
SensESPAppBuilder * enable_ota(const char *password)
Enable over-the-air updates for the device.
static std::shared_ptr< SensESPApp > get()
Get the singleton instance of the SensESPApp.
Definition sensesp_app.h:58
Abstract base class for other SensESP*AppBuilder classes.
static constexpr int kMaxTimeoutS
static bool is_valid_timeout_s(int timeout_s)
constexpr const char * kDefaultSystemInfoSensorPrefix
void connect_system_info_sensor(std::shared_ptr< ValueProducer< T > > &sensor, const String &prefix, const String &name)
Connect a system information sensor to SKOutput.
Definition system_info.h:18