2 * Copyright (c) 2010-2022 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.extensions.AbstractConsoleCommandExtension;
27 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingRegistry;
30 import org.openhab.core.thing.ThingUID;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.osgi.service.component.annotations.Activate;
33 import org.osgi.service.component.annotations.Component;
34 import org.osgi.service.component.annotations.Reference;
37 * The {@link NetatmoCommandExtension} is responsible for handling console commands
39 * @author Laurent Garnier - Initial contribution
43 @Component(service = ConsoleCommandExtension.class)
44 public class NetatmoCommandExtension extends AbstractConsoleCommandExtension {
46 private static final String SHOW_IDS = "showIds";
48 private final ThingRegistry thingRegistry;
49 private @Nullable Console console;
52 public NetatmoCommandExtension(final @Reference ThingRegistry thingRegistry) {
53 super(NetatmoBindingConstants.BINDING_ID, "Interact with the Netatmo binding.");
54 this.thingRegistry = thingRegistry;
58 public void execute(String[] args, Console console) {
59 if (args.length == 1 && SHOW_IDS.equals(args[0])) {
60 this.console = console;
61 for (Thing thing : thingRegistry.getAll()) {
62 ThingHandler thingHandler = thing.getHandler();
63 if (thingHandler instanceof ApiBridgeHandler) {
64 console.println("Account bridge: " + thing.getLabel());
65 ((ApiBridgeHandler) thingHandler).identifyAllModulesAndApplyAction(this::printThing);
73 private Optional<ThingUID> printThing(NAModule module, ThingUID bridgeUID) {
74 Console localConsole = this.console;
75 Optional<ThingUID> moduleUID = findThingUID(module.getType(), module.getId(), bridgeUID);
76 if (localConsole != null && moduleUID.isPresent()) {
78 for (int i = 2; i <= module.getType().getDepth(); i++) {
81 localConsole.println(String.format("%s- ID \"%s\" for \"%s\" (thing type %s)", indent, module.getId(),
82 module.getName() != null ? module.getName() : "...", module.getType().thingTypeUID));
87 private Optional<ThingUID> findThingUID(ModuleType moduleType, String thingId, ThingUID bridgeUID) {
88 return moduleType.apiName.isBlank() ? Optional.empty()
89 : Optional.ofNullable(
90 new ThingUID(moduleType.thingTypeUID, bridgeUID, thingId.replaceAll("[^a-zA-Z0-9_]", "")));
94 public List<String> getUsages() {
95 return Arrays.asList(buildCommandUsage(SHOW_IDS, "list all devices and modules ids"));