]> git.basschouten.com Git - openhab-addons.git/blob
b9ed380f8835d34ef2823307b0246d2bcbf5c24f
[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.somfytahoma.internal.console;
14
15 import java.util.List;
16 import java.util.stream.Collectors;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.somfytahoma.internal.SomfyTahomaBindingConstants;
21 import org.openhab.binding.somfytahoma.internal.handler.SomfyTahomaBridgeHandler;
22 import org.openhab.binding.somfytahoma.internal.model.SomfyTahomaActionGroup;
23 import org.openhab.core.io.console.Console;
24 import org.openhab.core.io.console.ConsoleCommandCompleter;
25 import org.openhab.core.io.console.StringsCompleter;
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 SomfyTahomaCommandExtension} 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 SomfyTahomaCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
45
46     private static final String SCENARIOS = "scenarios";
47     private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(List.of(SCENARIOS), false);
48
49     private final ThingRegistry thingRegistry;
50
51     @Activate
52     public SomfyTahomaCommandExtension(final @Reference ThingRegistry thingRegistry) {
53         super("somfytahoma", "Interact with the Somfy Tahoma binding.");
54         this.thingRegistry = thingRegistry;
55     }
56
57     @Override
58     public void execute(String[] args, Console console) {
59         if (args.length == 2) {
60             Thing thing = getThing(args[0]);
61             ThingHandler thingHandler = null;
62             SomfyTahomaBridgeHandler bridgeHandler = null;
63             if (thing != null) {
64                 thingHandler = thing.getHandler();
65                 if (thingHandler instanceof SomfyTahomaBridgeHandler) {
66                     bridgeHandler = (SomfyTahomaBridgeHandler) thingHandler;
67                 }
68             }
69             if (thing == null) {
70                 console.println("Bad thing id '" + args[0] + "'");
71                 printUsage(console);
72             } else if (thingHandler == null) {
73                 console.println("No handler initialized for the thingUID '" + args[0] + "'");
74                 printUsage(console);
75             } else if (bridgeHandler == null) {
76                 console.println("'" + args[0] + "' is not a Somfy Tahoma bridgeUID");
77                 printUsage(console);
78             } else if (args[1].equals(SCENARIOS)) {
79                 for (SomfyTahomaActionGroup actionGroup : bridgeHandler.listActionGroups()) {
80                     console.println("Id is \"" + actionGroup.getOid() + "\" for the scenario \""
81                             + actionGroup.getLabel() + "\"");
82                 }
83             } else {
84                 printUsage(console);
85             }
86         } else {
87             printUsage(console);
88         }
89     }
90
91     @Override
92     public List<String> getUsages() {
93         return List.of(buildCommandUsage("<bridgeUID> " + SCENARIOS, "list all the scenarios with their id"));
94     }
95
96     @Override
97     public @Nullable ConsoleCommandCompleter getCompleter() {
98         return this;
99     }
100
101     @Override
102     public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
103         if (cursorArgumentIndex <= 0) {
104             return new StringsCompleter(thingRegistry.getAll().stream()
105                     .filter(t -> SomfyTahomaBindingConstants.THING_TYPE_BRIDGE.equals(t.getThingTypeUID()))
106                     .map(t -> t.getUID().getAsString()).collect(Collectors.toList()), true).complete(args,
107                             cursorArgumentIndex, cursorPosition, candidates);
108         } else if (cursorArgumentIndex == 1) {
109             Thing thing = getThing(args[0]);
110             if (thing != null && SomfyTahomaBindingConstants.THING_TYPE_BRIDGE.equals(thing.getThingTypeUID())) {
111                 return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
112             }
113         }
114         return false;
115     }
116
117     private @Nullable Thing getThing(String uid) {
118         Thing thing = null;
119         try {
120             ThingUID thingUID = new ThingUID(uid);
121             thing = thingRegistry.get(thingUID);
122         } catch (IllegalArgumentException e) {
123             thing = null;
124         }
125         return thing;
126     }
127 }