]> git.basschouten.com Git - openhab-addons.git/blob
96c68c577463efbefdbc34d1afc571f7e575aefa
[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 import java.net.Socket;
19 import java.net.UnknownHostException;
20
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;
27
28 /**
29  * Handler for the IPBridge. Implements the connect and disconnect abstract methods of {@link PentairBaseBridgeHandler}
30  *
31  * @author Jeff James - Initial contribution
32  *
33  */
34 public class PentairIPBridgeHandler extends PentairBaseBridgeHandler {
35     private final Logger logger = LoggerFactory.getLogger(PentairIPBridgeHandler.class);
36
37     /** Socket object for connection */
38     protected Socket socket;
39
40     public PentairIPBridgeHandler(Bridge bridge) {
41         super(bridge);
42     }
43
44     @Override
45     protected synchronized void connect() {
46         PentairIPBridgeConfig configuration = getConfigAs(PentairIPBridgeConfig.class);
47
48         id = configuration.id;
49
50         try {
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);
58             return;
59         } catch (IOException e) {
60             String msg = String.format("cannot open connection to %s", configuration.address);
61             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
62             return;
63         }
64
65         parser = new Parser();
66         thread = new Thread(parser);
67         thread.start();
68
69         if (socket != null && reader != null && writer != null) {
70             updateStatus(ThingStatus.ONLINE);
71         } else {
72             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Unable to connect");
73         }
74     }
75
76     @Override
77     protected synchronized void disconnect() {
78         updateStatus(ThingStatus.OFFLINE);
79
80         if (thread != null) {
81             try {
82                 thread.interrupt();
83                 thread.join(); // wait for thread to complete
84             } catch (InterruptedException e) {
85                 // do nothing
86             }
87             thread = null;
88             parser = null;
89         }
90
91         if (reader != null) {
92             try {
93                 reader.close();
94             } catch (IOException e) {
95                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Error in closing reader");
96             }
97             reader = null;
98         }
99
100         if (writer != null) {
101             try {
102                 writer.close();
103             } catch (IOException e) {
104                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Error in closing writer");
105             }
106             writer = null;
107         }
108
109         if (socket != null) {
110             try {
111                 socket.close();
112             } catch (IOException e) {
113                 logger.error("error when closing socket ", e);
114             }
115             socket = null;
116         }
117     }
118 }