]> git.basschouten.com Git - openhab-addons.git/blob
98c8bfb3d78dcbd68f0d6d00ede98b8db4b4c35a
[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.getSimpleTcpPortAndLoadModuleIfNecessary();
67             var clientSocketFinal = new Socket(host, port);
68             clientSocketFinal.setSoTimeout(pulseaudioHandler.getBasicProtocolSOTimeout());
69             clientSocket = clientSocketFinal;
70         }
71     }
72
73     /**
74      * Disconnect the socket to pulseaudio simple protocol
75      */
76     public void disconnect() {
77         final Socket clientSocketLocal = clientSocket;
78         if (clientSocketLocal != null && isIdle) {
79             logger.debug("Simple TCP Stream disconnecting");
80             try {
81                 clientSocketLocal.close();
82             } catch (IOException ignored) {
83             }
84         } else {
85             logger.debug("Stream still running or socket not open");
86         }
87     }
88
89     public void scheduleDisconnect() {
90         var scheduledDisconnectionFinal = scheduledDisconnection;
91         if (scheduledDisconnectionFinal != null) {
92             scheduledDisconnectionFinal.cancel(true);
93         }
94         int idleTimeout = pulseaudioHandler.getIdleTimeout();
95         if (idleTimeout > -1) {
96             logger.debug("Scheduling disconnect");
97             scheduledDisconnection = scheduler.schedule(this::disconnect, idleTimeout, TimeUnit.MILLISECONDS);
98         }
99     }
100
101     public PercentType getVolume() {
102         return new PercentType(pulseaudioHandler.getLastVolume());
103     }
104
105     public void setVolume(PercentType volume) {
106         pulseaudioHandler.setVolume(volume.intValue());
107     }
108
109     public String getId() {
110         return pulseaudioHandler.getThing().getUID().toString();
111     }
112
113     public String getLabel(@Nullable Locale locale) {
114         var label = pulseaudioHandler.getThing().getLabel();
115         return label != null ? label : pulseaudioHandler.getThing().getUID().getId();
116     }
117
118     public void setIdle(boolean idle) {
119         isIdle = idle;
120     }
121 }