]> git.basschouten.com Git - openhab-addons.git/blob
0a5a0c76499bf66d0c3c1870ca6e4730e6dc2de2
[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.mynice.internal.handler;
14
15 import java.io.IOException;
16 import java.io.InputStreamReader;
17 import java.io.OutputStreamWriter;
18 import java.security.KeyManagementException;
19 import java.security.NoSuchAlgorithmException;
20
21 import javax.net.ssl.SSLContext;
22 import javax.net.ssl.SSLSocket;
23 import javax.net.ssl.TrustManager;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.openhab.core.io.net.http.TrustAllTrustManager;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The {@link It4WifiConnector} is responsible for connecting reading, writing and disconnecting from the It4Wifi.
32  *
33  * @author GaĆ«l L'hopital - Initial Contribution
34  */
35 @NonNullByDefault
36 public class It4WifiConnector extends Thread {
37     private static final int SERVER_PORT = 443;
38     private static final char ETX = '\u0003';
39     private static final char STX = '\u0002';
40
41     private final Logger logger = LoggerFactory.getLogger(It4WifiConnector.class);
42     private final It4WifiHandler handler;
43     private final SSLSocket sslsocket;
44
45     private @NonNullByDefault({}) InputStreamReader in;
46     private @NonNullByDefault({}) OutputStreamWriter out;
47
48     public It4WifiConnector(String hostname, It4WifiHandler handler) {
49         super(It4WifiConnector.class.getName());
50         this.handler = handler;
51         try {
52             SSLContext sslContext = SSLContext.getInstance("SSL");
53             sslContext.init(null, new TrustManager[] { TrustAllTrustManager.getInstance() }, null);
54             sslsocket = (SSLSocket) sslContext.getSocketFactory().createSocket(hostname, SERVER_PORT);
55             setDaemon(true);
56         } catch (NoSuchAlgorithmException | KeyManagementException | IOException e) {
57             throw new IllegalArgumentException(e);
58         }
59     }
60
61     @Override
62     public void run() {
63         String buffer = "";
64         try {
65             connect();
66             while (!interrupted()) {
67                 int data;
68                 while ((data = in.read()) != -1) {
69                     if (data == STX) {
70                         buffer = "";
71                     } else if (data == ETX) {
72                         handler.received(buffer);
73                     } else {
74                         buffer += (char) data;
75                     }
76                 }
77             }
78             handler.connectorInterrupted("IT4WifiConnector interrupted");
79             dispose();
80         } catch (IOException e) {
81             handler.connectorInterrupted(e.getMessage());
82         }
83     }
84
85     public synchronized void sendCommand(String command) {
86         logger.debug("Sending ItT4Wifi :{}", command);
87         try {
88             out.write(STX + command + ETX);
89             out.flush();
90         } catch (IOException e) {
91             handler.connectorInterrupted(e.getMessage());
92         }
93     }
94
95     private void disconnect() {
96         logger.debug("Disconnecting");
97
98         if (in != null) {
99             try {
100                 in.close();
101             } catch (IOException ignore) {
102             }
103         }
104         if (out != null) {
105             try {
106                 out.close();
107             } catch (IOException ignore) {
108             }
109         }
110
111         in = null;
112         out = null;
113
114         logger.debug("Disconnected");
115     }
116
117     /**
118      * Stop the device thread
119      *
120      * @throws IOException
121      */
122     public void dispose() {
123         interrupt();
124         disconnect();
125         try {
126             sslsocket.close();
127         } catch (IOException e) {
128             logger.warn("Error closing sslsocket : {}", e.getMessage());
129         }
130     }
131
132     private void connect() throws IOException {
133         disconnect();
134         logger.debug("Initiating connection to IT4Wifi on port {}...", SERVER_PORT);
135
136         sslsocket.startHandshake();
137         in = new InputStreamReader(sslsocket.getInputStream());
138         out = new OutputStreamWriter(sslsocket.getOutputStream());
139         handler.handShaked();
140     }
141 }