Skip to content

Commit

Permalink
add retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
horihiro committed Nov 21, 2020
1 parent 49a649a commit 8176ff5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
24 changes: 16 additions & 8 deletions src/google-tts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ void GoogleTTS::setWiFiClientSecure(WiFiClientSecure *pClient)
m_pClient = pClient;
}

String GoogleTTS::getTKK()
String GoogleTTS::getTKK(int maxRetry, int currentRetry)
{
unsigned long current = millis();
bool bClientCreated = false;
char buf[100];
// WiFiClientSecure client;
if (m_pClient == nullptr)
{
Expand All @@ -129,13 +130,14 @@ String GoogleTTS::getTKK()
}
if (!m_pClient->connect(HOST_GTRANS, 443))
{
Serial.println("connection failed");
if (bClientCreated == true)
{
delete m_pClient;
m_pClient = nullptr;
}
return "_ERROR";
sprintf(buf, "Failed to connect to Google Translate. trying again... (current: %d, maxRetry: %d)", currentRetry, maxRetry);
Serial.println(buf);
return (maxRetry >= 0 && maxRetry <= currentRetry) ? "_ERROR_EXCEED_MAX_RETRY_COUNT" : this->getTKK(maxRetry, currentRetry + 1);
}

m_pClient->print(
Expand All @@ -157,7 +159,9 @@ String GoogleTTS::getTKK()
delete m_pClient;
m_pClient = nullptr;
}
return "_TIMEOUT";
sprintf(buf, "Timeout. trying again... (current: %d, maxRetry: %d)", currentRetry, maxRetry);
Serial.println(buf);
return (maxRetry >= 0 && maxRetry <= currentRetry) ? "_EXCEED_MAX_RETRY_COUNT" : this->getTKK(maxRetry, currentRetry + 1);
}
}
String line = "";
Expand All @@ -181,7 +185,9 @@ String GoogleTTS::getTKK()
m_pClient = nullptr;
}
delay(1);
return this->getTKK();
sprintf(buf, "Failed to get TKK. trying again... (current: %d, maxRetry: %d)", currentRetry, maxRetry);
Serial.println(buf);
return (maxRetry >= 0 && maxRetry <= currentRetry) ? "_EXCEED_MAX_RETRY_COUNT" : this->getTKK(maxRetry, currentRetry + 1);
}
if (isHeader)
continue;
Expand Down Expand Up @@ -223,12 +229,14 @@ String GoogleTTS::getTKK()
delete m_pClient;
m_pClient = nullptr;
}
return "_ERROR";
sprintf(buf, "Failed to get TKK. trying again... (current: %d, maxRetry: %d)", currentRetry, maxRetry);
Serial.println(buf);
return (maxRetry >= 0 && maxRetry <= currentRetry) ? "_ERROR_EXCEED_MAX_RETRY_COUNT" : this->getTKK(maxRetry, currentRetry + 1);
}

String GoogleTTS::getSpeechUrl(String text, String lang)
String GoogleTTS::getSpeechUrl(String text, String lang, int maxRetry)
{
String tkk = this->getTKK();
String tkk = this->getTKK(maxRetry, 0);
if (tkk.indexOf("_") == 0)
{
return tkk;
Expand Down
10 changes: 7 additions & 3 deletions src/google-tts.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ typedef class GoogleTTS
unsigned long m_lastTimestamp = 0;
String m_tkk;

String getTKK();
String getTKK(int maxRetry, int currentRetry);
String createToken(const char *text, const char *key);
String urlencode(String str);
char *lltoa(long long val, int base);
long long int XL(long long int a, char *b);

public:
void setWiFiClientSecure(WiFiClientSecure *pClient);
String getSpeechUrl(String text, String lang);
String getSpeechUrl(String text, String lang, int maxRetryCount);
String getSpeechUrl(String text, String lang)
{
return getSpeechUrl(text, lang, 10);
};
String getSpeechUrl(String text)
{
return getSpeechUrl(text, "en");
return getSpeechUrl(text, "en", 10);
}
} TTS;

Expand Down

0 comments on commit 8176ff5

Please sign in to comment.