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