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.hdpowerview.internal.console;
15 import java.util.Arrays;
16 import java.util.List;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.hdpowerview.internal.GatewayWebTargets;
21 import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants;
22 import org.openhab.binding.hdpowerview.internal.HDPowerViewWebTargets;
23 import org.openhab.binding.hdpowerview.internal.dto.ShadeData;
24 import org.openhab.binding.hdpowerview.internal.dto.gen3.Shade;
25 import org.openhab.binding.hdpowerview.internal.dto.responses.RepeaterData;
26 import org.openhab.binding.hdpowerview.internal.exceptions.HubException;
27 import org.openhab.binding.hdpowerview.internal.handler.GatewayBridgeHandler;
28 import org.openhab.binding.hdpowerview.internal.handler.HDPowerViewHubHandler;
29 import org.openhab.core.io.console.Console;
30 import org.openhab.core.io.console.ConsoleCommandCompleter;
31 import org.openhab.core.io.console.StringsCompleter;
32 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
33 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingRegistry;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.osgi.service.component.annotations.Activate;
38 import org.osgi.service.component.annotations.Component;
39 import org.osgi.service.component.annotations.Reference;
42 * The {@link HDPowerViewCommandExtension} is responsible for handling console commands
44 * @author Jacob Laursen - Initial contribution
47 @Component(service = ConsoleCommandExtension.class)
48 public class HDPowerViewCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
50 private static final String SHOW_IDS = "showIds";
51 private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(List.of(SHOW_IDS), false);
53 private final ThingRegistry thingRegistry;
56 public HDPowerViewCommandExtension(final @Reference ThingRegistry thingRegistry) {
57 super(HDPowerViewBindingConstants.BINDING_ID, "Interact with the Hunter Douglas PowerView binding.");
58 this.thingRegistry = thingRegistry;
62 public void execute(String[] args, Console console) {
63 if (args.length != 1 || !SHOW_IDS.equals(args[0])) {
68 for (Thing thing : thingRegistry.getAll()) {
69 ThingHandler thingHandler = thing.getHandler();
70 if (thingHandler instanceof HDPowerViewHubHandler) {
71 console.println("Generation 1/2 API hub: " + thing.getLabel());
72 HDPowerViewWebTargets webTargets = ((HDPowerViewHubHandler) thingHandler).getWebTargets();
75 List<ShadeData> shades = webTargets.getShades().shadeData;
77 console.println(" - Shades:");
78 for (ShadeData shade : shades) {
79 console.println(" - ID: " + shade.id + " (" + shade.getName() + ")");
83 List<RepeaterData> repeaters = webTargets.getRepeaters().repeaterData;
84 if (repeaters != null) {
85 console.println(" - Repeaters:");
86 for (RepeaterData repeater : repeaters) {
87 console.println(" - ID: " + repeater.id + " (" + repeater.getName() + ")");
90 } catch (HubException e) {
91 console.println("Error retrieving ID's: " + e.getMessage());
93 } else if (thingHandler instanceof GatewayBridgeHandler) {
94 console.println("Generation 3 API gateway: " + thing.getLabel());
95 GatewayWebTargets webTargets = ((GatewayBridgeHandler) thingHandler).getWebTargets();
98 List<Shade> shades = webTargets.getShades();
99 if (!shades.isEmpty()) {
100 console.println(" - Shades:");
101 for (Shade shade : shades) {
102 console.println(" - ID: " + shade.getId() + " (" + shade.getName() + ")");
105 } catch (HubException e) {
106 console.println("Error retrieving ID's: " + e.getMessage());
113 public List<String> getUsages() {
114 return Arrays.asList(buildCommandUsage(SHOW_IDS, "list all shades and repeaters"));
118 public @Nullable ConsoleCommandCompleter getCompleter() {
123 public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
124 if (cursorArgumentIndex <= 0) {
125 return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);