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