]> git.basschouten.com Git - openhab-addons.git/commitdiff
[watsonstt] lazy abort (#12318)
authorGiviMAD <GiviMAD@users.noreply.github.com>
Sun, 20 Feb 2022 13:25:47 +0000 (14:25 +0100)
committerGitHub <noreply@github.com>
Sun, 20 Feb 2022 13:25:47 +0000 (14:25 +0100)
* [watsonstt] lazy abort

Signed-off-by: Miguel Álvarez Díez <miguelwork92@gmail.com>
bundles/org.openhab.voice.watsonstt/src/main/java/org/openhab/voice/watsonstt/internal/WatsonSTTService.java

index a3dc80d0d7eb5a20a8baed00b71156ff28246613..ebd5c0759a069eb3c0e7461185e90d80adaefdf8 100644 (file)
@@ -19,6 +19,7 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.stream.Collectors;
 
@@ -132,12 +133,13 @@ public class WatsonSTTService implements STTService {
                 .speechDetectorSensitivity(config.speechDetectorSensitivity).inactivityTimeout(config.inactivityTimeout)
                 .build();
         final AtomicReference<@Nullable WebSocket> socketRef = new AtomicReference<>();
-        var task = executor.submit(() -> {
+        final AtomicBoolean aborted = new AtomicBoolean(false);
+        executor.submit(() -> {
             int retries = 2;
             while (retries > 0) {
                 try {
                     socketRef.set(speechToText.recognizeUsingWebSocket(wsOptions,
-                            new TranscriptionListener(sttListener, config)));
+                            new TranscriptionListener(sttListener, config, aborted)));
                     break;
                 } catch (RuntimeException e) {
                     var cause = e.getCause();
@@ -157,16 +159,17 @@ public class WatsonSTTService implements STTService {
         return new STTServiceHandle() {
             @Override
             public void abort() {
-                var socket = socketRef.get();
-                if (socket != null) {
-                    socket.close(1000, null);
-                    socket.cancel();
-                    try {
-                        Thread.sleep(100);
-                    } catch (InterruptedException ignored) {
+                if (!aborted.getAndSet(true)) {
+                    var socket = socketRef.get();
+                    if (socket != null) {
+                        socket.close(1000, null);
+                        socket.cancel();
+                        try {
+                            Thread.sleep(100);
+                        } catch (InterruptedException ignored) {
+                        }
                     }
                 }
-                task.cancel(true);
             }
         };
     }
@@ -226,13 +229,15 @@ public class WatsonSTTService implements STTService {
         private final StringBuilder transcriptBuilder = new StringBuilder();
         private final STTListener sttListener;
         private final WatsonSTTConfiguration config;
+        private final AtomicBoolean aborted;
         private float confidenceSum = 0f;
         private int responseCount = 0;
         private boolean disconnected = false;
 
-        public TranscriptionListener(STTListener sttListener, WatsonSTTConfiguration config) {
+        public TranscriptionListener(STTListener sttListener, WatsonSTTConfiguration config, AtomicBoolean aborted) {
             this.sttListener = sttListener;
             this.config = config;
+            this.aborted = aborted;
         }
 
         @Override
@@ -267,24 +272,28 @@ public class WatsonSTTService implements STTService {
                 return;
             }
             logger.warn("TranscriptionError: {}", errorMessage);
-            sttListener.sttEventReceived(
-                    new SpeechRecognitionErrorEvent(errorMessage != null ? errorMessage : "Unknown error"));
+            if (!aborted.get()) {
+                sttListener.sttEventReceived(
+                        new SpeechRecognitionErrorEvent(errorMessage != null ? errorMessage : "Unknown error"));
+            }
         }
 
         @Override
         public void onDisconnected() {
             logger.debug("onDisconnected");
             disconnected = true;
-            sttListener.sttEventReceived(new RecognitionStopEvent());
-            float averageConfidence = confidenceSum / (float) responseCount;
-            String transcript = transcriptBuilder.toString();
-            if (!transcript.isBlank()) {
-                sttListener.sttEventReceived(new SpeechRecognitionEvent(transcript, averageConfidence));
-            } else {
-                if (!config.noResultsMessage.isBlank()) {
-                    sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.noResultsMessage));
+            if (!aborted.getAndSet(true)) {
+                sttListener.sttEventReceived(new RecognitionStopEvent());
+                float averageConfidence = confidenceSum / (float) responseCount;
+                String transcript = transcriptBuilder.toString();
+                if (!transcript.isBlank()) {
+                    sttListener.sttEventReceived(new SpeechRecognitionEvent(transcript, averageConfidence));
                 } else {
-                    sttListener.sttEventReceived(new SpeechRecognitionErrorEvent("No results"));
+                    if (!config.noResultsMessage.isBlank()) {
+                        sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(config.noResultsMessage));
+                    } else {
+                        sttListener.sttEventReceived(new SpeechRecognitionErrorEvent("No results"));
+                    }
                 }
             }
         }