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);
}
}
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);
}
}
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);
}
}
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;
}
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;
}
Aggregations