-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Thorsten Ludewig
committed
Mar 20, 2020
1 parent
bc8f547
commit c6bce96
Showing
10 changed files
with
298 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
lib/InternetConnectionCheckHandler/InternetConnectionCheckHandler.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
#include <Arduino.h> | ||
#include <Esp.h> | ||
#include <ESP8266WiFi.h> | ||
#include <ESPAsyncTCP.h> | ||
#include <App.hpp> | ||
#include <RelayHandler.hpp> | ||
#include <WifiHandler.hpp> | ||
|
||
#include "InternetConnectionCheckHandler.hpp" | ||
|
||
InternetConnectionCheckHandler internetConnectionCheckHandler; | ||
|
||
static AsyncClient *aClient = NULL; | ||
|
||
static bool successFound; | ||
static char *successTxt = (char *)"Success"; | ||
static char *matchPtr; | ||
static time_t startTimestamp; | ||
static bool initStateRead; | ||
static bool lastState; | ||
|
||
void runAction( bool connected ) | ||
{ | ||
LOG1( "run acion = %s\n", successFound ? "true" : "false" ); | ||
if ( initStateRead ) | ||
{ | ||
if ( lastState != connected ) | ||
{ | ||
LOG0( "Internet connection state changed\n" ); | ||
|
||
switch( appcfg.inet_check_action ) | ||
{ | ||
case INET_CHECK_ACTION_ON_CONNECT_SWITCH_OFF: | ||
if ( connected ) | ||
{ | ||
relayHandler.delayedOff(); | ||
} | ||
break; | ||
|
||
case INET_CHECK_ACTION_ON_CONNECT_SWITCH_ON: | ||
if ( connected ) | ||
{ | ||
relayHandler.delayedOn(); | ||
} | ||
break; | ||
|
||
case INET_CHECK_ACTION_ON_DISCONNECT_SWITCH_OFF: | ||
if ( !connected ) | ||
{ | ||
relayHandler.delayedOff(); | ||
} | ||
break; | ||
|
||
case INET_CHECK_ACTION_ON_DISCONNECT_SWITCH_ON: | ||
if ( !connected ) | ||
{ | ||
relayHandler.delayedOn(); | ||
} | ||
break; | ||
|
||
case INET_CHECK_ACTION_SHOW_CONNECTION_STATE: | ||
if ( connected ) | ||
{ | ||
relayHandler.delayedOn(); | ||
} | ||
else | ||
{ | ||
relayHandler.delayedOff(); | ||
} | ||
break; | ||
|
||
case INET_CHECK_ACTION_SHOW_CONNECTION_STATE_INV: | ||
if ( connected ) | ||
{ | ||
relayHandler.delayedOff(); | ||
} | ||
else | ||
{ | ||
relayHandler.delayedOn(); | ||
} | ||
break; | ||
|
||
default: | ||
LOG0( "Unknown action\n" ); | ||
} | ||
|
||
lastState = connected; | ||
} | ||
} | ||
else | ||
{ | ||
LOG0( "Saving internet connection state\n" ); | ||
lastState = connected; | ||
initStateRead = true; | ||
} | ||
} | ||
|
||
InternetConnectionCheckHandler::InternetConnectionCheckHandler() | ||
{ | ||
initialized = false; | ||
initStateRead = false; | ||
} | ||
|
||
void InternetConnectionCheckHandler::setup() | ||
{ | ||
LOG0("Internet Connection Check Setup...\n"); | ||
initialized = true; | ||
lastCheckedTimestamp = 0l; | ||
LOG0("done\n"); | ||
} | ||
|
||
void InternetConnectionCheckHandler::handle(time_t now) | ||
{ | ||
if ( appcfg.inet_check_enabled == true && wifiHandler.isInStationMode()) | ||
{ | ||
if ( !initialized ) | ||
{ | ||
setup(); | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////// | ||
|
||
if (( now - lastCheckedTimestamp ) >= (((time_t)appcfg.inet_check_period) * 1000l )) | ||
{ | ||
LOG0("Checking internet connection...\n"); | ||
|
||
if ( !wifiHandler.isConnected() ) | ||
{ | ||
runAction( false ); | ||
lastCheckedTimestamp = now; | ||
return; | ||
} | ||
|
||
startTimestamp = millis(); | ||
aClient = new AsyncClient(); | ||
|
||
successFound = false; | ||
matchPtr = successTxt; | ||
|
||
if (aClient) | ||
{ | ||
aClient->onError([](void *arg, AsyncClient *client, int error) { | ||
Serial.println("\nConnect Error"); | ||
aClient = NULL; | ||
delete client; | ||
runAction(false); | ||
}, NULL); | ||
|
||
aClient->onConnect([](void *arg, AsyncClient *client) { | ||
Serial.println("Connected"); | ||
|
||
aClient->onError(NULL, NULL); | ||
|
||
client->onDisconnect([](void *arg, AsyncClient *client) { | ||
Serial.printf("Disconnected duration=%ld\n", millis() - startTimestamp ); | ||
aClient = NULL; | ||
delete client; | ||
runAction( successFound ); | ||
}, NULL); | ||
|
||
client->onData([](void *arg, AsyncClient *client, void *data, size_t len) { | ||
Serial.print("Data: "); | ||
Serial.println(len); | ||
char *d = (char *)data; | ||
for (size_t i = 0; i < len; i++) | ||
{ | ||
// Serial.write(d[i]); | ||
if ( *matchPtr == 0 ) | ||
{ | ||
successFound = true; | ||
} | ||
else | ||
{ | ||
if ( *matchPtr == d[i] ) | ||
{ | ||
matchPtr++; | ||
} | ||
else | ||
{ | ||
matchPtr = successTxt; | ||
} | ||
} | ||
} | ||
}, NULL); | ||
|
||
//send the request | ||
client->write("GET / HTTP/1.0\r\nHost: captive.apple.com\r\n\r\n"); | ||
}, NULL); | ||
|
||
if (!aClient->connect("captive.apple.com", 80)) | ||
{ | ||
Serial.println("Connect Fail"); | ||
AsyncClient *client = aClient; | ||
aClient = NULL; | ||
delete client; | ||
runAction( false ); | ||
} | ||
} | ||
|
||
|
||
lastCheckedTimestamp = now; | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////// | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
lib/InternetConnectionCheckHandler/InternetConnectionCheckHandler.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef __INTERNET_CONNECTION_CHECK_HANDLER_HPP__ | ||
#define __INTERNET_CONNECTION_CHECK_HANDLER_HPP__ | ||
|
||
#include <Arduino.h> | ||
|
||
class InternetConnectionCheckHandler | ||
{ | ||
private: | ||
bool initialized; | ||
time_t lastCheckedTimestamp; | ||
void setup(); | ||
|
||
public: | ||
InternetConnectionCheckHandler(); | ||
void handle( time_t now ); | ||
}; | ||
|
||
extern InternetConnectionCheckHandler internetConnectionCheckHandler; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters