]> git.basschouten.com Git - openhab-addons.git/blob
92f413f9af54d925158d769e2d5fce58bbdd651a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.powermax.internal.console;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.stream.Collectors;
18
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;
36
37 /**
38  * The {@link PowermaxCommandExtension} is responsible for handling console commands
39  *
40  * @author Laurent Garnier - Initial contribution
41  */
42 @NonNullByDefault
43 @Component(service = ConsoleCommandExtension.class)
44 public class PowermaxCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
45
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);
51
52     private final ThingRegistry thingRegistry;
53
54     @Activate
55     public PowermaxCommandExtension(final @Reference ThingRegistry thingRegistry) {
56         super("powermax", "Interact with the Powermax binding.");
57         this.thingRegistry = thingRegistry;
58     }
59
60     @Override
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;
66             if (thing != null) {
67                 thingHandler = thing.getHandler();
68                 if (thingHandler instanceof PowermaxBridgeHandler) {
69                     handler = (PowermaxBridgeHandler) thingHandler;
70                 }
71             }
72             if (thing == null) {
73                 console.println("Bad thing id '" + args[0] + "'");
74                 printUsage(console);
75             } else if (thingHandler == null) {
76                 console.println("No handler initialized for the thing id '" + args[0] + "'");
77                 printUsage(console);
78             } else if (handler == null) {
79                 console.println("'" + args[0] + "' is not a powermax bridge id");
80                 printUsage(console);
81             } else {
82                 switch (args[1]) {
83                     case INFO_SETUP:
84                         for (String line : handler.getInfoSetup().split("\n")) {
85                             console.println(line);
86                         }
87                         break;
88                     case DOWNLOAD_SETUP:
89                         handler.downloadSetup();
90                         console.println("Command '" + args[1] + "' handled.");
91                         break;
92                     case BRIDGE_STATE:
93                         PowermaxState state = handler.getCurrentState();
94                         if (state != null) {
95                             for (String line : state.toString().split("\n")) {
96                                 console.println(line);
97                             }
98                         }
99                         break;
100                     default:
101                         console.println("Unknown Powermax sub command '" + args[1] + "'");
102                         printUsage(console);
103                         break;
104                 }
105             }
106         } else {
107             printUsage(console);
108         }
109     }
110
111     @Override
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") });
116     }
117
118     @Override
119     public @Nullable ConsoleCommandCompleter getCompleter() {
120         return this;
121     }
122
123     @Override
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).complete(args,
130                             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);
136             }
137         }
138         return false;
139     }
140
141     private @Nullable Thing getThing(String uid) {
142         Thing thing = null;
143         try {
144             ThingUID thingUID = new ThingUID(uid);
145             thing = thingRegistry.get(thingUID);
146         } catch (IllegalArgumentException e) {
147             thing = null;
148         }
149         return thing;
150     }
151 }