]> git.basschouten.com Git - openhab-addons.git/blob
44049eec9369728552881e47b9ed1e9291efa546
[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.benqprojector.internal.connector;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.nio.charset.StandardCharsets;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.benqprojector.internal.BenqProjectorException;
23
24 /**
25  * Base class for BenQ projector communication.
26  *
27  * @author Michael Lobstein - Initial contribution
28  */
29 @NonNullByDefault
30 public interface BenqProjectorConnector {
31     public static final int TIMEOUT_MS = 5 * 1000;
32
33     public static final String START = "\r*";
34     public static final String END = "#\r";
35     public static final String BLANK = "";
36
37     /**
38      * Procedure for connecting to projector.
39      *
40      * @throws BenqProjectorException
41      */
42     void connect() throws BenqProjectorException;
43
44     /**
45      * Procedure for disconnecting to projector controller.
46      *
47      * @throws BenqProjectorException
48      */
49     void disconnect() throws BenqProjectorException;
50
51     /**
52      * Procedure for sending raw data to projector.
53      *
54      * @param data
55      *            Message to send.
56      *
57      * @throws BenqProjectorException
58      */
59     String sendMessage(String data) throws BenqProjectorException;
60
61     /**
62      * Common method called by the Serial or Tcp connector to send the message to the projector, wait for a response and
63      * return it after processing.
64      *
65      * @param data
66      *            Message to send.
67      * @param in
68      *            The connector's input stream.
69      * @param out
70      *            The connector's output stream.
71      *
72      * @throws BenqProjectorException
73      */
74     default String sendMsgReadResp(String data, @Nullable InputStream in, @Nullable OutputStream out)
75             throws IOException, BenqProjectorException {
76         String resp = BLANK;
77
78         if (in != null && out != null) {
79             out.write((START + data + END).getBytes(StandardCharsets.US_ASCII));
80             out.flush();
81
82             long startTime = System.currentTimeMillis();
83             long elapsedTime = 0;
84
85             while (elapsedTime < TIMEOUT_MS) {
86                 int availableBytes = in.available();
87                 if (availableBytes > 0) {
88                     byte[] tmpData = new byte[availableBytes];
89                     int readBytes = in.read(tmpData, 0, availableBytes);
90                     resp = resp.concat(new String(tmpData, 0, readBytes, StandardCharsets.US_ASCII));
91                     if (resp.contains(END)) {
92                         return resp.replaceAll("[\\r\\n*#>]", BLANK).replace(data, BLANK);
93                     }
94                 } else {
95                     try {
96                         Thread.sleep(100);
97                     } catch (InterruptedException e) {
98                         throw new BenqProjectorException(e);
99                     }
100                 }
101
102                 elapsedTime = System.currentTimeMillis() - startTime;
103             }
104         }
105         return resp;
106     }
107 }