]> git.basschouten.com Git - openhab-addons.git/blob
2e16968bab892becc06bde18231eaf3d53268bee
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.hdpowerview.internal.console;
14
15 import java.util.Arrays;
16 import java.util.List;
17
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;
37
38 /**
39  * The {@link HDPowerViewCommandExtension} is responsible for handling console commands
40  *
41  * @author Jacob Laursen - Initial contribution
42  */
43 @NonNullByDefault
44 @Component(service = ConsoleCommandExtension.class)
45 public class HDPowerViewCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
46
47     private static final String SHOW_IDS = "showIds";
48     private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(List.of(SHOW_IDS), false);
49
50     private final ThingRegistry thingRegistry;
51
52     @Activate
53     public HDPowerViewCommandExtension(final @Reference ThingRegistry thingRegistry) {
54         super(HDPowerViewBindingConstants.BINDING_ID, "Interact with the Hunter Douglas PowerView binding.");
55         this.thingRegistry = thingRegistry;
56     }
57
58     @Override
59     public void execute(String[] args, Console console) {
60         if (args.length != 1 || !SHOW_IDS.equals(args[0])) {
61             printUsage(console);
62             return;
63         }
64
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();
70
71                 try {
72                     List<ShadeData> shades = webTargets.getShades().shadeData;
73                     if (shades != null) {
74                         console.println(" - Shades:");
75                         for (ShadeData shade : shades) {
76                             console.println("    - ID: " + shade.id + " (" + shade.getName() + ")");
77                         }
78                     }
79
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() + ")");
85                         }
86                     }
87                 } catch (HubException e) {
88                     console.println("Error retrieving ID's: " + e.getMessage());
89                 }
90             }
91         }
92     }
93
94     @Override
95     public List<String> getUsages() {
96         return Arrays.asList(buildCommandUsage(SHOW_IDS, "list all shades and repeaters"));
97     }
98
99     @Override
100     public @Nullable ConsoleCommandCompleter getCompleter() {
101         return this;
102     }
103
104     @Override
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);
108         }
109         return false;
110     }
111 }