mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-25 11:45:49 +01:00
[voice] Avoid build a context to stop a dialog (#3206)
Signed-off-by: Miguel Álvarez <miguelwork92@gmail.com>
This commit is contained in:
parent
1b8280812b
commit
5e6770749e
@ -325,16 +325,15 @@ public class VoiceResource implements RESTResource {
|
||||
@ApiResponse(responseCode = "400", description = "No dialog processing is started for the audio source.") })
|
||||
public Response stopDialog(
|
||||
@QueryParam("sourceId") @Parameter(description = "source ID") @Nullable String sourceId) {
|
||||
var dialogContextBuilder = voiceManager.getDialogContextBuilder();
|
||||
AudioSource source = null;
|
||||
if (sourceId != null) {
|
||||
AudioSource source = audioManager.getSource(sourceId);
|
||||
source = audioManager.getSource(sourceId);
|
||||
if (source == null) {
|
||||
return JSONResponse.createErrorResponse(Status.NOT_FOUND, "Audio source not found");
|
||||
}
|
||||
dialogContextBuilder.withSource(source);
|
||||
}
|
||||
try {
|
||||
voiceManager.stopDialog(dialogContextBuilder.build());
|
||||
voiceManager.stopDialog(source);
|
||||
return Response.ok(null, MediaType.TEXT_PLAIN).build();
|
||||
} catch (IllegalStateException e) {
|
||||
return JSONResponse.createErrorResponse(Status.BAD_REQUEST, e.getMessage());
|
||||
|
@ -327,16 +327,15 @@ public class Voice {
|
||||
@ActionDoc(text = "stops dialog processing for a given audio source")
|
||||
public static void stopDialog(@ParamDoc(name = "source") @Nullable String source) {
|
||||
try {
|
||||
var dialogContextBuilder = VoiceActionService.voiceManager.getDialogContextBuilder();
|
||||
AudioSource audioSource = null;
|
||||
if (source != null) {
|
||||
AudioSource audioSource = VoiceActionService.audioManager.getSource(source);
|
||||
audioSource = VoiceActionService.audioManager.getSource(source);
|
||||
if (audioSource == null) {
|
||||
logger.warn("Failed stopping dialog processing: audio source '{}' not found", source);
|
||||
return;
|
||||
}
|
||||
dialogContextBuilder.withSource(audioSource);
|
||||
}
|
||||
VoiceActionService.voiceManager.stopDialog(dialogContextBuilder.build());
|
||||
VoiceActionService.voiceManager.stopDialog(audioSource);
|
||||
} catch (IllegalStateException e) {
|
||||
logger.warn("Failed stopping dialog processing: {}", e.getMessage());
|
||||
}
|
||||
|
@ -223,7 +223,6 @@ public interface VoiceManager {
|
||||
* @param source the audio source or null to consider the default audio source
|
||||
* @throws IllegalStateException if no dialog is started for the audio source
|
||||
*/
|
||||
@Deprecated
|
||||
void stopDialog(@Nullable AudioSource source) throws IllegalStateException;
|
||||
|
||||
/**
|
||||
|
@ -586,30 +586,30 @@ public class VoiceManagerImpl implements VoiceManager, ConfigOptionProvider, Dia
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public void stopDialog(@Nullable AudioSource source) throws IllegalStateException {
|
||||
var builder = getDialogContextBuilder();
|
||||
if (source != null) {
|
||||
builder.withSource(source);
|
||||
AudioSource audioSource = (source == null) ? audioManager.getSource() : source;
|
||||
if (audioSource != null) {
|
||||
DialogProcessor processor = dialogProcessors.remove(audioSource.getId());
|
||||
singleDialogProcessors.values().removeIf(e -> !e.isProcessing());
|
||||
if (processor == null) {
|
||||
processor = singleDialogProcessors.get(audioSource.getId());
|
||||
}
|
||||
if (processor != null) {
|
||||
processor.stop();
|
||||
logger.debug("Dialog stopped for source {} ({})", audioSource.getLabel(null), audioSource.getId());
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
String.format("Cannot stop dialog as no dialog is started for audio source '%s'.",
|
||||
audioSource.getLabel(null)));
|
||||
}
|
||||
} else {
|
||||
throw new IllegalStateException("Cannot stop dialog as audio source is missing.");
|
||||
}
|
||||
stopDialog(builder.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopDialog(DialogContext context) throws IllegalStateException {
|
||||
AudioSource audioSource = context.source();
|
||||
DialogProcessor processor = dialogProcessors.remove(audioSource.getId());
|
||||
singleDialogProcessors.values().removeIf(e -> !e.isProcessing());
|
||||
if (processor == null) {
|
||||
processor = singleDialogProcessors.get(audioSource.getId());
|
||||
}
|
||||
if (processor != null) {
|
||||
processor.stop();
|
||||
logger.debug("Dialog stopped for source {} ({})", audioSource.getLabel(null), audioSource.getId());
|
||||
} else {
|
||||
throw new IllegalStateException(String.format(
|
||||
"Cannot stop dialog as no dialog is started for audio source '%s'.", audioSource.getLabel(null)));
|
||||
}
|
||||
stopDialog(context.source());
|
||||
}
|
||||
|
||||
private void stopAllDialogs() {
|
||||
|
Loading…
Reference in New Issue
Block a user