diff --git a/ugs-cli/src/main/java/com/willwinder/ugs/cli/BackendInitializerHelper.java b/ugs-cli/src/main/java/com/willwinder/ugs/cli/BackendInitializerHelper.java index 99987755c3..8638e79357 100644 --- a/ugs-cli/src/main/java/com/willwinder/ugs/cli/BackendInitializerHelper.java +++ b/ugs-cli/src/main/java/com/willwinder/ugs/cli/BackendInitializerHelper.java @@ -19,11 +19,10 @@ This file is part of Universal Gcode Sender (UGS). package com.willwinder.ugs.cli; import com.willwinder.universalgcodesender.connection.ConnectionFactory; +import com.willwinder.universalgcodesender.connection.IConnectionDevice; import com.willwinder.universalgcodesender.listeners.ControllerState; -import com.willwinder.universalgcodesender.listeners.UGSEventListener; import com.willwinder.universalgcodesender.model.BackendAPI; import com.willwinder.universalgcodesender.model.GUIBackend; -import com.willwinder.universalgcodesender.model.UGSEvent; import com.willwinder.universalgcodesender.utils.Settings; import com.willwinder.universalgcodesender.utils.SettingsFactory; import com.willwinder.universalgcodesender.utils.ThreadHelper; @@ -31,6 +30,7 @@ This file is part of Universal Gcode Sender (UGS). import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; /** * Helper for initializing the backend. It will attempt to connect to controller using the given @@ -38,7 +38,7 @@ This file is part of Universal Gcode Sender (UGS). * * @author Joacim Breiler */ -public class BackendInitializerHelper implements UGSEventListener { +public class BackendInitializerHelper { private static BackendInitializerHelper instance; @@ -66,24 +66,20 @@ public BackendAPI initialize(Configuration configuration) { BackendAPI backend = new GUIBackend(); try { - backend.addUGSEventListener(this); backend.applySettings(backendSettings); backend.getSettings().setFirmwareVersion(firmware); // Only connect if port is available Settings settings = SettingsFactory.loadSettings(); - List portNames = ConnectionFactory.getPortNames(settings.getConnectionDriver()); + List portNames = ConnectionFactory.getDevices(settings.getConnectionDriver()).stream() + .map(IConnectionDevice::getAddress).toList(); if (portNames.contains(port)) { backend.connect(firmware, port, baudRate); } - ThreadHelper.waitUntil(() -> backend.getControllerState() == ControllerState.IDLE || backend.getControllerState() == ControllerState.ALARM, 8000, TimeUnit.MILLISECONDS); - Thread.sleep(4000); - - if (backend.isConnected()) { - System.out.println("Connected to \"" + backend.getController().getFirmwareVersion() + "\" on " + port + " baud " + baudRate); - } else { - throw new RuntimeException(); + // If we want to send a file we must wait for the controller to be connected + if (configuration.hasOption(OptionEnum.FILE) || configuration.hasOption(OptionEnum.HOME)) { + waitForMachineToBeIdle(port, baudRate, backend); } } catch (Exception e) { System.err.println("Couldn't connect to controller with firmware \"" + firmware + "\" on " + port + " baud " + baudRate); @@ -92,15 +88,19 @@ public BackendAPI initialize(Configuration configuration) { System.err.println(e.getMessage()); } System.exit(-1); - } finally { - backend.removeUGSEventListener(this); } return backend; } - @Override - public void UGSEvent(UGSEvent evt) { - // TODO handle controller status events + private static void waitForMachineToBeIdle(String port, int baudRate, BackendAPI backend) throws TimeoutException, InterruptedException { + ThreadHelper.waitUntil(() -> backend.getControllerState() == ControllerState.IDLE || backend.getControllerState() == ControllerState.ALARM, 8000, TimeUnit.MILLISECONDS); + Thread.sleep(1000); + + if (backend.isConnected()) { + System.out.println("Connected to \"" + backend.getController().getFirmwareVersion() + "\" on " + port + " baud " + baudRate); + } else { + throw new RuntimeException(); + } } } diff --git a/ugs-cli/src/main/java/com/willwinder/ugs/cli/TerminalClient.java b/ugs-cli/src/main/java/com/willwinder/ugs/cli/TerminalClient.java index 22a81029a3..1ea804a3b6 100644 --- a/ugs-cli/src/main/java/com/willwinder/ugs/cli/TerminalClient.java +++ b/ugs-cli/src/main/java/com/willwinder/ugs/cli/TerminalClient.java @@ -20,6 +20,7 @@ This file is part of Universal Gcode Sender (UGS). import com.willwinder.universalgcodesender.connection.ConnectionDriver; import com.willwinder.universalgcodesender.connection.ConnectionFactory; +import com.willwinder.universalgcodesender.connection.IConnectionDevice; import com.willwinder.universalgcodesender.listeners.ControllerState; import com.willwinder.universalgcodesender.model.BackendAPI; import com.willwinder.universalgcodesender.pendantui.PendantUI; @@ -164,7 +165,7 @@ private void resetAlarm() { */ private void listPorts() { Settings settings = SettingsFactory.loadSettings(); - List portNames = ConnectionFactory.getPortNames(settings.getConnectionDriver()); + List portNames = ConnectionFactory.getDevices(settings.getConnectionDriver()).stream().map(IConnectionDevice::getAddress).toList(); System.out.println("Available ports: " + Arrays.toString(portNames.toArray())); }