]> git.basschouten.com Git - openhab-addons.git/blob
2de9565227d11d7d61a1f220761cf522259fb92a
[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.herzborg.internal;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.io.transport.serial.PortInUseException;
22 import org.openhab.core.io.transport.serial.SerialPort;
23 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
24 import org.openhab.core.io.transport.serial.SerialPortManager;
25 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
26 import org.openhab.core.thing.ThingStatusDetail;
27
28 /**
29  * The {@link SerialBus} implements specific handling for Herzborg serial bus,
30  * connected directly via a serial port.
31  *
32  * @author Pavel Fedin - Initial contribution
33  */
34 @NonNullByDefault
35 public class SerialBus extends Bus {
36     private SerialPortManager serialPortManager;
37     private @Nullable SerialPort serialPort;
38
39     public SerialBus(SerialPortManager manager) {
40         serialPortManager = manager;
41     }
42
43     public Result initialize(@Nullable String port) {
44         if (port == null) {
45             return new Result(ThingStatusDetail.CONFIGURATION_ERROR, "Port is not specified");
46         }
47         SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(port);
48         if (portIdentifier == null) {
49             return new Result(ThingStatusDetail.CONFIGURATION_ERROR, "No such port: " + port);
50         }
51
52         SerialPort commPort;
53         try {
54             commPort = portIdentifier.open(this.getClass().getName(), 2000);
55         } catch (PortInUseException e1) {
56             return new Result(ThingStatusDetail.CONFIGURATION_ERROR, "Port " + port + " is in use");
57         }
58
59         try {
60             // Herzborg serial bus operates with fixed parameters
61             commPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
62             commPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
63         } catch (UnsupportedCommOperationException e) {
64             return new Result(ThingStatusDetail.CONFIGURATION_ERROR, "Invalid port configuration");
65         }
66
67         try {
68             commPort.enableReceiveThreshold(8);
69             commPort.enableReceiveTimeout(1000);
70         } catch (UnsupportedCommOperationException e) {
71             // OpenHAB's serial-over-IP doesn't support these, so let's ignore the exception
72         }
73
74         InputStream dataIn = null;
75         OutputStream dataOut = null;
76         String error = null;
77
78         try {
79             dataIn = commPort.getInputStream();
80             dataOut = commPort.getOutputStream();
81
82             if (dataIn == null) {
83                 error = "No input stream available on the serial port";
84             } else if (dataOut == null) {
85                 error = "No output stream available on the serial port";
86             } else {
87                 dataOut.flush();
88                 if (dataIn.markSupported()) {
89                     dataIn.reset();
90                 }
91             }
92         } catch (IOException e) {
93             error = e.getMessage();
94         }
95
96         if (error != null) {
97             return new Result(ThingStatusDetail.HANDLER_INITIALIZING_ERROR, error);
98         }
99
100         this.serialPort = commPort;
101         this.dataIn = dataIn;
102         this.dataOut = dataOut;
103
104         return new Result(ThingStatusDetail.NONE);
105     }
106
107     @Override
108     public void dispose() {
109         SerialPort port = serialPort;
110
111         if (port == null) {
112             return; // Nothing to do in this case
113         }
114
115         port.removeEventListener();
116         super.dispose();
117         port.close();
118         serialPort = null;
119     }
120 }