[voice] Support managed dialogs (#3264)

* [voice] introduce methods for dialog persistency

Signed-off-by: Miguel Álvarez <miguelwork92@gmail.com>
This commit is contained in:
GiviMAD
2023-02-08 21:12:22 +01:00
committed by GitHub
parent a91b7f6425
commit c30e4b8eea
12 changed files with 395 additions and 37 deletions
@@ -63,6 +63,7 @@ Fragment-Host: org.openhab.core.voice
biz.aQute.tester.junit-platform;version='[6.4.0,6.4.1)',\
org.openhab.core;version='[4.0.0,4.0.1)',\
org.openhab.core.audio;version='[4.0.0,4.0.1)',\
org.openhab.core;version='[4.0.0,4.0.1)',\
org.openhab.core.config.core;version='[4.0.0,4.0.1)',\
org.openhab.core.io.console;version='[4.0.0,4.0.1)',\
org.openhab.core.io.http;version='[4.0.0,4.0.1)',\
@@ -14,7 +14,11 @@ package org.openhab.core.voice.internal;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.net.URI;
@@ -34,6 +38,7 @@ import org.openhab.core.config.core.ParameterOption;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.test.java.JavaOSGiTest;
import org.openhab.core.voice.DialogRegistration;
import org.openhab.core.voice.Voice;
import org.openhab.core.voice.VoiceManager;
import org.openhab.core.voice.text.InterpretationException;
@@ -74,16 +79,15 @@ public class VoiceManagerImplTest extends JavaOSGiTest {
@BeforeEach
public void setUp() throws IOException {
registerVolatileStorageService();
BundleContext context = bundleContext;
ttsService = new TTSServiceStub(context);
sink = new SinkStub();
voice = new VoiceStub();
source = new AudioSourceStub();
registerService(sink);
registerService(voice);
registerService(source);
ConfigurationAdmin configAdmin = super.getService(ConfigurationAdmin.class);
audioManager = getService(AudioManager.class);
@@ -582,4 +586,48 @@ public class VoiceManagerImplTest extends JavaOSGiTest {
Voice voice = voiceManager.getPreferredVoice(Set.of());
assertNull(voice);
}
@Test
public void registerDialog() throws IOException, InterruptedException {
// register services
sttService = new STTServiceStub();
ksService = new KSServiceStub();
hliStub = new HumanLanguageInterpreterStub();
registerService(sttService);
registerService(ksService);
registerService(ttsService);
registerService(hliStub);
// configure
Dictionary<String, Object> config = new Hashtable<>();
config.put(CONFIG_KEYWORD, "word");
config.put(CONFIG_DEFAULT_STT, sttService.getId());
config.put(CONFIG_DEFAULT_KS, ksService.getId());
config.put(CONFIG_DEFAULT_HLI, hliStub.getId());
config.put(CONFIG_DEFAULT_VOICE, voice.getUID());
ConfigurationAdmin configAdmin = super.getService(ConfigurationAdmin.class);
Configuration configuration = configAdmin.getConfiguration(VoiceManagerImpl.CONFIGURATION_PID);
configuration.update(config);
// Wait some time to be sure that the configuration will be updated
Thread.sleep(2000);
// Add a dialog registration
var dialogRegistration = new DialogRegistration(source.getId(), sink.getId());
voiceManager.registerDialog(dialogRegistration);
// Wait some time to be sure dialog build has been fired and check dialog has been started
Thread.sleep(6000);
// Assert registration is available and running
var registrations = voiceManager.getDialogRegistrations();
assertThat(registrations.size(), is(1));
assertTrue(registrations.stream().findAny().map(r -> r.running).orElse(false));
// Assert dialog has been stated
assertTrue(ksService.isWordSpotted());
assertTrue(sttService.isRecognized());
assertThat(hliStub.getQuestion(), is("Recognized text"));
assertThat(hliStub.getAnswer(), is("Interpreted text"));
assertThat(ttsService.getSynthesized(), is("Interpreted text"));
assertTrue(sink.getIsStreamProcessed());
// Remove the dialog registration
voiceManager.unregisterDialog(dialogRegistration);
// Assert dialog has been stopped
assertTrue(ksService.isAborted());
}
}
@@ -51,6 +51,7 @@ public class InterpretCommandTest extends VoiceConsoleCommandExtensionTest {
@BeforeEach
public void setUp() throws IOException, InterruptedException {
registerVolatileStorageService();
ttsService = new TTSServiceStub();
hliStub = new HumanLanguageInterpreterStub();
voice = new VoiceStub();
@@ -61,6 +61,7 @@ public class SayCommandTest extends VoiceConsoleCommandExtensionTest {
@BeforeEach
public void setUp() {
registerVolatileStorageService();
sink = new SinkStub();
voice = new VoiceStub();
@@ -44,6 +44,7 @@ public abstract class VoiceConsoleCommandExtensionTest extends JavaOSGiTest {
@BeforeEach
public void setup() {
registerVolatileStorageService();
voiceManager = getService(VoiceManager.class, VoiceManagerImpl.class);
assertNotNull(voiceManager);
audioManager = getService(AudioManager.class, AudioManager.class);
@@ -50,6 +50,7 @@ public class VoicesCommandTest extends VoiceConsoleCommandExtensionTest {
@BeforeEach
public void setUp() throws IOException {
registerVolatileStorageService();
localeProvider = getService(LocaleProvider.class);
assertNotNull(localeProvider);