Search in sources :

Example 1 with EpsonProjectorException

use of org.openhab.binding.epsonprojector.internal.EpsonProjectorException in project openhab1-addons by openhab.

the class EpsonProjectorSerialConnector method sendMessage.

/**
     * {@inheritDoc}
     */
@Override
public String sendMessage(String data, int timeout) throws EpsonProjectorException {
    if (in == null || out == null) {
        connect();
    }
    try {
        // flush input stream
        if (in.markSupported()) {
            in.reset();
        } else {
            while (in.available() > 0) {
                int availableBytes = in.available();
                if (availableBytes > 0) {
                    byte[] tmpData = new byte[availableBytes];
                    in.read(tmpData, 0, availableBytes);
                }
            }
        }
        return sendMmsg(data, timeout);
    } catch (IOException e) {
        logger.debug("IO error occured...reconnect and resend ones");
        disconnect();
        connect();
        try {
            return sendMmsg(data, timeout);
        } catch (IOException e1) {
            throw new EpsonProjectorException(e);
        }
    } catch (Exception e) {
        throw new EpsonProjectorException(e);
    }
}
Also used : IOException(java.io.IOException) EpsonProjectorException(org.openhab.binding.epsonprojector.internal.EpsonProjectorException) IOException(java.io.IOException) EpsonProjectorException(org.openhab.binding.epsonprojector.internal.EpsonProjectorException)

Example 2 with EpsonProjectorException

use of org.openhab.binding.epsonprojector.internal.EpsonProjectorException in project openhab1-addons by openhab.

the class EpsonProjectorTcpConnector method connect.

/**
     * {@inheritDoc}
     */
@Override
public void connect() throws EpsonProjectorException {
    logger.debug("Open connection to address'{}:{}'", ip, port);
    try {
        socket = new Socket(ip, port);
        in = socket.getInputStream();
        out = socket.getOutputStream();
    } catch (Exception e) {
        throw new EpsonProjectorException(e);
    }
}
Also used : EpsonProjectorException(org.openhab.binding.epsonprojector.internal.EpsonProjectorException) Socket(java.net.Socket) IOException(java.io.IOException) EpsonProjectorException(org.openhab.binding.epsonprojector.internal.EpsonProjectorException)

Example 3 with EpsonProjectorException

use of org.openhab.binding.epsonprojector.internal.EpsonProjectorException in project openhab1-addons by openhab.

the class EpsonProjectorSerialConnector method connect.

/**
     * {@inheritDoc}
     */
@Override
public void connect() throws EpsonProjectorException {
    try {
        logger.debug("Open connection to serial port '{}'", serialPortName);
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
        CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
        serialPort = (SerialPort) commPort;
        serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        serialPort.enableReceiveThreshold(1);
        serialPort.disableReceiveTimeout();
        in = serialPort.getInputStream();
        out = serialPort.getOutputStream();
        out.flush();
        if (in.markSupported()) {
            in.reset();
        }
        // RXTX serial port library causes high CPU load
        // Start event listener, which will just sleep and slow down event loop
        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);
    } catch (Exception e) {
        throw new EpsonProjectorException(e);
    }
}
Also used : CommPortIdentifier(gnu.io.CommPortIdentifier) CommPort(gnu.io.CommPort) EpsonProjectorException(org.openhab.binding.epsonprojector.internal.EpsonProjectorException) IOException(java.io.IOException) EpsonProjectorException(org.openhab.binding.epsonprojector.internal.EpsonProjectorException)

Example 4 with EpsonProjectorException

use of org.openhab.binding.epsonprojector.internal.EpsonProjectorException in project openhab1-addons by openhab.

the class EpsonProjectorSerialConnector method sendMmsg.

private String sendMmsg(String data, int timeout) throws IOException, EpsonProjectorException {
    out.write(data.getBytes());
    out.write("\r\n".getBytes());
    out.flush();
    String resp = "";
    long startTime = System.currentTimeMillis();
    long elapsedTime = 0;
    while (elapsedTime < timeout) {
        int availableBytes = in.available();
        if (availableBytes > 0) {
            byte[] tmpData = new byte[availableBytes];
            int readBytes = in.read(tmpData, 0, availableBytes);
            resp = resp.concat(new String(tmpData, 0, readBytes));
            if (resp.contains(":")) {
                return resp;
            }
        } else {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new EpsonProjectorException(e);
            }
        }
        elapsedTime = Math.abs((new Date()).getTime() - startTime);
    }
    return null;
}
Also used : EpsonProjectorException(org.openhab.binding.epsonprojector.internal.EpsonProjectorException) Date(java.util.Date)

Example 5 with EpsonProjectorException

use of org.openhab.binding.epsonprojector.internal.EpsonProjectorException in project openhab1-addons by openhab.

the class EpsonProjectorTcpConnector method sendMmsg.

private String sendMmsg(String data, int timeout) throws IOException, EpsonProjectorException {
    out.write(data.getBytes());
    out.write("\r\n".getBytes());
    out.flush();
    String resp = "";
    long startTime = System.currentTimeMillis();
    long elapsedTime = 0;
    while (elapsedTime < timeout) {
        int availableBytes = in.available();
        if (availableBytes > 0) {
            byte[] tmpData = new byte[availableBytes];
            int readBytes = in.read(tmpData, 0, availableBytes);
            resp = resp.concat(new String(tmpData, 0, readBytes));
            if (resp.contains(":")) {
                return resp;
            }
        } else {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new EpsonProjectorException(e);
            }
        }
        elapsedTime = Math.abs((new Date()).getTime() - startTime);
    }
    return null;
}
Also used : EpsonProjectorException(org.openhab.binding.epsonprojector.internal.EpsonProjectorException) Date(java.util.Date)

Aggregations

EpsonProjectorException (org.openhab.binding.epsonprojector.internal.EpsonProjectorException)6 IOException (java.io.IOException)4 Date (java.util.Date)2 CommPort (gnu.io.CommPort)1 CommPortIdentifier (gnu.io.CommPortIdentifier)1 Socket (java.net.Socket)1