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.lgwebos.internal.console;
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.stream.Collectors;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.lgwebos.internal.LGWebOSBindingConstants;
22 import org.openhab.binding.lgwebos.internal.handler.LGWebOSHandler;
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;
37 * The {@link LGWebOSCommandExtension} is responsible for handling console commands
39 * @author Laurent Garnier - Initial contribution
43 @Component(service = ConsoleCommandExtension.class)
44 public class LGWebOSCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
46 private static final String APPLICATIONS = "applications";
47 private static final String CHANNELS = "channels";
48 private static final String ACCESS_KEY = "accesskey";
49 private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(
50 List.of(APPLICATIONS, CHANNELS, ACCESS_KEY), false);
52 private final ThingRegistry thingRegistry;
55 public LGWebOSCommandExtension(final @Reference ThingRegistry thingRegistry) {
56 super("lgwebos", "Interact with the LG webOS binding.");
57 this.thingRegistry = thingRegistry;
61 public void execute(String[] args, Console console) {
62 if (args.length == 2) {
63 Thing thing = getThing(args[0]);
64 ThingHandler thingHandler = null;
65 LGWebOSHandler handler = null;
67 thingHandler = thing.getHandler();
68 if (thingHandler instanceof LGWebOSHandler) {
69 handler = (LGWebOSHandler) thingHandler;
73 console.println("Bad thing id '" + args[0] + "'");
75 } else if (thingHandler == null) {
76 console.println("No handler initialized for the thing id '" + args[0] + "'");
78 } else if (handler == null) {
79 console.println("'" + args[0] + "' is not a LG webOS thing id");
84 handler.reportApplications().forEach(console::println);
87 handler.reportChannels().forEach(console::println);
90 console.println("Your access key is " + handler.getKey());
103 public List<String> getUsages() {
104 return Arrays.asList(new String[] { buildCommandUsage("<thingUID> " + APPLICATIONS, "list applications"),
105 buildCommandUsage("<thingUID> " + CHANNELS, "list channels"),
106 buildCommandUsage("<thingUID> " + ACCESS_KEY, "show the access key") });
110 public @Nullable ConsoleCommandCompleter getCompleter() {
115 public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
116 if (cursorArgumentIndex <= 0) {
117 return new StringsCompleter(thingRegistry.getAll().stream()
118 .filter(t -> LGWebOSBindingConstants.THING_TYPE_WEBOSTV.equals(t.getThingTypeUID()))
119 .map(t -> t.getUID().getAsString()).collect(Collectors.toList()), true).complete(args,
120 cursorArgumentIndex, cursorPosition, candidates);
121 } else if (cursorArgumentIndex == 1) {
122 Thing thing = getThing(args[0]);
123 if (thing != null && LGWebOSBindingConstants.THING_TYPE_WEBOSTV.equals(thing.getThingTypeUID())) {
124 return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
130 private @Nullable Thing getThing(String uid) {
133 ThingUID thingUID = new ThingUID(uid);
134 thing = thingRegistry.get(thingUID);
135 } catch (IllegalArgumentException e) {