2 * Copyright (c) 2010-2023 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.monopriceaudio.internal.communication;
15 import java.io.DataInputStream;
16 import java.io.DataOutputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.Socket;
20 import java.net.SocketTimeoutException;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.monopriceaudio.internal.MonopriceAudioException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Class for communicating with the MonopriceAudio device through a serial over IP connection
31 * @author Laurent Garnier - Initial contribution
32 * @author Michael Lobstein - Adapted for the MonopriceAudio binding
35 public class MonopriceAudioIpConnector extends MonopriceAudioConnector {
37 private final Logger logger = LoggerFactory.getLogger(MonopriceAudioIpConnector.class);
39 private final @Nullable String address;
40 private final int port;
41 private final String uid;
43 private @Nullable Socket clientSocket;
48 * @param address the IP address of the serial over IP device
49 * @param port the TCP port to be used
50 * @param uid the thing uid string
52 public MonopriceAudioIpConnector(@Nullable String address, int port, String uid) {
53 this.address = address;
59 public synchronized void open() throws MonopriceAudioException {
60 logger.debug("Opening IP connection on IP {} port {}", this.address, this.port);
62 Socket clientSocket = new Socket(this.address, this.port);
63 clientSocket.setSoTimeout(100);
65 dataOut = new DataOutputStream(clientSocket.getOutputStream());
66 dataIn = new DataInputStream(clientSocket.getInputStream());
68 Thread thread = new MonopriceAudioReaderThread(this, this.uid, this.address + "." + this.port);
70 setReaderThread(thread);
73 this.clientSocket = clientSocket;
77 logger.debug("IP connection opened");
78 } catch (IOException | SecurityException | IllegalArgumentException e) {
80 throw new MonopriceAudioException("Opening IP connection failed: " + e.getMessage(), e);
85 public synchronized void close() {
86 logger.debug("Closing IP connection");
88 Socket clientSocket = this.clientSocket;
89 if (clientSocket != null) {
92 } catch (IOException e) {
94 this.clientSocket = null;
97 logger.debug("IP connection closed");
101 * Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes
102 * actually read is returned as an integer.
103 * In case of socket timeout, the returned value is 0.
105 * @param dataBuffer the buffer into which the data is read.
107 * @return the total number of bytes read into the buffer, or -1 if there is no more data because the end of the
108 * stream has been reached.
110 * @throws MonopriceAudioException - If the input stream is null, if the first byte cannot be read for any reason
111 * other than the end of the file, if the input stream has been closed, or if some other I/O error
115 protected int readInput(byte[] dataBuffer) throws MonopriceAudioException {
116 InputStream dataIn = this.dataIn;
117 if (dataIn == null) {
118 throw new MonopriceAudioException("readInput failed: input stream is null");
121 return dataIn.read(dataBuffer);
122 } catch (SocketTimeoutException e) {
124 } catch (IOException e) {
125 throw new MonopriceAudioException("readInput failed: " + e.getMessage(), e);