org.bidib.wizard.common.script.common.SelectFirstNodeCommand Maven / Gradle / Ivy
package org.bidib.wizard.common.script.common;
import java.util.List;
import java.util.Scanner;
import org.apache.commons.collections4.CollectionUtils;
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.ScriptExecutionException;
import org.bidib.wizard.common.script.ScriptUtils;
import org.bidib.wizard.common.script.switching.NodeTreeScripting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SelectFirstNodeCommand extends AbstractScriptCommand {
private static final Logger LOGGER = LoggerFactory.getLogger(SelectFirstNodeCommand.class);
public static final String KEY = "selectFirstNode";
public static final String HELP =
CODE_BLOCK_START + KEY + prepareHelpHtml(" --V:P") + DIV_END + prepareHelpHtml(
"\nSelect the first node that matches the povided VID and PID in the node list. Be careful if multiple nodes are connected.\nUse selectNode to specify the node by uniqueId.\n\nExample:\n")
+ CODE_BLOCK_START + prepareHelpHtml("selectFirstNode --V251:P202") + DIV_END;
private int[] vidPid;
public SelectFirstNodeCommand(final ConsoleService consoleService) {
super(consoleService, KEY, HELP);
}
@Override
public void parse(String commandLine) {
setLine(commandLine.trim());
try (Scanner scanner = new Scanner(commandLine)) {
if (!getKey().equals(scanner.next())) {
LOGGER.info("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 {
vidPid = ScriptUtils.scanVidPid(scanner, false);
if (vidPid != null) {
LOGGER.info("Current vidPid: {}:{}", vidPid[0], vidPid[1]);
}
else {
throw new IllegalArgumentException("Parse VID/PID failed.");
}
}
catch (IllegalArgumentException ex) {
LOGGER.info("Parse VID/PID failed.", ex);
throw new IllegalArgumentException(
"Parse VID/PID failed. Make sure to use correct format.\n" + getHelpHtml());
}
}
LOGGER.info("Current vidPid: {}", vidPid);
}
public int[] getVidPid() {
return vidPid;
}
@Override
protected void internalExecute(NodeTreeScripting scripting, ApplicationContext context) {
if (vidPid == null) {
addError(context, "No VID/PID provided.");
throw new ScriptExecutionException("No VID/PID provided.", this);
}
int vid = vidPid[0];
int pid = vidPid[1];
List nodes = scripting.getNodesByVidAndPid(vid, pid);
if (CollectionUtils.isEmpty(nodes)) {
// addError(context, "No matching node with provided VID/PID found.");
throw new ScriptExecutionException("No matching node with provided VID/PID found.", this);
}
final NodeInterface node = nodes.get(0);
LOGGER.info("Register the selected node: {}", node);
context.register(DefaultScriptContext.KEY_SELECTED_NODE, node);
scripting.echo("Selected node: " + ByteUtils.getUniqueIdAsString(node.getUniqueId()));
}
}