2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mynice.internal.handler;
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;
21 import javax.net.ssl.SSLContext;
22 import javax.net.ssl.SSLSocket;
23 import javax.net.ssl.TrustManager;
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;
31 * The {@link It4WifiConnector} is responsible for connecting reading, writing and disconnecting from the It4Wifi.
33 * @author Gaƫl L'hopital - Initial Contribution
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';
41 private final Logger logger = LoggerFactory.getLogger(It4WifiConnector.class);
42 private final It4WifiHandler handler;
43 private final SSLSocket sslsocket;
45 private @NonNullByDefault({}) InputStreamReader in;
46 private @NonNullByDefault({}) OutputStreamWriter out;
48 public It4WifiConnector(String hostname, It4WifiHandler handler) {
49 super(It4WifiConnector.class.getName());
50 this.handler = handler;
52 SSLContext sslContext = SSLContext.getInstance("SSL");
53 sslContext.init(null, new TrustManager[] { TrustAllTrustManager.getInstance() }, null);
54 sslsocket = (SSLSocket) sslContext.getSocketFactory().createSocket(hostname, SERVER_PORT);
56 } catch (NoSuchAlgorithmException | KeyManagementException | IOException e) {
57 throw new IllegalArgumentException(e);
66 while (!interrupted()) {
68 while ((data = in.read()) != -1) {
71 } else if (data == ETX) {
72 handler.received(buffer);
74 buffer += (char) data;
78 handler.connectorInterrupted("IT4WifiConnector interrupted");
80 } catch (IOException e) {
81 handler.connectorInterrupted(e.getMessage());
85 public synchronized void sendCommand(String command) {
86 logger.debug("Sending ItT4Wifi :{}", command);
88 out.write(STX + command + ETX);
90 } catch (IOException e) {
91 handler.connectorInterrupted(e.getMessage());
95 private void disconnect() {
96 logger.debug("Disconnecting");
101 } catch (IOException ignore) {
107 } catch (IOException ignore) {
114 logger.debug("Disconnected");
118 * Stop the device thread
120 * @throws IOException
122 public void dispose() {
127 } catch (IOException e) {
128 logger.warn("Error closing sslsocket : {}", e.getMessage());
132 private void connect() throws IOException {
134 logger.debug("Initiating connection to IT4Wifi on port {}...", SERVER_PORT);
136 sslsocket.startHandshake();
137 in = new InputStreamReader(sslsocket.getInputStream());
138 out = new OutputStreamWriter(sslsocket.getOutputStream());
139 handler.handShaked();