Search in sources :

Example 1 with GrblFeedbackMessage

use of com.willwinder.universalgcodesender.types.GrblFeedbackMessage in project Universal-G-Code-Sender by winder.

the class GrblController method rawResponseHandler.

@Override
protected void rawResponseHandler(String response) {
    String processed = response;
    try {
        boolean verbose = false;
        if (GrblUtils.isOkResponse(response)) {
            this.commandComplete(processed);
        } else // Error case.
        if (GrblUtils.isOkErrorAlarmResponse(response)) {
            if (GrblUtils.isAlarmResponse(response)) {
                // this is not updating the state to Alarm in the GUI, and the alarm is no longer being processed
                // TODO: Find a builder library.
                this.controllerStatus = new ControllerStatus(lookupCode(response, true), this.controllerStatus.getMachineCoord(), this.controllerStatus.getWorkCoord(), this.controllerStatus.getFeedSpeed(), this.controllerStatus.getSpindleSpeed(), this.controllerStatus.getOverrides(), this.controllerStatus.getWorkCoordinateOffset(), this.controllerStatus.getEnabledPins(), this.controllerStatus.getAccessoryStates());
                dispatchStatusString(this.controllerStatus);
                dispatchStateChange(COMM_IDLE);
            }
            // If there is an active command, mark it as completed with error
            Optional<GcodeCommand> activeCommand = this.getActiveCommand();
            if (activeCommand.isPresent()) {
                processed = String.format(Localization.getString("controller.exception.sendError"), activeCommand.get().getCommandString(), lookupCode(response, false)).replaceAll("\\.\\.", "\\.");
                this.errorMessageForConsole(processed + "\n");
                this.commandComplete(processed);
            } else {
                processed = String.format(Localization.getString("controller.exception.unexpectedError"), lookupCode(response, false)).replaceAll("\\.\\.", "\\.");
                this.errorMessageForConsole(processed + "\n");
            }
            checkStreamFinished();
            processed = "";
        } else if (GrblUtils.isGrblVersionString(response)) {
            this.isReady = true;
            resetBuffers();
            // single step mode
            if (getControlState() != COMM_CHECK) {
                this.controllerStatus = null;
            }
            this.stopPollingPosition();
            positionPollTimer = createPositionPollTimer();
            this.beginPollingPosition();
            // In case a reset occurred while streaming.
            if (this.isStreaming()) {
                checkStreamFinished();
            }
            this.grblVersion = GrblUtils.getVersionDouble(response);
            this.grblVersionLetter = GrblUtils.getVersionLetter(response);
            this.capabilities = GrblUtils.getGrblStatusCapabilities(this.grblVersion, this.grblVersionLetter);
            try {
                this.sendCommandImmediately(createCommand(GrblUtils.GRBL_VIEW_SETTINGS_COMMAND));
                this.sendCommandImmediately(createCommand(GrblUtils.GRBL_VIEW_PARSER_STATE_COMMAND));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            Logger.getLogger(GrblController.class.getName()).log(Level.CONFIG, "{0} = {1}{2}", new Object[] { Localization.getString("controller.log.version"), this.grblVersion, this.grblVersionLetter });
            Logger.getLogger(GrblController.class.getName()).log(Level.CONFIG, "{0} = {1}", new Object[] { Localization.getString("controller.log.realtime"), this.capabilities.hasCapability(GrblCapabilitiesConstants.REAL_TIME) });
        } else if (GrblUtils.isGrblProbeMessage(response)) {
            Position p = GrblUtils.parseProbePosition(response, getFirmwareSettings().getReportingUnits());
            if (p != null) {
                dispatchProbeCoordinates(p);
            }
        } else if (GrblUtils.isGrblStatusString(response)) {
            // Only 1 poll is sent at a time so don't decrement, reset to zero.
            this.outstandingPolls = 0;
            // Status string goes to verbose console
            verbose = true;
            this.handleStatusString(response);
        } else if (GrblUtils.isGrblFeedbackMessage(response, capabilities)) {
            GrblFeedbackMessage grblFeedbackMessage = new GrblFeedbackMessage(response);
            // Convert feedback message to raw commands to update modal state.
            this.updateParserModalState(new GcodeCommand(GrblUtils.parseFeedbackMessage(response, capabilities)));
            this.verboseMessageForConsole(grblFeedbackMessage.toString() + "\n");
            setDistanceModeCode(grblFeedbackMessage.getDistanceMode());
            setUnitsCode(grblFeedbackMessage.getUnits());
            dispatchStateChange(COMM_IDLE);
        } else if (GrblUtils.isGrblSettingMessage(response)) {
            GrblSettingMessage message = new GrblSettingMessage(response);
            processed = message.toString();
        }
        if (StringUtils.isNotBlank(processed)) {
            if (verbose) {
                this.verboseMessageForConsole(processed + "\n");
            } else {
                this.messageForConsole(processed + "\n");
            }
        }
    } catch (Exception e) {
        String message = "";
        if (e.getMessage() != null) {
            message = ": " + e.getMessage();
        }
        message = Localization.getString("controller.error.response") + " <" + processed + ">" + message;
        logger.log(Level.SEVERE, message, e);
        this.errorMessageForConsole(message + "\n");
    }
}
Also used : GrblSettingMessage(com.willwinder.universalgcodesender.types.GrblSettingMessage) ControllerStatus(com.willwinder.universalgcodesender.listeners.ControllerStatus) Position(com.willwinder.universalgcodesender.model.Position) GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) GrblFeedbackMessage(com.willwinder.universalgcodesender.types.GrblFeedbackMessage)

Aggregations

ControllerStatus (com.willwinder.universalgcodesender.listeners.ControllerStatus)1 Position (com.willwinder.universalgcodesender.model.Position)1 GcodeCommand (com.willwinder.universalgcodesender.types.GcodeCommand)1 GrblFeedbackMessage (com.willwinder.universalgcodesender.types.GrblFeedbackMessage)1 GrblSettingMessage (com.willwinder.universalgcodesender.types.GrblSettingMessage)1