Fix socket bugs, timeout for connection

This commit is contained in:
Christoph Hagen 2023-12-05 22:54:47 +01:00
parent 4c23565b9c
commit 6f8838c32b
3 changed files with 31 additions and 16 deletions

View File

@ -84,6 +84,8 @@ private:
void disconnect(); void disconnect();
bool shouldReconnect = true; bool shouldReconnect = true;
bool isConnecting = false;
uint32_t connectionTimeout = 0;
uint32_t nextReconnectAttemptMs = 0; uint32_t nextReconnectAttemptMs = 0;
void didDisconnect(); void didDisconnect();

View File

@ -22,20 +22,21 @@ void SesameController::configure(ServoConfiguration servoConfig, ServerConfigura
Ethernet.init(ethernetConfig.spiPinSS); Ethernet.init(ethernetConfig.spiPinSS);
// Check for Ethernet hardware present if (Ethernet.begin(ethernetConfig.macAddress, ethernetConfig.dhcpLeaseTimeoutMs, ethernetConfig.dhcpLeaseResponseTimeoutMs) == 1) {
if (Ethernet.hardwareStatus() == EthernetNoHardware) { Serial.print("[INFO] DHCP assigned IP ");
Serial.println("[ERROR] Ethernet shield not found."); Serial.println(Ethernet.localIP());
} else if (Ethernet.linkStatus() == LinkOFF) { ethernetIsConfigured = true;
Serial.println("[ERROR] Ethernet cable is not connected."); } else {
} else if (Ethernet.linkStatus() == Unknown) { // Check for Ethernet hardware present
Serial.println("[ERROR] Ethernet cable status unknown."); if (Ethernet.hardwareStatus() == EthernetNoHardware) {
} else if (Ethernet.linkStatus() == LinkON) { Serial.println("[ERROR] Ethernet shield not found.");
Serial.println("[INFO] Ethernet cable is connected."); } else if (Ethernet.linkStatus() == LinkOFF) {
if (Ethernet.begin(ethernetConfig.macAddress, ethernetConfig.dhcpLeaseTimeoutMs, ethernetConfig.dhcpLeaseResponseTimeoutMs) == 1) { Serial.println("[ERROR] Ethernet cable is not connected.");
Serial.print("[INFO] DHCP assigned IP "); } else if (Ethernet.linkStatus() == Unknown) {
Serial.println(Ethernet.localIP()); Serial.println("[ERROR] Ethernet cable status unknown.");
ethernetIsConfigured = true; } else if (Ethernet.linkStatus() == LinkON) {
} else { Serial.println("[INFO] Ethernet cable is connected.");
// Try to configure using IP address instead of DHCP // Try to configure using IP address instead of DHCP
Ethernet.begin(ethernetConfig.macAddress, ethernetConfig.manualIp, ethernetConfig.manualDnsAddress); Ethernet.begin(ethernetConfig.macAddress, ethernetConfig.manualIp, ethernetConfig.manualDnsAddress);
Serial.print("[WARNING] DHCP failed, using self-assigned IP "); Serial.print("[WARNING] DHCP failed, using self-assigned IP ");

View File

@ -20,6 +20,9 @@ void ServerConnection::connect() {
return; return;
} }
isConnecting = true;
Serial.printf("[INFO] Connecting to %s:%d%s\n", configuration.url, configuration.port, configuration.path);
connectionTimeout = currentTime + configuration.socketHeartbeatIntervalMs;
webSocket.begin(configuration.url, configuration.port, configuration.path); webSocket.begin(configuration.url, configuration.port, configuration.path);
webSocket.setAuthorization(configuration.key); webSocket.setAuthorization(configuration.key);
@ -31,7 +34,7 @@ void ServerConnection::connect() {
} }
void ServerConnection::didDisconnect() { void ServerConnection::didDisconnect() {
if (shouldReconnect) { if (shouldReconnect || isConnecting) {
return; // Disconnect already registered. return; // Disconnect already registered.
} }
Serial.println("[INFO] Socket disconnected"); Serial.println("[INFO] Socket disconnected");
@ -40,6 +43,7 @@ void ServerConnection::didDisconnect() {
} }
void ServerConnection::didConnect() { void ServerConnection::didConnect() {
isConnecting = false;
Serial.println("[INFO] Socket connected"); Serial.println("[INFO] Socket connected");
webSocket.sendTXT(configuration.key); webSocket.sendTXT(configuration.key);
webSocket.enableHeartbeat(configuration.socketHeartbeatIntervalMs, configuration.socketHeartbeatTimeoutMs, configuration.socketHeartbeatFailureReconnectCount); webSocket.enableHeartbeat(configuration.socketHeartbeatIntervalMs, configuration.socketHeartbeatTimeoutMs, configuration.socketHeartbeatFailureReconnectCount);
@ -52,10 +56,16 @@ void ServerConnection::disconnect() {
void ServerConnection::loop(uint32_t millis) { void ServerConnection::loop(uint32_t millis) {
currentTime = millis; currentTime = millis;
webSocket.loop(); webSocket.loop();
if (shouldReconnect) { if (shouldReconnect && !isConnecting) {
shouldReconnect = false; shouldReconnect = false;
connect(); connect();
} }
if (isConnecting && millis > connectionTimeout) {
Serial.println("[INFO] Failed to connect");
disconnect();
shouldReconnect = true;
isConnecting = false;
}
} }
void ServerConnection::webSocketEventHandler(WStype_t type, uint8_t * payload, size_t length) { void ServerConnection::webSocketEventHandler(WStype_t type, uint8_t * payload, size_t length) {
@ -75,11 +85,13 @@ switch(type) {
case WStype_PONG: case WStype_PONG:
break; break;
case WStype_PING: case WStype_PING:
break;
case WStype_ERROR: case WStype_ERROR:
case WStype_FRAGMENT_TEXT_START: case WStype_FRAGMENT_TEXT_START:
case WStype_FRAGMENT_BIN_START: case WStype_FRAGMENT_BIN_START:
case WStype_FRAGMENT: case WStype_FRAGMENT:
case WStype_FRAGMENT_FIN: case WStype_FRAGMENT_FIN:
Serial.printf("[WARN] Unexpected socket event %d\n", type);
controller->sendServerError(MessageResult::UnexpectedSocketEvent); controller->sendServerError(MessageResult::UnexpectedSocketEvent);
break; break;
} }