]> git.basschouten.com Git - openhab-addons.git/blob
3d58f5250eed9e531c7b6e4beb3ba067f3ec6e4b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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
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;
28
29 /**
30  * The {@link PowermaxCommandExtension} is responsible for handling console commands
31  *
32  * @author Laurent Garnier - Initial contribution
33  */
34 @Component(service = ConsoleCommandExtension.class)
35 public class PowermaxCommandExtension extends AbstractConsoleCommandExtension {
36
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";
40
41     private ThingRegistry thingRegistry;
42
43     public PowermaxCommandExtension() {
44         super("powermax", "Interact with the Powermax binding.");
45     }
46
47     @Override
48     public void execute(String[] args, Console console) {
49         if (args.length >= 2) {
50             Thing thing = null;
51             try {
52                 ThingUID thingUID = new ThingUID(args[0]);
53                 thing = thingRegistry.get(thingUID);
54             } catch (IllegalArgumentException e) {
55                 thing = null;
56             }
57             ThingHandler thingHandler = null;
58             PowermaxBridgeHandler handler = null;
59             if (thing != null) {
60                 thingHandler = thing.getHandler();
61                 if (thingHandler instanceof PowermaxBridgeHandler) {
62                     handler = (PowermaxBridgeHandler) thingHandler;
63                 }
64             }
65             if (thing == null) {
66                 console.println("Bad thing id '" + args[0] + "'");
67                 printUsage(console);
68             } else if (thingHandler == null) {
69                 console.println("No handler initialized for the thing id '" + args[0] + "'");
70                 printUsage(console);
71             } else if (handler == null) {
72                 console.println("'" + args[0] + "' is not a powermax bridge id");
73                 printUsage(console);
74             } else {
75                 switch (args[1]) {
76                     case INFO_SETUP:
77                         for (String line : handler.getInfoSetup().split("\n")) {
78                             console.println(line);
79                         }
80                         break;
81                     case DOWNLOAD_SETUP:
82                         handler.downloadSetup();
83                         console.println("Command '" + args[1] + "' handled.");
84                         break;
85                     case BRIDGE_STATE:
86                         for (String line : handler.getCurrentState().toString().split("\n")) {
87                             console.println(line);
88                         }
89                         break;
90                     default:
91                         console.println("Unknown Powermax sub command '" + args[1] + "'");
92                         printUsage(console);
93                         break;
94                 }
95             }
96         } else {
97             printUsage(console);
98         }
99     }
100
101     @Override
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") });
106     }
107
108     @Reference
109     protected void setThingRegistry(ThingRegistry thingRegistry) {
110         this.thingRegistry = thingRegistry;
111     }
112
113     protected void unsetThingRegistry(ThingRegistry thingRegistry) {
114         this.thingRegistry = null;
115     }
116 }