From 1b5471cf65d9e37dfacec25f16724bcd8543fbec Mon Sep 17 00:00:00 2001 From: GiviMAD Date: Tue, 24 May 2022 20:45:49 +0200 Subject: [PATCH] [watsonstt] reuse token (#12800) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [watsonstt] reuse token Signed-off-by: Miguel Álvarez Díez --- .../watsonstt/internal/WatsonSTTService.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.voice.watsonstt/src/main/java/org/openhab/voice/watsonstt/internal/WatsonSTTService.java b/bundles/org.openhab.voice.watsonstt/src/main/java/org/openhab/voice/watsonstt/internal/WatsonSTTService.java index 311ebb769c1..9454a778a4e 100644 --- a/bundles/org.openhab.voice.watsonstt/src/main/java/org/openhab/voice/watsonstt/internal/WatsonSTTService.java +++ b/bundles/org.openhab.voice.watsonstt/src/main/java/org/openhab/voice/watsonstt/internal/WatsonSTTService.java @@ -77,15 +77,26 @@ public class WatsonSTTService implements STTService { private final Set supportedLocales = models.stream().map(name -> name.split("_")[0]) .map(Locale::forLanguageTag).collect(Collectors.toSet()); private WatsonSTTConfiguration config = new WatsonSTTConfiguration(); + private @Nullable SpeechToText speechToText = null; @Activate protected void activate(Map config) { - this.config = new Configuration(config).as(WatsonSTTConfiguration.class); + modified(config); } @Modified protected void modified(Map config) { this.config = new Configuration(config).as(WatsonSTTConfiguration.class); + if (this.config.apiKey.isBlank() || this.config.instanceUrl.isBlank()) { + this.speechToText = null; + } else { + var speechToText = new SpeechToText(new IamAuthenticator.Builder().apikey(this.config.apiKey).build()); + speechToText.setServiceUrl(this.config.instanceUrl); + if (this.config.optOutLogging) { + speechToText.setDefaultHeaders(Map.of("X-Watson-Learning-Opt-Out", "1")); + } + this.speechToText = speechToText; + } } @Override @@ -112,7 +123,8 @@ public class WatsonSTTService implements STTService { @Override public STTServiceHandle recognize(STTListener sttListener, AudioStream audioStream, Locale locale, Set set) throws STTException { - if (config.apiKey.isBlank() || config.instanceUrl.isBlank()) { + var stt = this.speechToText; + if (stt == null) { throw new STTException("service is not correctly configured"); } String contentType = getContentType(audioStream); @@ -120,11 +132,6 @@ public class WatsonSTTService implements STTService { throw new STTException("Unsupported format, unable to resolve audio content type"); } logger.debug("Content-Type: {}", contentType); - var speechToText = new SpeechToText(new IamAuthenticator.Builder().apikey(config.apiKey).build()); - speechToText.setServiceUrl(config.instanceUrl); - if (config.optOutLogging) { - speechToText.setDefaultHeaders(Map.of("X-Watson-Learning-Opt-Out", "1")); - } RecognizeWithWebsocketsOptions wsOptions = new RecognizeWithWebsocketsOptions.Builder().audio(audioStream) .contentType(contentType).redaction(config.redaction).smartFormatting(config.smartFormatting) .model(locale.toLanguageTag() + "_BroadbandModel").interimResults(true) @@ -134,7 +141,7 @@ public class WatsonSTTService implements STTService { final AtomicReference<@Nullable WebSocket> socketRef = new AtomicReference<>(); final AtomicBoolean aborted = new AtomicBoolean(false); executor.submit(() -> { - socketRef.set(speechToText.recognizeUsingWebSocket(wsOptions, + socketRef.set(stt.recognizeUsingWebSocket(wsOptions, new TranscriptionListener(socketRef, sttListener, config, aborted))); }); return new STTServiceHandle() {