Skip to content

Commit

Permalink
Fixed a problem with cli which will only require a connection if homi…
Browse files Browse the repository at this point in the history
…ng or a file should be runned
  • Loading branch information
breiler committed May 15, 2024
1 parent ba8754e commit 9036c2e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
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 @@ -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 9036c2e

Please sign in to comment.