]> git.basschouten.com Git - openhab-addons.git/blob
06c9a674b616c287fc38c1d3617e68951e196c32
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.io.homekit.internal;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.concurrent.ExecutionException;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
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.io.homekit.Homekit;
24 import org.osgi.service.component.annotations.Component;
25 import org.osgi.service.component.annotations.Reference;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Console commands for interacting with the HomeKit integration
31  *
32  * @author Andy Lintner - Initial contribution
33  */
34 @Component(service = ConsoleCommandExtension.class)
35 @NonNullByDefault
36 public class HomekitCommandExtension extends AbstractConsoleCommandExtension {
37     private static final String SUBCMD_CLEAR_PAIRINGS = "clearPairings";
38     private static final String SUBCMD_LIST_ACCESSORIES = "list";
39     private static final String SUBCMD_PRINT_ACCESSORY = "show";
40     private static final String SUBCMD_ALLOW_UNAUTHENTICATED = "allowUnauthenticated";
41     @Deprecated
42     private static final String LEGACY_SUBCMD_LIST_ACCESSORIES = "listAccessories";
43     @Deprecated
44     private static final String LEGACY_SUBCMD_PRINT_ACCESSORY = "printAccessory";
45
46     private final Logger logger = LoggerFactory.getLogger(HomekitCommandExtension.class);
47
48     private @NonNullByDefault({}) Homekit homekit;
49
50     public HomekitCommandExtension() {
51         super("homekit", "Interact with the HomeKit integration.");
52     }
53
54     @Override
55     public void execute(String[] args, Console console) {
56         if (args.length > 0) {
57             String subCommand = args[0];
58             switch (subCommand) {
59                 case SUBCMD_CLEAR_PAIRINGS:
60                     clearHomekitPairings(console);
61                     break;
62
63                 case SUBCMD_ALLOW_UNAUTHENTICATED:
64                     if (args.length > 1) {
65                         boolean allow = Boolean.parseBoolean(args[1]);
66                         allowUnauthenticatedHomekitRequests(allow, console);
67                     } else {
68                         console.println("true/false is required as an argument");
69                     }
70                     break;
71                 case SUBCMD_LIST_ACCESSORIES:
72                 case LEGACY_SUBCMD_LIST_ACCESSORIES:
73                     listAccessories(console);
74                     if (subCommand.equalsIgnoreCase(LEGACY_SUBCMD_LIST_ACCESSORIES)) {
75                         console.println("");
76                         console.println("Hey, you can use the shorter command 'homekit list'");
77                     }
78                     break;
79                 case SUBCMD_PRINT_ACCESSORY:
80                 case LEGACY_SUBCMD_PRINT_ACCESSORY:
81                     if (args.length > 1) {
82                         printAccessory(args[1], console);
83                     } else {
84                         console.println("accessory id or name is required as an argument");
85                     }
86                     if (subCommand.equalsIgnoreCase(LEGACY_SUBCMD_PRINT_ACCESSORY)) {
87                         console.println("");
88                         console.println("Hey, you can use the shorter command 'homekit show <accessory_id|name>'");
89                     }
90                     break;
91                 default:
92                     console.println("Unknown command '" + subCommand + "'");
93                     printUsage(console);
94                     break;
95             }
96         } else {
97             printUsage(console);
98         }
99     }
100
101     @Override
102     public List<String> getUsages() {
103         return Arrays.asList(buildCommandUsage(SUBCMD_LIST_ACCESSORIES, "list all HomeKit accessories"),
104                 buildCommandUsage(SUBCMD_PRINT_ACCESSORY + " <accessory id | accessory name>",
105                         "print additional details of the accessories which partially match provided ID or name."),
106                 buildCommandUsage(SUBCMD_CLEAR_PAIRINGS, "removes all pairings with HomeKit clients."),
107                 buildCommandUsage(SUBCMD_ALLOW_UNAUTHENTICATED + " <boolean>",
108                         "enables or disables unauthenticated access to facilitate debugging"));
109     }
110
111     @Reference
112     public void setHomekit(Homekit homekit) {
113         this.homekit = homekit;
114     }
115
116     private void clearHomekitPairings(Console console) {
117         homekit.clearHomekitPairings();
118         console.println("Cleared HomeKit pairings");
119     }
120
121     private void allowUnauthenticatedHomekitRequests(boolean allow, Console console) {
122         homekit.allowUnauthenticatedRequests(allow);
123         console.println((allow ? "Enabled " : "Disabled ") + "unauthenticated HomeKit access");
124     }
125
126     private void listAccessories(Console console) {
127         homekit.getAccessories().forEach(v -> {
128             try {
129                 console.println(v.getId() + " " + v.getName().get());
130             } catch (InterruptedException | ExecutionException e) {
131                 logger.warn("Cannot list accessories", e);
132             }
133         });
134     }
135
136     private void printAccessory(String id, Console console) {
137         homekit.getAccessories().forEach(v -> {
138             try {
139                 if (("" + v.getId()).contains(id) || ((v.getName().get() != null)
140                         && (v.getName().get().toUpperCase().contains(id.toUpperCase())))) {
141                     console.println(v.getId() + " " + v.getName().get());
142                     console.println("Services:");
143                     v.getServices().forEach(s -> {
144                         console.println("    Service Type: " + s.getType());
145                         console.println("    Characteristics: ");
146                         s.getCharacteristics().forEach(c -> console.println("      : " + c.getClass()));
147                     });
148                     console.println("");
149                 }
150             } catch (InterruptedException | ExecutionException e) {
151                 logger.warn("Cannot print accessory", e);
152             }
153         });
154     }
155 }