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

org.bidib.jbidibc.BidibNodeCommand Maven / Gradle / Ivy

There is a newer version: 2.0.25
Show newest version
/**
 * 
 */
package org.bidib.jbidibc;

import org.bidib.jbidibc.core.node.BidibNode;
import org.bidib.jbidibc.messages.BidibLibrary;
import org.bidib.jbidibc.messages.Feature;
import org.bidib.jbidibc.messages.Node;
import org.bidib.jbidibc.messages.exception.ProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import picocli.CommandLine.Command;
import picocli.CommandLine.ITypeConverter;
import picocli.CommandLine.Option;
import picocli.CommandLine.TypeConversionException;

@Command(separator = "=")
public abstract class BidibNodeCommand extends BidibCommand {
    private static final Logger LOGGER = LoggerFactory.getLogger(BidibNodeCommand.class);

    private static final Logger LOGGER_NODE = LoggerFactory.getLogger(Node.class);

    private final org.bidib.jbidibc.messages.logger.Logger nodeLogger;

    @Option(names = {
        "-nodeId" }, description = "Unique id of node, e.g. 0xc0000d68000100", required = true, converter = NodeIdConverter.class)
    private Long nodeId;

    protected BidibNodeCommand() {
        this.nodeLogger = new org.bidib.jbidibc.messages.logger.Logger() {

            @Override
            public void debug(String format, Object... arguments) {
                LOGGER_NODE.debug(format, arguments);
            }

            @Override
            public void info(String format, Object... arguments) {
                LOGGER_NODE.info(format, arguments);
            }

            @Override
            public void warn(String format, Object... arguments) {
                LOGGER_NODE.warn(format, arguments);
            }

            @Override
            public void error(String format, Object... arguments) {
                LOGGER_NODE.error(format, arguments);
            }
        };
    }

    protected Long getNodeId() {
        return nodeId;
    }

    protected String getNodeIdentifier() {
        return String.format("0x%08x", nodeId);
    }

    protected Node findNode() throws ProtocolException {
        Node result = null;

        BidibNode rootNode = getBidib().getRootNode();
        int count = rootNode.getNodeCount();

        for (int index = 1; index <= count; index++) {
            Node node = rootNode.getNextNode(this.nodeLogger);

            if (node != null && (node.getUniqueId() & 0xffffffffffffffL) == nodeId.longValue()) {
                int magic = getBidib().getNode(node).getMagic(null);
                node.setMagic(magic);

                LOGGER.info("Node returned magic: {}", magic);
                // after we fetch the magic we must try to get the FEATURE_RELEVANT_PID_BITS
                Feature relevantPidBits = getBidib().getNode(node).getFeature(BidibLibrary.FEATURE_RELEVANT_PID_BITS);
                if (relevantPidBits != null) {
                    node.setFeature(relevantPidBits);
                    node.setRelevantPidBits(relevantPidBits.getValue());
                }

                result = node;
                break;
            }
        }
        return result;
    }

    public static final class NodeIdConverter implements ITypeConverter {

        public NodeIdConverter() {
        }

        @Override
        public Long convert(String value) {
            try {
                LOGGER.debug("Parse NodeId: {}", value);
                return Long.parseLong(value.substring(value.indexOf("0x") + 2), 16);
            }
            catch (NumberFormatException ex) {
                throw new TypeConversionException(String.format("'%s' is not a %s", value, "a long"));
            }
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy