]> git.basschouten.com Git - openhab-addons.git/blob
6487941b0b89b1ba7cbbdd44862502e94f973783
[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.sonyprojector.internal.communication.serial;
14
15 import java.io.DataInputStream;
16 import java.io.DataOutputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.Socket;
20 import java.net.SocketTimeoutException;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.sonyprojector.internal.SonyProjectorException;
25 import org.openhab.binding.sonyprojector.internal.SonyProjectorModel;
26 import org.openhab.core.io.transport.serial.SerialPortManager;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Class for communicating with Sony Projectors through a serial over IP connection
32  *
33  * @author Laurent Garnier - Initial contribution
34  */
35 @NonNullByDefault
36 public class SonyProjectorSerialOverIpConnector extends SonyProjectorSerialConnector {
37
38     private final Logger logger = LoggerFactory.getLogger(SonyProjectorSerialOverIpConnector.class);
39
40     private String address;
41     private int port;
42
43     private @Nullable Socket clientSocket;
44
45     /**
46      * Constructor
47      *
48      * @param serialPortManager the serial port manager
49      * @param address the IP address of the projector
50      * @param port the TCP port to be used
51      * @param model the projector model in use
52      */
53     public SonyProjectorSerialOverIpConnector(SerialPortManager serialPortManager, String address, Integer port,
54             SonyProjectorModel model) {
55         super(serialPortManager, "dummyPort", model);
56
57         this.address = address;
58         this.port = port;
59     }
60
61     @Override
62     public synchronized void open() throws SonyProjectorException {
63         if (!connected) {
64             logger.debug("Opening serial over IP connection on IP {} port {}", this.address, this.port);
65             try {
66                 Socket clientSocket = new Socket(this.address, this.port);
67                 clientSocket.setSoTimeout(100);
68
69                 dataOut = new DataOutputStream(clientSocket.getOutputStream());
70                 dataIn = new DataInputStream(clientSocket.getInputStream());
71
72                 this.clientSocket = clientSocket;
73
74                 connected = true;
75
76                 logger.debug("Serial over IP connection opened");
77             } catch (IOException | SecurityException | IllegalArgumentException e) {
78                 logger.debug("Opening serial over IP connection failed: {}", e.getMessage());
79                 throw new SonyProjectorException("Opening serial over IP connection failed: " + e.getMessage());
80             }
81         }
82     }
83
84     @Override
85     public synchronized void close() {
86         if (connected) {
87             logger.debug("Closing serial over IP connection");
88             super.close();
89             Socket clientSocket = this.clientSocket;
90             if (clientSocket != null) {
91                 try {
92                     clientSocket.close();
93                 } catch (IOException e) {
94                 }
95                 this.clientSocket = null;
96             }
97             connected = false;
98         }
99     }
100
101     /**
102      * Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes
103      * actually read is returned as an integer.
104      * In case of socket timeout, the returned value is 0.
105      *
106      * @param dataBuffer the buffer into which the data is read.
107      * @return the total number of bytes read into the buffer, or -1 if there is no more data because the end of the
108      *         stream has been reached.
109      * @throws SonyProjectorException - If the input stream is null, if the first byte cannot be read for any reason
110      *             other than the end of the file, if the input stream has been closed, or if some other I/O error
111      *             occurs.
112      */
113     @Override
114     protected int readInput(byte[] dataBuffer) throws SonyProjectorException {
115         InputStream dataIn = this.dataIn;
116         if (dataIn == null) {
117             throw new SonyProjectorException("readInput failed: input stream is null");
118         }
119         try {
120             return dataIn.read(dataBuffer);
121         } catch (SocketTimeoutException e) {
122             return 0;
123         } catch (IOException e) {
124             logger.debug("readInput failed: {}", e.getMessage());
125             throw new SonyProjectorException("readInput failed: " + e.getMessage());
126         }
127     }
128 }