use of org.openhab.binding.swegonventilation.internal.SwegonVentilationException in project openhab1-addons by openhab.
the class SwegonVentilationUDPConnector method connect.
@Override
public void connect() throws SwegonVentilationException {
if (socket == null) {
try {
socket = new DatagramSocket(port);
logger.debug("Swegon ventilation UDP message listener started");
} catch (SocketException e) {
throw new SwegonVentilationException(e);
}
}
}
use of org.openhab.binding.swegonventilation.internal.SwegonVentilationException in project openhab1-addons by openhab.
the class SwegonVentilationSerialConnector method connect.
@Override
public void connect() throws SwegonVentilationException {
try {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(BAUDRATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
in = serialPort.getInputStream();
logger.debug("Swegon ventilation Serial Port message listener started");
} catch (Exception e) {
throw new SwegonVentilationException(e);
}
}
use of org.openhab.binding.swegonventilation.internal.SwegonVentilationException in project openhab1-addons by openhab.
the class SwegonVentilationSerialConnector method receiveDatagram.
@Override
public byte[] receiveDatagram() throws SwegonVentilationException {
if (in == null) {
connect();
}
byte[] buffer = new byte[1024];
byte[] message = new byte[1024];
int len = -1;
int msgLen = 0;
boolean start_found = false;
int index = 0;
try {
while ((len = this.in.read(buffer)) > 0) {
for (int i = 0; i < len; i++) {
if (buffer[i] == (byte) 0xCC) {
start_found = true;
index = 0;
msgLen = 0;
} else if (start_found) {
message[index++] = buffer[i];
if (index == 5) {
if (message[0] == (byte) 0x64) {
msgLen = message[4];
}
}
if (msgLen > 0 && index == (5 + msgLen + 2)) {
int calculatedCRC = calculateCRC(message, index - 2);
int msgCRC = toInt(message[5 + msgLen], message[5 + msgLen + 1]);
if (msgCRC == calculatedCRC) {
byte[] data = new byte[5 + msgLen];
for (int j = 0; j < (5 + msgLen); j++) {
data[j] = message[j];
}
return data;
} else {
throw new SwegonVentilationException("CRC does not match");
}
}
}
}
}
} catch (IOException e) {
throw new SwegonVentilationException("Error occured while receiving data", e);
}
return null;
}
use of org.openhab.binding.swegonventilation.internal.SwegonVentilationException in project openhab1-addons by openhab.
the class SwegonVentilationSimulator method receiveDatagram.
@Override
public byte[] receiveDatagram() throws SwegonVentilationException {
try {
Thread.sleep(5000);
final byte[] testdata21 = new byte[] { // (byte) 0xCC,
(byte) 0x64, (byte) 0x85, (byte) 0xFD, (byte) 0x0A, (byte) 0x0B, (byte) 0x21, (byte) 0x01, (byte) 0xA0, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x0A, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x8C, (byte) 0x17 };
final byte[] testdata71 = new byte[] { // (byte) 0xCC,
(byte) 0x64, (byte) 0x85, (byte) 0xFD, (byte) 0x0A, (byte) 0x13, (byte) 0x71, (byte) 0x00, (byte) 0xA0, (byte) 0xFD, (byte) 0x09, (byte) 0x15, (byte) 0x09, (byte) 0x0A, (byte) 0x04, (byte) 0xFB, (byte) 0x09, (byte) 0x00, (byte) 0x00, (byte) 0x9B, (byte) 0xA8, (byte) 0x2D, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x97 };
final byte[] testdata73a = new byte[] { // (byte) 0xCC,
(byte) 0x64, (byte) 0x85, (byte) 0xFD, (byte) 0x0A, (byte) 0x18, (byte) 0x73, (byte) 0x00, (byte) 0xA0, (byte) 0x00, (byte) 0x00, (byte) 0x14, (byte) 0x00, (byte) 0x21, (byte) 0x00, (byte) 0x0A, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x02, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x07, (byte) 0x01, (byte) 0x49 };
final byte[] testdata73b = new byte[] { // (byte) 0xCC,
(byte) 0x64, (byte) 0x85, (byte) 0xFD, (byte) 0x0A, (byte) 0x18, (byte) 0x73, (byte) 0x00, (byte) 0xA0, (byte) 0x80, (byte) 0x00, (byte) 0x14, (byte) 0x00, (byte) 0x21, (byte) 0x00, (byte) 0x0A, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x86, (byte) 0x00, (byte) 0x02, (byte) 0x02, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x07, (byte) 0x4F, (byte) 0xFB };
final byte[] testdata73c = new byte[] { // (byte) 0xCC,
(byte) 0x64, (byte) 0x85, (byte) 0xFD, (byte) 0x0A, (byte) 0x18, (byte) 0x73, (byte) 0x00, (byte) 0xA0, (byte) 0x10, (byte) 0x00, (byte) 0x14, (byte) 0x00, (byte) 0x21, (byte) 0x00, (byte) 0x0A, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xE3, (byte) 0x00, (byte) 0x02, (byte) 0x02, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x07, (byte) 0x9D, (byte) 0xB7 };
final byte[] testdata73d = new byte[] { // (byte) 0xCC,
(byte) 0x64, (byte) 0x85, (byte) 0xFD, (byte) 0x0A, (byte) 0x18, (byte) 0x73, (byte) 0x00, (byte) 0xA0, (byte) 0x90, (byte) 0x00, (byte) 0x14, (byte) 0x00, (byte) 0x21, (byte) 0x00, (byte) 0x0A, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xE3, (byte) 0x00, (byte) 0x02, (byte) 0x02, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x07, (byte) 0xAB, (byte) 0x5B };
final byte[][] messages = new byte[][] { testdata21, testdata71, testdata73a, testdata73b, testdata73c, testdata73d };
if (++counter >= messages.length) {
counter = 0;
}
byte[] message = messages[counter];
int msgLen = 0;
if (message[0] == (byte) 0x64) {
msgLen = message[4];
}
int calculatedCRC = calculateCRC(message, message.length - 2);
int msgCRC = toInt(message[5 + msgLen], message[5 + msgLen + 1]);
if (msgCRC == calculatedCRC) {
byte[] data = new byte[5 + msgLen];
for (int j = 0; j < (5 + msgLen); j++) {
data[j] = message[j];
}
return data;
} else {
throw new SwegonVentilationException("CRC does not match");
}
} catch (InterruptedException e) {
throw new SwegonVentilationException(e);
}
}
use of org.openhab.binding.swegonventilation.internal.SwegonVentilationException in project openhab1-addons by openhab.
the class SwegonVentilationUDPConnector method receiveDatagram.
@Override
public byte[] receiveDatagram() throws SwegonVentilationException {
try {
if (socket == null) {
socket = new DatagramSocket(port);
}
// Create a packet
DatagramPacket packet = new DatagramPacket(new byte[MAX_PACKET_SIZE], MAX_PACKET_SIZE);
// Receive a packet (blocking)
socket.receive(packet);
return Arrays.copyOfRange(packet.getData(), 0, packet.getLength() - 1);
} catch (SocketException e) {
throw new SwegonVentilationException(e);
} catch (IOException e) {
throw new SwegonVentilationException(e);
}
}
Aggregations