SensESP 3.6.1-alpha
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
wifi_watchdog.cpp
Go to the documentation of this file.
1#include "sensesp.h"
2
3#include "wifi_watchdog.h"
4
5#include <Arduino.h>
6
7namespace sensesp {
8
9namespace {
10
11constexpr unsigned int kPollIntervalMs = 1000;
12constexpr unsigned int kRestartDelayMs = 1000;
13
14} // namespace
15
17 std::shared_ptr<ValueProducer<NetworkState>> network_state, int timeout_s,
18 const String& config_path)
19 : FileSystemSaveable(config_path),
20 timeout_s_(timeout_s),
21 network_state_(network_state) {
22 load();
24 [this]() { on_network_state(network_state_->get()); });
25 poll_event_ = event_loop()->onRepeat(kPollIntervalMs, [this]() { poll(); });
26}
27
30 poll_event_->remove(event_loop());
31 if (restart_event_ != nullptr) {
32 restart_event_->remove(event_loop());
33 }
34}
35
38 policy_.set_link_state(link, millis());
39 if (link == WiFiWatchdogLink::kUp && restart_event_ != nullptr) {
40 ESP_LOGI(__FILENAME__, "Network is back; cancelling the restart.");
41 restart_event_->remove(event_loop());
42 restart_event_ = nullptr;
43 }
44}
45
47 if (restart_event_ != nullptr) {
48 return;
49 }
50 if (policy_.restart_due(millis(), timeout_ms())) {
51 ESP_LOGW(__FILENAME__, "Network down for more than %d s; restarting.",
53 restart_event_ = event_loop()->onDelay(kRestartDelayMs, [this]() {
54 restart_event_ = nullptr;
55 // The timeout may have been raised from the web UI meanwhile.
56 if (policy_.restart_due(millis(), timeout_ms())) {
57 ESP.restart();
58 }
59 });
60 }
61}
62
63bool WiFiWatchdog::to_json(JsonObject& root) {
64 root["timeout_s"] = timeout_s_;
65 return true;
66}
67
68bool WiFiWatchdog::from_json(const JsonObject& config) {
69 if (!config["timeout_s"].is<int>()) {
70 return false;
71 }
72 const int timeout_s = config["timeout_s"].as<int>();
73 if (!is_valid_timeout_s(timeout_s)) {
74 return false;
75 }
76 timeout_s_ = timeout_s;
77 return true;
78}
79
80const String ConfigSchema(const WiFiWatchdog& obj) {
81 String schema = R"json({"type":"object","properties":{"timeout_s":{"title":"Timeout (s)","description":"Restart the device after the network has been down for this long. Choose a value that outlasts an access point reboot (1 to {{max}} seconds).","type":"integer","minimum":1,"maximum":{{max}}}}})json";
82 schema.replace("{{max}}", String(WiFiWatchdog::kMaxTimeoutS));
83 return schema;
84}
85
86} // namespace sensesp
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
A base class for any sensor or piece of code that outputs a value for consumption elsewhere.
Restart the device after a prolonged network outage.
WiFiWatchdogPolicy policy_
static constexpr int kMaxTimeoutS
WiFiWatchdog(std::shared_ptr< ValueProducer< NetworkState > > network_state, int timeout_s, const String &config_path="/System/WiFi Watchdog")
void on_network_state(NetworkState state)
uint32_t timeout_ms() const
virtual bool to_json(JsonObject &root) override
static bool is_valid_timeout_s(int timeout_s)
reactesp::RepeatEvent * poll_event_
reactesp::DelayEvent * restart_event_
virtual bool from_json(const JsonObject &config) override
std::shared_ptr< ValueProducer< NetworkState > > network_state_
void set_link_state(WiFiWatchdogLink link, uint32_t now_ms)
bool restart_due(uint32_t now_ms, uint32_t timeout_ms) const
const String ConfigSchema(const SmartSwitchController &obj)
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9
WiFiWatchdogLink wifi_watchdog_link_from_state(WiFiState state)
Map a NetworkState (WiFiState) to the watchdog's view of the link.
WiFiWatchdogLink
Link state as seen by the WiFi watchdog.