diff --git a/java_console/io/src/com/rusefi/io/ConnectionWatchdog.java b/java_console/io/src/com/rusefi/io/ConnectionWatchdog.java index 9f2a2b9e2e..922f4e16c1 100644 --- a/java_console/io/src/com/rusefi/io/ConnectionWatchdog.java +++ b/java_console/io/src/com/rusefi/io/ConnectionWatchdog.java @@ -1,31 +1,26 @@ package com.rusefi.io; -import com.rusefi.FileLog; -import com.rusefi.Timeouts; - import javax.swing.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; +/** + * todo: open question if it's OK to use AWT timer in headless cases? + */ public class ConnectionWatchdog { - private static final Timer reconnectTimer = new Timer(Timeouts.CONNECTION_RESTART_DELAY, new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - FileLog.MAIN.logLine("ConnectionWatchdog.reconnectTimer restarting"); - LinkManager.restart(); - onDataArrived(); - } - }); + private final Timer reconnectTimer; - private ConnectionWatchdog() { + public ConnectionWatchdog(int timeoutMs, Runnable action) { + reconnectTimer = new Timer(timeoutMs, e -> { + action.run(); + onDataArrived(); + }); } - public static void start() { - HeartBeatListeners.INSTANCE.addListener(ConnectionWatchdog::onDataArrived); + public void start() { + HeartBeatListeners.INSTANCE.addListener(this::onDataArrived); onDataArrived(); } - private static void onDataArrived() { + private void onDataArrived() { /** * this timer will reconnect */ diff --git a/java_console/ui/src/com/rusefi/Launcher.java b/java_console/ui/src/com/rusefi/Launcher.java index bcd71bd645..674c3db9d9 100644 --- a/java_console/ui/src/com/rusefi/Launcher.java +++ b/java_console/ui/src/com/rusefi/Launcher.java @@ -84,8 +84,10 @@ public class Launcher extends rusEFIVersion { if (LinkManager.isLogViewerMode(port)) tabbedPane.addTab("Log Viewer", new LogViewer(engineSnifferPanel)); - ConnectionWatchdog.start(); - + new ConnectionWatchdog(Timeouts.CONNECTION_RESTART_DELAY, () -> { + FileLog.MAIN.logLine("ConnectionWatchdog.reconnectTimer restarting"); + LinkManager.restart(); + }).start(); GaugesPanel.DetachedRepository.INSTANCE.init(getConfig().getRoot().getChild("detached")); GaugesPanel.DetachedRepository.INSTANCE.load();