<commit_before>/**
 * Copyright (c) 2016-2019 by the respective copyright holders.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package com.zsmartsystems.zigbee.console;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.zsmartsystems.zigbee.ZigBeeDeviceType;
import com.zsmartsystems.zigbee.ZigBeeEndpoint;
import com.zsmartsystems.zigbee.ZigBeeNetworkManager;
import com.zsmartsystems.zigbee.ZigBeeNode;
import com.zsmartsystems.zigbee.ZigBeeProfileType;

/**
 * Lists the devices in the network
 *
 * @author <NAME>
 *
 */
public class ZigBeeConsoleNodeListCommand extends ZigBeeConsoleAbstractCommand {
    @Override
    public String getCommand() {
        return "nodelist";
    }

    @Override
    public String getDescription() {
        return "Lists the known nodes in the network.";
    }

    @Override
    public String getSyntax() {
        return "";
    }

    @Override
    public String getHelp() {
        return "";
    }

    @Override
    public void process(ZigBeeNetworkManager networkManager, String[] args, PrintStream out)
            throws IllegalArgumentException {
        final Set<ZigBeeNode> nodes = networkManager.getNodes();
        final List<Integer> nodeIds = new ArrayList<Integer>();

        for (ZigBeeNode node : nodes) {
            nodeIds.add(node.getNetworkAddress());
        }

        Collections.sort(nodeIds);
        out.println("Network Addr  IEEE Address      Logical Type  EP   Profile                    Device Type");
        for (Integer nodeId : nodeIds) {
            ZigBeeNode node = networkManager.getNode(nodeId);
            out.print(String.format("%-6d  %04X  %s  %-12s  ", node.getNetworkAddress(), node.getNetworkAddress(),
                    node.getIeeeAddress(), node.getLogicalType()));
            List<Integer> endpointIds = new ArrayList<Integer>();
            for (final ZigBeeEndpoint endpoint : node.getEndpoints()) {
                endpointIds.add(endpoint.getEndpointId());
            }
            Collections.sort(endpointIds);
            boolean first = true;
            for (Integer endpointId : endpointIds) {
                if (!first) {
                    out.print("                                             ");
                }
                first = false;
                ZigBeeEndpoint endpoint = node.getEndpoint(endpointId);
                out.println(String.format("%-3d  %-25s  %s", endpoint.getEndpointId(),
                        ZigBeeProfileType.getByValue(endpoint.getProfileId()),
                        ZigBeeDeviceType.getByValue(endpoint.getDeviceId())));
            }
            if (first) {
                out.println();
            }
        }
    }
}
<commit_msg>Add manufacturer and model information to the nodelist command (#541)<commit_after>/**
 * Copyright (c) 2016-2019 by the respective copyright holders.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package com.zsmartsystems.zigbee.console;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.zsmartsystems.zigbee.ZigBeeDeviceType;
import com.zsmartsystems.zigbee.ZigBeeEndpoint;
import com.zsmartsystems.zigbee.ZigBeeNetworkManager;
import com.zsmartsystems.zigbee.ZigBeeNode;
import com.zsmartsystems.zigbee.ZigBeeProfileType;
import com.zsmartsystems.zigbee.zcl.ZclCluster;
import com.zsmartsystems.zigbee.zcl.clusters.ZclBasicCluster;

/**
 * Lists the devices in the network
 *
 * @author <NAME>
 * @author <NAME> - add manufacturer and model info
 */
public class ZigBeeConsoleNodeListCommand extends ZigBeeConsoleAbstractCommand {

    @Override
    public String getCommand() {
        return "nodelist";
    }

    @Override
    public String getDescription() {
        return "Lists the known nodes in the network.";
    }

    @Override
    public String getSyntax() {
        return "";
    }

    @Override
    public String getHelp() {
        return "";
    }

    @Override
    public void process(ZigBeeNetworkManager networkManager, String[] args, PrintStream out) {
        final Set<ZigBeeNode> nodes = networkManager.getNodes();
        final List<Integer> nodeIds = new ArrayList<>();

        for (ZigBeeNode node : nodes) {
            nodeIds.add(node.getNetworkAddress());
        }

        Collections.sort(nodeIds);
        String tableHeader = String.format("%7s  %4s  %17s  %13s  %3s  %25s  %15s  %15s  %15s", "Network", "Addr",
                "IEEE Address", "Logical Type", "EP", "Profile", "Device Type", "Manufacturer", "Model");
        out.println(tableHeader);

        for (Integer nodeId : nodeIds) {
            printNode(networkManager.getNode(nodeId), out);
        }
    }

    private void printNode(ZigBeeNode node, PrintStream out) {
        String nodeInfo = String.format("%7d  %04X  %17s  %13s ", node.getNetworkAddress(), node.getNetworkAddress(),
                node.getIeeeAddress(), node.getLogicalType());
        String nodeInfoPadding = String.format("%7s  %4s  %17s  %13s ", "", "", "", "");

        List<ZigBeeEndpoint> endpoints = new ArrayList<>(node.getEndpoints());
        Collections.sort(endpoints, (ep1, ep2) -> ep1.getEndpointId() - ep2.getEndpointId());

        boolean first = true;
        for (ZigBeeEndpoint endpoint : endpoints) {
            boolean showManufacturerAndModel = endpoint.getParentNode().getNetworkAddress() != 0;
            String endpointInfo = String.format("%3d  %25s  %15s  %15s  %15s", endpoint.getEndpointId(),
                    ZigBeeProfileType.getByValue(endpoint.getProfileId()),
                    ZigBeeDeviceType.getByValue(endpoint.getDeviceId()),
                    showManufacturerAndModel ? getManufacturer(endpoint) : "",
                    showManufacturerAndModel ? getModel(endpoint) : "");

            String tableLine = String.format("%s %s", first ? nodeInfo : nodeInfoPadding, endpointInfo);
            out.println(tableLine);

            first = false;
        }
    }

    private String getManufacturer(ZigBeeEndpoint endpoint) {
        ZclBasicCluster cluster = getBasicCluster(endpoint);
        return cluster != null ? cluster.getManufacturerName(Long.MAX_VALUE) : "";
    }

    private String getModel(ZigBeeEndpoint endpoint) {
        ZclBasicCluster cluster = getBasicCluster(endpoint);
        return cluster != null ? cluster.getModelIdentifier(Long.MAX_VALUE) : "";
    }

    private ZclBasicCluster getBasicCluster(ZigBeeEndpoint endpoint) {
        ZclCluster cluster = endpoint.getInputCluster(0);
        if (cluster instanceof ZclBasicCluster) {
            return (ZclBasicCluster) cluster;
        } else {
            return null;
        }
    }

}

{'fraction_non_alphanumeric': 0.10375521557719054, 'fraction_numerical': 0.010848400556328233, 'mean_word_length': 2.653963414634146, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3879018', 'n_tokens_mistral': 2082, 'n_tokens_neox': 1977, 'n_words': 470}