]> git.basschouten.com Git - openhab-addons.git/blob
b8b28bef761a2e32ab9d24b90b2998f034fa5152
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.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;
35
36 /**
37  * The {@link NetatmoCommandExtension} is responsible for handling console commands
38  *
39  * @author Laurent Garnier - Initial contribution
40  */
41
42 @NonNullByDefault
43 @Component(service = ConsoleCommandExtension.class)
44 public class NetatmoCommandExtension extends AbstractConsoleCommandExtension {
45
46     private static final String SHOW_IDS = "showIds";
47
48     private final ThingRegistry thingRegistry;
49     private @Nullable Console console;
50
51     @Activate
52     public NetatmoCommandExtension(final @Reference ThingRegistry thingRegistry) {
53         super(NetatmoBindingConstants.BINDING_ID, "Interact with the Netatmo binding.");
54         this.thingRegistry = thingRegistry;
55     }
56
57     @Override
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);
66                 }
67             }
68         } else {
69             printUsage(console);
70         }
71     }
72
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()) {
77             String indent = "";
78             for (int i = 2; i <= module.getType().getDepth(); i++) {
79                 indent += "    ";
80             }
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));
83         }
84         return moduleUID;
85     }
86
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_]", "")));
91     }
92
93     @Override
94     public List<String> getUsages() {
95         return Arrays.asList(buildCommandUsage(SHOW_IDS, "list all devices and modules ids"));
96     }
97 }