Add null annotations to org.openhab.core.voice (#2255)

Allows for using null annotations with the voice TTS add-ons.

Signed-off-by: Wouter Born <github@maindrain.net>
pull/2254/head
Wouter Born 2021-03-25 20:19:11 +01:00 committed by GitHub
parent 07b95ca668
commit db11cfda51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 134 additions and 42 deletions

View File

@ -12,10 +12,13 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* A {@link STTEvent} fired when the {@link STTService} starts hearing audio. * A {@link STTEvent} fired when the {@link STTService} starts hearing audio.
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public class AudioStartEvent implements STTEvent { public class AudioStartEvent implements STTEvent {
} }

View File

@ -12,10 +12,13 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* A {@link STTEvent} fired when the {@link STTService} stops hearing audio. * A {@link STTEvent} fired when the {@link STTService} stops hearing audio.
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public class AudioStopEvent implements STTEvent { public class AudioStopEvent implements STTEvent {
} }

View File

@ -12,11 +12,14 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* A {@link KSEvent} fired when the {@link KSService} encounters an error. * A {@link KSEvent} fired when the {@link KSService} encounters an error.
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public class KSErrorEvent implements KSEvent { public class KSErrorEvent implements KSEvent {
/** /**
* The message describing the error * The message describing the error

View File

@ -12,10 +12,13 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* A tagging interface for keyword spotting events. * A tagging interface for keyword spotting events.
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public interface KSEvent { public interface KSEvent {
} }

View File

@ -12,11 +12,14 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* General purpose keyword spotting exception * General purpose keyword spotting exception
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public class KSException extends Exception { public class KSException extends Exception {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;

View File

@ -12,6 +12,8 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* The listener interface for receiving {@link KSEvent} events. * The listener interface for receiving {@link KSEvent} events.
* *
@ -22,6 +24,7 @@ package org.openhab.core.voice;
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public interface KSListener { public interface KSListener {
/** /**
* Invoked when a {@link KSEvent} event occurs during keyword spotting. * Invoked when a {@link KSEvent} event occurs during keyword spotting.

View File

@ -15,6 +15,8 @@ package org.openhab.core.voice;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.audio.AudioFormat; import org.openhab.core.audio.AudioFormat;
import org.openhab.core.audio.AudioStream; import org.openhab.core.audio.AudioStream;
@ -24,6 +26,7 @@ import org.openhab.core.audio.AudioStream;
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
* @author Kai Kreuzer - Refactored to use AudioStream * @author Kai Kreuzer - Refactored to use AudioStream
*/ */
@NonNullByDefault
public interface KSService { public interface KSService {
/** /**
@ -39,7 +42,7 @@ public interface KSService {
* @param locale the locale to provide the label for * @param locale the locale to provide the label for
* @return a localized string to be used in UIs * @return a localized string to be used in UIs
*/ */
public String getLabel(Locale locale); public String getLabel(@Nullable Locale locale);
/** /**
* Obtain the Locales available from this KSService * Obtain the Locales available from this KSService

View File

@ -12,11 +12,14 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* An handle to a {@link KSService} * An handle to a {@link KSService}
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public interface KSServiceHandle { public interface KSServiceHandle {
/** /**
* Aborts keyword spotting in the associated {@link KSService} * Aborts keyword spotting in the associated {@link KSService}

View File

@ -12,11 +12,14 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* A {@link KSEvent} fired when the {@link KSService} spots a keyword. * A {@link KSEvent} fired when the {@link KSService} spots a keyword.
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
* @author Yannick Schaus - Removed AudioSource information * @author Yannick Schaus - Removed AudioSource information
*/ */
@NonNullByDefault
public class KSpottedEvent implements KSEvent { public class KSpottedEvent implements KSEvent {
} }

View File

@ -12,10 +12,13 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* A {@link STTEvent} fired when the {@link STTService} starts recognition. * A {@link STTEvent} fired when the {@link STTService} starts recognition.
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public class RecognitionStartEvent implements STTEvent { public class RecognitionStartEvent implements STTEvent {
} }

View File

@ -12,10 +12,13 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* A {@link STTEvent} fired when the {@link STTService} stops recognition. * A {@link STTEvent} fired when the {@link STTService} stops recognition.
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public class RecognitionStopEvent implements STTEvent { public class RecognitionStopEvent implements STTEvent {
} }

View File

@ -12,10 +12,13 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* A tagging interface for speech-to-text events. * A tagging interface for speech-to-text events.
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public interface STTEvent { public interface STTEvent {
} }

View File

@ -12,11 +12,14 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* General purpose STT exception * General purpose STT exception
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public class STTException extends Exception { public class STTException extends Exception {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;

View File

@ -12,6 +12,8 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* The listener interface for receiving {@link STTEvent} events. * The listener interface for receiving {@link STTEvent} events.
* *
@ -22,6 +24,7 @@ package org.openhab.core.voice;
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public interface STTListener { public interface STTListener {
/** /**

View File

@ -15,6 +15,8 @@ package org.openhab.core.voice;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.audio.AudioFormat; import org.openhab.core.audio.AudioFormat;
import org.openhab.core.audio.AudioStream; import org.openhab.core.audio.AudioStream;
@ -23,6 +25,7 @@ import org.openhab.core.audio.AudioStream;
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public interface STTService { public interface STTService {
/** /**
@ -38,7 +41,7 @@ public interface STTService {
* @param locale the locale to provide the label for * @param locale the locale to provide the label for
* @return a localized string to be used in UIs * @return a localized string to be used in UIs
*/ */
public String getLabel(Locale locale); public String getLabel(@Nullable Locale locale);
/** /**
* Obtain the Locales available from this STTService * Obtain the Locales available from this STTService

View File

@ -12,11 +12,14 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* An handle to a {@link STTService} * An handle to a {@link STTService}
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public interface STTServiceHandle { public interface STTServiceHandle {
/** /**
* Aborts recognition in the associated {@link STTService} * Aborts recognition in the associated {@link STTService}

View File

@ -12,11 +12,14 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* A {@link STTEvent} fired when the {@link STTService} encounters an error. * A {@link STTEvent} fired when the {@link STTService} encounters an error.
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public class SpeechRecognitionErrorEvent implements STTEvent { public class SpeechRecognitionErrorEvent implements STTEvent {
/** /**
* The message describing the error * The message describing the error

View File

@ -12,11 +12,14 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* A {@link STTEvent} fired when the {@link STTService} recognizes speech. * A {@link STTEvent} fired when the {@link STTService} recognizes speech.
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public class SpeechRecognitionEvent implements STTEvent { public class SpeechRecognitionEvent implements STTEvent {
/** /**
* Confidence of recognized speech * Confidence of recognized speech

View File

@ -12,10 +12,13 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* A {@link STTEvent} fired when the {@link STTService} starts hearing speech. * A {@link STTEvent} fired when the {@link STTService} starts hearing speech.
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public class SpeechStartEvent implements STTEvent { public class SpeechStartEvent implements STTEvent {
} }

View File

@ -12,10 +12,13 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* A {@link STTEvent} fired when the {@link STTService} stops hearing speech. * A {@link STTEvent} fired when the {@link STTService} stops hearing speech.
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public class SpeechStopEvent implements STTEvent { public class SpeechStopEvent implements STTEvent {
} }

View File

@ -12,11 +12,14 @@
*/ */
package org.openhab.core.voice; package org.openhab.core.voice;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* General purpose TTS exception * General purpose TTS exception
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public class TTSException extends Exception { public class TTSException extends Exception {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;

View File

@ -15,6 +15,8 @@ package org.openhab.core.voice;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.audio.AudioFormat; import org.openhab.core.audio.AudioFormat;
import org.openhab.core.audio.AudioStream; import org.openhab.core.audio.AudioStream;
@ -24,6 +26,7 @@ import org.openhab.core.audio.AudioStream;
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
* @author Kai Kreuzer - Refactored to use AudioStreams * @author Kai Kreuzer - Refactored to use AudioStreams
*/ */
@NonNullByDefault
public interface TTSService { public interface TTSService {
/** /**
@ -39,7 +42,7 @@ public interface TTSService {
* @param locale the locale to provide the label for * @param locale the locale to provide the label for
* @return a localized string to be used in UIs * @return a localized string to be used in UIs
*/ */
public String getLabel(Locale locale); public String getLabel(@Nullable Locale locale);
/** /**
* Obtain the voices available from this TTSService * Obtain the voices available from this TTSService

View File

@ -14,11 +14,14 @@ package org.openhab.core.voice;
import java.util.Locale; import java.util.Locale;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* This is the interface that a text-to-speech voice has to implement. * This is the interface that a text-to-speech voice has to implement.
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public interface Voice { public interface Voice {
/** /**

View File

@ -12,8 +12,11 @@
*/ */
package org.openhab.core.voice.internal; package org.openhab.core.voice.internal;
import static org.openhab.core.voice.internal.VoiceManagerImpl.getBestMatch;
import java.util.HashSet; import java.util.HashSet;
import java.util.Locale; import java.util.Locale;
import java.util.Set;
import org.openhab.core.audio.AudioException; import org.openhab.core.audio.AudioException;
import org.openhab.core.audio.AudioFormat; import org.openhab.core.audio.AudioFormat;
@ -104,7 +107,7 @@ public class DialogProcessor implements KSListener, STTListener {
public void start() { public void start() {
try { try {
ks.spot(this, source.getInputStream(format), locale, this.keyword); ks.spot(this, source.getInputStream(format), locale, keyword);
} catch (KSException e) { } catch (KSException e) {
logger.error("Encountered error calling spot: {}", e.getMessage()); logger.error("Encountered error calling spot: {}", e.getMessage());
} catch (AudioException e) { } catch (AudioException e) {
@ -113,10 +116,10 @@ public class DialogProcessor implements KSListener, STTListener {
} }
private void toggleProcessing(boolean value) { private void toggleProcessing(boolean value) {
if (this.processing == value) { if (processing == value) {
return; return;
} }
this.processing = value; processing = value;
if (listeningItem != null && ItemUtil.isValidItemName(listeningItem)) { if (listeningItem != null && ItemUtil.isValidItemName(listeningItem)) {
OnOffType command = (value) ? OnOffType.ON : OnOffType.OFF; OnOffType command = (value) ? OnOffType.ON : OnOffType.OFF;
eventPublisher.post(ItemEventFactory.createCommandEvent(listeningItem, command)); eventPublisher.post(ItemEventFactory.createCommandEvent(listeningItem, command));
@ -126,13 +129,12 @@ public class DialogProcessor implements KSListener, STTListener {
@Override @Override
public void ksEventReceived(KSEvent ksEvent) { public void ksEventReceived(KSEvent ksEvent) {
if (!processing) { if (!processing) {
this.isSTTServerAborting = false; isSTTServerAborting = false;
if (ksEvent instanceof KSpottedEvent) { if (ksEvent instanceof KSpottedEvent) {
toggleProcessing(true); toggleProcessing(true);
if (stt != null) { if (stt != null) {
try { try {
this.sttServiceHandle = stt.recognize(this, source.getInputStream(format), this.locale, sttServiceHandle = stt.recognize(this, source.getInputStream(format), locale, new HashSet<>());
new HashSet<>());
} catch (STTException e) { } catch (STTException e) {
say("Error during recognition: " + e.getMessage()); say("Error during recognition: " + e.getMessage());
} catch (AudioException e) { } catch (AudioException e) {
@ -149,14 +151,14 @@ public class DialogProcessor implements KSListener, STTListener {
@Override @Override
public synchronized void sttEventReceived(STTEvent sttEvent) { public synchronized void sttEventReceived(STTEvent sttEvent) {
if (sttEvent instanceof SpeechRecognitionEvent) { if (sttEvent instanceof SpeechRecognitionEvent) {
if (!this.isSTTServerAborting) { if (!isSTTServerAborting) {
this.sttServiceHandle.abort(); sttServiceHandle.abort();
this.isSTTServerAborting = true; isSTTServerAborting = true;
SpeechRecognitionEvent sre = (SpeechRecognitionEvent) sttEvent; SpeechRecognitionEvent sre = (SpeechRecognitionEvent) sttEvent;
String question = sre.getTranscript(); String question = sre.getTranscript();
try { try {
toggleProcessing(false); toggleProcessing(false);
String answer = hli.interpret(this.locale, question); String answer = hli.interpret(locale, question);
if (answer != null) { if (answer != null) {
say(answer); say(answer);
} }
@ -167,9 +169,9 @@ public class DialogProcessor implements KSListener, STTListener {
} else if (sttEvent instanceof RecognitionStopEvent) { } else if (sttEvent instanceof RecognitionStopEvent) {
toggleProcessing(false); toggleProcessing(false);
} else if (sttEvent instanceof SpeechRecognitionErrorEvent) { } else if (sttEvent instanceof SpeechRecognitionErrorEvent) {
if (!this.isSTTServerAborting) { if (!isSTTServerAborting) {
this.sttServiceHandle.abort(); sttServiceHandle.abort();
this.isSTTServerAborting = true; isSTTServerAborting = true;
toggleProcessing(false); toggleProcessing(false);
SpeechRecognitionErrorEvent sre = (SpeechRecognitionErrorEvent) sttEvent; SpeechRecognitionErrorEvent sre = (SpeechRecognitionErrorEvent) sttEvent;
say("Encountered error: " + sre.getMessage()); say("Encountered error: " + sre.getMessage());
@ -186,15 +188,23 @@ public class DialogProcessor implements KSListener, STTListener {
try { try {
Voice voice = null; Voice voice = null;
for (Voice currentVoice : tts.getAvailableVoices()) { for (Voice currentVoice : tts.getAvailableVoices()) {
if (this.locale.getLanguage().equals(currentVoice.getLocale().getLanguage())) { if (locale.getLanguage().equals(currentVoice.getLocale().getLanguage())) {
voice = currentVoice; voice = currentVoice;
break; break;
} }
} }
if (null == voice) { if (voice == null) {
throw new TTSException("Unable to find a suitable voice"); throw new TTSException("Unable to find a suitable voice");
} }
AudioStream audioStream = tts.synthesize(text, voice, null);
Set<AudioFormat> audioFormats = tts.getSupportedFormats();
AudioFormat audioFormat = getBestMatch(audioFormats, sink.getSupportedFormats());
if (audioFormat == null) {
throw new TTSException("No compatible audio format found for TTS '" + tts.getId() + "' and sink '"
+ sink.getId() + "'");
}
AudioStream audioStream = tts.synthesize(text, voice, audioFormat);
if (sink.getSupportedStreams().stream().anyMatch(clazz -> clazz.isInstance(audioStream))) { if (sink.getSupportedStreams().stream().anyMatch(clazz -> clazz.isInstance(audioStream))) {
try { try {

View File

@ -14,6 +14,7 @@ package org.openhab.core.voice;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**
@ -21,6 +22,7 @@ import org.junit.jupiter.api.Test;
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public class STTExceptionTest { public class STTExceptionTest {
/** /**

View File

@ -14,6 +14,7 @@ package org.openhab.core.voice;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**
@ -21,6 +22,7 @@ import org.junit.jupiter.api.Test;
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public class SpeechRecognitionErrorEventTest { public class SpeechRecognitionErrorEventTest {
/** /**

View File

@ -14,6 +14,7 @@ package org.openhab.core.voice;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**
@ -21,6 +22,7 @@ import org.junit.jupiter.api.Test;
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public class SpeechRecognitionEventTest { public class SpeechRecognitionEventTest {
/** /**

View File

@ -14,6 +14,7 @@ package org.openhab.core.voice;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**
@ -21,6 +22,7 @@ import org.junit.jupiter.api.Test;
* *
* @author Kelly Davis - Initial contribution * @author Kelly Davis - Initial contribution
*/ */
@NonNullByDefault
public class TTSExceptionTest { public class TTSExceptionTest {
/** /**

View File

@ -54,7 +54,7 @@ public class KSServiceStub implements KSService {
@Override @Override
public Set<Locale> getSupportedLocales() { public Set<Locale> getSupportedLocales() {
return null; return Set.of();
} }
@Override @Override

View File

@ -15,6 +15,8 @@ package org.openhab.core.voice.internal;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.audio.AudioFormat; import org.openhab.core.audio.AudioFormat;
import org.openhab.core.audio.AudioStream; import org.openhab.core.audio.AudioStream;
import org.openhab.core.voice.STTException; import org.openhab.core.voice.STTException;
@ -28,6 +30,7 @@ import org.openhab.core.voice.STTServiceHandle;
* @author Mihaela Memova - Initial contribution * @author Mihaela Memova - Initial contribution
* @author Velin Yordanov - migrated from groovy to java * @author Velin Yordanov - migrated from groovy to java
*/ */
@NonNullByDefault
public class STTServiceStub implements STTService { public class STTServiceStub implements STTService {
private static final Set<AudioFormat> SUPPORTED_FORMATS = Set.of(AudioFormat.MP3, AudioFormat.WAV); private static final Set<AudioFormat> SUPPORTED_FORMATS = Set.of(AudioFormat.MP3, AudioFormat.WAV);
@ -41,13 +44,13 @@ public class STTServiceStub implements STTService {
} }
@Override @Override
public String getLabel(Locale locale) { public String getLabel(@Nullable Locale locale) {
return STTSERVICE_STUB_LABEL; return STTSERVICE_STUB_LABEL;
} }
@Override @Override
public Set<Locale> getSupportedLocales() { public Set<Locale> getSupportedLocales() {
return null; return Set.of();
} }
@Override @Override

View File

@ -14,10 +14,12 @@ package org.openhab.core.voice.internal;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.audio.AudioFormat; import org.openhab.core.audio.AudioFormat;
import org.openhab.core.audio.AudioStream; import org.openhab.core.audio.AudioStream;
import org.openhab.core.voice.TTSException; import org.openhab.core.voice.TTSException;
@ -33,6 +35,7 @@ import org.osgi.framework.ServiceReference;
* @author Mihaela Memova - Initial contribution * @author Mihaela Memova - Initial contribution
* @author Velin Yordanov - migrated from groovy to java * @author Velin Yordanov - migrated from groovy to java
*/ */
@NonNullByDefault
public class TTSServiceStub implements TTSService { public class TTSServiceStub implements TTSService {
private static final Set<AudioFormat> SUPPORTED_FORMATS = Set.of(AudioFormat.MP3, AudioFormat.WAV); private static final Set<AudioFormat> SUPPORTED_FORMATS = Set.of(AudioFormat.MP3, AudioFormat.WAV);
@ -40,45 +43,42 @@ public class TTSServiceStub implements TTSService {
private static final String TTS_SERVICE_STUB_ID = "ttsServiceStubID"; private static final String TTS_SERVICE_STUB_ID = "ttsServiceStubID";
private static final String TTS_SERVICE_STUB_LABEL = "ttsServiceStubLabel"; private static final String TTS_SERVICE_STUB_LABEL = "ttsServiceStubLabel";
private Set<Voice> availableVoices; private @Nullable BundleContext context;
private BundleContext context;
public TTSServiceStub() {
}
public TTSServiceStub(BundleContext context) { public TTSServiceStub(BundleContext context) {
this.context = context; this.context = context;
} }
public TTSServiceStub() {
}
@Override @Override
public String getId() { public String getId() {
return TTS_SERVICE_STUB_ID; return TTS_SERVICE_STUB_ID;
} }
@Override @Override
public String getLabel(Locale locale) { public String getLabel(@Nullable Locale locale) {
return TTS_SERVICE_STUB_LABEL; return TTS_SERVICE_STUB_LABEL;
} }
@Override @Override
public Set<Voice> getAvailableVoices() { public Set<Voice> getAvailableVoices() {
availableVoices = new HashSet<>(); BundleContext bundleContext = this.context;
Collection<ServiceReference<Voice>> refs; if (bundleContext == null) {
return Set.of();
}
try { try {
refs = context.getServiceReferences(Voice.class, null); Collection<ServiceReference<Voice>> refs = bundleContext.getServiceReferences(Voice.class, null);
if (refs != null) { return refs.stream() //
for (ServiceReference<Voice> ref : refs) { .map(ref -> bundleContext.getService(ref)) //
Voice service = context.getService(ref); .filter(service -> service.getUID().startsWith(getId())) //
if (service.getUID().startsWith(getId())) { .collect(Collectors.toSet());
availableVoices.add(service);
}
}
}
} catch (InvalidSyntaxException e) { } catch (InvalidSyntaxException e) {
// If the specified filter contains an invalid filter expression that cannot be parsed. // If the specified filter contains an invalid filter expression that cannot be parsed.
return null; return Set.of();
} }
return availableVoices;
} }
@Override @Override

View File

@ -14,6 +14,7 @@ package org.openhab.core.voice.internal;
import java.util.Locale; import java.util.Locale;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.voice.Voice; import org.openhab.core.voice.Voice;
/** /**
@ -22,6 +23,7 @@ import org.openhab.core.voice.Voice;
* @author Mihaela Memova - Initial contribution * @author Mihaela Memova - Initial contribution
* @author Velin Yordanov - migrated from groovy to java * @author Velin Yordanov - migrated from groovy to java
*/ */
@NonNullByDefault
public class VoiceStub implements Voice { public class VoiceStub implements Voice {
private TTSServiceStub ttsService = new TTSServiceStub(); private TTSServiceStub ttsService = new TTSServiceStub();