From 79ec0396b3a3643f9fbacff0c158e9d98bbe6f00 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Sun, 20 Feb 2022 15:47:28 +0100 Subject: [PATCH] [voice] Check that the provided locale is supported by KS/STT/HLI services (#2789) Signed-off-by: Laurent Garnier --- .../io/rest/voice/internal/VoiceResource.java | 2 +- .../org/openhab/core/voice/VoiceManager.java | 8 ++++---- .../core/voice/internal/VoiceManagerImpl.java | 11 +++++++---- .../internal/HumanLanguageInterpreterStub.java | 5 +++-- .../core/voice/internal/KSServiceStub.java | 3 ++- .../core/voice/internal/STTServiceStub.java | 3 ++- .../voice/internal/VoiceManagerImplTest.java | 18 ++++++++++++++++++ 7 files changed, 37 insertions(+), 13 deletions(-) diff --git a/bundles/org.openhab.core.io.rest.voice/src/main/java/org/openhab/core/io/rest/voice/internal/VoiceResource.java b/bundles/org.openhab.core.io.rest.voice/src/main/java/org/openhab/core/io/rest/voice/internal/VoiceResource.java index ec7cffad97..0324de82d8 100644 --- a/bundles/org.openhab.core.io.rest.voice/src/main/java/org/openhab/core/io/rest/voice/internal/VoiceResource.java +++ b/bundles/org.openhab.core.io.rest.voice/src/main/java/org/openhab/core/io/rest/voice/internal/VoiceResource.java @@ -226,7 +226,7 @@ public class VoiceResource implements RESTResource { @Operation(operationId = "startDialog", summary = "Start dialog processing for a given audio source.", responses = { @ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "404", description = "One of the given ids is wrong."), - @ApiResponse(responseCode = "400", description = "Services are missing or dialog processing is already started for the audio source.") }) + @ApiResponse(responseCode = "400", description = "Services are missing or language is not supported by services or dialog processing is already started for the audio source.") }) public Response startDialog( @HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @Parameter(description = "language") @Nullable String language, @QueryParam("sourceId") @Parameter(description = "source ID") @Nullable String sourceId, diff --git a/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/VoiceManager.java b/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/VoiceManager.java index b77b0be035..ddf896de12 100644 --- a/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/VoiceManager.java +++ b/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/VoiceManager.java @@ -128,8 +128,8 @@ public interface VoiceManager { * * Only one dialog can be started for the default audio source. * - * @throws IllegalStateException if required services are not available or the dialog is already started for the - * default audio source + * @throws IllegalStateException if required services are not all available or the provided locale is not supported + * by all these services or the dialog is already started for the default audio source */ void startDialog() throws IllegalStateException; @@ -149,8 +149,8 @@ public interface VoiceManager { * @param Locale the locale to use or null to use the default locale * @param keyword the keyword to use during keyword spotting or null to use the default keyword * @param listeningItem the item to switch ON while listening to a question - * @throws IllegalStateException if required services are not available or the dialog is already started for this - * audio source + * @throws IllegalStateException if required services are not all available or the provided locale is not supported + * by all these services or the dialog is already started for this audio source */ void startDialog(@Nullable KSService ks, @Nullable STTService stt, @Nullable TTSService tts, @Nullable HumanLanguageInterpreter hli, @Nullable AudioSource source, @Nullable AudioSink sink, diff --git a/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/internal/VoiceManagerImpl.java b/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/internal/VoiceManagerImpl.java index 626e2b1f3c..552bd74f1c 100644 --- a/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/internal/VoiceManagerImpl.java +++ b/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/internal/VoiceManagerImpl.java @@ -500,8 +500,13 @@ public class VoiceManagerImpl implements VoiceManager, ConfigOptionProvider { String item = (listeningItem == null) ? this.listeningItem : listeningItem; Bundle b = bundle; - if (ksService != null && sttService != null && ttsService != null && interpreter != null && audioSource != null - && audioSink != null && b != null) { + if (ksService == null || sttService == null || ttsService == null || interpreter == null || audioSource == null + || audioSink == null || b == null) { + throw new IllegalStateException("Cannot start dialog as services are missing."); + } else if (!ksService.getSupportedLocales().contains(loc) || !sttService.getSupportedLocales().contains(loc) + || !interpreter.getSupportedLocales().contains(loc)) { + throw new IllegalStateException("Cannot start dialog as provided locale is not supported by all services."); + } else { DialogProcessor processor = dialogProcessors.get(audioSource.getId()); if (processor == null) { logger.debug("Starting a new dialog for source {} ({})", audioSource.getLabel(null), @@ -515,8 +520,6 @@ public class VoiceManagerImpl implements VoiceManager, ConfigOptionProvider { String.format("Cannot start dialog as a dialog is already started for audio source '%s'.", audioSource.getLabel(null))); } - } else { - throw new IllegalStateException("Cannot start dialog as services are missing."); } } diff --git a/itests/org.openhab.core.voice.tests/src/main/java/org/openhab/core/voice/internal/HumanLanguageInterpreterStub.java b/itests/org.openhab.core.voice.tests/src/main/java/org/openhab/core/voice/internal/HumanLanguageInterpreterStub.java index e8cb676e59..b394073644 100644 --- a/itests/org.openhab.core.voice.tests/src/main/java/org/openhab/core/voice/internal/HumanLanguageInterpreterStub.java +++ b/itests/org.openhab.core.voice.tests/src/main/java/org/openhab/core/voice/internal/HumanLanguageInterpreterStub.java @@ -32,6 +32,8 @@ import org.openhab.core.voice.text.InterpretationException; @NonNullByDefault public class HumanLanguageInterpreterStub implements HumanLanguageInterpreter { + private static final Set SUPPORTED_LOCALES = Set.of(Locale.ENGLISH); + private static final String INTERPRETED_TEXT = "Interpreted text"; private static final String EXCEPTION_MESSAGE = "interpretation exception"; @@ -71,8 +73,7 @@ public class HumanLanguageInterpreterStub implements HumanLanguageInterpreter { @Override public Set getSupportedLocales() { - // This method will not be used in the tests - return Set.of(); + return SUPPORTED_LOCALES; } @Override diff --git a/itests/org.openhab.core.voice.tests/src/main/java/org/openhab/core/voice/internal/KSServiceStub.java b/itests/org.openhab.core.voice.tests/src/main/java/org/openhab/core/voice/internal/KSServiceStub.java index ac2728c489..4f42f455ce 100644 --- a/itests/org.openhab.core.voice.tests/src/main/java/org/openhab/core/voice/internal/KSServiceStub.java +++ b/itests/org.openhab.core.voice.tests/src/main/java/org/openhab/core/voice/internal/KSServiceStub.java @@ -36,6 +36,7 @@ import org.openhab.core.voice.KSpottedEvent; public class KSServiceStub implements KSService { private static final Set SUPPORTED_FORMATS = Set.of(AudioFormat.MP3, AudioFormat.WAV); + private static final Set SUPPORTED_LOCALES = Set.of(Locale.ENGLISH); private static final String KSSERVICE_STUB_ID = "ksServiceStubID"; private static final String KSSERVICE_STUB_LABEL = "ksServiceStubLabel"; @@ -60,7 +61,7 @@ public class KSServiceStub implements KSService { @Override public Set getSupportedLocales() { - return Set.of(); + return SUPPORTED_LOCALES; } @Override diff --git a/itests/org.openhab.core.voice.tests/src/main/java/org/openhab/core/voice/internal/STTServiceStub.java b/itests/org.openhab.core.voice.tests/src/main/java/org/openhab/core/voice/internal/STTServiceStub.java index 12ac6523a8..c94dddb05a 100644 --- a/itests/org.openhab.core.voice.tests/src/main/java/org/openhab/core/voice/internal/STTServiceStub.java +++ b/itests/org.openhab.core.voice.tests/src/main/java/org/openhab/core/voice/internal/STTServiceStub.java @@ -36,6 +36,7 @@ import org.openhab.core.voice.SpeechRecognitionEvent; public class STTServiceStub implements STTService { private static final Set SUPPORTED_FORMATS = Set.of(AudioFormat.MP3, AudioFormat.WAV); + private static final Set SUPPORTED_LOCALES = Set.of(Locale.ENGLISH); private static final String STTSERVICE_STUB_ID = "sttServiceStubID"; private static final String STTSERVICE_STUB_LABEL = "sttServiceStubLabel"; @@ -60,7 +61,7 @@ public class STTServiceStub implements STTService { @Override public Set getSupportedLocales() { - return Set.of(); + return SUPPORTED_LOCALES; } @Override diff --git a/itests/org.openhab.core.voice.tests/src/main/java/org/openhab/core/voice/internal/VoiceManagerImplTest.java b/itests/org.openhab.core.voice.tests/src/main/java/org/openhab/core/voice/internal/VoiceManagerImplTest.java index 3ea9f9b27d..ddc0d9e72a 100644 --- a/itests/org.openhab.core.voice.tests/src/main/java/org/openhab/core/voice/internal/VoiceManagerImplTest.java +++ b/itests/org.openhab.core.voice.tests/src/main/java/org/openhab/core/voice/internal/VoiceManagerImplTest.java @@ -193,6 +193,24 @@ public class VoiceManagerImplTest extends JavaOSGiTest { assertFalse(sink.getIsStreamProcessed()); } + @Test + public void verifyThatADialogIsNotStartedWhenLocaleIsNotSupported() { + sttService = new STTServiceStub(); + ksService = new KSServiceStub(); + hliStub = new HumanLanguageInterpreterStub(); + + registerService(sttService); + registerService(ksService); + registerService(ttsService); + registerService(hliStub); + + assertThrows(IllegalStateException.class, () -> voiceManager.startDialog(ksService, sttService, ttsService, + hliStub, source, sink, Locale.FRENCH, "mot", null)); + + assertFalse(ksService.isWordSpotted()); + assertFalse(sink.getIsStreamProcessed()); + } + @Test public void startDialogWhenAllOfTheRequiredServicesAreAvailable() { sttService = new STTServiceStub();