]> git.basschouten.com Git - openhab-addons.git/blob
14e8acef507928fccfe0e9bd151dbc28de41df8a
[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.heos.internal.resources;
14
15 import java.io.IOException;
16
17 import org.openhab.binding.heos.internal.json.HeosJsonParser;
18 import org.openhab.binding.heos.internal.json.dto.HeosResponseObject;
19 import org.openhab.binding.heos.internal.resources.Telnet.ReadException;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * The {@link HeosSendCommand} is responsible to send a command
25  * to the HEOS bridge
26  *
27  * @author Johannes Einig - Initial contribution
28  */
29 public class HeosSendCommand {
30     private final Logger logger = LoggerFactory.getLogger(HeosSendCommand.class);
31
32     private final Telnet client;
33     private final HeosJsonParser parser = new HeosJsonParser();
34
35     public HeosSendCommand(Telnet client) {
36         this.client = client;
37     }
38
39     public <T> HeosResponseObject<T> send(String command, Class<T> clazz) throws IOException, ReadException {
40         HeosResponseObject<T> result;
41         int attempt = 0;
42
43         boolean send = client.send(command);
44         if (clazz == null) {
45             return null;
46         } else if (send) {
47             String line = client.readLine();
48             if (line == null) {
49                 throw new IOException("No valid input was received");
50             }
51             result = parser.parseResponse(line, clazz);
52
53             while (!result.isFinished() && attempt < 3) {
54                 attempt++;
55                 logger.trace("Retrying \"{}\" (attempt {})", command, attempt);
56                 line = client.readLine(15000);
57
58                 if (line != null) {
59                     result = parser.parseResponse(line, clazz);
60                 }
61             }
62
63             if (attempt >= 3 && !result.isFinished()) {
64                 throw new IOException("No valid input was received after multiple attempts");
65             }
66
67             return result;
68         } else {
69             throw new IOException("Not connected");
70         }
71     }
72
73     public boolean isHostReachable() {
74         return client.isHostReachable();
75     }
76
77     public boolean isConnected() {
78         return client.isConnected();
79     }
80
81     public void stopInputListener(String registerChangeEventOFF) {
82         logger.debug("Stopping HEOS event line listener");
83         client.stopInputListener();
84
85         if (client.isConnected()) {
86             try {
87                 client.send(registerChangeEventOFF);
88             } catch (IOException e) {
89                 logger.debug("Failure during closing connection to HEOS with message: {}", e.getMessage());
90             }
91         }
92     }
93
94     public void disconnect() {
95         if (client.isConnected()) {
96             return;
97         }
98
99         try {
100             logger.debug("Disconnecting HEOS command line");
101             client.disconnect();
102         } catch (IOException e) {
103             logger.debug("Failure during closing connection to HEOS with message: {}", e.getMessage());
104         }
105
106         logger.debug("Connection to HEOS system closed");
107     }
108
109     public void startInputListener(String command) throws IOException, ReadException {
110         HeosResponseObject<Void> response = send(command, Void.class);
111         if (response.result) {
112             client.startInputListener();
113         }
114     }
115 }