]> git.basschouten.com Git - openhab-addons.git/blob
1ec4501d556490ff3543dd58f8b6738f7cb6a5f1
[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.elroconnects.internal.console;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.Optional;
18 import java.util.stream.Collectors;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsBridgeHandler;
22 import org.openhab.core.io.console.Console;
23 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
24 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
25 import org.openhab.core.thing.ThingRegistry;
26 import org.openhab.core.thing.ThingStatus;
27 import org.osgi.service.component.annotations.Activate;
28 import org.osgi.service.component.annotations.Component;
29 import org.osgi.service.component.annotations.Reference;
30
31 /**
32  * The {@link ElroConnectsCommandExtension} is responsible for handling console commands
33  *
34  * @author Mark Herwege - Initial contribution
35  */
36
37 @NonNullByDefault
38 @Component(service = ConsoleCommandExtension.class)
39 public class ElroConnectsCommandExtension extends AbstractConsoleCommandExtension {
40
41     private static final String CONNECTORS = "connectors";
42     private static final String DEVICES = "devices";
43     private static final String REFRESH = "refresh";
44     private static final String RENAME = "rename";
45     private static final String JOIN = "join";
46     private static final String REPLACE = "replace";
47     private static final String REMOVE = "remove";
48     private static final String CANCEL = "cancel";
49
50     private final ThingRegistry thingRegistry;
51
52     @Activate
53     public ElroConnectsCommandExtension(final @Reference ThingRegistry thingRegistry) {
54         super("elroconnects", "Interact with the ELRO Connects binding");
55         this.thingRegistry = thingRegistry;
56     }
57
58     @Override
59     public void execute(String[] args, Console console) {
60         if ((args.length < 1) || (args.length > 4)) {
61             console.println("Invalid number of arguments");
62             printUsage(console);
63             return;
64         }
65
66         List<ElroConnectsBridgeHandler> bridgeHandlers = thingRegistry.getAll().stream()
67                 .filter(t -> t.getHandler() instanceof ElroConnectsBridgeHandler)
68                 .map(b -> ((ElroConnectsBridgeHandler) b.getHandler())).collect(Collectors.toList());
69
70         if (CONNECTORS.equals(args[0])) {
71             if (args.length > 1) {
72                 console.println("No extra argument allowed after 'connectors'");
73                 printUsage(console);
74             } else if (bridgeHandlers.isEmpty()) {
75                 console.println("No K1 hubs added as a bridge");
76             } else {
77                 bridgeHandlers.forEach(b -> console.printf("%s%n", b.getConnectorId()));
78             }
79             return;
80         }
81
82         Optional<ElroConnectsBridgeHandler> bridgeOptional = bridgeHandlers.stream()
83                 .filter(b -> b.getConnectorId().equals(args[0])).findAny();
84         if (bridgeOptional.isEmpty()) {
85             console.println("'" + args[0] + "' is not a valid connectorId for an ELRO Connects bridge");
86             printUsage(console);
87             return;
88         }
89         ElroConnectsBridgeHandler bridgeHandler = bridgeOptional.get();
90         if (!ThingStatus.ONLINE.equals(bridgeHandler.getThing().getStatus())) {
91             console.println("ELRO Connects bridge not online, no commands allowed");
92             return;
93         }
94
95         if (args.length < 2) {
96             console.println("Invalid number of arguments");
97             printUsage(console);
98             return;
99         }
100
101         switch (args[1]) {
102             case REFRESH:
103                 if (args.length > 2) {
104                     console.println("No extra argument allowed after '" + args[1] + "'");
105                     printUsage(console);
106                 } else {
107                     bridgeHandler.refreshFromConsole();
108                 }
109                 break;
110             case DEVICES:
111                 if (args.length > 2) {
112                     console.println("No extra argument allowed after '" + args[1] + "'");
113                     printUsage(console);
114                 } else {
115                     bridgeHandler.listDevicesFromConsole().forEach((id, name) -> console.printf("%5d %s%n", id, name));
116                 }
117                 break;
118             case JOIN:
119                 if (args.length > 2) {
120                     console.println("No extra argument allowed after '" + args[1] + "'");
121                     printUsage(console);
122                 } else {
123                     bridgeHandler.joinDeviceFromConsole();
124                     console.println("Device join mode active");
125                 }
126                 break;
127             case CANCEL:
128                 if (args.length < 3) {
129                     console.println("Invalid number of arguments");
130                     printUsage(console);
131                 } else if (JOIN.equals(args[2]) || REPLACE.equals(args[2])) {
132                     if (args.length > 3) {
133                         console.println("No extra argument allowed after '" + args[2] + "'");
134                         printUsage(console);
135                     } else {
136                         bridgeHandler.cancelJoinDeviceFromConsole();
137                         console.println("Device join mode inactive");
138                     }
139                 } else {
140                     console.println("Command argument '" + args[2] + "' not recognized");
141                     printUsage(console);
142                     return;
143                 }
144                 break;
145             case REPLACE:
146                 if (args.length < 3) {
147                     console.println("Invalid number of arguments");
148                     printUsage(console);
149                 } else {
150                     try {
151                         if (args.length > 3) {
152                             console.println("No extra argument allowed after '" + args[2] + "'");
153                             printUsage(console);
154                         } else if (!bridgeHandler.replaceDeviceFromConsole(Integer.valueOf(args[2]))) {
155                             console.println("Command argument '" + args[2] + "' is not a known deviceId");
156                             printUsage(console);
157                         } else {
158                             console.println("Device join mode active");
159                         }
160                     } catch (NumberFormatException e) {
161                         console.println("Command argument '" + args[2] + "' is not a numeric deviceId");
162                         printUsage(console);
163                     }
164                 }
165                 break;
166             case REMOVE:
167                 if (args.length < 3) {
168                     console.println("Invalid number of arguments");
169                     printUsage(console);
170                 } else {
171                     try {
172                         if (args.length > 3) {
173                             console.println("No extra argument allowed after '" + args[2] + "'");
174                             printUsage(console);
175                         } else if (!bridgeHandler.removeDeviceFromConsole(Integer.valueOf(args[2]))) {
176                             console.println("Command argument '" + args[2] + "' is not a known deviceId");
177                             printUsage(console);
178                         }
179                     } catch (NumberFormatException e) {
180                         console.println("Command argument '" + args[2] + "' is not a numeric deviceId");
181                         printUsage(console);
182                     }
183                 }
184                 break;
185             case RENAME:
186                 if (args.length < 4) {
187                     console.println("Invalid number of arguments");
188                     printUsage(console);
189                 } else {
190                     try {
191                         if (args.length > 4) {
192                             console.println("No extra argument allowed after '" + args[2] + " " + args[3] + "'");
193                             printUsage(console);
194                         } else if (!bridgeHandler.renameDeviceFromConsole(Integer.valueOf(args[2]), args[3])) {
195                             console.println("Command argument '" + args[2] + "' is not a known deviceId");
196                             printUsage(console);
197                         }
198                     } catch (NumberFormatException e) {
199                         console.println("Command argument '" + args[2] + "' is not a numeric deviceId");
200                         printUsage(console);
201                     }
202                 }
203                 break;
204             default:
205                 console.println("Command argument '" + args[1] + "' not recognized");
206                 printUsage(console);
207         }
208     }
209
210     public void joinDeviceCancelled(Console console) {
211         console.println("Device join mode inactive");
212     }
213
214     @Override
215     public List<String> getUsages() {
216         return Arrays.asList(new String[] { buildCommandUsage(CONNECTORS, "list all K1 hub connector ID's"),
217                 buildCommandUsage("<connectorId> " + REFRESH, "refresh device list, names and status"),
218                 buildCommandUsage("<connectorId> " + DEVICES, "list all devices connected to the K1 hub"),
219                 buildCommandUsage("<connectorId> " + RENAME + " <deviceId> <name>", "rename device with ID"),
220                 buildCommandUsage("<connectorId> " + JOIN,
221                         "put K1 hub in device join mode, 3 short presses on the device will join it to the hub"),
222                 buildCommandUsage("<connectorId> " + CANCEL + " " + JOIN, "cancel K1 hub device join mode"),
223                 buildCommandUsage("<connectorId> " + REPLACE + " <deviceId>",
224                         "replace device with ID by newly joined device, puts K1 hub in join mode"),
225                 buildCommandUsage("<connectorId> " + CANCEL + " " + REPLACE, "cancel K1 hub device replace mode"),
226                 buildCommandUsage("<connectorId> " + REMOVE + " <deviceId>", "remove device with ID from K1 hub") });
227     }
228 }