[voice] Check that the provided locale is supported by KS/STT/HLI services (#2789)

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
pull/2792/head
lolodomo 2022-02-20 15:47:28 +01:00 committed by GitHub
parent 417098e1ad
commit 79ec0396b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 37 additions and 13 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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.");
}
}

View File

@ -32,6 +32,8 @@ import org.openhab.core.voice.text.InterpretationException;
@NonNullByDefault
public class HumanLanguageInterpreterStub implements HumanLanguageInterpreter {
private static final Set<Locale> 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<Locale> getSupportedLocales() {
// This method will not be used in the tests
return Set.of();
return SUPPORTED_LOCALES;
}
@Override

View File

@ -36,6 +36,7 @@ import org.openhab.core.voice.KSpottedEvent;
public class KSServiceStub implements KSService {
private static final Set<AudioFormat> SUPPORTED_FORMATS = Set.of(AudioFormat.MP3, AudioFormat.WAV);
private static final Set<Locale> 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<Locale> getSupportedLocales() {
return Set.of();
return SUPPORTED_LOCALES;
}
@Override

View File

@ -36,6 +36,7 @@ import org.openhab.core.voice.SpeechRecognitionEvent;
public class STTServiceStub implements STTService {
private static final Set<AudioFormat> SUPPORTED_FORMATS = Set.of(AudioFormat.MP3, AudioFormat.WAV);
private static final Set<Locale> 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<Locale> getSupportedLocales() {
return Set.of();
return SUPPORTED_LOCALES;
}
@Override

View File

@ -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();