]> git.basschouten.com Git - openhab-addons.git/commitdiff
[pulseaudio] Fix #8843. Stop spamming logs when pulseaudio device is offline (#8850)
authorRobert von Könemann <vKnmnn@users.noreply.github.com>
Sat, 24 Oct 2020 19:00:17 +0000 (21:00 +0200)
committerGitHub <noreply@github.com>
Sat, 24 Oct 2020 19:00:17 +0000 (12:00 -0700)
Signed-off-by: Robert von Könemann <lordtaifleh@gmail.com>
bundles/org.openhab.binding.pulseaudio/src/main/java/org/openhab/binding/pulseaudio/internal/PulseaudioClient.java

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