]> git.basschouten.com Git - openhab-addons.git/blob
e8e3449ab00e20fb5eae70ea331bb954c0fb6d4e
[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
40     private ThingRegistry thingRegistry;
41
42     public PowermaxCommandExtension() {
43         super("powermax", "Interact with the Powermax binding.");
44     }
45
46     @Override
47     public void execute(String[] args, Console console) {
48         if (args.length >= 2) {
49             Thing thing = null;
50             try {
51                 ThingUID thingUID = new ThingUID(args[0]);
52                 thing = thingRegistry.get(thingUID);
53             } catch (IllegalArgumentException e) {
54                 thing = null;
55             }
56             ThingHandler thingHandler = null;
57             PowermaxBridgeHandler handler = null;
58             if (thing != null) {
59                 thingHandler = thing.getHandler();
60                 if (thingHandler instanceof PowermaxBridgeHandler) {
61                     handler = (PowermaxBridgeHandler) thingHandler;
62                 }
63             }
64             if (thing == null) {
65                 console.println("Bad thing id '" + args[0] + "'");
66                 printUsage(console);
67             } else if (thingHandler == null) {
68                 console.println("No handler initialized for the thing id '" + args[0] + "'");
69                 printUsage(console);
70             } else if (handler == null) {
71                 console.println("'" + args[0] + "' is not a powermax bridge id");
72                 printUsage(console);
73             } else {
74                 switch (args[1]) {
75                     case INFO_SETUP:
76                         for (String line : handler.getInfoSetup().split("\n")) {
77                             console.println(line);
78                         }
79                         break;
80                     case DOWNLOAD_SETUP:
81                         handler.downloadSetup();
82                         console.println("Command '" + args[1] + "' handled.");
83                         break;
84                     default:
85                         console.println("Unknown Powermax sub command '" + args[1] + "'");
86                         printUsage(console);
87                         break;
88                 }
89             }
90         } else {
91             printUsage(console);
92         }
93     }
94
95     @Override
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") });
99     }
100
101     @Reference
102     protected void setThingRegistry(ThingRegistry thingRegistry) {
103         this.thingRegistry = thingRegistry;
104     }
105
106     protected void unsetThingRegistry(ThingRegistry thingRegistry) {
107         this.thingRegistry = null;
108     }
109 }