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;
18 import java.net.Socket;
19 import java.net.UnknownHostException;
21 import org.openhab.binding.pentair.internal.config.PentairIPBridgeConfig;
22 import org.openhab.core.thing.Bridge;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.ThingStatusDetail;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Handler for the IPBridge. Implements the connect and disconnect abstract methods of {@link PentairBaseBridgeHandler}
31 * @author Jeff James - Initial contribution
34 public class PentairIPBridgeHandler extends PentairBaseBridgeHandler {
35 private final Logger logger = LoggerFactory.getLogger(PentairIPBridgeHandler.class);
37 /** Socket object for connection */
38 protected Socket socket;
40 public PentairIPBridgeHandler(Bridge bridge) {
45 protected synchronized void connect() {
46 PentairIPBridgeConfig configuration = getConfigAs(PentairIPBridgeConfig.class);
48 id = configuration.id;
51 socket = new Socket(configuration.address, configuration.port);
52 reader = new BufferedInputStream(socket.getInputStream());
53 writer = new BufferedOutputStream(socket.getOutputStream());
54 logger.info("Pentair IPBridge connected to {}:{}", configuration.address, configuration.port);
55 } catch (UnknownHostException e) {
56 String msg = String.format("unknown host name: %s", configuration.address);
57 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
59 } catch (IOException e) {
60 String msg = String.format("cannot open connection to %s", configuration.address);
61 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
65 parser = new Parser();
66 thread = new Thread(parser);
69 if (socket != null && reader != null && writer != null) {
70 updateStatus(ThingStatus.ONLINE);
72 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Unable to connect");
77 protected synchronized void disconnect() {
78 updateStatus(ThingStatus.OFFLINE);
83 thread.join(); // wait for thread to complete
84 } catch (InterruptedException e) {
94 } catch (IOException e) {
95 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Error in closing reader");
100 if (writer != null) {
103 } catch (IOException e) {
104 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Error in closing writer");
109 if (socket != null) {
112 } catch (IOException e) {
113 logger.error("error when closing socket ", e);