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.lgwebos.internal.console;
15 import java.util.Arrays;
16 import java.util.List;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.lgwebos.internal.handler.LGWebOSHandler;
20 import org.openhab.core.io.console.Console;
21 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
22 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingRegistry;
25 import org.openhab.core.thing.ThingUID;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.osgi.service.component.annotations.Activate;
28 import org.osgi.service.component.annotations.Component;
29 import org.osgi.service.component.annotations.Reference;
32 * The {@link LGWebOSCommandExtension} is responsible for handling console commands
34 * @author Laurent Garnier - Initial contribution
38 @Component(service = ConsoleCommandExtension.class)
39 public class LGWebOSCommandExtension extends AbstractConsoleCommandExtension {
41 private static final String APPLICATIONS = "applications";
42 private static final String CHANNELS = "channels";
43 private static final String ACCESS_KEY = "accesskey";
45 private final ThingRegistry thingRegistry;
48 public LGWebOSCommandExtension(final @Reference ThingRegistry thingRegistry) {
49 super("lgwebos", "Interact with the LG webOS binding.");
50 this.thingRegistry = thingRegistry;
54 public void execute(String[] args, Console console) {
55 if (args.length == 2) {
58 ThingUID thingUID = new ThingUID(args[0]);
59 thing = thingRegistry.get(thingUID);
60 } catch (IllegalArgumentException e) {
63 ThingHandler thingHandler = null;
64 LGWebOSHandler handler = null;
66 thingHandler = thing.getHandler();
67 if (thingHandler instanceof LGWebOSHandler) {
68 handler = (LGWebOSHandler) thingHandler;
72 console.println("Bad thing id '" + args[0] + "'");
74 } else if (thingHandler == null) {
75 console.println("No handler initialized for the thing id '" + args[0] + "'");
77 } else if (handler == null) {
78 console.println("'" + args[0] + "' is not a LG webOS thing id");
83 handler.reportApplications().forEach(console::println);
86 handler.reportChannels().forEach(console::println);
89 console.println("Your access key is " + handler.getKey());
102 public List<String> getUsages() {
103 return Arrays.asList(new String[] { buildCommandUsage("<thingUID> " + APPLICATIONS, "list applications"),
104 buildCommandUsage("<thingUID> " + CHANNELS, "list channels"),
105 buildCommandUsage("<thingUID> " + ACCESS_KEY, "show the access key") });