]> git.basschouten.com Git - openhab-addons.git/blob
661a5fbdba7beb2e174441a6ac5c65524598e48b
[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.Closeable;
16 import java.io.IOException;
17 import java.io.InputStreamReader;
18 import java.io.OutputStreamWriter;
19
20 import javax.net.ssl.SSLSocket;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * The {@link It4WifiConnector} is responsible for reading and writing to the It4Wifi.
28  *
29  * @author GaĆ«l L'hopital - Initial Contribution
30  */
31 @NonNullByDefault
32 public class It4WifiConnector extends Thread {
33     private static final char ETX = '\u0003';
34     private static final char STX = '\u0002';
35
36     private final Logger logger = LoggerFactory.getLogger(It4WifiConnector.class);
37     private final It4WifiHandler handler;
38     private final InputStreamReader in;
39     private final OutputStreamWriter out;
40
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());
46         setDaemon(true);
47     }
48
49     @Override
50     public void run() {
51         String buffer = "";
52         int data;
53
54         while (!interrupted()) {
55             try {
56                 while ((data = in.read()) != -1) {
57                     if (data == STX) {
58                         buffer = "";
59                     } else if (data == ETX) {
60                         handler.received(buffer);
61                     } else {
62                         buffer += (char) data;
63                     }
64                 }
65             } catch (IOException e) {
66                 handler.communicationError(e.toString());
67                 interrupt();
68             }
69         }
70     }
71
72     @Override
73     public void interrupt() {
74         logger.debug("Closing streams");
75         tryClose(in);
76         tryClose(out);
77
78         super.interrupt();
79     }
80
81     public synchronized void sendCommand(String command) {
82         logger.debug("Sending ItT4Wifi :{}", command);
83         try {
84             out.write(STX + command + ETX);
85             out.flush();
86         } catch (IOException e) {
87             handler.communicationError(e.toString());
88         }
89     }
90
91     private void tryClose(Closeable closeable) {
92         try {
93             closeable.close();
94         } catch (IOException e) {
95             logger.debug("Exception closing stream : {}", e.getMessage());
96         }
97     }
98 }