org.bidib.wizard.common.script.node.QueryIdentifyStateAndExpectCommand Maven / Gradle / Ivy
package org.bidib.wizard.common.script.node;
import java.util.Scanner;
import org.bidib.jbidibc.messages.enums.IdentifyState;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class QueryIdentifyStateAndExpectCommand extends AbstractScriptCommand {
private static final Logger LOGGER = LoggerFactory.getLogger(QueryIdentifyStateAndExpectCommand.class);
public static final String KEY = "queryIdentifyState";
public static final String HELP =
CODE_BLOCK_START + KEY + prepareHelpHtml(" ") + DIV_END + prepareHelpHtml(
"\nQuery the identify state and compare to expected status.\nA dialog is displayed for a maximum of 30s or when the identify state is changed.");
private IdentifyState expectedIdentifyState;
public QueryIdentifyStateAndExpectCommand(final ConsoleService consoleService) {
super(consoleService, KEY, HELP);
}
@Override
public void parse(String commandLine) {
try (Scanner scanner = new Scanner(commandLine)) {
if (!getKey().equals(scanner.next())) {
LOGGER.info("Invalid command is scanned, key does not match.");
}
line = commandLine.trim();
expectedIdentifyState = IdentifyState.fromString(scanner.next().toUpperCase());
}
LOGGER.info("Parsed command, expectedIdentifyState: {}", expectedIdentifyState);
}
@Override
protected void internalExecute(T scripting, ApplicationContext context) {
LOGGER.info("Query the identify status, expectedIdentifyState: {}", expectedIdentifyState);
final NodeInterface node = context.get(DefaultScriptContext.KEY_SELECTED_NODE, NodeInterface.class);
final IdentifyState currentIdentifyState = scripting.queryIdentifyState(node.getUniqueId());
if (expectedIdentifyState != currentIdentifyState) {
LOGGER
.warn("The expected identify status ({}) does not match the current status: {}", expectedIdentifyState,
currentIdentifyState);
throw new IllegalStateException("Identify status does not match the expected status: "
+ expectedIdentifyState + ", current status: " + currentIdentifyState);
}
LOGGER.info("The expected identify status matches the current status: {}", currentIdentifyState);
}
}