2 * Copyright (c) 2010-2021 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.powermax.internal.console;
15 import java.util.Arrays;
16 import java.util.List;
18 import org.openhab.binding.powermax.internal.handler.PowermaxBridgeHandler;
19 import org.openhab.core.io.console.Console;
20 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
21 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingRegistry;
24 import org.openhab.core.thing.ThingUID;
25 import org.openhab.core.thing.binding.ThingHandler;
26 import org.osgi.service.component.annotations.Component;
27 import org.osgi.service.component.annotations.Reference;
30 * The {@link PowermaxCommandExtension} is responsible for handling console commands
32 * @author Laurent Garnier - Initial contribution
34 @Component(service = ConsoleCommandExtension.class)
35 public class PowermaxCommandExtension extends AbstractConsoleCommandExtension {
37 private static final String INFO_SETUP = "info_setup";
38 private static final String DOWNLOAD_SETUP = "download_setup";
40 private ThingRegistry thingRegistry;
42 public PowermaxCommandExtension() {
43 super("powermax", "Interact with the Powermax binding.");
47 public void execute(String[] args, Console console) {
48 if (args.length >= 2) {
51 ThingUID thingUID = new ThingUID(args[0]);
52 thing = thingRegistry.get(thingUID);
53 } catch (IllegalArgumentException e) {
56 ThingHandler thingHandler = null;
57 PowermaxBridgeHandler handler = null;
59 thingHandler = thing.getHandler();
60 if (thingHandler instanceof PowermaxBridgeHandler) {
61 handler = (PowermaxBridgeHandler) thingHandler;
65 console.println("Bad thing id '" + args[0] + "'");
67 } else if (thingHandler == null) {
68 console.println("No handler initialized for the thing id '" + args[0] + "'");
70 } else if (handler == null) {
71 console.println("'" + args[0] + "' is not a powermax bridge id");
76 for (String line : handler.getInfoSetup().split("\n")) {
77 console.println(line);
81 handler.downloadSetup();
82 console.println("Command '" + args[1] + "' handled.");
85 console.println("Unknown Powermax sub command '" + args[1] + "'");
96 public List<String> getUsages() {
97 return Arrays.asList(new String[] { buildCommandUsage("<bridgeUID> " + INFO_SETUP, "information on setup"),
98 buildCommandUsage("<bridgeUID> " + DOWNLOAD_SETUP, "download setup") });
102 protected void setThingRegistry(ThingRegistry thingRegistry) {
103 this.thingRegistry = thingRegistry;
106 protected void unsetThingRegistry(ThingRegistry thingRegistry) {
107 this.thingRegistry = null;