]> git.basschouten.com Git - openhab-addons.git/blob
74df11541e5c5ecd1af6f8f1df9e8a410d356a89
[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.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.powermax.internal.handler.PowermaxBridgeHandler;
20 import org.openhab.binding.powermax.internal.state.PowermaxState;
21 import org.openhab.core.io.console.Console;
22 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
23 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingRegistry;
26 import org.openhab.core.thing.ThingUID;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.osgi.service.component.annotations.Activate;
29 import org.osgi.service.component.annotations.Component;
30 import org.osgi.service.component.annotations.Reference;
31
32 /**
33  * The {@link PowermaxCommandExtension} is responsible for handling console commands
34  *
35  * @author Laurent Garnier - Initial contribution
36  */
37 @NonNullByDefault
38 @Component(service = ConsoleCommandExtension.class)
39 public class PowermaxCommandExtension extends AbstractConsoleCommandExtension {
40
41     private static final String INFO_SETUP = "info_setup";
42     private static final String DOWNLOAD_SETUP = "download_setup";
43     private static final String BRIDGE_STATE = "bridge_state";
44
45     private final ThingRegistry thingRegistry;
46
47     @Activate
48     public PowermaxCommandExtension(final @Reference ThingRegistry thingRegistry) {
49         super("powermax", "Interact with the Powermax binding.");
50         this.thingRegistry = thingRegistry;
51     }
52
53     @Override
54     public void execute(String[] args, Console console) {
55         if (args.length >= 2) {
56             Thing thing = null;
57             try {
58                 ThingUID thingUID = new ThingUID(args[0]);
59                 thing = thingRegistry.get(thingUID);
60             } catch (IllegalArgumentException e) {
61                 thing = null;
62             }
63             ThingHandler thingHandler = null;
64             PowermaxBridgeHandler handler = null;
65             if (thing != null) {
66                 thingHandler = thing.getHandler();
67                 if (thingHandler instanceof PowermaxBridgeHandler) {
68                     handler = (PowermaxBridgeHandler) thingHandler;
69                 }
70             }
71             if (thing == null) {
72                 console.println("Bad thing id '" + args[0] + "'");
73                 printUsage(console);
74             } else if (thingHandler == null) {
75                 console.println("No handler initialized for the thing id '" + args[0] + "'");
76                 printUsage(console);
77             } else if (handler == null) {
78                 console.println("'" + args[0] + "' is not a powermax bridge id");
79                 printUsage(console);
80             } else {
81                 switch (args[1]) {
82                     case INFO_SETUP:
83                         for (String line : handler.getInfoSetup().split("\n")) {
84                             console.println(line);
85                         }
86                         break;
87                     case DOWNLOAD_SETUP:
88                         handler.downloadSetup();
89                         console.println("Command '" + args[1] + "' handled.");
90                         break;
91                     case BRIDGE_STATE:
92                         PowermaxState state = handler.getCurrentState();
93                         if (state != null) {
94                             for (String line : state.toString().split("\n")) {
95                                 console.println(line);
96                             }
97                         }
98                         break;
99                     default:
100                         console.println("Unknown Powermax sub command '" + args[1] + "'");
101                         printUsage(console);
102                         break;
103                 }
104             }
105         } else {
106             printUsage(console);
107         }
108     }
109
110     @Override
111     public List<String> getUsages() {
112         return Arrays.asList(new String[] { buildCommandUsage("<bridgeUID> " + INFO_SETUP, "information on setup"),
113                 buildCommandUsage("<bridgeUID> " + DOWNLOAD_SETUP, "download setup"),
114                 buildCommandUsage("<bridgeUID> " + BRIDGE_STATE, "show current state") });
115     }
116 }