]> git.basschouten.com Git - openhab-addons.git/blob
b8421e0c548256c2f7d75aeea9dd07932dfa1c79
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.monopriceaudio.internal.communication;
14
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;
21
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;
27
28 /**
29  * Class for communicating with the MonopriceAudio device through a serial over IP connection
30  *
31  * @author Laurent Garnier - Initial contribution
32  * @author Michael Lobstein - Adapted for the MonopriceAudio binding
33  */
34 @NonNullByDefault
35 public class MonopriceAudioIpConnector extends MonopriceAudioConnector {
36
37     private final Logger logger = LoggerFactory.getLogger(MonopriceAudioIpConnector.class);
38
39     private final @Nullable String address;
40     private final int port;
41     private final String uid;
42
43     private @Nullable Socket clientSocket;
44
45     /**
46      * Constructor
47      *
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
51      * @param amp the AmplifierModel being used
52      */
53     public MonopriceAudioIpConnector(@Nullable String address, int port, String uid, AmplifierModel amp) {
54         this.address = address;
55         this.port = port;
56         this.uid = uid;
57         setAmplifierModel(amp);
58     }
59
60     @Override
61     public synchronized void open() throws MonopriceAudioException {
62         logger.debug("Opening IP connection on IP {} port {}", this.address, this.port);
63         try {
64             Socket clientSocket = new Socket(this.address, this.port);
65             clientSocket.setSoTimeout(100);
66
67             dataOut = new DataOutputStream(clientSocket.getOutputStream());
68             dataIn = new DataInputStream(clientSocket.getInputStream());
69
70             Thread thread = new MonopriceAudioReaderThread(this, this.uid, this.address + "." + this.port);
71
72             setReaderThread(thread);
73             thread.start();
74
75             this.clientSocket = clientSocket;
76
77             setConnected(true);
78
79             logger.debug("IP connection opened");
80         } catch (IOException | SecurityException | IllegalArgumentException e) {
81             setConnected(false);
82             throw new MonopriceAudioException("Opening IP connection failed: " + e.getMessage(), e);
83         }
84     }
85
86     @Override
87     public synchronized void close() {
88         logger.debug("Closing IP connection");
89         super.cleanup();
90         Socket clientSocket = this.clientSocket;
91         if (clientSocket != null) {
92             try {
93                 clientSocket.close();
94             } catch (IOException e) {
95             }
96             this.clientSocket = null;
97         }
98         setConnected(false);
99         logger.debug("IP connection closed");
100     }
101
102     /**
103      * Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes
104      * actually read is returned as an integer.
105      * In case of socket timeout, the returned value is 0.
106      *
107      * @param dataBuffer the buffer into which the data is read.
108      *
109      * @return the total number of bytes read into the buffer, or -1 if there is no more data because the end of the
110      *         stream has been reached.
111      *
112      * @throws MonopriceAudioException - If the input stream is null, if the first byte cannot be read for any reason
113      *             other than the end of the file, if the input stream has been closed, or if some other I/O error
114      *             occurs.
115      */
116     @Override
117     protected int readInput(byte[] dataBuffer) throws MonopriceAudioException {
118         InputStream dataIn = this.dataIn;
119         if (dataIn == null) {
120             throw new MonopriceAudioException("readInput failed: input stream is null");
121         }
122         try {
123             return dataIn.read(dataBuffer);
124         } catch (SocketTimeoutException e) {
125             return 0;
126         } catch (IOException e) {
127             throw new MonopriceAudioException("readInput failed: " + e.getMessage(), e);
128         }
129     }
130 }