From 473198f96430697238ea8193fc502d2d42dd4ddd Mon Sep 17 00:00:00 2001 From: lolodomo Date: Sun, 20 Feb 2022 20:45:12 +0100 Subject: [PATCH] [audio] Add missing method getSource with sourceId as parameter (#2792) Signed-off-by: Laurent Garnier --- .../org/openhab/core/audio/AudioManager.java | 25 +++++++++----- .../core/audio/internal/AudioManagerImpl.java | 33 +++++++++++-------- .../io/rest/voice/internal/VoiceResource.java | 15 ++------- .../VoiceConsoleCommandExtension.java | 15 ++------- 4 files changed, 40 insertions(+), 48 deletions(-) diff --git a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/AudioManager.java b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/AudioManager.java index 00cdde080b..de11b2fcbe 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/AudioManager.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/AudioManager.java @@ -149,6 +149,23 @@ public interface AudioManager { */ Set getAllSources(); + /** + * Retrieves the audio sources for a given id + * + * @param sourceId the id of the audio sources or null for the default + * @return the audio sources for the id or the default audio sources + */ + @Nullable + AudioSource getSource(@Nullable String sourceId); + + /** + * Get a list of source ids that match a given pattern + * + * @param pattern pattern to search, can include `*` and `?` placeholders + * @return ids of matching sources + */ + Set getSourceIds(String pattern); + /** * Retrieves an AudioSink. * If a default name is configured and the service available, this is returned. If no default name is configured, @@ -168,14 +185,6 @@ public interface AudioManager { */ Set getAllSinks(); - /** - * Get a list of source ids that match a given pattern - * - * @param pattern pattern to search, can include `*` and `?` placeholders - * @return ids of matching sources - */ - Set getSourceIds(String pattern); - /** * Retrieves the sink for a given id * diff --git a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/AudioManagerImpl.java b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/AudioManagerImpl.java index 8ac7184d98..37d033faa2 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/AudioManagerImpl.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/AudioManagerImpl.java @@ -231,6 +231,25 @@ public class AudioManagerImpl implements AudioManager, ConfigOptionProvider { return new HashSet<>(audioSources.values()); } + @Override + public @Nullable AudioSource getSource(@Nullable String sourceId) { + return (sourceId == null) ? getSource() : audioSources.get(sourceId); + } + + @Override + public Set getSourceIds(String pattern) { + String regex = pattern.replace("?", ".?").replace("*", ".*?"); + Set matchedSources = new HashSet<>(); + + for (String aSource : audioSources.keySet()) { + if (aSource.matches(regex)) { + matchedSources.add(aSource); + } + } + + return matchedSources; + } + @Override public @Nullable AudioSink getSink() { AudioSink sink = null; @@ -252,20 +271,6 @@ public class AudioManagerImpl implements AudioManager, ConfigOptionProvider { return new HashSet<>(audioSinks.values()); } - @Override - public Set getSourceIds(String pattern) { - String regex = pattern.replace("?", ".?").replace("*", ".*?"); - Set matchedSources = new HashSet<>(); - - for (String aSource : audioSources.keySet()) { - if (aSource.matches(regex)) { - matchedSources.add(aSource); - } - } - - return matchedSources; - } - @Override public @Nullable AudioSink getSink(@Nullable String sinkId) { return (sinkId == null) ? getSink() : audioSinks.get(sinkId); 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 0324de82d8..13ed4403b0 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 @@ -14,7 +14,6 @@ package org.openhab.core.io.rest.voice.internal; import java.util.List; import java.util.Locale; -import java.util.Set; import java.util.stream.Collectors; import javax.annotation.security.RolesAllowed; @@ -239,7 +238,7 @@ public class VoiceResource implements RESTResource { @QueryParam("listeningItem") @Parameter(description = "listening item") @Nullable String listeningItem) { AudioSource source = null; if (sourceId != null) { - source = getSource(sourceId); + source = audioManager.getSource(sourceId); if (source == null) { return JSONResponse.createErrorResponse(Status.NOT_FOUND, "Audio source not found"); } @@ -300,7 +299,7 @@ public class VoiceResource implements RESTResource { @QueryParam("sourceId") @Parameter(description = "source ID") @Nullable String sourceId) { AudioSource source = null; if (sourceId != null) { - source = getSource(sourceId); + source = audioManager.getSource(sourceId); if (source == null) { return JSONResponse.createErrorResponse(Status.NOT_FOUND, "Audio source not found"); } @@ -313,14 +312,4 @@ public class VoiceResource implements RESTResource { return JSONResponse.createErrorResponse(Status.BAD_REQUEST, e.getMessage()); } } - - private @Nullable AudioSource getSource(String sourceId) { - Set sources = audioManager.getAllSources(); - for (AudioSource source : sources) { - if (source.getId().equals(sourceId)) { - return source; - } - } - return null; - } } diff --git a/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/internal/VoiceConsoleCommandExtension.java b/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/internal/VoiceConsoleCommandExtension.java index bf85da76fb..1af0367f11 100644 --- a/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/internal/VoiceConsoleCommandExtension.java +++ b/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/internal/VoiceConsoleCommandExtension.java @@ -16,7 +16,6 @@ import java.util.Arrays; import java.util.List; import java.util.Locale; import java.util.Objects; -import java.util.Set; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @@ -117,7 +116,7 @@ public class VoiceConsoleCommandExtension extends AbstractConsoleCommandExtensio return; case SUBCMD_START_DIALOG: try { - AudioSource source = args.length < 2 ? null : getSource(args[1]); + AudioSource source = args.length < 2 ? null : audioManager.getSource(args[1]); HumanLanguageInterpreter hli = args.length < 3 ? null : voiceManager.getHLI(args[2]); AudioSink sink = args.length < 4 ? null : audioManager.getSink(args[3]); String keyword = args.length < 5 ? null : args[4]; @@ -129,7 +128,7 @@ public class VoiceConsoleCommandExtension extends AbstractConsoleCommandExtensio break; case SUBCMD_STOP_DIALOG: try { - voiceManager.stopDialog(args.length < 2 ? null : getSource(args[1])); + voiceManager.stopDialog(args.length < 2 ? null : audioManager.getSource(args[1])); } catch (IllegalStateException e) { console.println(Objects.requireNonNullElse(e.getMessage(), "An error occurred while stopping the dialog")); @@ -195,14 +194,4 @@ public class VoiceConsoleCommandExtension extends AbstractConsoleCommandExtensio } voiceManager.say(msg.toString()); } - - private @Nullable AudioSource getSource(@Nullable String sourceId) { - Set sources = audioManager.getAllSources(); - for (AudioSource source : sources) { - if (source.getId().equals(sourceId)) { - return source; - } - } - return null; - } }