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

org.bidib.wizard.api.utils.NodeUtils Maven / Gradle / Ivy

The newest version!
package org.bidib.wizard.api.utils;

import java.util.Arrays;
import java.util.Collection;

import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.lang3.StringUtils;
import org.bidib.jbidibc.messages.StringData;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.bidib.jbidibc.messages.utils.ProductUtils;
import org.bidib.wizard.api.model.CommandStationNodeInterface;
import org.bidib.wizard.api.model.NodeInterface;
import org.bidib.wizard.api.model.RfBasisNode;
import org.bidib.wizard.api.model.RfBasisNodeInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NodeUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(NodeUtils.class);

    /**
     * Find a node by its node address in the provided list of nodes.
     * 
     * @param nodes
     *            the list of nodes
     * @param address
     *            the address of the node to find
     * @return the found node or null if no node was found with the provided node address
     */
    public static NodeInterface findNodeByAddress(Collection nodes, final byte[] address) {
        NodeInterface node = IterableUtils.find(nodes, new Predicate() {

            @Override
            public boolean evaluate(final NodeInterface node) {
                if (Arrays.equals(node.getNode().getAddr(), address)) {
                    LOGGER.debug("Found node: {}", node);
                    return true;
                }
                return false;
            }
        });
        return node;
    }

    /**
     * Find the first command station node in the provided list of nodes.
     * 
     * @param nodes
     *            the list of nodes
     * @return the found command station node or null if no command station node was found
     */
    public static CommandStationNodeInterface findFirstCommandStationNode(Iterable nodes) {
        NodeInterface node = IterableUtils.find(nodes, new Predicate() {

            @Override
            public boolean evaluate(final NodeInterface node) {
                if (node.getCommandStationNode() != null
                    && org.bidib.jbidibc.messages.utils.NodeUtils.hasCommandStationFunctions(node.getUniqueId())
                    && !(ProductUtils.isRFBasisNode(node.getUniqueId())
                        || ProductUtils.isSpeedometer(node.getUniqueId()))) {
                    LOGGER.info("Found command station node: {}", node);
                    return true;
                }
                return false;
            }
        });
        return node != null ? node.getCommandStationNode() : null;
    }

    /**
     * Find the related command station node in the provided list of nodes for the provided node.
     * 
     * @param nodes
     *            the list of nodes
     * @param subNode
     *            the node
     * @return the found command station node or null if no command station node was found
     */
    public static CommandStationNodeInterface findRelatedCommandStationNode(
        Iterable nodes, final NodeInterface subNode) {
        NodeInterface relatedNode = IterableUtils.find(nodes, new Predicate() {

            @Override
            public boolean evaluate(final NodeInterface node) {
                if (node.getCommandStationNode() != null
                    && org.bidib.jbidibc.messages.utils.NodeUtils.hasCommandStationFunctions(node.getUniqueId())
                    && org.bidib.jbidibc.messages.utils.NodeUtils.isNodeOrSubNode(node.getNode(), subNode.getNode())
                    && !(ProductUtils.isRFBasisNode(node.getUniqueId())
                        || ProductUtils.isSpeedometer(node.getUniqueId()))) {
                    LOGGER.info("Found command station node: {}", node);
                    return true;
                }
                return false;
            }
        });
        return relatedNode != null ? relatedNode.getCommandStationNode() : null;
    }

    /**
     * Find the first RF basis master node in the provided list of nodes.
     *
     * @param nodes
     *            the list of nodes
     * @return the found RF basis master node or null if no command station node was found
     */
    public static RfBasisNodeInterface findFirstRfBasisMasterNode(Collection nodes) {

        RfBasisNodeInterface rfBasisNode =
            nodes
                .stream().filter(n -> ProductUtils.isRFBasisNode(n.getUniqueId())).map(n -> new RfBasisNode(n))
                .filter(rf -> (rf.getNode().getBaseNumber() == 0 || rf.getNode().getBaseNumber() == 1)).findFirst()
                .orElse(null);

        return rfBasisNode;
    }

    /**
     * Get the node name.
     * 
     * @param node
     *            the node
     * @return the node name
     */
    public static String getNodeName(final NodeInterface node) {
        StringBuilder labelText = new StringBuilder();

        if (node != null) {
            if (node.getNode().hasStoredStrings()) {
                // the node has stored strings, prepare the label with the stored strings
                String userString = node.getNode().getStoredString(StringData.INDEX_USERNAME);
                if (StringUtils.isNotBlank(userString)) {
                    // user string is available

                    String label = node.toString();
                    if (label.startsWith(userString)) {

                        labelText.append(label);

                    }
                    else {
                        labelText.append(userString);
                    }
                }
                else {
                    // no user string available
                    labelText.append(ByteUtils.getUniqueIdAsString(node.getUniqueId()));
                }
            }
            else {
                // node has no stored strings
                labelText.append(ByteUtils.getUniqueIdAsString(node.getUniqueId()));
            }
        }
        return labelText.toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy