]> git.basschouten.com Git - openhab-addons.git/blob
5a0964b01f1d647a53dac7c0b9737e605a342c5a
[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.meater.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.meater.internal.MeaterBindingConstants;
21 import org.openhab.binding.meater.internal.handler.MeaterBridgeHandler;
22 import org.openhab.core.io.console.Console;
23 import org.openhab.core.io.console.ConsoleCommandCompleter;
24 import org.openhab.core.io.console.StringsCompleter;
25 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
26 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingRegistry;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
33
34 /**
35  * The {@link MeaterCommandExtension} is responsible for handling console commands
36  *
37  * @author Jacob Laursen - Initial contribution
38  */
39 @NonNullByDefault
40 @Component(service = ConsoleCommandExtension.class)
41 public class MeaterCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
42
43     private static final String SHOW_IDS = "showIds";
44     private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(List.of(SHOW_IDS), false);
45
46     private final ThingRegistry thingRegistry;
47
48     @Activate
49     public MeaterCommandExtension(final @Reference ThingRegistry thingRegistry) {
50         super(MeaterBindingConstants.BINDING_ID, "Interact with the Meater binding.");
51         this.thingRegistry = thingRegistry;
52     }
53
54     @Override
55     public void execute(String[] args, Console console) {
56         if (args.length != 1 || !SHOW_IDS.equals(args[0])) {
57             printUsage(console);
58             return;
59         }
60
61         for (Thing thing : thingRegistry.getAll()) {
62             ThingHandler thingHandler = thing.getHandler();
63             if (thingHandler instanceof MeaterBridgeHandler) {
64                 console.println("API bridge: " + thing.getLabel());
65                 ((MeaterBridgeHandler) thingHandler).getMeaterThings().entrySet().stream().forEach(t -> {
66                     console.println("    - ID: " + t.getKey() + " (ambient temperature: "
67                             + t.getValue().temperature.ambient + ")");
68                 });
69             }
70         }
71     }
72
73     @Override
74     public List<String> getUsages() {
75         return Arrays.asList(buildCommandUsage(SHOW_IDS, "list all probes"));
76     }
77
78     @Override
79     public @Nullable ConsoleCommandCompleter getCompleter() {
80         return this;
81     }
82
83     @Override
84     public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
85         if (cursorArgumentIndex <= 0) {
86             return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
87         }
88         return false;
89     }
90 }