Skip to content

Commit

Permalink
[tacmi] Applied requested changes from code review - final changes
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Niessner <[email protected]>
  • Loading branch information
marvkis committed Aug 9, 2020
1 parent 0287bcf commit 16574f0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 43 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ private class ReceiveThread extends Thread {

@Override
public void run() {
try {
final DatagramSocket coeSocket = TACmiCoEBridgeHandler.this.coeSocket;
if (coeSocket == null) {
logger.warn("coeSocket is NULL - Reader disabled!");
return;
}
while (!isInterrupted()) {
final byte[] receiveData = new byte[14];
final DatagramSocket coeSocket = TACmiCoEBridgeHandler.this.coeSocket;
if (coeSocket == null) {
logger.warn("coeSocket is NULL - Reader disabled!");
return;
}
while (!isInterrupted()) {
final byte[] receiveData = new byte[14];

try {
final DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
try {
coeSocket.receive(receivePacket);
Expand All @@ -98,44 +99,47 @@ public void run() {
continue;
}

try {
final byte[] data = receivePacket.getData();
Message message;
if (data[1] > 0 && data[1] < 9) {
message = new AnalogMessage(data);
} else if (data[1] == 0 || data[1] == 9) {
message = new DigitalMessage(data);
} else {
logger.debug("Invalid message received");
continue;
}
logger.debug("{}", message.toString());

final InetAddress remoteAddress = receivePacket.getAddress();
final int node = message.canNode;
boolean found = false;
for (final TACmiHandler cmi : registeredCMIs) {
if (cmi.isFor(remoteAddress, node)) {
cmi.handleCoE(message);
found = true;
}
}
if (!found) {
logger.info("Received CoE-Packet from {} Node {} and we don't have a Thing for!",
remoteAddress, node);
final byte[] data = receivePacket.getData();
Message message;
if (data[1] > 0 && data[1] < 9) {
message = new AnalogMessage(data);
} else if (data[1] == 0 || data[1] == 9) {
message = new DigitalMessage(data);
} else {
logger.debug("Invalid message received");
continue;
}
logger.debug("{}", message.toString());

final InetAddress remoteAddress = receivePacket.getAddress();
final int node = message.canNode;
boolean found = false;
for (final TACmiHandler cmi : registeredCMIs) {
if (cmi.isFor(remoteAddress, node)) {
cmi.handleCoE(message);
found = true;
}
} catch (final Exception t) {
logger.error("Error processing data: {}", t.getMessage(), t);
}
if (!found) {
logger.info("Received CoE-Packet from {} Node {} and we don't have a Thing for!", remoteAddress,
node);
}
} catch (final IOException e) {
if (isInterrupted()) {
return;
}
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Error processing data: " + e.getMessage());

} catch (RuntimeException e) {
// we catch runtime exceptions here to prevent the receiving thread to stop accidentally if
// something like a IllegalStateException or NumberFormatExceptions are thrown. This indicates a bug
// or a situation / setup I'm not thinking of ;)
if (isInterrupted()) {
return;
}
logger.error("Error processing data: {}", e.getMessage(), e);
}
logger.debug("ReceiveThread exiting.");
} catch (final Exception t) {
if (isInterrupted()) {
return;
}
// logged by framework via updateStatus
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Error processing data: " + t.getMessage());
}
}
}
Expand Down

0 comments on commit 16574f0

Please sign in to comment.