use of java.io.BufferedWriter in project openhab1-addons by openhab.
the class AlarmDecoderBinding method connect.
private synchronized void connect() {
try {
// make sure we have disconnected
disconnect();
markAllItemsUnupdated();
if (m_tcpHostName != null && m_tcpPort > 0) {
m_socket = new Socket(m_tcpHostName, m_tcpPort);
m_reader = new BufferedReader(new InputStreamReader(m_socket.getInputStream()));
m_writer = new BufferedWriter(new OutputStreamWriter(m_socket.getOutputStream()));
logger.info("connected to {}:{}", m_tcpHostName, m_tcpPort);
startMsgReader();
} else if (this.m_serialDeviceName != null) {
/*
* by default, RXTX searches only devices /dev/ttyS* and
* /dev/ttyUSB*, and will so not find symlinks. The
* setProperty() call below helps
*/
updateSerialProperties(m_serialDeviceName);
CommPortIdentifier ci = CommPortIdentifier.getPortIdentifier(m_serialDeviceName);
CommPort cp = ci.open("openhabalarmdecoder", 10000);
if (cp == null) {
throw new IllegalStateException("cannot open serial port!");
}
if (cp instanceof SerialPort) {
m_port = (SerialPort) cp;
} else {
throw new IllegalStateException("unknown port type");
}
m_port.setSerialPortParams(m_portSpeed, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
m_port.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
m_port.disableReceiveFraming();
m_port.disableReceiveThreshold();
m_reader = new BufferedReader(new InputStreamReader(m_port.getInputStream()));
m_writer = new BufferedWriter(new OutputStreamWriter(m_port.getOutputStream()));
logger.info("connected to serial port: {}", m_serialDeviceName);
startMsgReader();
} else {
logger.warn("alarmdecoder hostname or port not configured!");
}
} catch (PortInUseException e) {
logger.error("cannot open serial port: {}, it is in use!", m_serialDeviceName);
} catch (UnsupportedCommOperationException e) {
logger.error("got unsupported operation {} on port {}", e.getMessage(), m_serialDeviceName);
} catch (NoSuchPortException e) {
logger.error("got no such port for {}", m_serialDeviceName);
} catch (IllegalStateException e) {
logger.error("got unknown port type for {}", m_serialDeviceName);
} catch (UnknownHostException e) {
logger.error("unknown host name :{}: ", m_tcpHostName, e);
} catch (IOException e) {
logger.error("cannot open connection to {}", m_connectString);
}
}
use of java.io.BufferedWriter in project jodd by oblac.
the class PropsUtilTest method canUseBufferedWriterToWriteBasePropertiesToProps.
@Test
public void canUseBufferedWriterToWriteBasePropertiesToProps() throws IOException, URISyntaxException {
final Properties properties = new Properties();
properties.setProperty("myOneProperty", "and it's value");
final StringWriter stringWriter = new StringWriter();
final BufferedWriter writer = new BufferedWriter(stringWriter);
safelyWritePropertiesToProps(writer, properties);
final String expectedResourceFileName = getResourcePath("/singleProperty.props");
final String actual = stringWriter.toString();
// assertEqualProps(actual, expectedResourceFileName);
assertEqualsToPropsFile(actual, expectedResourceFileName);
}
use of java.io.BufferedWriter in project openhab1-addons by openhab.
the class AirConditioner method writeLine.
private void writeLine(String line) throws Exception {
logger.debug("Sending request:'{}'", line);
if (!isConnected()) {
login();
}
BufferedWriter writer;
try {
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
writer.write(line);
writer.newLine();
writer.flush();
} catch (Exception e) {
logger.debug("Could not write line. Disconnecting.", e);
disconnect();
throw (e);
}
}
use of java.io.BufferedWriter in project openhab1-addons by openhab.
the class SerialConnector method connect.
public void connect() throws EHealthException {
logger.debug("Going to open Serial connection on port '{}'", portName);
// parse ports and if the default port is found, initialized the reader
Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (id.getName().equals(portName)) {
logger.debug("Serial port '{}' has been found.", portName);
portId = id;
}
}
}
if (portId != null) {
// initialize serial port
try {
serialPort = portId.open("openHAB", 2000);
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
bReader = new BufferedReader(new InputStreamReader(inputStream));
bWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
serialPort.setSerialPortParams(BAUD, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (IOException e) {
throw new EHealthException(e);
} catch (PortInUseException e) {
throw new EHealthException(e);
} catch (UnsupportedCommOperationException e) {
throw new EHealthException(e);
} catch (TooManyListenersException e) {
throw new EHealthException(e);
}
} else {
StringBuilder sb = new StringBuilder();
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
sb.append(id.getName() + "\n");
}
}
throw new EHealthException("Serial port '" + portName + "' could not be found. Available ports are:\n" + sb.toString());
}
}
use of java.io.BufferedWriter in project openhab1-addons by openhab.
the class LgTvChannelSet method savetofile.
/**
* Save Channel List to File f
*
* @param f
*/
public void savetofile(String f) {
Writer writer = null;
JAXBContext jc;
try {
jc = JAXBContext.newInstance(envelope.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "utf-8"));
marshaller.marshal(envel, writer);
} catch (PropertyException e) {
logger.error("error in savetofile", e);
} catch (JAXBException e) {
logger.error("error in savetofile", e);
} catch (IOException ex) {
logger.error("error in savetofile", ex);
} finally {
try {
writer.close();
} catch (Exception ex) {
}
}
}
Aggregations