Skip to content

Commit

Permalink
Fix cli so that it does not require a connection (#2521)
Browse files Browse the repository at this point in the history
* Fixed a problem with cli which will only require a connection if homing or a file should be runned
* Updated vulnerable library
  • Loading branch information
breiler committed May 15, 2024
1 parent ba8754e commit f041d2a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<ugs.junit.version>4.13.2</ugs.junit.version>
<ugs.hamcrest-core.version>2.2</ugs.hamcrest-core.version>
<ugs.maven-shade-plugin.version>3.4.1</ugs.maven-shade-plugin.version>
<ugs.progressbar.version>0.7.2</ugs.progressbar.version>
<ugs.progressbar.version>0.10.1</ugs.progressbar.version>
<ugs.commons-cli.version>1.4</ugs.commons-cli.version>
<ugs.easymock.version>5.2.0</ugs.easymock.version>
<ugs.maven-assembly-plugin.version>2.5.3</ugs.maven-assembly-plugin.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ 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;
import org.apache.commons.lang3.StringUtils;

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
* configuration and wait until it is idling or returns an alarm.
*
* @author Joacim Breiler
*/
public class BackendInitializerHelper implements UGSEventListener {
public class BackendInitializerHelper {

private static BackendInitializerHelper instance;

Expand Down Expand Up @@ -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<String> portNames = ConnectionFactory.getPortNames(settings.getConnectionDriver());
List<String> 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);
Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -164,7 +165,7 @@ private void resetAlarm() {
*/
private void listPorts() {
Settings settings = SettingsFactory.loadSettings();
List<String> portNames = ConnectionFactory.getPortNames(settings.getConnectionDriver());
List<String> portNames = ConnectionFactory.getDevices(settings.getConnectionDriver()).stream().map(IConnectionDevice::getAddress).toList();
System.out.println("Available ports: " + Arrays.toString(portNames.toArray()));
}

Expand Down

0 comments on commit f041d2a

Please sign in to comment.