]> git.basschouten.com Git - openhab-addons.git/blob
f607e7f83b01477119c1ba03e9e1f374353219fe
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.danfossairunit.internal;
14
15 import static org.openhab.binding.danfossairunit.internal.Commands.EMPTY;
16
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.OutputStream;
20 import java.net.InetAddress;
21 import java.net.Socket;
22 import java.util.Arrays;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * The {@link DanfossAirUnitCommunicationController} class does the actual network communication with the air unit.
31  *
32  * @author Robert Bach - initial contribution
33  */
34
35 @NonNullByDefault
36 public class DanfossAirUnitCommunicationController {
37
38     private final Logger logger = LoggerFactory.getLogger(DanfossAirUnitCommunicationController.class);
39
40     private final InetAddress inetAddr;
41     private final int port;
42     private boolean connected = false;
43     private @Nullable Socket socket;
44     private @Nullable OutputStream oStream;
45     private @Nullable InputStream iStream;
46
47     public DanfossAirUnitCommunicationController(InetAddress inetAddr, int port) {
48         this.inetAddr = inetAddr;
49         this.port = port;
50     }
51
52     public synchronized void connect() throws IOException {
53         if (connected) {
54             return;
55         }
56         socket = new Socket(inetAddr, port);
57         oStream = socket.getOutputStream();
58         iStream = socket.getInputStream();
59         connected = true;
60     }
61
62     public synchronized void disconnect() {
63         if (!connected) {
64             return;
65         }
66         try {
67             if (socket != null) {
68                 socket.close();
69             }
70         } catch (IOException ioe) {
71             logger.debug("Connection to air unit could not be closed gracefully. {}", ioe.getMessage());
72         } finally {
73             socket = null;
74             iStream = null;
75             oStream = null;
76         }
77         connected = false;
78     }
79
80     public byte[] sendRobustRequest(byte[] operation, byte[] register) throws IOException {
81         return sendRobustRequest(operation, register, EMPTY);
82     }
83
84     public synchronized byte[] sendRobustRequest(byte[] operation, byte[] register, byte[] value) throws IOException {
85         connect();
86         byte[] request = new byte[4 + value.length];
87         System.arraycopy(operation, 0, request, 0, 2);
88         System.arraycopy(register, 0, request, 2, 2);
89         System.arraycopy(value, 0, request, 4, value.length);
90         try {
91             return sendRequestInternal(request);
92         } catch (IOException ioe) {
93             // retry once if there was connection problem
94             disconnect();
95             connect();
96             return sendRequestInternal(request);
97         }
98     }
99
100     private synchronized byte[] sendRequestInternal(byte[] request) throws IOException {
101
102         if (oStream == null) {
103             throw new IOException(
104                     String.format("Output stream is null while sending request: %s", Arrays.toString(request)));
105         }
106         oStream.write(request);
107         oStream.flush();
108
109         byte[] result = new byte[63];
110         if (iStream == null) {
111             throw new IOException(
112                     String.format("Input stream is null while sending request: %s", Arrays.toString(request)));
113         }
114         // noinspection ResultOfMethodCallIgnored
115         iStream.read(result, 0, 63);
116
117         return result;
118     }
119 }