/**
 * Copyright (c) 2016-2022 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 com.zsmartsystems.zigbee.CommandResult;
import com.zsmartsystems.zigbee.ZigBeeEndpoint;
import com.zsmartsystems.zigbee.ZigBeeNetworkManager;
import com.zsmartsystems.zigbee.zcl.clusters.ZclIdentifyCluster;
import com.zsmartsystems.zigbee.zcl.clusters.identify.IdentifyCommand;

/**
 * Sends the identify command to a device
 *
 * @author Chris Jackson
 *
 */
public class ZigBeeConsoleIdentifyCommand extends ZigBeeConsoleAbstractCommand {
    @Override
    public String getCommand() {
        return "identify";
    }

    @Override
    public String getDescription() {
        return "Identifies a device";
    }

    @Override
    public String getSyntax() {
        return "ENDPOINT [TIME]";
    }

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

    @Override
    public void process(ZigBeeNetworkManager networkManager, String[] args, PrintStream out)
            throws IllegalArgumentException {
        if (args.length < 2) {
            throw new IllegalArgumentException("Invalid number of arguments");
        }

        int period = 10;
        if (args.length == 3) {
            period = parseInteger(args[2]);
        }

        ZigBeeEndpoint endpoint = getEndpoint(networkManager, args[1]);
        ZclIdentifyCluster cluster = (ZclIdentifyCluster) endpoint.getInputCluster(ZclIdentifyCluster.CLUSTER_ID);

        CommandResult result;
        IdentifyCommand command = new IdentifyCommand(period);
        try {
            result = cluster.sendCommand(command).get();
        } catch (Exception e) {
            out.println(
                    "[Endpoint: " + cluster.getZigBeeAddress() + "] Fail to send command [" + e.getMessage() + "]");
            return;
        }

    }
}

@@ -381,16 +381,67 @@
 tStream;
+%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0Aimport
@@ -579,24 +579,68 @@
 orkManager;%0A
+import com.zsmartsystems.zigbee.ZigBeeNode;%0A
 import com.z
@@ -802,23 +802,99 @@
 and to a
- device
+n endpoint, or all endpoints on a device supporting the %7B@link ZclIdentifyCluster%7D.
 %0A *%0A * @
@@ -1250,16 +1250,23 @@
 NDPOINT 
+%7C NODE 
 %5BTIME%5D%22;
@@ -1639,17 +1639,17 @@
 riod = 1
-0
+5
 ;%0A      
@@ -1741,72 +1741,435 @@
     
-ZigBeeEndpoint endpoint = getEndpoint(networkManager, args%5B1%5D);%0A
+List%3CZclIdentifyCluster%3E clusters = new ArrayList%3C%3E();%0A        if (args%5B1%5D.contains(%22/%22)) %7B%0A            ZigBeeEndpoint endpoint = getEndpoint(networkManager, args%5B1%5D);%0A            clusters.add((ZclIdentifyCluster) endpoint.getInputCluster(ZclIdentifyCluster.CLUSTER_ID));%0A        %7D else %7B%0A            ZigBeeNode node = getNode(networkManager, args%5B1%5D);%0A            for (ZigBeeEndpoint endpoint : node.getEndpoints()) %7B%0A        
     
@@ -2222,32 +2222,57 @@
 luster) endpoint
+%0A                        
 .getInputCluster
@@ -2304,17 +2304,285 @@
 ER_ID);%0A
-%0A
+                if (cluster != null) %7B%0A                    clusters.add(cluster);%0A                %7D%0A            %7D%0A        %7D%0A%0A        for (ZclIdentifyCluster cluster : clusters) %7B%0A            out.println(%22Sending identify command to %22 + cluster.getZigBeeAddress());%0A    
         
@@ -2603,16 +2603,20 @@
 result;%0A
+    
         
@@ -2682,14 +2682,22 @@
     
+    
 try %7B%0A
+    
     
@@ -2745,24 +2745,222 @@
 and).get();%0A
+                if (result.isError()) %7B%0A                    out.println(%0A                            %22%5BEndpoint: %22 + cluster.getZigBeeAddress() + %22%5D Failed to send command%5D%22);%0A                %7D%0A    
         %7D ca
@@ -2983,32 +2983,36 @@
 ) %7B%0A            
+    
 out.println(%0A   
@@ -3000,32 +3000,36 @@
    out.println(%0A
+    
                 
@@ -3084,16 +3084,18 @@
  %22%5D Fail
+ed
  to send
@@ -3122,16 +3122,48 @@
 essage()
+%0A                               
  + %22%5D%22);
@@ -3179,15 +3179,9 @@
     
-return;
+%7D
 %0A   
@@ -3183,21 +3183,20 @@
 %7D%0A        %7D%0A
-%0A
     %7D%0A%7D%0A

Improve identify console command to support command all endpoints on a node in single command (#1339)