]> git.basschouten.com Git - openhab-addons.git/blob
f34c8158b14d10b66c6e78fbf271145e1d085bd7
[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.androidtv.internal.console;
14
15 import java.util.List;
16
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;
34
35 /**
36  * The {@link AndroidTVCommandExtension} is responsible for handling console commands
37  *
38  * @author Ben Rosenblum - Initial contribution
39  */
40
41 @NonNullByDefault
42 @Component(service = ConsoleCommandExtension.class)
43 public class AndroidTVCommandExtension extends AbstractConsoleCommandExtension {
44
45     private final Logger logger = LoggerFactory.getLogger(AndroidTVCommandExtension.class);
46     private final ThingRegistry thingRegistry;
47
48     @Activate
49     public AndroidTVCommandExtension(final @Reference ThingRegistry thingRegistry) {
50         super("androidtv", "Interact with the AndroidTV binding channels directly.");
51         this.thingRegistry = thingRegistry;
52     }
53
54     @Override
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]);
58             Thing thing = null;
59             try {
60                 ThingUID thingUID = new ThingUID(args[0]);
61                 thing = thingRegistry.get(thingUID);
62             } catch (IllegalArgumentException e) {
63                 thing = null;
64             }
65             ThingHandler thingHandler = null;
66             AndroidTVHandler handler = null;
67             if (thing != null) {
68                 thingHandler = thing.getHandler();
69                 if (thingHandler instanceof AndroidTVHandler) {
70                     handler = (AndroidTVHandler) thingHandler;
71                 }
72             }
73             if (thing == null) {
74                 console.println("Bad thing uid '" + args[0] + "'");
75                 printUsage(console);
76             } else if (thingHandler == null) {
77                 console.println("No handler initialized for the thing uid '" + args[0] + "'");
78                 printUsage(console);
79             } else if (handler == null) {
80                 console.println("'" + args[0] + "' is not an AndroidTV thing uid");
81                 printUsage(console);
82             } else {
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);
88             }
89         } else {
90             printUsage(console);
91         }
92     }
93
94     @Override
95     public List<String> getUsages() {
96         return List.of(buildCommandUsage("<thingUID> <channelname> <command>", "Send a command to a specific channel"));
97     }
98 }