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.Closeable;
16 import java.io.IOException;
17 import java.io.InputStreamReader;
18 import java.io.OutputStreamWriter;
20 import javax.net.ssl.SSLSocket;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * The {@link It4WifiConnector} is responsible for reading and writing to the It4Wifi.
29 * @author Gaƫl L'hopital - Initial Contribution
32 public class It4WifiConnector extends Thread {
33 private static final char ETX = '\u0003';
34 private static final char STX = '\u0002';
36 private final Logger logger = LoggerFactory.getLogger(It4WifiConnector.class);
37 private final It4WifiHandler handler;
38 private final InputStreamReader in;
39 private final OutputStreamWriter out;
41 public It4WifiConnector(It4WifiHandler handler, SSLSocket sslSocket) throws IOException {
42 super(It4WifiConnector.class.getName());
43 this.handler = handler;
44 this.in = new InputStreamReader(sslSocket.getInputStream());
45 this.out = new OutputStreamWriter(sslSocket.getOutputStream());
54 while (!interrupted()) {
56 while ((data = in.read()) != -1) {
59 } else if (data == ETX) {
60 handler.received(buffer);
62 buffer += (char) data;
65 } catch (IOException e) {
66 handler.communicationError(e.toString());
73 public void interrupt() {
74 logger.debug("Closing streams");
81 public synchronized void sendCommand(String command) {
82 logger.debug("Sending ItT4Wifi :{}", command);
84 out.write(STX + command + ETX);
86 } catch (IOException e) {
87 handler.communicationError(e.toString());
91 private void tryClose(Closeable closeable) {
94 } catch (IOException e) {
95 logger.debug("Exception closing stream : {}", e.getMessage());