Skip to content

Managed exit cases for GETTIME state. Removed IDLE state. #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/ConnectionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <Client.h>

enum NetworkConnectionState {
CONNECTION_STATE_IDLE,
CONNECTION_STATE_INIT,
CONNECTION_STATE_CONNECTING,
CONNECTION_STATE_CONNECTED,
Expand All @@ -30,7 +29,7 @@ class ConnectionManager {

protected:
unsigned long lastValidTimestamp = 0;
NetworkConnectionState netConnectionState = CONNECTION_STATE_IDLE;
NetworkConnectionState netConnectionState = CONNECTION_STATE_INIT;

};

Expand Down
20 changes: 13 additions & 7 deletions src/WiFiConnectionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class WiFiConnectionManager : public ConnectionManager {

void changeConnectionState(NetworkConnectionState _newState);

const int CHECK_INTERVAL_IDLE = 100;
const int CHECK_INTERVAL_INIT = 100;
const int CHECK_INTERVAL_CONNECTING = 500;
const int CHECK_INTERVAL_GETTIME = 100;
Expand All @@ -24,8 +23,12 @@ class WiFiConnectionManager : public ConnectionManager {
const int CHECK_INTERVAL_DISCONNECTED = 1000;
const int CHECK_INTERVAL_ERROR = 500;

const int MAX_GETTIME_RETRY = 30;

const char *ssid, *pass;
unsigned long lastConnectionTickTime, lastNetworkStep;
unsigned long getTimeRetries;

WiFiClient wifiClient;
int connectionTickTimeInterval;
};
Expand All @@ -35,7 +38,8 @@ static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000;
WiFiConnectionManager::WiFiConnectionManager(const char *ssid, const char *pass) :
ssid(ssid), pass(pass),
lastConnectionTickTime(millis()),
connectionTickTimeInterval(CHECK_INTERVAL_IDLE) {
connectionTickTimeInterval(CHECK_INTERVAL_INIT),
getTimeRetries(MAX_GETTIME_RETRY) {
}

unsigned long WiFiConnectionManager::getTime() {
Expand All @@ -47,7 +51,7 @@ void WiFiConnectionManager::init() {

void WiFiConnectionManager::changeConnectionState(NetworkConnectionState _newState) {
char msgBuffer[120];
int newInterval = CHECK_INTERVAL_IDLE;
int newInterval = CHECK_INTERVAL_INIT;
switch (_newState) {
case CONNECTION_STATE_INIT:
newInterval = CHECK_INTERVAL_INIT;
Expand Down Expand Up @@ -87,9 +91,6 @@ void WiFiConnectionManager::check() {
int networkStatus = 0;
if (now - lastConnectionTickTime > connectionTickTimeInterval) {
switch (netConnectionState) {
case CONNECTION_STATE_IDLE:
changeConnectionState(CONNECTION_STATE_INIT);
break;
case CONNECTION_STATE_INIT:
networkStatus = WiFi.status();
*msgBuffer = 0;
Expand Down Expand Up @@ -133,6 +134,7 @@ void WiFiConnectionManager::check() {
sprintf(msgBuffer, "Connected to \"%s\"", ssid);
debugMessage(msgBuffer, 2);
changeConnectionState(CONNECTION_STATE_GETTIME);
getTimeRetries = MAX_GETTIME_RETRY;
return;
}
break;
Expand All @@ -148,7 +150,11 @@ void WiFiConnectionManager::check() {
sprintf(msgBuffer, "Network Time: %u", networkTime);
debugMessage(msgBuffer, 3);
changeConnectionState(CONNECTION_STATE_CONNECTED);
}
} else if (WiFi.status() != WL_CONNECTED) {
changeConnectionState(CONNECTION_STATE_DISCONNECTED);
} else if (!getTimeRetries--) {
changeConnectionState(CONNECTION_STATE_DISCONNECTED);
}
break;
case CONNECTION_STATE_CONNECTED:
// keep testing connection
Expand Down