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

org.bidib.wizard.common.script.switching.QueryFeedbackAndExpectCommand Maven / Gradle / Ivy

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

import java.util.Scanner;

import org.apache.commons.lang3.StringUtils;
import org.bidib.jbidibc.messages.enums.LcOutputType;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.bidib.wizard.api.context.ApplicationContext;
import org.bidib.wizard.api.model.NodeInterface;
import org.bidib.wizard.api.service.console.ConsoleService;
import org.bidib.wizard.common.script.AbstractScriptCommand;
import org.bidib.wizard.common.script.DefaultScriptContext;
import org.bidib.wizard.common.script.ScriptUtils;
import org.bidib.wizard.model.status.FeedbackPortStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QueryFeedbackAndExpectCommand extends AbstractScriptCommand {
    private static final Logger LOGGER = LoggerFactory.getLogger(QueryFeedbackAndExpectCommand.class);

    public static final String KEY = "feedbackQueryState";

    public static final String HELP =
        CODE_BLOCK_START + KEY + prepareHelpHtml(
            " [--wait=] [--message=]  ")
            + DIV_END
            + prepareHelpHtml(
                "\nQuery the status of a feedback port.\n\nUse the --wait parameter to show a dialog if the status is not the expected status already."
                    + "\n\nExample:\n")
            + CODE_BLOCK_START + prepareHelpHtml("feedbackQueryState --wait=50 5 OCCUPIED") + DIV_END
            + prepareHelpHtml("This will wait for max. 50s if status of feedback port is not OCCUPIED\n\n")
            + CODE_BLOCK_START
            + prepareHelpHtml(
                "feedbackQueryState --wait=50 --message=\"Press the red button on the device!\" 5 OCCUPIED")
            + DIV_END + prepareHelpHtml(
                "This will wait for max. 50s if status of feedback port is not OCCUPIED. The message in the dialog is: Press the red button on the device!");

    private boolean showWaitDialog;

    private int waitSeconds = 60;

    private int portNum;

    private String dialogMessage;

    private FeedbackPortStatus expectedPortStatus;

    protected QueryFeedbackAndExpectCommand(final ConsoleService consoleService) {
        super(consoleService, KEY, HELP);
    }

    @Override
    public void parse(String commandLine) {

        line = commandLine.trim();

        if (!line.startsWith(getKey())) {
            LOGGER.info("Invalid command is scanned, key does not match.");
            throw new IllegalArgumentException("Invalid command is scanned, key does not match.");
        }

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

        int skipItems = 0;
        int startIndex = arguments.indexOf("--wait=");
        if (startIndex > -1) {
            LOGGER.info("Show wait dialog if status does not match.");
            showWaitDialog = true;

            this.waitSeconds = ScriptUtils.parseIntParam("--wait=\\d+", arguments);

            skipItems = startIndex + "--wait=".length() + Integer.toString(this.waitSeconds).length();
        }

        startIndex = arguments.indexOf("--message=");
        if (startIndex > -1) {
            this.dialogMessage = ScriptUtils.parseStringParam("(?<=--message=)?\\\"(?s)(.*)?\\\"", arguments);

            skipItems = startIndex + "--message=".length() + 2 + this.dialogMessage.length();
        }

        try (Scanner scanner = new Scanner(arguments.substring(skipItems))) {

            portNum = scanner.nextInt();

            expectedPortStatus = FeedbackPortStatus.fromString(scanner.next().toUpperCase());
        }

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

    public boolean isShowWaitDialog() {
        return showWaitDialog;
    }

    public int getWaitSeconds() {
        return waitSeconds;
    }

    public String getDialogMessage() {
        return dialogMessage;
    }

    public int getPortNum() {
        return portNum;
    }

    public FeedbackPortStatus getExpectedPortStatus() {
        return expectedPortStatus;
    }

    @Override
    protected void internalExecute(T scripting, ApplicationContext context) {
        LOGGER.info("Query the feedback status, portNum: {}, expectedPortStatus: {}", portNum, expectedPortStatus);

        final NodeInterface node = context.get(DefaultScriptContext.KEY_SELECTED_NODE, NodeInterface.class);
        if (showWaitDialog) {

            if (StringUtils.isBlank(dialogMessage)) {
                final String nodeLabel =
                    StringUtils.isNotBlank(node.getLabel()) ? node.getLabel()
                        : ByteUtils.getUniqueIdAsString(node.getUniqueId());
                this.dialogMessage = "Wait for change of occupancy state of port " + portNum + " on node: " + nodeLabel;
            }

            scripting
                .assertPortStatusActionWithDialog(node.getUniqueId(), waitSeconds, this.dialogMessage,
                    LcOutputType.FEEDBACKPORT, portNum, expectedPortStatus);
        }
        else {
            scripting
                .assertPortStatusAction(node.getUniqueId(), LcOutputType.FEEDBACKPORT, portNum, expectedPortStatus);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy