]> git.basschouten.com Git - openhab-addons.git/blob
276ec38f7b2de6f30490bdf7957d74e470fbb328
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.pulseaudio.internal;
14
15 import java.io.IOException;
16 import java.net.Socket;
17 import java.util.Locale;
18 import java.util.concurrent.ScheduledExecutorService;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.pulseaudio.internal.handler.PulseaudioHandler;
25 import org.openhab.core.library.types.PercentType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * A connection to a pulseaudio Simple TCP Protocol
31  *
32  * @author Gwendal Roulleau - Initial contribution
33  * @author Miguel Álvarez - Refactor some code from PulseAudioAudioSink here
34  *
35  */
36 @NonNullByDefault
37 public abstract class PulseaudioSimpleProtocolStream {
38
39     private final Logger logger = LoggerFactory.getLogger(PulseaudioSimpleProtocolStream.class);
40
41     protected PulseaudioHandler pulseaudioHandler;
42     protected ScheduledExecutorService scheduler;
43
44     protected @Nullable Socket clientSocket;
45
46     private boolean isIdle = true;
47
48     private @Nullable ScheduledFuture<?> scheduledDisconnection;
49
50     public PulseaudioSimpleProtocolStream(PulseaudioHandler pulseaudioHandler, ScheduledExecutorService scheduler) {
51         this.pulseaudioHandler = pulseaudioHandler;
52         this.scheduler = scheduler;
53     }
54
55     /**
56      * Connect to pulseaudio with the simple protocol
57      *
58      * @throws IOException
59      * @throws InterruptedException when interrupted during the loading module wait
60      */
61     public void connectIfNeeded() throws IOException, InterruptedException {
62         Socket clientSocketLocal = clientSocket;
63         if (clientSocketLocal == null || !clientSocketLocal.isConnected() || clientSocketLocal.isClosed()) {
64             logger.debug("Simple TCP Stream connecting");
65             String host = pulseaudioHandler.getHost();
66             int port = pulseaudioHandler.getSimpleTcpPort();
67             clientSocket = new Socket(host, port);
68             clientSocket.setSoTimeout(pulseaudioHandler.getBasicProtocolSOTimeout());
69         }
70     }
71
72     /**
73      * Disconnect the socket to pulseaudio simple protocol
74      */
75     public void disconnect() {
76         final Socket clientSocketLocal = clientSocket;
77         if (clientSocketLocal != null && isIdle) {
78             logger.debug("Simple TCP Stream disconnecting");
79             try {
80                 clientSocketLocal.close();
81             } catch (IOException ignored) {
82             }
83         } else {
84             logger.debug("Stream still running or socket not open");
85         }
86     }
87
88     public void scheduleDisconnect() {
89         if (scheduledDisconnection != null) {
90             scheduledDisconnection.cancel(true);
91         }
92         int idleTimeout = pulseaudioHandler.getIdleTimeout();
93         if (idleTimeout > -1) {
94             logger.debug("Scheduling disconnect");
95             scheduledDisconnection = scheduler.schedule(this::disconnect, idleTimeout, TimeUnit.MILLISECONDS);
96         }
97     }
98
99     public PercentType getVolume() {
100         return new PercentType(pulseaudioHandler.getLastVolume());
101     }
102
103     public void setVolume(PercentType volume) {
104         pulseaudioHandler.setVolume(volume.intValue());
105     }
106
107     public String getId() {
108         return pulseaudioHandler.getThing().getUID().toString();
109     }
110
111     public String getLabel(@Nullable Locale locale) {
112         var label = pulseaudioHandler.getThing().getLabel();
113         return label != null ? label : pulseaudioHandler.getThing().getUID().getId();
114     }
115
116     public void setIdle(boolean idle) {
117         isIdle = idle;
118     }
119 }