]> git.basschouten.com Git - openhab-addons.git/blob
e031d420f31c12cd22f2bfac4b1164bea498ed92
[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.rfxcom.internal.connector;
14
15 import java.io.IOException;
16
17 import org.openhab.binding.rfxcom.internal.config.RFXComBridgeConfiguration;
18 import org.openhab.core.util.HexUtils;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import jd2xx.JD2XX;
23 import jd2xx.JD2XXInputStream;
24 import jd2xx.JD2XXOutputStream;
25
26 /**
27  * RFXCOM connector for direct access via D2XX driver.
28  *
29  * @author Pauli Anttila - Initial contribution
30  */
31 public class RFXComJD2XXConnector extends RFXComBaseConnector {
32     private final Logger logger = LoggerFactory.getLogger(RFXComJD2XXConnector.class);
33
34     private JD2XX serialPort;
35     private JD2XXOutputStream out;
36
37     private final String readerThreadName;
38     private Thread readerThread;
39
40     public RFXComJD2XXConnector(String readerThreadName) {
41         super();
42         this.readerThreadName = readerThreadName;
43     }
44
45     @Override
46     public void connect(RFXComBridgeConfiguration device) throws IOException {
47         logger.info("Connecting to RFXCOM device '{}' using JD2XX.", device.bridgeId);
48
49         if (serialPort == null) {
50             serialPort = new JD2XX();
51         }
52         serialPort.openBySerialNumber(device.bridgeId);
53         serialPort.setBaudRate(38400);
54         serialPort.setDataCharacteristics(8, JD2XX.STOP_BITS_1, JD2XX.PARITY_NONE);
55         serialPort.setFlowControl(JD2XX.FLOW_NONE, 0, 0);
56         serialPort.setTimeouts(100, 100);
57
58         in = new JD2XXInputStream(serialPort);
59         out = new JD2XXOutputStream(serialPort);
60
61         out.flush();
62         if (in.markSupported()) {
63             in.reset();
64         }
65
66         readerThread = new RFXComStreamReader(this, readerThreadName);
67         readerThread.start();
68     }
69
70     @Override
71     public void disconnect() {
72         logger.debug("Disconnecting");
73
74         if (readerThread != null) {
75             logger.debug("Interrupt serial listener");
76             readerThread.interrupt();
77             try {
78                 readerThread.join();
79             } catch (InterruptedException e) {
80             }
81         }
82
83         if (out != null) {
84             logger.debug("Close serial out stream");
85             try {
86                 out.close();
87             } catch (IOException e) {
88                 logger.debug("Error while closing the out stream: {}", e.getMessage());
89             }
90         }
91         if (in != null) {
92             logger.debug("Close serial in stream");
93             try {
94                 in.close();
95             } catch (IOException e) {
96                 logger.debug("Error while closing the in stream: {}", e.getMessage());
97             }
98         }
99
100         if (serialPort != null) {
101             logger.debug("Close serial port");
102             try {
103                 serialPort.close();
104             } catch (IOException e) {
105                 logger.debug("Serial port closing error: {}", e.getMessage());
106             }
107         }
108
109         readerThread = null;
110         serialPort = null;
111         out = null;
112         in = null;
113
114         logger.debug("Closed");
115     }
116
117     @Override
118     public void sendMessage(byte[] data) throws IOException {
119         if (logger.isTraceEnabled()) {
120             logger.trace("Send data (len={}): {}", data.length, HexUtils.bytesToHex(data));
121         }
122         out.write(data);
123     }
124 }