2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.pentair.internal.handler;
15 import java.io.BufferedInputStream;
16 import java.io.BufferedOutputStream;
17 import java.io.IOException;
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;
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;
34 * Handler for the IPBridge. Implements the connect and disconnect abstract methods of {@link PentairBaseBridgeHandler}
36 * @author Jeff James - initial contribution
39 public class PentairSerialBridgeHandler extends PentairBaseBridgeHandler {
40 private final Logger logger = LoggerFactory.getLogger(PentairSerialBridgeHandler.class);
42 /** SerialPort object representing the port where the RS485 adapter is connected */
45 public PentairSerialBridgeHandler(Bridge bridge) {
50 protected synchronized void connect() {
51 PentairSerialBridgeConfig configuration = getConfigAs(PentairSerialBridgeConfig.class);
54 CommPortIdentifier ci = CommPortIdentifier.getPortIdentifier(configuration.serialPort);
55 CommPort cp = ci.open("openhabpentairbridge", 10000);
57 throw new IllegalStateException("cannot open serial port!");
60 if (cp instanceof SerialPort) {
61 port = (SerialPort) cp;
63 throw new IllegalStateException("unknown port type");
65 port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
66 port.disableReceiveFraming();
67 port.disableReceiveThreshold();
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);
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);
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);
85 } catch (IllegalStateException e) {
86 String msg = String.format("receive IllegalStateException for port %s", configuration.serialPort);
87 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
89 } catch (IOException e) {
90 String msg = String.format("IOException on port %s", configuration.serialPort);
91 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
95 parser = new Parser();
96 thread = new Thread(parser);
99 if (port != null && reader != null && writer != null) {
100 updateStatus(ThingStatus.ONLINE);
102 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Unable to connect");
107 protected synchronized void disconnect() {
108 updateStatus(ThingStatus.OFFLINE);
110 if (thread != null) {
113 thread.join(); // wait for thread to complete
114 } catch (InterruptedException e) {
121 if (reader != null) {
124 } catch (IOException e) {
125 logger.trace("IOException when closing serial reader", e);
130 if (writer != null) {
133 } catch (IOException e) {
134 logger.trace("IOException when closing serial writer", e);