Skip to content

Commit

Permalink
After data loss, try reconnecting 2 times, after that reset the whole…
Browse files Browse the repository at this point in the history
… device
  • Loading branch information
Jan Dvořák committed Dec 20, 2018
1 parent 2de0957 commit 9afa1fc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/data_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,14 @@ void DataSource::loop()
}

if (m_connected && (millis() - m_last_data_received_at > c_no_data_reconnect_interval)) {
DEBUG_SERIAL.printf_P(PSTR("[WSc] No data received for %i secs, forcing reconnect\n"), c_no_data_reconnect_interval / 1000);
reconnect();
if (++m_num_connection_tries < 2) {
DEBUG_SERIAL.printf_P(PSTR("[WSc] No data received for %i secs, forcing reconnect\n"), c_no_data_reconnect_interval / 1000);
reconnect();
} else {
DEBUG_SERIAL.println(F("[WSc] Max reconnection attempts, forcing restart"));
ESP.restart();
}

}

// send heartbeat
Expand Down Expand Up @@ -241,6 +247,7 @@ void DataSource::callback(WStype_t type, uint8_t * payload, size_t length)
m_connected = true;
m_last_connected_at = millis();
m_last_data_received_at = millis();
m_num_connection_tries = 0;
if (payload==nullptr)
DEBUG_SERIAL.printf_P(PSTR("[WSc] Connected to url: <nullptr>\n"));
else
Expand Down
5 changes: 4 additions & 1 deletion src/data_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ class DataSource
m_should_send_hello(false), m_hello_sent(true), m_last_heartbeat_sent_at(0),
m_last_data_received_at(0), m_text_last_sent_at(0),
m_on_price_change(nullptr), m_on_price_ath(nullptr), m_on_update_request(nullptr), m_on_announcement(nullptr),
m_on_countdown(nullptr), m_on_sound(nullptr), m_on_otp(nullptr), m_on_otp_ack(nullptr), m_on_price_timeout_set(nullptr), m_on_new_settings(nullptr)
m_on_countdown(nullptr), m_on_sound(nullptr), m_on_otp(nullptr), m_on_otp_ack(nullptr), m_on_price_timeout_set(nullptr),
m_on_new_settings(nullptr), m_num_connection_tries(0)

{
m_websocket.onEvent(DataSource::s_callback);
}
Expand Down Expand Up @@ -110,4 +112,5 @@ class DataSource
on_new_settings_t m_on_new_settings;
WebSocketsClient m_websocket;
std::queue<String> m_send_queue;
int m_num_connection_tries;
};

0 comments on commit 9afa1fc

Please sign in to comment.