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