Skip to content

Commit

Permalink
[googletts] Dispose oAuth2 service (openhab#14937)
Browse files Browse the repository at this point in the history
* [googletts] Dispose oAuth2 service

Related to openhab#14818

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
Signed-off-by: Jørgen Austvik <jaustvik@acm.org>
  • Loading branch information
lolodomo authored and austvik committed Mar 27, 2024
1 parent 8393747 commit 001a678
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,6 @@ class GoogleCloudAPI {
*/
private @Nullable GoogleTTSConfig config;

/**
* Status flag
*/
private boolean initialized;

private final Gson gson = new GsonBuilder().create();
private final ConfigurationAdmin configAdmin;
private final OAuthFactory oAuthFactory;
Expand All @@ -143,26 +138,27 @@ class GoogleCloudAPI {
void setConfig(GoogleTTSConfig config) {
this.config = config;

if (oAuthService != null) {
oAuthFactory.ungetOAuthService(GoogleTTSService.SERVICE_PID);
oAuthService = null;
}

String clientId = config.clientId;
String clientSecret = config.clientSecret;
if (clientId != null && !clientId.isEmpty() && clientSecret != null && !clientSecret.isEmpty()) {
final OAuthClientService oAuthService = oAuthFactory.createOAuthClientService(GoogleTTSService.SERVICE_PID,
GCP_TOKEN_URI, GCP_AUTH_URI, clientId, clientSecret, GCP_SCOPE, false);
this.oAuthService = oAuthService;
try {
final OAuthClientService oAuthService = oAuthFactory.createOAuthClientService(
GoogleTTSService.SERVICE_PID, GCP_TOKEN_URI, GCP_AUTH_URI, clientId, clientSecret, GCP_SCOPE,
false);
this.oAuthService = oAuthService;
getAccessToken();
initialized = true;
initVoices();
} catch (AuthenticationException | CommunicationException e) {
logger.warn("Error initializing Google Cloud TTS service: {}", e.getMessage());
oAuthService = null;
initialized = false;
oAuthFactory.ungetOAuthService(GoogleTTSService.SERVICE_PID);
this.oAuthService = null;
voices.clear();
}
} else {
oAuthService = null;
initialized = false;
voices.clear();
}

Expand All @@ -176,6 +172,14 @@ void setConfig(GoogleTTSConfig config) {
}
}

public void dispose() {
if (oAuthService != null) {
oAuthFactory.ungetOAuthService(GoogleTTSService.SERVICE_PID);
oAuthService = null;
}
voices.clear();
}

/**
* Fetches the OAuth2 tokens from Google Cloud Platform if the auth-code is set in the configuration. If successful
* the auth-code will be removed from the configuration.
Expand Down Expand Up @@ -367,8 +371,10 @@ public byte[] synthesizeSpeech(String text, GoogleTTSVoice voice, String codec)
return audio;
} catch (AuthenticationException | CommunicationException e) {
logger.warn("Error initializing Google Cloud TTS service: {}", e.getMessage());
oAuthService = null;
initialized = false;
if (oAuthService != null) {
oAuthFactory.ungetOAuthService(GoogleTTSService.SERVICE_PID);
oAuthService = null;
}
voices.clear();
} catch (FileNotFoundException e) {
logger.warn("Could not write file {} to cache: {}", audioFileInCache, e.getMessage());
Expand Down Expand Up @@ -490,6 +496,6 @@ private String getUniqueFilenameForText(String text, String voiceName) {
}

boolean isInitialized() {
return initialized;
return oAuthService != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
Expand Down Expand Up @@ -139,6 +140,13 @@ protected void activate(Map<String, Object> config) {
updateConfig(config);
}

@Deactivate
protected void dispose() {
apiImpl.dispose();
audioFormats.clear();
allVoices.clear();
}

/**
* Initializing audio formats. Google supports 3 formats:
* LINEAR16
Expand Down

0 comments on commit 001a678

Please sign in to comment.