Skip to content

Commit

Permalink
Remove event for communicator state (#2093)
Browse files Browse the repository at this point in the history
Communicator state changes will no longer be dispatched as they aren't used by the UI. Also removed the boolean for notifying if the file send was a success as it was always true.
  • Loading branch information
breiler authored Dec 22, 2022
1 parent 14fdd32 commit 6c69e1b
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1843,21 +1843,17 @@ public void UGSEvent(UGSEvent evt) {
case FILE_STREAM_COMPLETE:
remainingTimeValueLabel.setText(Utils.formattedMillis(0));
remainingRowsValueLabel.setText("" + backend.getNumRemainingRows());
if (fileStateEvent.isSuccess()) {
EventQueue.invokeLater(() -> {
JOptionPane.showMessageDialog(new JFrame(),
Localization.getString("mainWindow.ui.jobComplete") + " " + Utils.formattedMillis(backend.getSendDuration()),
Localization.getString("success"), JOptionPane.INFORMATION_MESSAGE);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {}

// Stop the timer after a delay to make sure it is updated.
timer.stop();
});
} else {
displayErrorDialog(Localization.getString("mainWindow.error.jobComplete"));
}
EventQueue.invokeLater(() -> {
JOptionPane.showMessageDialog(new JFrame(),
Localization.getString("mainWindow.ui.jobComplete") + " " + Utils.formattedMillis(backend.getSendDuration()),
Localization.getString("success"), JOptionPane.INFORMATION_MESSAGE);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {}

// Stop the timer after a delay to make sure it is updated.
timer.stop();
});
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ This file is part of Universal Gcode Sender (UGS).
import org.apache.commons.lang3.time.StopWatch;

import java.io.IOException;
import java.io.NotActiveException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -652,7 +651,7 @@ private void updateNumCommands() {
}

// No longer a listener event
protected void fileStreamComplete(String filename, boolean success) {
protected void fileStreamComplete(String filename) {

String duration =
com.willwinder.universalgcodesender.Utils.
Expand All @@ -661,7 +660,7 @@ protected void fileStreamComplete(String filename, boolean success) {
this.dispatchConsoleMessage(MessageType.INFO,"\n**** Finished sending file in "+duration+" ****\n\n");
this.streamStopWatch.stop();
this.isStreaming = false;
dispatchStreamComplete(filename, success);
dispatchStreamComplete(filename);
}

@Override
Expand All @@ -687,8 +686,6 @@ public void communicatorPausedOnError() {
}
else {
this.pauseStreaming();
// In check mode there is no state transition, so we need to manually make the notification.
this.dispatchStateChange(COMM_SENDING_PAUSED);
}
} catch (Exception ignored) {
logger.log(Level.SEVERE, "Couldn't set the state to paused.");
Expand All @@ -703,10 +700,7 @@ public void checkStreamFinished() {
rowsRemaining() <= 0 &&
(state == ControllerState.CHECK || state == ControllerState.IDLE)) {
String streamName = "queued commands";

// Make sure the GUI gets updated when the file finishes
this.dispatchStateChange(getCommunicatorState());
this.fileStreamComplete(streamName, true);
this.fileStreamComplete(streamName);
}
}

Expand Down Expand Up @@ -788,9 +782,6 @@ public void rawResponseListener(String response) {

protected void setCurrentState(CommunicatorState state) {
this.currentState = state;
if (!this.handlesAllStateChangeEvents()) {
this.dispatchStateChange(state);
}
}

@Override
Expand All @@ -810,11 +801,7 @@ public void removeListener(ControllerListener listener) {
}

protected void dispatchStatusString(ControllerStatus status) {
if (listeners != null) {
for (ControllerListener c : listeners) {
c.statusStringListener(status);
}
}
listeners.forEach(l -> l.statusStringListener(status));
}

protected void dispatchConsoleMessage(MessageType type, String message) {
Expand All @@ -825,58 +812,28 @@ protected void dispatchConsoleMessage(MessageType type, String message) {
}
}

protected void dispatchStateChange(CommunicatorState state) {
if (listeners != null) {
for (ControllerListener c : listeners) {
c.controlStateChange(state);
}
}
}

protected void dispatchStreamComplete(String filename, Boolean success) {
if (listeners != null) {
for (ControllerListener c : listeners) {
c.fileStreamComplete(filename, success);
}
}
protected void dispatchStreamComplete(String filename) {
listeners.forEach(l -> l.fileStreamComplete(filename));
}

protected void dispatchCommandSkipped(GcodeCommand command) {
if (listeners != null) {
for (ControllerListener c : listeners) {
c.commandSkipped(command);
}
}
listeners.forEach(l -> l.commandSkipped(command));
}

protected void dispatchCommandSent(GcodeCommand command) {
if (listeners != null) {
for (ControllerListener c : listeners) {
c.commandSent(command);
}
}
listeners.forEach(l -> l.commandSent(command));
}

protected void dispatchCommandComplete(GcodeCommand command) {
if (listeners != null) {
for (ControllerListener c : listeners) {
c.commandComplete(command);
}
}
listeners.forEach(l -> l.commandComplete(command));
}

protected void dispatchAlarm(Alarm alarm) {
if (listeners != null) {
listeners.forEach(l -> l.receivedAlarm(alarm));
}
listeners.forEach(l -> l.receivedAlarm(alarm));
}

protected void dispatchProbeCoordinates(Position p) {
if (listeners != null) {
for (ControllerListener c : listeners) {
c.probeCoordinates(p);
}
}
listeners.forEach(l -> l.probeCoordinates(p));
}

protected String getUnitsCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,6 @@ public GrblController() {
this(new GrblCommunicator());
}

@Override
public boolean handlesAllStateChangeEvents() {
return capabilities.hasCapability(GrblCapabilitiesConstants.REAL_TIME);
}

@Override
public Capabilities getCapabilities() {
return capabilities;
Expand Down Expand Up @@ -163,7 +158,6 @@ else if (GrblUtils.isOkErrorAlarmResponse(response)) {
Alarm alarm = GrblUtils.parseAlarmResponse(response);
dispatchAlarm(alarm);
dispatchStatusString(controllerStatus);
dispatchStateChange(COMM_IDLE);
}

// If there is an active command, mark it as completed with error
Expand Down Expand Up @@ -250,7 +244,6 @@ else if (GrblUtils.isGrblFeedbackMessage(response, capabilities)) {
this.dispatchConsoleMessage(MessageType.VERBOSE, grblFeedbackMessage + "\n");
setDistanceModeCode(grblFeedbackMessage.getDistanceMode());
setUnitsCode(grblFeedbackMessage.getUnits());
dispatchStateChange(COMM_IDLE);
}

else if (GrblUtils.isGrblSettingMessage(response)) {
Expand Down Expand Up @@ -343,7 +336,6 @@ protected void cancelSendBeforeEvent() throws Exception {
else if (!paused && this.capabilities.hasCapability(GrblCapabilitiesConstants.REAL_TIME)) {
try {
this.pauseStreaming();
this.dispatchStateChange(CommunicatorState.COMM_SENDING_PAUSED);
} catch (Exception e) {
// Oh well, was worth a shot.
System.out.println("Exception while trying to issue a soft reset: " + e.getMessage());
Expand Down Expand Up @@ -628,11 +620,6 @@ private void handleStatusString(final String string) {
detectAxisCapabilityFromControllerStatus(Axis.B, CapabilitiesConstants.B_AXIS);
detectAxisCapabilityFromControllerStatus(Axis.C, CapabilitiesConstants.C_AXIS);

// Make UGS more responsive to the state being reported by GRBL.
if (before != getCommunicatorState()) {
this.dispatchStateChange(getCommunicatorState());
}

// GRBL 1.1 jog complete transition
if (beforeState == ControllerState.JOG && controllerStatus.getState() == ControllerState.IDLE) {
this.comm.cancelSend();
Expand All @@ -655,9 +642,6 @@ private void handleStatusString(final String string) {
// If the machine goes into idle, we no longer need to cancel.
if (controllerStatus.getState() == ControllerState.IDLE || controllerStatus.getState() == ControllerState.CHECK) {
isCanceling = false;

// Make sure the GUI gets updated
this.dispatchStateChange(getCommunicatorState());
}
// Otherwise check if the machine is Hold/Queue and stopped.
else if ((controllerStatus.getState() == ControllerState.HOLD || controllerStatus.getState() == ControllerState.DOOR) && lastLocation.equals(this.controllerStatus.getMachineCoord())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,6 @@ public interface IController {
* In case a controller reset is detected.
*/
void resetBuffers();

/**
* Indicator to abstract GUIBackend implementation that the contract class
* will handle ALL state change events. When this returns true it means
* things like completing the final command in a stream will not
* automatically re-enable buttons.
*/
boolean handlesAllStateChangeEvents();

/*
Stream content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ private void handleStatusResponse(String response) {

UnitUtils.Units currentUnits = getCurrentGcodeState().getUnits();
controllerStatus = SmoothieUtils.getStatusFromStatusString(controllerStatus, response, currentUnits);
dispatchStateChange(getCommunicatorState());
dispatchStatusString(controllerStatus);
}

Expand Down Expand Up @@ -235,11 +234,6 @@ public void performHomingCycle() throws Exception {
dispatchStatusString(controllerStatus);
}

@Override
public boolean handlesAllStateChangeEvents() {
return false;
}

@Override
public Capabilities getCapabilities() {
return capabilities;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ protected void setControllerState(ControllerState controllerState) {
dispatchStatusString(controllerStatus);
}

@Override
public boolean handlesAllStateChangeEvents() {
return false;
}

@Override
public Capabilities getCapabilities() {
return capabilities;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,11 +589,6 @@ public void resetBuffers() {
communicator.resetBuffers();
}

@Override
public boolean handlesAllStateChangeEvents() {
return true;
}

@Override
public GcodeCommand createCommand(String command) throws Exception {
return new FluidNCCommand(command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ This file is part of Universal Gcode Sender (UGS).

import com.willwinder.universalgcodesender.model.Alarm;
import com.willwinder.universalgcodesender.model.Position;
import com.willwinder.universalgcodesender.model.CommunicatorState;
import com.willwinder.universalgcodesender.types.GcodeCommand;

/**
Expand All @@ -29,16 +28,10 @@ This file is part of Universal Gcode Sender (UGS).
* @author wwinder
*/
public interface ControllerListener {
/**
* The controller has modified the state by itself, such as pausing a job on
* an error.
*/
void controlStateChange(CommunicatorState state);

/**
* The file streaming has completed.
*/
void fileStreamComplete(String filename, boolean success);
void fileStreamComplete(String filename);

/**
* If an alarm is received from the controller
Expand All @@ -51,12 +44,12 @@ public interface ControllerListener {
* A command in the stream has been skipped.
*/
void commandSkipped(GcodeCommand command);

/**
* A command has successfully been sent to the controller.
*/
void commandSent(GcodeCommand command);

/**
* A command has been processed by the the controller.
*/
Expand All @@ -71,4 +64,4 @@ public interface ControllerListener {
* Controller status information.
*/
void statusStringListener(ControllerStatus status);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class GUIBackend implements BackendAPI, ControllerListener, SettingChange
private boolean streamFailed = false;
private boolean autoconnect = false;

private GcodeParser gcp = new GcodeParser();
private final GcodeParser gcp = new GcodeParser();
private ControllerStatus controllerStatus = new ControllerStatus();

@Override
Expand Down Expand Up @@ -675,12 +675,8 @@ public void requestParserState() throws Exception {
// Controller Listener //
/////////////////////////
@Override
public void controlStateChange(CommunicatorState state) {
}

@Override
public void fileStreamComplete(String filename, boolean success) {
this.sendUGSEvent(new FileStateEvent(FileState.FILE_STREAM_COMPLETE, filename, success));
public void fileStreamComplete(String filename) {
this.sendUGSEvent(new FileStateEvent(FileState.FILE_STREAM_COMPLETE, filename));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ This file is part of Universal Gcode Sender (UGS).
public class FileStateEvent implements UGSEvent {
private final FileState fileState;
private final String file;
private final boolean success;

/**
* Create a file state event
Expand All @@ -39,23 +38,8 @@ public class FileStateEvent implements UGSEvent {
* @param filepath the file related to the file event.
*/
public FileStateEvent(FileState state, String filepath) {
this(state, filepath, true);
}

/**
* Create a file state event
* FILE_LOADING: This event provides a path to an unprocessed gcode file.
* FILE_LOADED: This event provides a path to a processed gcode file which
* should be opened with a GcodeStreamReader.
*
* @param state the new file state.
* @param filepath the file related to the file event.
* @param success if the given file state was a success, usually used together with {@link FileState#FILE_STREAM_COMPLETE} to indicate if file was sent
*/
public FileStateEvent(FileState state, String filepath, boolean success) {
this.fileState = state;
this.file = filepath;
this.success = success;
}

public FileState getFileState() {
Expand All @@ -65,8 +49,4 @@ public FileState getFileState() {
public String getFile() {
return file;
}

public boolean isSuccess() {
return success;
}
}
Loading

0 comments on commit 6c69e1b

Please sign in to comment.