2 * Copyright (c) 2010-2022 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.HDPowerViewBindingConstants;
21 import org.openhab.binding.hdpowerview.internal.HDPowerViewWebTargets;
22 import org.openhab.binding.hdpowerview.internal.dto.ShadeData;
23 import org.openhab.binding.hdpowerview.internal.dto.responses.RepeaterData;
24 import org.openhab.binding.hdpowerview.internal.exceptions.HubException;
25 import org.openhab.binding.hdpowerview.internal.handler.HDPowerViewHubHandler;
26 import org.openhab.core.io.console.Console;
27 import org.openhab.core.io.console.ConsoleCommandCompleter;
28 import org.openhab.core.io.console.StringsCompleter;
29 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
30 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingRegistry;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
39 * The {@link HDPowerViewCommandExtension} is responsible for handling console commands
41 * @author Jacob Laursen - Initial contribution
44 @Component(service = ConsoleCommandExtension.class)
45 public class HDPowerViewCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
47 private static final String SHOW_IDS = "showIds";
48 private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(List.of(SHOW_IDS), false);
50 private final ThingRegistry thingRegistry;
53 public HDPowerViewCommandExtension(final @Reference ThingRegistry thingRegistry) {
54 super(HDPowerViewBindingConstants.BINDING_ID, "Interact with the Hunter Douglas PowerView binding.");
55 this.thingRegistry = thingRegistry;
59 public void execute(String[] args, Console console) {
60 if (args.length != 1 || !SHOW_IDS.equals(args[0])) {
65 for (Thing thing : thingRegistry.getAll()) {
66 ThingHandler thingHandler = thing.getHandler();
67 if (thingHandler instanceof HDPowerViewHubHandler) {
68 console.println("API bridge: " + thing.getLabel());
69 HDPowerViewWebTargets webTargets = ((HDPowerViewHubHandler) thingHandler).getWebTargets();
72 List<ShadeData> shades = webTargets.getShades().shadeData;
74 console.println(" - Shades:");
75 for (ShadeData shade : shades) {
76 console.println(" - ID: " + shade.id + " (" + shade.getName() + ")");
80 List<RepeaterData> repeaters = webTargets.getRepeaters().repeaterData;
81 if (repeaters != null) {
82 console.println(" - Repeaters:");
83 for (RepeaterData repeater : repeaters) {
84 console.println(" - ID: " + repeater.id + " (" + repeater.getName() + ")");
87 } catch (HubException e) {
88 console.println("Error retrieving ID's: " + e.getMessage());
95 public List<String> getUsages() {
96 return Arrays.asList(buildCommandUsage(SHOW_IDS, "list all shades and repeaters"));
100 public @Nullable ConsoleCommandCompleter getCompleter() {
105 public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
106 if (cursorArgumentIndex <= 0) {
107 return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);