]> git.basschouten.com Git - openhab-addons.git/blob
e417a2836c66524965e163ec86b278ad524713bf
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.insteon.internal.driver;
14
15 import java.io.IOException;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.io.transport.serial.PortInUseException;
20 import org.openhab.core.io.transport.serial.SerialPort;
21 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
22 import org.openhab.core.io.transport.serial.SerialPortManager;
23 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Implements IOStream for serial devices.
29  *
30  * @author Bernd Pfrommer - Initial contribution
31  * @author Daniel Pfrommer - openHAB 1 insteonplm binding
32  * @author Rob Nielsen - Port to openHAB 2 insteon binding
33  */
34 @NonNullByDefault
35 @SuppressWarnings("null")
36 public class SerialIOStream extends IOStream {
37     private final Logger logger = LoggerFactory.getLogger(SerialIOStream.class);
38     private @Nullable SerialPort port = null;
39     private final String appName = "PLM";
40     private int baudRate = 19200;
41     private String devName;
42     private boolean validConfig = true;
43     private @Nullable SerialPortManager serialPortManager;
44
45     public SerialIOStream(@Nullable SerialPortManager serialPortManager, String config) {
46         this.serialPortManager = serialPortManager;
47
48         String[] parts = config.split(",");
49         devName = parts[0];
50         for (int i = 1; i < parts.length; i++) {
51             String parameter = parts[i];
52             String[] paramParts = parameter.split("=");
53             if (paramParts.length != 2) {
54                 logger.warn("{} invalid parameter format '{}', must be 'key=value'.", config, parameter);
55
56                 validConfig = false;
57             } else {
58                 String key = paramParts[0];
59                 String value = paramParts[1];
60                 if (key.equals("baudRate")) {
61                     try {
62                         baudRate = Integer.parseInt(value);
63                     } catch (NumberFormatException e) {
64                         logger.warn("{} baudRate {} must be an integer.", config, value);
65
66                         validConfig = false;
67                     }
68                 } else {
69                     logger.warn("{} invalid parameter '{}'.", config, parameter);
70
71                     validConfig = false;
72                 }
73             }
74         }
75     }
76
77     @Override
78     public boolean open() {
79         if (!validConfig) {
80             logger.warn("{} has an invalid configuration.", devName);
81             return false;
82         }
83
84         try {
85             SerialPortIdentifier spi = serialPortManager.getIdentifier(devName);
86             if (spi == null) {
87                 logger.warn("{} is not a valid serial port.", devName);
88                 return false;
89             }
90
91             port = spi.open(appName, 1000);
92             port.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
93             port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
94             logger.debug("setting {} baud rate to {}", devName, baudRate);
95             port.enableReceiveThreshold(1);
96             port.enableReceiveTimeout(1000);
97             in = port.getInputStream();
98             out = port.getOutputStream();
99             logger.debug("successfully opened port {}", devName);
100             return true;
101         } catch (IOException e) {
102             logger.warn("cannot open port: {}, got IOException {}", devName, e.getMessage());
103         } catch (PortInUseException e) {
104             logger.warn("cannot open port: {}, it is in use!", devName);
105         } catch (UnsupportedCommOperationException e) {
106             logger.warn("got unsupported operation {} on port {}", e.getMessage(), devName);
107         }
108
109         return false;
110     }
111
112     @Override
113     public void close() {
114         if (in != null) {
115             try {
116                 in.close();
117             } catch (IOException e) {
118                 logger.warn("failed to close input stream", e);
119             }
120             in = null;
121         }
122
123         if (out != null) {
124             try {
125                 out.close();
126             } catch (IOException e) {
127                 logger.warn("failed to close output stream", e);
128             }
129             out = null;
130         }
131
132         if (port != null) {
133             port.close();
134             port = null;
135         }
136     }
137 }