]> git.basschouten.com Git - openhab-addons.git/blob
9e63eb4390cdc5e452928f57b9fe737c32bf8873
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.vizio.internal.console;
14
15 import static org.openhab.binding.vizio.internal.VizioBindingConstants.*;
16
17 import java.math.BigDecimal;
18 import java.util.List;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.vizio.internal.VizioException;
22 import org.openhab.binding.vizio.internal.handler.VizioHandler;
23 import org.openhab.core.io.console.Console;
24 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
25 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingRegistry;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
33
34 /**
35  * The {@link VizioCommandExtension} is responsible for handling console commands
36  *
37  * @author Michael Lobstein - Initial contribution
38  */
39
40 @NonNullByDefault
41 @Component(service = ConsoleCommandExtension.class)
42 public class VizioCommandExtension extends AbstractConsoleCommandExtension {
43     private static final String START_PAIRING = "start_pairing";
44     private static final String SUBMIT_CODE = "submit_code";
45
46     private final ThingRegistry thingRegistry;
47
48     @Activate
49     public VizioCommandExtension(final @Reference ThingRegistry thingRegistry) {
50         super("vizio", "Interact with the Vizio binding to get an authentication token from the TV.");
51         this.thingRegistry = thingRegistry;
52     }
53
54     @Override
55     public void execute(String[] args, Console console) {
56         if (args.length == 3) {
57             Thing thing = null;
58             try {
59                 ThingUID thingUID = new ThingUID(args[0]);
60                 thing = thingRegistry.get(thingUID);
61             } catch (IllegalArgumentException e) {
62                 thing = null;
63             }
64             ThingHandler thingHandler = null;
65             VizioHandler handler = null;
66             if (thing != null) {
67                 thingHandler = thing.getHandler();
68                 if (thingHandler instanceof VizioHandler vizioHandler) {
69                     handler = vizioHandler;
70                 }
71             }
72             if (thing == null) {
73                 console.println("Bad thing id '" + args[0] + "'");
74                 printUsage(console);
75             } else if (thingHandler == null) {
76                 console.println("No handler initialized for the thing id '" + args[0] + "'");
77                 printUsage(console);
78             } else if (handler == null) {
79                 console.println("'" + args[0] + "' is not a Vizio thing id");
80                 printUsage(console);
81             } else {
82                 String host = (String) thing.getConfiguration().get(PROPERTY_HOST_NAME);
83                 BigDecimal port = (BigDecimal) thing.getConfiguration().get(PROPERTY_PORT);
84
85                 if (host == null || host.isEmpty() || port.signum() < 1) {
86                     console.println(
87                             "Error! Host Name and Port must be specified in thing configuration before paring.");
88                     return;
89                 }
90
91                 switch (args[1]) {
92                     case START_PAIRING:
93                         try {
94                             int pairingToken = handler.startPairing(args[2]);
95
96                             if (pairingToken != -1) {
97                                 console.println("Pairing has been started!");
98                                 console.println(
99                                         "Please note the 4 digit code displayed on the TV and substitute it into the following console command:");
100                                 console.println(
101                                         "openhab:vizio " + handler.getThing().getUID() + " " + SUBMIT_CODE + " <NNNN>");
102                             } else {
103                                 console.println("Unable to obtain pairing token!");
104                             }
105                         } catch (VizioException e) {
106                             console.println("Error! Unable to start pairing process.");
107                             console.println("Exception was: " + e.getMessage());
108                         }
109                         break;
110                     case SUBMIT_CODE:
111                         try {
112                             Integer.valueOf(args[2]);
113                             String authToken = handler.submitPairingCode(args[2]);
114
115                             if (authToken != EMPTY) {
116                                 console.println("Pairing complete!");
117                                 console.println("The auth token: " + authToken
118                                         + " was received and will be added to the thing configuration.");
119                                 console.println(
120                                         "If the thing is provisioned via a file, the token must be manually added to the thing configuration.");
121
122                                 handler.saveAuthToken(authToken);
123                             } else {
124                                 console.println("Unable to obtain auth token!");
125                             }
126                         } catch (NumberFormatException nfe) {
127                             console.println(
128                                     "Error! Pairing code must be numeric. Check console command and try again.");
129                         } catch (IllegalStateException ise) {
130                             console.println("Error! '" + START_PAIRING + "' command must be completed first.");
131                             console.println(
132                                     "Please issue the following command and substitute the desired device name.");
133                             console.println("openhab:vizio " + handler.getThing().getUID() + " " + START_PAIRING
134                                     + " <deviceName>");
135                         } catch (VizioException e) {
136                             console.println("Error! Unable to complete pairing process.");
137                             console.println("Exception was: " + e.getMessage());
138                         }
139                         break;
140                     default:
141                         printUsage(console);
142                         break;
143                 }
144             }
145         } else {
146             printUsage(console);
147         }
148     }
149
150     @Override
151     public List<String> getUsages() {
152         return List.of(new String[] {
153                 buildCommandUsage("<thingUID> " + START_PAIRING + " <deviceName>", "start pairing process"),
154                 buildCommandUsage("<thingUID> " + SUBMIT_CODE + " <pairingCode>", "submit pairing code") });
155     }
156 }