2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.netatmo.internal.console;
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.netatmo.internal.NetatmoBindingConstants;
22 import org.openhab.binding.netatmo.internal.api.data.ModuleType;
23 import org.openhab.binding.netatmo.internal.api.dto.NAModule;
24 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
25 import org.openhab.core.io.console.Console;
26 import org.openhab.core.io.console.ConsoleCommandCompleter;
27 import org.openhab.core.io.console.StringsCompleter;
28 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
29 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingRegistry;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
39 * The {@link NetatmoCommandExtension} is responsible for handling console commands
41 * @author Laurent Garnier - Initial contribution
45 @Component(service = ConsoleCommandExtension.class)
46 public class NetatmoCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
48 private static final String SHOW_IDS = "showIds";
49 private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(List.of(SHOW_IDS), false);
51 private final ThingRegistry thingRegistry;
52 private @Nullable Console console;
55 public NetatmoCommandExtension(final @Reference ThingRegistry thingRegistry) {
56 super(NetatmoBindingConstants.BINDING_ID, "Interact with the Netatmo binding.");
57 this.thingRegistry = thingRegistry;
61 public void execute(String[] args, Console console) {
62 if (args.length == 1 && SHOW_IDS.equals(args[0])) {
63 this.console = console;
64 for (Thing thing : thingRegistry.getAll()) {
65 ThingHandler thingHandler = thing.getHandler();
66 if (thingHandler instanceof ApiBridgeHandler bridgeHandler) {
67 console.println("Account bridge: " + thing.getLabel());
68 bridgeHandler.identifyAllModulesAndApplyAction(this::printThing);
76 private Optional<ThingUID> printThing(NAModule module, ThingUID bridgeUID) {
77 Console localConsole = this.console;
78 Optional<ThingUID> moduleUID = findThingUID(module.getType(), module.getId(), bridgeUID);
79 if (localConsole != null && moduleUID.isPresent()) {
81 for (int i = 2; i <= module.getType().getDepth(); i++) {
84 localConsole.println(String.format("%s- ID \"%s\" for \"%s\" (thing type %s)", indent, module.getId(),
85 module.getName() != null ? module.getName() : "...", module.getType().thingTypeUID));
90 private Optional<ThingUID> findThingUID(ModuleType moduleType, String thingId, ThingUID bridgeUID) {
91 return moduleType.apiName.isBlank() ? Optional.empty()
92 : Optional.ofNullable(
93 new ThingUID(moduleType.thingTypeUID, bridgeUID, thingId.replaceAll("[^a-zA-Z0-9_]", "")));
97 public List<String> getUsages() {
98 return Arrays.asList(buildCommandUsage(SHOW_IDS, "list all devices and modules ids"));
102 public @Nullable ConsoleCommandCompleter getCompleter() {
107 public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
108 if (cursorArgumentIndex <= 0) {
109 return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);