]> git.basschouten.com Git - openhab-addons.git/blob
4f2b7c9088c620180c289c6b82f48820806385aa
[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.lgwebos.internal.console;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.stream.Collectors;
18
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;
35
36 /**
37  * The {@link LGWebOSCommandExtension} 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 LGWebOSCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
45
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);
51
52     private final ThingRegistry thingRegistry;
53
54     @Activate
55     public LGWebOSCommandExtension(final @Reference ThingRegistry thingRegistry) {
56         super("lgwebos", "Interact with the LG webOS binding.");
57         this.thingRegistry = thingRegistry;
58     }
59
60     @Override
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;
66             if (thing != null) {
67                 thingHandler = thing.getHandler();
68                 if (thingHandler instanceof LGWebOSHandler) {
69                     handler = (LGWebOSHandler) thingHandler;
70                 }
71             }
72             if (thing == null) {
73                 console.println("Bad thing id '" + args[0] + "'");
74                 printUsage(console);
75             } else if (thingHandler == null) {
76                 console.println("No handler initialized for the thing id '" + args[0] + "'");
77                 printUsage(console);
78             } else if (handler == null) {
79                 console.println("'" + args[0] + "' is not a LG webOS thing id");
80                 printUsage(console);
81             } else {
82                 switch (args[1]) {
83                     case APPLICATIONS:
84                         handler.reportApplications().forEach(console::println);
85                         break;
86                     case CHANNELS:
87                         handler.reportChannels().forEach(console::println);
88                         break;
89                     case ACCESS_KEY:
90                         console.println("Your access key is " + handler.getKey());
91                         break;
92                     default:
93                         printUsage(console);
94                         break;
95                 }
96             }
97         } else {
98             printUsage(console);
99         }
100     }
101
102     @Override
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") });
107     }
108
109     @Override
110     public @Nullable ConsoleCommandCompleter getCompleter() {
111         return this;
112     }
113
114     @Override
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)
120                     .complete(args, 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);
125             }
126         }
127         return false;
128     }
129
130     private @Nullable Thing getThing(String uid) {
131         Thing thing = null;
132         try {
133             ThingUID thingUID = new ThingUID(uid);
134             thing = thingRegistry.get(thingUID);
135         } catch (IllegalArgumentException e) {
136             thing = null;
137         }
138         return thing;
139     }
140 }