Skip to content

Commit

Permalink
[voice] Support managed dialogs (#3264)
Browse files Browse the repository at this point in the history
* [voice] introduce methods for dialog persistency

Signed-off-by: Miguel Álvarez <miguelwork92@gmail.com>
  • Loading branch information
GiviMAD authored Feb 8, 2023
1 parent a91b7f6 commit c30e4b8
Show file tree
Hide file tree
Showing 12 changed files with 395 additions and 37 deletions.
11 changes: 11 additions & 0 deletions bundles/org.openhab.core.voice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@
<artifactId>org.openhab.core.io.console</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core.test</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,23 @@ public Builder withSink(@Nullable AudioSink sink) {
}

public Builder withKS(@Nullable KSService service) {
this.ks = service;
if (service != null) {
this.ks = service;
}
return this;
}

public Builder withSTT(@Nullable STTService service) {
this.stt = service;
if (service != null) {
this.stt = service;
}
return this;
}

public Builder withTTS(@Nullable TTSService service) {
this.tts = service;
if (service != null) {
this.tts = service;
}
return this;
}

Expand All @@ -156,32 +162,44 @@ public Builder withHLIs(Collection<HumanLanguageInterpreter> services) {
}

public Builder withHLIs(List<HumanLanguageInterpreter> services) {
this.hlis = services;
if (!services.isEmpty()) {
this.hlis = services;
}
return this;
}

public Builder withKeyword(String keyword) {
this.keyword = keyword;
public Builder withKeyword(@Nullable String keyword) {
if (keyword != null && !keyword.isBlank()) {
this.keyword = keyword;
}
return this;
}

public Builder withVoice(@Nullable Voice voice) {
this.voice = voice;
if (voice != null) {
this.voice = voice;
}
return this;
}

public Builder withListeningItem(@Nullable String listeningItem) {
this.listeningItem = listeningItem;
if (listeningItem != null) {
this.listeningItem = listeningItem;
}
return this;
}

public Builder withMelody(@Nullable String listeningMelody) {
this.listeningMelody = listeningMelody;
if (listeningMelody != null) {
this.listeningMelody = listeningMelody;
}
return this;
}

public Builder withLocale(Locale locale) {
this.locale = locale;
public Builder withLocale(@Nullable Locale locale) {
if (locale != null) {
this.locale = locale;
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.voice;

import java.util.List;
import java.util.Locale;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* Describes dialog desired services and options.
*
* @author Miguel Álvarez - Initial contribution
*/
@NonNullByDefault
public class DialogRegistration {
/**
* Dialog audio source id
*/
public String sourceId;
/**
* Dialog audio sink id
*/
public String sinkId;
/**
* Preferred keyword-spotting service
*/
public @Nullable String ksId;
/**
* Selected keyword for spotting
*/
public @Nullable String keyword;
/**
* Preferred speech-to-text service id
*/
public @Nullable String sttId;
/**
* Preferred text-to-speech service id
*/
public @Nullable String ttsId;
/**
* Preferred voice id
*/
public @Nullable String voiceId;
/**
* List of interpreters
*/
public List<String> hliIds = List.of();
/**
* Dialog locale
*/
public @Nullable Locale locale;
/**
* Linked listening item
*/
public @Nullable String listeningItem;
/**
* Custom listening melody
*/
public @Nullable String listeningMelody;
/**
* True if an associated dialog is running
*/
public boolean running = false;

public DialogRegistration(String sourceId, String sinkId) {
this.sourceId = sourceId;
this.sinkId = sinkId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,38 @@ void listenAndAnswer(@Nullable STTService stt, @Nullable TTSService tts, @Nullab
*/
void listenAndAnswer(DialogContext context) throws IllegalStateException;

/**
* Register a dialog, so it will be persisted and started any time the required services are available.
*
* Only one registration can be done for an audio source.
*
* @param registration with the desired services ids and options for the dialog
*
* @throws IllegalStateException if there is another registration for the same source
*/
void registerDialog(DialogRegistration registration) throws IllegalStateException;

/**
* Removes a dialog registration and stops the associate dialog.
*
* @param registration with the desired services ids and options for the dialog
*/
void unregisterDialog(DialogRegistration registration);

/**
* Removes a dialog registration and stops the associate dialog.
*
* @param sourceId the registration audio source id.
*/
void unregisterDialog(String sourceId);

/**
* List current dialog registrations
*
* @return a list of {@link DialogRegistration}
*/
List<DialogRegistration> getDialogRegistrations();

/**
* Retrieves a TTS service.
* If a default name is configured and the service available, this is returned. Otherwise, the first available
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public class DialogProcessor implements KSListener, STTListener {

private final Logger logger = LoggerFactory.getLogger(DialogProcessor.class);

private final DialogContext dialogContext;
public final DialogContext dialogContext;
private @Nullable List<ToneSynthesizer.Tone> listeningMelody;
private final EventPublisher eventPublisher;
private final TranslationProvider i18nProvider;
Expand Down Expand Up @@ -220,6 +220,7 @@ public void stop() {
closeStreamKS();
toggleProcessing(false);
playStopSound();
eventListener.onDialogStopped(dialogContext);
}

/**
Expand Down Expand Up @@ -458,5 +459,12 @@ public interface DialogEventListener {
* @param context used by the dialog processor
*/
void onBeforeDialogInterpretation(DialogContext context);

/**
* Runs whenever the dialog it stopped
*
* @param context used by the dialog processor
*/
void onDialogStopped(DialogContext context);
}
}
Loading

0 comments on commit c30e4b8

Please sign in to comment.