]> git.basschouten.com Git - openhab-addons.git/blob
7e9bc0a9673194b8609f33f07b1ed684a4e569fe
[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.pentair.internal.handler;
14
15 import java.io.BufferedInputStream;
16 import java.io.BufferedOutputStream;
17 import java.io.IOException;
18
19 import org.openhab.binding.pentair.internal.config.PentairSerialBridgeConfig;
20 import org.openhab.core.thing.Bridge;
21 import org.openhab.core.thing.ThingStatus;
22 import org.openhab.core.thing.ThingStatusDetail;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import gnu.io.CommPort;
27 import gnu.io.CommPortIdentifier;
28 import gnu.io.NoSuchPortException;
29 import gnu.io.PortInUseException;
30 import gnu.io.SerialPort;
31 import gnu.io.UnsupportedCommOperationException;
32
33 /**
34  * Handler for the IPBridge. Implements the connect and disconnect abstract methods of {@link PentairBaseBridgeHandler}
35  *
36  * @author Jeff James - initial contribution
37  *
38  */
39 public class PentairSerialBridgeHandler extends PentairBaseBridgeHandler {
40     private final Logger logger = LoggerFactory.getLogger(PentairSerialBridgeHandler.class);
41
42     /** SerialPort object representing the port where the RS485 adapter is connected */
43     SerialPort port;
44
45     public PentairSerialBridgeHandler(Bridge bridge) {
46         super(bridge);
47     }
48
49     @Override
50     protected synchronized void connect() {
51         PentairSerialBridgeConfig configuration = getConfigAs(PentairSerialBridgeConfig.class);
52
53         try {
54             CommPortIdentifier ci = CommPortIdentifier.getPortIdentifier(configuration.serialPort);
55             CommPort cp = ci.open("openhabpentairbridge", 10000);
56             if (cp == null) {
57                 throw new IllegalStateException("cannot open serial port!");
58             }
59
60             if (cp instanceof SerialPort) {
61                 port = (SerialPort) cp;
62             } else {
63                 throw new IllegalStateException("unknown port type");
64             }
65             port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
66             port.disableReceiveFraming();
67             port.disableReceiveThreshold();
68
69             reader = new BufferedInputStream(port.getInputStream());
70             writer = new BufferedOutputStream(port.getOutputStream());
71             logger.info("Pentair Bridge connected to serial port: {}", configuration.serialPort);
72         } catch (PortInUseException e) {
73             String msg = String.format("cannot open serial port: %s", configuration.serialPort);
74             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
75             return;
76         } catch (UnsupportedCommOperationException e) {
77             String msg = String.format("got unsupported operation %s on port %s", e.getMessage(),
78                     configuration.serialPort);
79             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
80             return;
81         } catch (NoSuchPortException e) {
82             String msg = String.format("got no such port for %s", configuration.serialPort);
83             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
84             return;
85         } catch (IllegalStateException e) {
86             String msg = String.format("receive IllegalStateException for port %s", configuration.serialPort);
87             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
88             return;
89         } catch (IOException e) {
90             String msg = String.format("IOException on port %s", configuration.serialPort);
91             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
92             return;
93         }
94
95         parser = new Parser();
96         thread = new Thread(parser);
97         thread.start();
98
99         if (port != null && reader != null && writer != null) {
100             updateStatus(ThingStatus.ONLINE);
101         } else {
102             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Unable to connect");
103         }
104     }
105
106     @Override
107     protected synchronized void disconnect() {
108         updateStatus(ThingStatus.OFFLINE);
109
110         if (thread != null) {
111             try {
112                 thread.interrupt();
113                 thread.join(); // wait for thread to complete
114             } catch (InterruptedException e) {
115                 // do nothing
116             }
117             thread = null;
118             parser = null;
119         }
120
121         if (reader != null) {
122             try {
123                 reader.close();
124             } catch (IOException e) {
125                 logger.trace("IOException when closing serial reader", e);
126             }
127             reader = null;
128         }
129
130         if (writer != null) {
131             try {
132                 writer.close();
133             } catch (IOException e) {
134                 logger.trace("IOException when closing serial writer", e);
135             }
136             writer = null;
137         }
138
139         if (port != null) {
140             port.close();
141             port = null;
142         }
143     }
144 }