diff --git a/bundles/org.openhab.voice.googletts/src/main/java/org/openhab/voice/googletts/internal/GoogleCloudAPI.java b/bundles/org.openhab.voice.googletts/src/main/java/org/openhab/voice/googletts/internal/GoogleCloudAPI.java index 8acb1cbaf..692d47170 100644 --- a/bundles/org.openhab.voice.googletts/src/main/java/org/openhab/voice/googletts/internal/GoogleCloudAPI.java +++ b/bundles/org.openhab.voice.googletts/src/main/java/org/openhab/voice/googletts/internal/GoogleCloudAPI.java @@ -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; @@ -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(); } @@ -176,6 +172,14 @@ class GoogleCloudAPI { } } + 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. @@ -367,8 +371,10 @@ class GoogleCloudAPI { 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()); @@ -490,6 +496,6 @@ class GoogleCloudAPI { } boolean isInitialized() { - return initialized; + return oAuthService != null; } } diff --git a/bundles/org.openhab.voice.googletts/src/main/java/org/openhab/voice/googletts/internal/GoogleTTSService.java b/bundles/org.openhab.voice.googletts/src/main/java/org/openhab/voice/googletts/internal/GoogleTTSService.java index 3e95e75bc..66f682daf 100644 --- a/bundles/org.openhab.voice.googletts/src/main/java/org/openhab/voice/googletts/internal/GoogleTTSService.java +++ b/bundles/org.openhab.voice.googletts/src/main/java/org/openhab/voice/googletts/internal/GoogleTTSService.java @@ -41,6 +41,7 @@ import org.osgi.framework.Constants; 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; @@ -139,6 +140,13 @@ public class GoogleTTSService implements TTSService { updateConfig(config); } + @Deactivate + protected void dispose() { + apiImpl.dispose(); + audioFormats.clear(); + allVoices.clear(); + } + /** * Initializing audio formats. Google supports 3 formats: * LINEAR16