2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.pulseaudio.internal;
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;
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;
30 * A connection to a pulseaudio Simple TCP Protocol
32 * @author Gwendal Roulleau - Initial contribution
33 * @author Miguel Álvarez - Refactor some code from PulseAudioAudioSink here
37 public abstract class PulseaudioSimpleProtocolStream {
39 private final Logger logger = LoggerFactory.getLogger(PulseaudioSimpleProtocolStream.class);
41 protected PulseaudioHandler pulseaudioHandler;
42 protected ScheduledExecutorService scheduler;
44 protected @Nullable Socket clientSocket;
46 private boolean isIdle = true;
48 private @Nullable ScheduledFuture<?> scheduledDisconnection;
50 public PulseaudioSimpleProtocolStream(PulseaudioHandler pulseaudioHandler, ScheduledExecutorService scheduler) {
51 this.pulseaudioHandler = pulseaudioHandler;
52 this.scheduler = scheduler;
56 * Connect to pulseaudio with the simple protocol
59 * @throws InterruptedException when interrupted during the loading module wait
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());
73 * Disconnect the socket to pulseaudio simple protocol
75 public void disconnect() {
76 final Socket clientSocketLocal = clientSocket;
77 if (clientSocketLocal != null && isIdle) {
78 logger.debug("Simple TCP Stream disconnecting");
80 clientSocketLocal.close();
81 } catch (IOException ignored) {
84 logger.debug("Stream still running or socket not open");
88 public void scheduleDisconnect() {
89 if (scheduledDisconnection != null) {
90 scheduledDisconnection.cancel(true);
92 int idleTimeout = pulseaudioHandler.getIdleTimeout();
93 if (idleTimeout > -1) {
94 logger.debug("Scheduling disconnect");
95 scheduledDisconnection = scheduler.schedule(this::disconnect, idleTimeout, TimeUnit.MILLISECONDS);
99 public PercentType getVolume() {
100 return new PercentType(pulseaudioHandler.getLastVolume());
103 public void setVolume(PercentType volume) {
104 pulseaudioHandler.setVolume(volume.intValue());
107 public String getId() {
108 return pulseaudioHandler.getThing().getUID().toString();
111 public String getLabel(@Nullable Locale locale) {
112 var label = pulseaudioHandler.getThing().getLabel();
113 return label != null ? label : pulseaudioHandler.getThing().getUID().getId();
116 public void setIdle(boolean idle) {