org.bidib.wizard.common.script.switching.QueryInputAndExpectCommand Maven / Gradle / Ivy
package org.bidib.wizard.common.script.switching;
import java.util.Scanner;
import org.bidib.jbidibc.messages.enums.LcOutputType;
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.model.status.InputPortStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class QueryInputAndExpectCommand extends AbstractScriptCommand {
private static final Logger LOGGER = LoggerFactory.getLogger(QueryInputAndExpectCommand.class);
public static final String KEY = "inputQueryState";
public static final String HELP =
CODE_BLOCK_START + KEY + prepareHelpHtml(" ") + DIV_END
+ prepareHelpHtml("\nQuery the input state of the port and compare the state to the expected state.");
private int portNum;
private InputPortStatus expectedPortStatus;
protected QueryInputAndExpectCommand(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();
portNum = scanner.nextInt();
expectedPortStatus = InputPortStatus.fromString(scanner.next().toUpperCase());
}
LOGGER.info("Parsed command, portNum: {}, expectedPortStatus: {}", portNum, expectedPortStatus);
}
public int getPortNum() {
return portNum;
}
@Override
protected void internalExecute(T scripting, ApplicationContext context) {
LOGGER.info("Query the input status, portNum: {}, expectedPortStatus: {}", portNum, expectedPortStatus);
final NodeInterface node = context.get(DefaultScriptContext.KEY_SELECTED_NODE, NodeInterface.class);
scripting
.assertPortStatusAction(node != null ? node.getSwitchingNode() : null, LcOutputType.INPUTPORT, portNum,
expectedPortStatus);
}
}