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.powermax.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.powermax.internal.PowermaxBindingConstants;
22 import org.openhab.binding.powermax.internal.handler.PowermaxBridgeHandler;
23 import org.openhab.binding.powermax.internal.state.PowermaxState;
24 import org.openhab.core.io.console.Console;
25 import org.openhab.core.io.console.ConsoleCommandCompleter;
26 import org.openhab.core.io.console.StringsCompleter;
27 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
28 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingRegistry;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Reference;
38 * The {@link PowermaxCommandExtension} is responsible for handling console commands
40 * @author Laurent Garnier - Initial contribution
43 @Component(service = ConsoleCommandExtension.class)
44 public class PowermaxCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
46 private static final String INFO_SETUP = "info_setup";
47 private static final String DOWNLOAD_SETUP = "download_setup";
48 private static final String BRIDGE_STATE = "bridge_state";
49 private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(
50 List.of(INFO_SETUP, DOWNLOAD_SETUP, BRIDGE_STATE), false);
52 private final ThingRegistry thingRegistry;
55 public PowermaxCommandExtension(final @Reference ThingRegistry thingRegistry) {
56 super("powermax", "Interact with the Powermax 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 PowermaxBridgeHandler handler = null;
67 thingHandler = thing.getHandler();
68 if (thingHandler instanceof PowermaxBridgeHandler bridgeHandler) {
69 handler = bridgeHandler;
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 powermax bridge id");
84 for (String line : handler.getInfoSetup().split("\n")) {
85 console.println(line);
89 handler.downloadSetup();
90 console.println("Command '" + args[1] + "' handled.");
93 PowermaxState state = handler.getCurrentState();
95 for (String line : state.toString().split("\n")) {
96 console.println(line);
101 console.println("Unknown Powermax sub command '" + args[1] + "'");
112 public List<String> getUsages() {
113 return Arrays.asList(new String[] { buildCommandUsage("<bridgeUID> " + INFO_SETUP, "information on setup"),
114 buildCommandUsage("<bridgeUID> " + DOWNLOAD_SETUP, "download setup"),
115 buildCommandUsage("<bridgeUID> " + BRIDGE_STATE, "show current state") });
119 public @Nullable ConsoleCommandCompleter getCompleter() {
124 public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
125 if (cursorArgumentIndex <= 0) {
126 return new StringsCompleter(thingRegistry.getAll().stream()
127 .filter(t -> PowermaxBindingConstants.BRIDGE_TYPE_SERIAL.equals(t.getThingTypeUID())
128 || PowermaxBindingConstants.BRIDGE_TYPE_IP.equals(t.getThingTypeUID()))
129 .map(t -> t.getUID().getAsString()).collect(Collectors.toList()), true)
130 .complete(args, cursorArgumentIndex, cursorPosition, candidates);
131 } else if (cursorArgumentIndex == 1) {
132 Thing thing = getThing(args[0]);
133 if (thing != null && (PowermaxBindingConstants.BRIDGE_TYPE_SERIAL.equals(thing.getThingTypeUID())
134 || PowermaxBindingConstants.BRIDGE_TYPE_IP.equals(thing.getThingTypeUID()))) {
135 return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
141 private @Nullable Thing getThing(String uid) {
144 ThingUID thingUID = new ThingUID(uid);
145 thing = thingRegistry.get(thingUID);
146 } catch (IllegalArgumentException e) {