[audio] Add missing method getSource with sourceId as parameter (#2792)

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
pull/2795/head
lolodomo 2022-02-20 20:45:12 +01:00 committed by GitHub
parent 2e0b242099
commit 473198f964
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 48 deletions

View File

@ -149,6 +149,23 @@ public interface AudioManager {
*/
Set<AudioSource> 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<String> 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<AudioSink> 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<String> getSourceIds(String pattern);
/**
* Retrieves the sink for a given id
*

View File

@ -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<String> getSourceIds(String pattern) {
String regex = pattern.replace("?", ".?").replace("*", ".*?");
Set<String> 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<String> getSourceIds(String pattern) {
String regex = pattern.replace("?", ".?").replace("*", ".*?");
Set<String> 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);

View File

@ -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<AudioSource> sources = audioManager.getAllSources();
for (AudioSource source : sources) {
if (source.getId().equals(sourceId)) {
return source;
}
}
return null;
}
}

View File

@ -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<AudioSource> sources = audioManager.getAllSources();
for (AudioSource source : sources) {
if (source.getId().equals(sourceId)) {
return source;
}
}
return null;
}
}