]> git.basschouten.com Git - openhab-addons.git/blob
0a1ae833f982fefa334e58c0296d1e2c504c20ca
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.netatmo.internal.console;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.Optional;
18
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;
37
38 /**
39  * The {@link NetatmoCommandExtension} is responsible for handling console commands
40  *
41  * @author Laurent Garnier - Initial contribution
42  */
43
44 @NonNullByDefault
45 @Component(service = ConsoleCommandExtension.class)
46 public class NetatmoCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
47
48     private static final String SHOW_IDS = "showIds";
49     private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(List.of(SHOW_IDS), false);
50
51     private final ThingRegistry thingRegistry;
52     private @Nullable Console console;
53
54     @Activate
55     public NetatmoCommandExtension(final @Reference ThingRegistry thingRegistry) {
56         super(NetatmoBindingConstants.BINDING_ID, "Interact with the Netatmo binding.");
57         this.thingRegistry = thingRegistry;
58     }
59
60     @Override
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);
69                 }
70             }
71         } else {
72             printUsage(console);
73         }
74     }
75
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()) {
80             String indent = "";
81             for (int i = 2; i <= module.getType().getDepth(); i++) {
82                 indent += "    ";
83             }
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));
86         }
87         return moduleUID;
88     }
89
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_]", "")));
94     }
95
96     @Override
97     public List<String> getUsages() {
98         return Arrays.asList(buildCommandUsage(SHOW_IDS, "list all devices and modules ids"));
99     }
100
101     @Override
102     public @Nullable ConsoleCommandCompleter getCompleter() {
103         return this;
104     }
105
106     @Override
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);
110         }
111         return false;
112     }
113 }