All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bidib.wizard.common.script.debug.ConnectDebugCommand Maven / Gradle / Ivy

package org.bidib.wizard.common.script.debug;

import java.util.Scanner;

import org.bidib.wizard.api.context.ApplicationContext;
import org.bidib.wizard.api.service.console.ConsoleService;
import org.bidib.wizard.common.script.AbstractScriptCommand;
import org.bidib.wizard.common.script.ScriptUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConnectDebugCommand extends AbstractScriptCommand {

    private static final Logger LOGGER = LoggerFactory.getLogger(ConnectDebugCommand.class);

    public static final String KEY = "connectDebugInterface";

    private String connectionId;

    private String portIdentifier;

    private Integer baudRate;

    public static final String HELP =
        CODE_BLOCK_START + KEY
            + prepareHelpHtml(" --id= [--port=] [--baudRate=]") + DIV_END
            + prepareHelpHtml(
                "\nConnect to debug interface. The connectionId is the connection identifier. The standard value is 'DebugMain'.\n\nExample:\n")
            + CODE_BLOCK_START + prepareHelpHtml("connectDebugInterface --id=DebugMain\n")
            + prepareHelpHtml("connectDebugInterface --id=DebugMain --port=COM10 --baudRate=115200") + DIV_END;

    public ConnectDebugCommand(ConsoleService consoleService) {
        super(consoleService, KEY, HELP);
    }

    @Override
    public void parse(String commandLine) {

        try (Scanner scanner = new Scanner(commandLine)) {
            if (!getKey().equals(scanner.next())) {
                LOGGER.warn("Invalid command is scanned, key does not match.");
                throw new IllegalArgumentException("Invalid command is scanned, key does not match.");
            }
            line = commandLine.trim();

            String arguments = line.substring(KEY.length() + 1);

            try {
                this.connectionId = ScriptUtils.parseStringParam("(?<=--id=)(?s)(\\w+)", arguments);
            }
            catch (Exception ex) {
                LOGGER.warn("No connectionId found.", ex);

                throw new RuntimeException("connect: No connectionId found.");
            }

            this.portIdentifier = ScriptUtils.parseOptionalStringParam("(?<=--port=)(?s)(\\w+)", arguments);
            this.baudRate = ScriptUtils.parseOptionalIntegerParam("--baudRate=\\d+", arguments);
        }

        LOGGER.info("Parsed command, connectionId: {}", connectionId);
    }

    public String getConnectionId() {
        return connectionId;
    }

    public String getPortIdentifier() {
        return portIdentifier;
    }

    public Integer getBaudRate() {
        return baudRate;
    }

    @Override
    protected void internalExecute(DebugInterfaceScripting scripting, ApplicationContext context) {
        LOGGER.info("Connect to debug interface with connectionId: {}", connectionId);

        scripting.connectDebug(connectionId, portIdentifier, baudRate);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy