From f041d2afcc4200e58be07939d00100a2655a5fe7 Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Wed, 15 May 2024 18:33:52 +0200 Subject: [PATCH] Fix cli so that it does not require a connection (#2521) * Fixed a problem with cli which will only require a connection if homing or a file should be runned * Updated vulnerable library --- pom.xml | 2 +- .../ugs/cli/BackendInitializerHelper.java | 34 +++++++++---------- .../ugs/cli/ProgressBarPrinter.java | 3 +- .../willwinder/ugs/cli/TerminalClient.java | 3 +- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/pom.xml b/pom.xml index 5a92ccd05..01d4b2026 100644 --- a/pom.xml +++ b/pom.xml @@ -76,7 +76,7 @@ 4.13.2 2.2 3.4.1 - 0.7.2 + 0.10.1 1.4 5.2.0 2.5.3 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 99987755c..8638e7935 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/ProgressBarPrinter.java b/ugs-cli/src/main/java/com/willwinder/ugs/cli/ProgressBarPrinter.java index 94954a1ab..23f3b797b 100644 --- a/ugs-cli/src/main/java/com/willwinder/ugs/cli/ProgressBarPrinter.java +++ b/ugs-cli/src/main/java/com/willwinder/ugs/cli/ProgressBarPrinter.java @@ -26,6 +26,7 @@ This file is part of Universal Gcode Sender (UGS). import com.willwinder.universalgcodesender.model.events.ControllerStateEvent; import com.willwinder.universalgcodesender.model.events.FileState; import com.willwinder.universalgcodesender.model.events.FileStateEvent; +import me.tongfei.progressbar.ConsoleProgressBarConsumer; import me.tongfei.progressbar.ProgressBar; import me.tongfei.progressbar.ProgressBarBuilder; import me.tongfei.progressbar.ProgressBarStyle; @@ -52,7 +53,7 @@ public void UGSEvent(UGSEvent event) { .setStyle(ProgressBarStyle.UNICODE_BLOCK) .setInitialMax(100) .setTaskName(backend.getGcodeFile().getName()) - .setPrintStream(System.out) + .setConsumer(new ConsoleProgressBarConsumer(System.out)) .build(); } else if(fileStateEvent.getFileState() == FileState.FILE_STREAM_COMPLETE) { if (pb != null) { 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 22a81029a..1ea804a3b 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())); }