Skip to content

Commit

Permalink
Better exception logging for modbus
Browse files Browse the repository at this point in the history
  • Loading branch information
micw committed Oct 15, 2024
1 parent 5110de1 commit a00d57b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ protected void setupLogger() {
log = LoggerFactory.getLogger(getClass().getName()+"."+id);
}

protected String getCanonicalExceptionMessage(Exception exception) {
return exception.getMessage();
}

protected boolean checkLastException(Exception newException) {
if (lastException==null || lastException.getClass()!=newException.getClass() ||
!lastException.getMessage().equals(newException.getMessage())) {
!getCanonicalExceptionMessage(lastException).equals(getCanonicalExceptionMessage(newException))) {
lastException=newException;
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.springframework.beans.factory.annotation.Autowired;

import com.ghgande.j2mod.modbus.ModbusIOException;
import com.ghgande.j2mod.modbus.msg.ReadInputRegistersRequest;
import com.ghgande.j2mod.modbus.msg.ReadInputRegistersResponse;

Expand Down Expand Up @@ -160,6 +161,14 @@ protected void schedule() {
}
}

protected String getCanonicalExceptionMessage(Exception exception) {
if (exception instanceof ModbusIOException) {
String message=exception.getMessage();
message=message.replaceAll("^Executing transaction .*? failed \\(tried \\d+ times\\) ","");
return message;
}
return super.getCanonicalExceptionMessage(exception);
}

// FIXME: copied from my SML decode - de-dupplicate later
public Number scaleNumber(Number value, Number scaler) {
Expand Down

0 comments on commit a00d57b

Please sign in to comment.