Skip to content

Commit

Permalink
Fixes for controller resets (#2584)
Browse files Browse the repository at this point in the history
Cancels the current stream if the controller is reset and prevent the initialization to trigger twice if already started.
  • Loading branch information
breiler authored Aug 2, 2024
1 parent a9fb590 commit e9b19de
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,18 @@ public Boolean openCommPort(ConnectionDriver connectionDriver, String port, int
}

private void initialize() {
if (initializer.isInitializing()) {
logger.info("Already initializing, skipping");
return;
}

if (comm.areActiveCommands()) {
comm.cancelSend();
messageService.dispatchMessage(MessageType.INFO, "*** Canceling current stream\n");
cancelCommands();
resetBuffers();
}
setControllerState(ControllerState.CONNECTING);

setControllerState(ControllerState.CONNECTING);
ThreadHelper.invokeLater(() -> {
positionPollTimer.stop();
initializer.initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ private void closeConnection() {
}
}

private void fetchControllerState() throws Exception {
private void fetchControllerState() throws InterruptedException {
controller.getMessageService().dispatchMessage(MessageType.INFO, "*** Fetching device settings\n");
sendAndWaitForCompletion(controller, new GetSettingsCommand());
controller.getMessageService().dispatchMessage(MessageType.INFO, "*** Fetching device state\n");
sendAndWaitForCompletion(controller, new GetParserStateCommand());
}

private void fetchControllerVersion() throws Exception {
// Send commands to get the state of the controller
private void fetchControllerVersion() throws InterruptedException {
controller.getMessageService().dispatchMessage(MessageType.INFO, "*** Fetching device version\n");
GetBuildInfoCommand getBuildInfoCommand = sendAndWaitForCompletion(controller, new GetBuildInfoCommand());
Optional<GrblVersion> optionalVersion = getBuildInfoCommand.getVersion();
if (optionalVersion.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1167,19 +1167,41 @@ public void controllerShouldGetAxisCapabilitiesOnStatusStringWithWorkPosition()
}

@Test
public void rawResponseHandlerOnVersionStringShouldResetStatus() throws Exception {
public void rawResponseHandlerOnVersionStringWhenSendingFileShouldCancelStream() throws Exception {
// Given
GrblController instance = initializeAndConnectController(VERSION_GRBL_1_1F);
instance.rawResponseHandler("<Run|MPos:0.000,0.000,0.000|FS:0,0|Pn:XYZ>");
assertEquals("We should be in sending mode", COMM_SENDING, instance.getCommunicatorState());
assertEquals(ControllerState.RUN, instance.getControllerStatus().getState());

// Simulate that there is an ongoing stream
mgc.areActiveCommands = true;

// When
instance.rawResponseHandler("Grbl " + VERSION_GRBL_1_1F);

// Then
assertEquals(1, mgc.numCancelSendCalls);
assertEquals(1, mgc.numResetBuffersCalls);
assertEquals(COMM_IDLE, instance.getCommunicatorState());
assertEquals(ControllerState.CONNECTING, instance.getControllerStatus().getState());
}

@Test
public void rawResponseHandlerOnVersionStringWhenNotSendingFileShouldNotCancelStream() throws Exception {
// Given
GrblController instance = initializeAndConnectController(VERSION_GRBL_1_1F);
instance.rawResponseHandler("<Run|MPos:0.000,0.000,0.000|FS:0,0|Pn:XYZ>");
assertEquals("We should be in sending mode", COMM_SENDING, instance.getCommunicatorState());
assertEquals(ControllerState.RUN, instance.getControllerStatus().getState());

// When
instance.rawResponseHandler("Grbl " + VERSION_GRBL_1_1F);

// Then
assertEquals(0, mgc.numCancelSendCalls);
assertEquals(0, mgc.numResetBuffersCalls);
assertEquals(COMM_IDLE, instance.getCommunicatorState());

}

/**
Expand Down Expand Up @@ -1383,7 +1405,8 @@ public void onConnectionClosedShouldDisconnectController() throws Exception {
*/
private GrblController initializeAndConnectController(String grblVersionString) throws Exception {
GrblControllerInitializer initializer = mock(GrblControllerInitializer.class);
when(initializer.isInitialized()).thenReturn(true);
when(initializer.isInitialized()).thenReturn(false);
when(initializer.isInitializing()).thenReturn(false);

GrblVersion version = new GrblVersion("[VER:" + grblVersionString + "]");
when(initializer.getVersion()).thenReturn(version);
Expand All @@ -1392,6 +1415,8 @@ private GrblController initializeAndConnectController(String grblVersionString)
instance.openCommPort(getSettings().getConnectionDriver(), "/dev/port", 1234);
Thread.sleep(50);

when(initializer.isInitialized()).thenReturn(true);
when(initializer.isInitializing()).thenReturn(false);
return instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class MockGrblCommunicator extends GrblCommunicator {
public int numPauseSendCalls;
public int numResumeSendCalls;
public int numCancelSendCalls;
public int numResetBuffersCalls;
private IGcodeStreamReader gcodeStreamReader;

public void resetInputsAndFunctionCalls() {
Expand All @@ -74,6 +75,7 @@ public void resetInputsAndFunctionCalls() {
this.numPauseSendCalls = 0;
this.numResumeSendCalls = 0;
this.numCancelSendCalls = 0;
this.numResetBuffersCalls = 0;
}

public MockGrblCommunicator() {
Expand Down Expand Up @@ -153,4 +155,9 @@ public void resumeSend() {
public void cancelSend() {
this.numCancelSendCalls++;
}

@Override
public void resetBuffers() {
this.numResetBuffersCalls++;
}
}

0 comments on commit e9b19de

Please sign in to comment.