/**
 * Copyright (c) 2016-2021 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.ember;

import java.io.PrintStream;

import com.zsmartsystems.zigbee.ZigBeeNetworkManager;
import com.zsmartsystems.zigbee.dongle.ember.ZigBeeDongleEzsp;
import com.zsmartsystems.zigbee.dongle.ember.ezsp.structure.EzspValueId;

/**
 * Reads or writes an NCP {@link EzspValueId}
 *
 * @author Chris Jackson
 *
 */
public class EmberConsoleNcpHandlerCommand extends EmberConsoleAbstractCommand {
    private final static String OPTION_STATEPOLL = "statepoll";

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

    @Override
    public String getDescription() {
        return "Configure the NCP handler";
    }

    @Override
    public String getSyntax() {
        return "[statepoll PERIOD]";
    }

    @Override
    public String getHelp() {
        return "statepoll sets the period in milliseconds the handler will poll the NCP to check it is alive";
    }

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

        if (!(networkManager.getZigBeeTransport() instanceof ZigBeeDongleEzsp)) {
            throw new IllegalArgumentException("Dongle is not an Ember NCP.");
        }
        ZigBeeDongleEzsp dongle = (ZigBeeDongleEzsp) networkManager.getZigBeeTransport();

        switch (args[1].toLowerCase()) {
            case OPTION_STATEPOLL:
                Integer pollRate = Integer.parseInt(args[2]);
                dongle.setPollRate(pollRate);
                break;
            default:
                throw new IllegalArgumentException("Unknown option " + args[1]);
        }
    }
}

@@ -387,16 +387,44 @@
 tStream;
+%0Aimport java.util.Map.Entry;
 %0A%0Aimport
@@ -999,16 +999,37 @@
 nfigure 
+or read the state of 
 the NCP 
@@ -1454,151 +1454,8 @@
 n %7B%0A
-        if (args.length %3C 1 %7C%7C args.length %3E 3) %7B%0A            throw new IllegalArgumentException(%22Incorrect number of arguments.%22);%0A        %7D%0A%0A
     
@@ -1712,16 +1712,412 @@
 ort();%0A%0A
+        if (args.length == 1) %7B%0A            for (Entry%3CString, Long%3E counter : dongle.getCounters().entrySet()) %7B%0A                out.println(String.format(%22%2520s %25d%22, counter.getKey(), counter.getValue()));%0A            %7D%0A%0A            return;%0A        %7D%0A%0A        if (args.length %3C 1 %7C%7C args.length %3E 3) %7B%0A            throw new IllegalArgumentException(%22Incorrect number of arguments.%22);%0A        %7D%0A%0A
         

Update ncphandler command to support reading statistics counters (#1249)