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