Skip to content

Commit

Permalink
Added listeners to commands to listen for when it completes. (#1809)
Browse files Browse the repository at this point in the history
* Added listeners to commands to listen for when it completes.
  • Loading branch information
breiler authored Mar 17, 2022
1 parent ff31d88 commit 63f4af8
Showing 18 changed files with 527 additions and 180 deletions.
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ This file is part of Universal Gcode Sender (UGS).

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -87,7 +89,7 @@ public abstract class AbstractController implements CommunicatorListener, IContr
private IGcodeStreamReader streamCommands; // The stream of commands to send.

// Listeners
private ArrayList<ControllerListener> listeners;
private List<ControllerListener> listeners;

//Track current mode to restore after jogging
private String distanceModeCode = null;
@@ -329,7 +331,7 @@ protected AbstractController(ICommunicator comm) {
this.comm.addListener(this);

this.activeCommands = new ArrayList<>();
this.listeners = new ArrayList<>();
this.listeners = Collections.synchronizedList(new ArrayList<>());
}

@Override
@@ -783,6 +785,7 @@ else if (hasComment && !hasCommand) {
this.dispatchConsoleMessage(MessageType.INFO, message.toString());
command.setResponse("<skipped by application>");
command.setSkipped(true);
command.setDone(true);
dispatchCommandSkipped(command);

checkStreamFinished();
@@ -799,10 +802,7 @@ public void commandComplete(String response) throws UnexpectedCommand {
}

GcodeCommand command = this.activeCommands.remove(0);

command.setResponse(response);
GrblUtils.updateGcodeCommandFromResponse(command, response);

updateCommandFromResponse(command, response);
updateParserModalState(command);

this.numCommandsCompleted++;
@@ -815,6 +815,8 @@ public void commandComplete(String response) throws UnexpectedCommand {
checkStreamFinished();
}

protected abstract void updateCommandFromResponse(GcodeCommand command, String response);

@Override
public void rawResponseListener(String response) {
rawResponseHandler(response);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2013-2020 Will Winder
Copyright 2013-2022 Will Winder
This file is part of Universal Gcode Sender (UGS).
@@ -75,7 +75,7 @@ public class GrblController extends AbstractController {
*/
private boolean temporaryCheckSingleStepMode = false;

public GrblController(AbstractCommunicator comm) {
public GrblController(ICommunicator comm) {
super(comm);

this.commandCreator = new GcodeCommandCreator();
@@ -691,4 +691,9 @@ public void sendOverrideCommand(Overrides command) throws Exception {
this.comm.sendByteImmediately(realTimeCommand);
}
}

@Override
protected void updateCommandFromResponse(GcodeCommand command, String response) {
GrblUtils.updateGcodeCommandFromResponse(command, response);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Will Winder
Copyright 2021-2022 Will Winder
This file is part of Universal Gcode Sender (UGS).
@@ -35,6 +35,11 @@ public GrblEsp32Controller() {
this.capabilities.addCapability(GrblCapabilitiesConstants.V1_FORMAT);
}

public GrblEsp32Controller(ICommunicator communicator) {
super(communicator);
this.capabilities.addCapability(GrblCapabilitiesConstants.V1_FORMAT);
}

Optional<Integer> getAxisCount(String response) {
Matcher m = axisCountPattern.matcher(response);
if (!m.find()) {
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2012-2020 Will Winder
Copyright 2012-2022 Will Winder
This file is part of Universal Gcode Sender (UGS).
@@ -654,5 +654,7 @@ public static void updateGcodeCommandFromResponse(GcodeCommand gcodeCommand, Str
gcodeCommand.setOk(false);
gcodeCommand.setError(true);
}

gcodeCommand.setDone(true);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2016-2019 Will Winder
Copyright 2016-2022 Will Winder
This file is part of Universal Gcode Sender (UGS).
@@ -310,4 +310,9 @@ protected void setControllerState(ControllerState controllerState) {
.build();
dispatchStatusString(controllerStatus);
}

@Override
protected void updateCommandFromResponse(GcodeCommand command, String response) {
GrblUtils.updateGcodeCommandFromResponse(command, response);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2013-2020 Will Winder
Copyright 2013-2022 Will Winder
This file is part of Universal Gcode Sender (UGS).
@@ -448,4 +448,19 @@ protected CommunicatorState getControlState(ControllerState controllerState) {
return COMM_IDLE;
}
}

@Override
protected void updateCommandFromResponse(GcodeCommand command, String response) {
JsonObject jo = TinyGUtils.jsonToObject(response);
command.setResponse(response);
if (TinyGUtils.isErrorResponse(jo)) {
command.setOk(false);
command.setError(true);
} else {
command.setOk(true);
command.setError(false);
}

command.setDone(true);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2013-2018 Will Winder
Copyright 2013-2022 Will Winder
This file is part of Universal Gcode Sender (UGS).
@@ -60,6 +60,7 @@ public class TinyGUtils {
public static final String FIELD_STATUS_REPORT = "sr";
private static final String FIELD_FIRMWARE_VERSION = "fv";
private static final String FIELD_RESPONSE = "r";
private static final String FIELD_STATUS_ERROR = "err";
private static final String FIELD_STATUS_REPORT_UNIT = "unit";
private static final String FIELD_STATUS_REPORT_POSX = "posx";
private static final String FIELD_STATUS_REPORT_POSY = "posy";
@@ -145,6 +146,10 @@ public static boolean isStatusResponse(JsonObject response) {
return response.has(TinyGUtils.FIELD_STATUS_REPORT) && response.get(TinyGUtils.FIELD_STATUS_REPORT).isJsonObject();
}

public static boolean isErrorResponse(JsonObject response) {
return response.has(TinyGUtils.FIELD_RESPONSE) && response.get(TinyGUtils.FIELD_RESPONSE).getAsJsonObject().has(FIELD_STATUS_ERROR);
}

/**
* Parses the TinyG status result response and creates a new current controller status
*
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2022 Will Winder
This file is part of Universal Gcode Sender (UGS).
UGS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
UGS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UGS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.willwinder.universalgcodesender.types;

/**
* A command listener that can be registered on a command to receive when it
* is considered to be completed.
*
* @author Joacim Breiler
*/
public interface CommandListener {
/**
* When the command has been completed with either status ok, error or skipped.
*
* @param command the command that was completed
*/
void onDone(GcodeCommand command);
}
Loading

0 comments on commit 63f4af8

Please sign in to comment.