[pulseaudio] Fix #8843. Stop spamming logs when pulseaudio device is offline (#8850)

Signed-off-by: Robert von Könemann <lordtaifleh@gmail.com>
This commit is contained in:
Robert von Könemann 2020-10-24 21:00:17 +02:00 committed by GitHub
parent 91fbe746e9
commit fa9e3db34b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,6 +17,7 @@ import static org.openhab.binding.pulseaudio.internal.PulseaudioBindingConstants
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.net.NoRouteToHostException;
import java.net.Socket; import java.net.Socket;
import java.net.SocketException; import java.net.SocketException;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
@ -132,7 +133,7 @@ public class PulseaudioClient {
} }
public boolean isConnected() { public boolean isConnected() {
return client.isConnected(); return client != null ? client.isConnected() : false;
} }
/** /**
@ -539,14 +540,16 @@ public class PulseaudioClient {
private void sendRawCommand(String command) { private void sendRawCommand(String command) {
checkConnection(); checkConnection();
try { if (client != null) {
PrintStream out = new PrintStream(client.getOutputStream(), true); try {
logger.trace("sending command {} to pa-server {}", command, host); PrintStream out = new PrintStream(client.getOutputStream(), true);
out.print(command + "\r\n"); logger.trace("sending command {} to pa-server {}", command, host);
out.close(); out.print(command + "\r\n");
client.close(); out.close();
} catch (IOException e) { client.close();
logger.error("{}", e.getLocalizedMessage(), e); } catch (IOException e) {
logger.error("{}", e.getLocalizedMessage(), e);
}
} }
} }
@ -554,41 +557,43 @@ public class PulseaudioClient {
logger.trace("_sendRawRequest({})", command); logger.trace("_sendRawRequest({})", command);
checkConnection(); checkConnection();
String result = ""; String result = "";
try { if (client != null) {
PrintStream out = new PrintStream(client.getOutputStream(), true);
out.print(command + "\r\n");
InputStream instr = client.getInputStream();
try { try {
byte[] buff = new byte[1024]; PrintStream out = new PrintStream(client.getOutputStream(), true);
int retRead = 0; out.print(command + "\r\n");
int lc = 0;
do { InputStream instr = client.getInputStream();
retRead = instr.read(buff);
lc++; try {
if (retRead > 0) { byte[] buff = new byte[1024];
String line = new String(buff, 0, retRead); int retRead = 0;
// System.out.println("'"+line+"'"); int lc = 0;
if (line.endsWith(">>> ") && lc > 1) { do {
result += line.substring(0, line.length() - 4); retRead = instr.read(buff);
break; lc++;
if (retRead > 0) {
String line = new String(buff, 0, retRead);
// System.out.println("'"+line+"'");
if (line.endsWith(">>> ") && lc > 1) {
result += line.substring(0, line.length() - 4);
break;
}
result += line.trim();
} }
result += line.trim(); } while (retRead > 0);
} } catch (SocketTimeoutException e) {
} while (retRead > 0); // Timeout -> as newer PA versions (>=5.0) do not send the >>> we have no chance
} catch (SocketTimeoutException e) { // to detect the end of the answer, except by this timeout
// Timeout -> as newer PA versions (>=5.0) do not send the >>> we have no chance } catch (IOException e) {
// to detect the end of the answer, except by this timeout logger.error("Exception while reading socket: {}", e.getMessage());
}
instr.close();
out.close();
client.close();
return result;
} catch (IOException e) { } catch (IOException e) {
logger.error("Exception while reading socket: {}", e.getMessage()); logger.error("{}", e.getLocalizedMessage(), e);
} }
instr.close();
out.close();
client.close();
return result;
} catch (IOException e) {
logger.error("{}", e.getLocalizedMessage(), e);
} }
return result; return result;
} }
@ -612,6 +617,8 @@ public class PulseaudioClient {
client.setSoTimeout(500); client.setSoTimeout(500);
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
logger.error("unknown socket host {}", host); logger.error("unknown socket host {}", host);
} catch (NoRouteToHostException e) {
logger.error("no route to host {}", host);
} catch (SocketException e) { } catch (SocketException e) {
logger.error("{}", e.getLocalizedMessage(), e); logger.error("{}", e.getLocalizedMessage(), e);
} }