Skip to content

Commit

Permalink
Use connector for monitor links
Browse files Browse the repository at this point in the history
  • Loading branch information
bmalinowsky committed Mar 10, 2024
1 parent 2c8ae00 commit ad8dda7
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/io/calimero/tools/NetworkMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@

import java.lang.System.Logger;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.HexFormat;
import java.util.List;
Expand All @@ -65,11 +67,13 @@
import io.calimero.cemi.CEMIBusMon;
import io.calimero.cemi.RFMediumInfo.RSS;
import io.calimero.knxnetip.KNXnetIPConnection;
import io.calimero.link.Connector;
import io.calimero.link.KNXNetworkMonitor;
import io.calimero.link.KNXNetworkMonitorFT12;
import io.calimero.link.KNXNetworkMonitorIP;
import io.calimero.link.KNXNetworkMonitorTpuart;
import io.calimero.link.KNXNetworkMonitorUsb;
import io.calimero.link.LinkEvent;
import io.calimero.link.LinkListener;
import io.calimero.link.MonitorFrameEvent;
import io.calimero.link.medium.KNXMediumSettings;
Expand All @@ -81,6 +85,7 @@
import io.calimero.link.medium.TP1Ack;
import io.calimero.link.medium.TPSettings;
import io.calimero.log.LogService;
import io.calimero.serial.ConnectionStatus;

/**
* A tool for Calimero allowing monitoring of KNX network messages.
Expand Down Expand Up @@ -267,7 +272,7 @@ public void start() throws KNXException, InterruptedException
return;
}

m = createMonitor();
m = newMonitor();

// on console we want to have all possible information, so enable
// decoding of a received raw frame by the monitor link
Expand Down Expand Up @@ -439,6 +444,19 @@ protected void onCompletion(final Exception thrown, final boolean canceled)
out.log(ERROR, thrown.getMessage() != null ? thrown.getMessage() : thrown.getClass().getName());
}

private KNXNetworkMonitor newMonitor() throws KNXException, InterruptedException {
@SuppressWarnings("resource")
final var monitor = new Connector().reconnectOn(false, true, true).reconnectDelay(Duration.ofSeconds(4))
.maxConnectAttempts(3).newMonitor(() -> createMonitor());
monitor.addMonitorListener(new LinkListener() {
@LinkEvent
void connectionStatus(final ConnectionStatus status) {
System.out.println(LocalTime.now().truncatedTo(ChronoUnit.MILLIS) + " connection status KNX " + status);
}
});
return monitor;
}

/**
* Creates the KNX network monitor link to access the network specified in <code>options</code>.
*
Expand Down

0 comments on commit ad8dda7

Please sign in to comment.