2 * Copyright (c) 2010-2020 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.danfossairunit.internal;
15 import static org.openhab.binding.danfossairunit.internal.Commands.EMPTY;
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;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * The {@link DanfossAirUnitCommunicationController} class does the actual network communication with the air unit.
32 * @author Robert Bach - initial contribution
36 public class DanfossAirUnitCommunicationController {
38 private final Logger logger = LoggerFactory.getLogger(DanfossAirUnitCommunicationController.class);
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;
47 public DanfossAirUnitCommunicationController(InetAddress inetAddr, int port) {
48 this.inetAddr = inetAddr;
52 public synchronized void connect() throws IOException {
56 socket = new Socket(inetAddr, port);
57 oStream = socket.getOutputStream();
58 iStream = socket.getInputStream();
62 public synchronized void disconnect() {
70 } catch (IOException ioe) {
71 logger.debug("Connection to air unit could not be closed gracefully. {}", ioe.getMessage());
80 public byte[] sendRobustRequest(byte[] operation, byte[] register) throws IOException {
81 return sendRobustRequest(operation, register, EMPTY);
84 public synchronized byte[] sendRobustRequest(byte[] operation, byte[] register, byte[] value) throws IOException {
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);
91 return sendRequestInternal(request);
92 } catch (IOException ioe) {
93 // retry once if there was connection problem
96 return sendRequestInternal(request);
100 private synchronized byte[] sendRequestInternal(byte[] request) throws IOException {
102 if (oStream == null) {
103 throw new IOException(
104 String.format("Output stream is null while sending request: %s", Arrays.toString(request)));
106 oStream.write(request);
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)));
114 // noinspection ResultOfMethodCallIgnored
115 iStream.read(result, 0, 63);