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.androidtv.internal.console;
15 import java.util.List;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.androidtv.internal.AndroidTVHandler;
19 import org.openhab.core.io.console.Console;
20 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
21 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
22 import org.openhab.core.library.types.StringType;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingRegistry;
26 import org.openhab.core.thing.ThingUID;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.openhab.core.types.Command;
29 import org.osgi.service.component.annotations.Activate;
30 import org.osgi.service.component.annotations.Component;
31 import org.osgi.service.component.annotations.Reference;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link AndroidTVCommandExtension} is responsible for handling console commands
38 * @author Ben Rosenblum - Initial contribution
42 @Component(service = ConsoleCommandExtension.class)
43 public class AndroidTVCommandExtension extends AbstractConsoleCommandExtension {
45 private final Logger logger = LoggerFactory.getLogger(AndroidTVCommandExtension.class);
46 private final ThingRegistry thingRegistry;
49 public AndroidTVCommandExtension(final @Reference ThingRegistry thingRegistry) {
50 super("androidtv", "Interact with the AndroidTV binding channels directly.");
51 this.thingRegistry = thingRegistry;
55 public void execute(String[] args, Console console) {
56 if (args.length == 3) {
57 logger.trace("Received CLI Command: {} {} |||{}|||", args[0], args[1], args[2]);
60 ThingUID thingUID = new ThingUID(args[0]);
61 thing = thingRegistry.get(thingUID);
62 } catch (IllegalArgumentException e) {
65 ThingHandler thingHandler = null;
66 AndroidTVHandler handler = null;
68 thingHandler = thing.getHandler();
69 if (thingHandler instanceof AndroidTVHandler) {
70 handler = (AndroidTVHandler) thingHandler;
74 console.println("Bad thing uid '" + args[0] + "'");
76 } else if (thingHandler == null) {
77 console.println("No handler initialized for the thing uid '" + args[0] + "'");
79 } else if (handler == null) {
80 console.println("'" + args[0] + "' is not an AndroidTV thing uid");
83 String channel = args[0] + ":" + args[1];
84 ChannelUID channelUID = new ChannelUID(channel);
85 Command command = (Command) new StringType(args[2]);
86 logger.debug("Sending CLI Command to Handler: {} |||{}|||", channelUID.toString(), command.toString());
87 handler.handleCommand(channelUID, command);
95 public List<String> getUsages() {
96 return List.of(buildCommandUsage("<thingUID> <channelname> <command>", "Send a command to a specific channel"));