]> git.basschouten.com Git - openhab-addons.git/blob
6fd96fe91d91ec453ea9694e5d893d50c78bac8d
[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.somfytahoma.internal.console;
14
15 import java.util.List;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.somfytahoma.internal.handler.SomfyTahomaBridgeHandler;
19 import org.openhab.binding.somfytahoma.internal.model.SomfyTahomaActionGroup;
20 import org.openhab.core.io.console.Console;
21 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
22 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingRegistry;
25 import org.openhab.core.thing.ThingUID;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.osgi.service.component.annotations.Activate;
28 import org.osgi.service.component.annotations.Component;
29 import org.osgi.service.component.annotations.Reference;
30
31 /**
32  * The {@link SomfyTahomaCommandExtension} is responsible for handling console commands
33  *
34  * @author Laurent Garnier - Initial contribution
35  */
36
37 @NonNullByDefault
38 @Component(service = ConsoleCommandExtension.class)
39 public class SomfyTahomaCommandExtension extends AbstractConsoleCommandExtension {
40
41     private static final String SCENARIOS = "scenarios";
42
43     private final ThingRegistry thingRegistry;
44
45     @Activate
46     public SomfyTahomaCommandExtension(final @Reference ThingRegistry thingRegistry) {
47         super("somfytahoma", "Interact with the Somfy Tahoma binding.");
48         this.thingRegistry = thingRegistry;
49     }
50
51     @Override
52     public void execute(String[] args, Console console) {
53         if (args.length == 2) {
54             Thing thing = null;
55             try {
56                 ThingUID thingUID = new ThingUID(args[0]);
57                 thing = thingRegistry.get(thingUID);
58             } catch (IllegalArgumentException e) {
59                 thing = null;
60             }
61             ThingHandler thingHandler = null;
62             SomfyTahomaBridgeHandler bridgeHandler = null;
63             if (thing != null) {
64                 thingHandler = thing.getHandler();
65                 if (thingHandler instanceof SomfyTahomaBridgeHandler) {
66                     bridgeHandler = (SomfyTahomaBridgeHandler) thingHandler;
67                 }
68             }
69             if (thing == null) {
70                 console.println("Bad thing id '" + args[0] + "'");
71                 printUsage(console);
72             } else if (thingHandler == null) {
73                 console.println("No handler initialized for the thingUID '" + args[0] + "'");
74                 printUsage(console);
75             } else if (bridgeHandler == null) {
76                 console.println("'" + args[0] + "' is not a Somfy Tahoma bridgeUID");
77                 printUsage(console);
78             } else if (args[1].equals(SCENARIOS)) {
79                 for (SomfyTahomaActionGroup actionGroup : bridgeHandler.listActionGroups()) {
80                     console.println("Id is \"" + actionGroup.getOid() + "\" for the scenario \""
81                             + actionGroup.getLabel() + "\"");
82                 }
83             } else {
84                 printUsage(console);
85             }
86         } else {
87             printUsage(console);
88         }
89     }
90
91     @Override
92     public List<String> getUsages() {
93         return List.of(buildCommandUsage("<bridgeUID> " + SCENARIOS, "list all the scenarios with their id"));
94     }
95 }