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";
39 private static final String BRIDGE_STATE = "bridge_state";
41 private ThingRegistry thingRegistry;
43 public PowermaxCommandExtension() {
44 super("powermax", "Interact with the Powermax binding.");
48 public void execute(String[] args, Console console) {
49 if (args.length >= 2) {
52 ThingUID thingUID = new ThingUID(args[0]);
53 thing = thingRegistry.get(thingUID);
54 } catch (IllegalArgumentException e) {
57 ThingHandler thingHandler = null;
58 PowermaxBridgeHandler handler = null;
60 thingHandler = thing.getHandler();
61 if (thingHandler instanceof PowermaxBridgeHandler) {
62 handler = (PowermaxBridgeHandler) thingHandler;
66 console.println("Bad thing id '" + args[0] + "'");
68 } else if (thingHandler == null) {
69 console.println("No handler initialized for the thing id '" + args[0] + "'");
71 } else if (handler == null) {
72 console.println("'" + args[0] + "' is not a powermax bridge id");
77 for (String line : handler.getInfoSetup().split("\n")) {
78 console.println(line);
82 handler.downloadSetup();
83 console.println("Command '" + args[1] + "' handled.");
86 for (String line : handler.getCurrentState().toString().split("\n")) {
87 console.println(line);
91 console.println("Unknown Powermax sub command '" + args[1] + "'");
102 public List<String> getUsages() {
103 return Arrays.asList(new String[] { buildCommandUsage("<bridgeUID> " + INFO_SETUP, "information on setup"),
104 buildCommandUsage("<bridgeUID> " + DOWNLOAD_SETUP, "download setup"),
105 buildCommandUsage("<bridgeUID> " + BRIDGE_STATE, "show current state") });
109 protected void setThingRegistry(ThingRegistry thingRegistry) {
110 this.thingRegistry = thingRegistry;
113 protected void unsetThingRegistry(ThingRegistry thingRegistry) {
114 this.thingRegistry = null;