]> git.basschouten.com Git - openhab-addons.git/commitdiff
Refactor pairing process to use handler httpClient (#14640)
authormlobstein <michael.lobstein@gmail.com>
Mon, 20 Mar 2023 06:54:31 +0000 (01:54 -0500)
committerGitHub <noreply@github.com>
Mon, 20 Mar 2023 06:54:31 +0000 (07:54 +0100)
Signed-off-by: Michael Lobstein <michael.lobstein@gmail.com>
bundles/org.openhab.binding.vizio/src/main/java/org/openhab/binding/vizio/internal/console/VizioCommandExtension.java
bundles/org.openhab.binding.vizio/src/main/java/org/openhab/binding/vizio/internal/handler/VizioHandler.java

index 5a55582b3ad6ba436771a766ff0763405c347b2b..f9d0b60ac319815320efa3695c97588d3ea4ed94 100644 (file)
@@ -16,18 +16,13 @@ import static org.openhab.binding.vizio.internal.VizioBindingConstants.*;
 
 import java.math.BigDecimal;
 import java.util.List;
-import java.util.Random;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
-import org.eclipse.jetty.client.HttpClient;
 import org.openhab.binding.vizio.internal.VizioException;
-import org.openhab.binding.vizio.internal.communication.VizioCommunicator;
-import org.openhab.binding.vizio.internal.dto.pairing.PairingComplete;
 import org.openhab.binding.vizio.internal.handler.VizioHandler;
 import org.openhab.core.io.console.Console;
 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
-import org.openhab.core.io.net.http.HttpClientFactory;
 import org.openhab.core.thing.Thing;
 import org.openhab.core.thing.ThingRegistry;
 import org.openhab.core.thing.ThingUID;
@@ -49,14 +44,11 @@ public class VizioCommandExtension extends AbstractConsoleCommandExtension {
     private static final String SUBMIT_CODE = "submit_code";
 
     private final ThingRegistry thingRegistry;
-    private final HttpClient httpClient;
 
     @Activate
-    public VizioCommandExtension(final @Reference ThingRegistry thingRegistry,
-            final @Reference HttpClientFactory httpClientFactory) {
+    public VizioCommandExtension(final @Reference ThingRegistry thingRegistry) {
         super("vizio", "Interact with the Vizio binding to get an authentication token from the TV.");
         this.thingRegistry = thingRegistry;
-        this.httpClient = httpClientFactory.getCommonHttpClient();
     }
 
     @Override
@@ -94,25 +86,14 @@ public class VizioCommandExtension extends AbstractConsoleCommandExtension {
                     console.println(
                             "Error! Host Name and Port must be specified in thing configuration before paring.");
                     return;
-                } else if (host.contains(":")) {
-                    // format for ipv6
-                    host = "[" + host + "]";
                 }
 
-                VizioCommunicator communicator = new VizioCommunicator(httpClient, host, port.intValue(), EMPTY);
-
                 switch (args[1]) {
                     case START_PAIRING:
                         try {
-                            Random rng = new Random();
+                            int pairingToken = handler.startPairing(args[2]);
 
-                            int pairingDeviceId = rng.nextInt(100000);
-                            int pairingToken = communicator.startPairing(args[2], pairingDeviceId).getItem()
-                                    .getPairingReqToken();
                             if (pairingToken != -1) {
-                                handler.setPairingDeviceId(pairingDeviceId);
-                                handler.setPairingToken(pairingToken);
-
                                 console.println("Pairing has been started!");
                                 console.println(
                                         "Please note the 4 digit code displayed on the TV and substitute it into the following console command:");
@@ -128,37 +109,29 @@ public class VizioCommandExtension extends AbstractConsoleCommandExtension {
                         break;
                     case SUBMIT_CODE:
                         try {
-                            int pairingDeviceId = handler.getPairingDeviceId();
-                            int pairingToken = handler.getPairingToken();
-
-                            if (pairingDeviceId < 0 || pairingToken < 0) {
-                                console.println("Error! '" + START_PAIRING + "' command must be completed first.");
-                                console.println(
-                                        "Please issue the following command and substitute the desired device name.");
-                                console.println("openhab:vizio " + handler.getThing().getUID() + " " + START_PAIRING
-                                        + " <deviceName>");
-                                break;
-                            }
-
                             Integer.valueOf(args[2]);
-                            PairingComplete authTokenResp = communicator.submitPairingCode(pairingDeviceId, args[2],
-                                    pairingToken);
-                            if (authTokenResp.getItem().getAuthToken() != EMPTY) {
+                            String authToken = handler.submitPairingCode(args[2]);
+
+                            if (authToken != EMPTY) {
                                 console.println("Pairing complete!");
-                                console.println("The auth token: " + authTokenResp.getItem().getAuthToken()
+                                console.println("The auth token: " + authToken
                                         + " was received and will be added to the thing configuration.");
                                 console.println(
                                         "If the thing is provisioned via a file, the token must be manually added to the thing configuration.");
 
-                                handler.setPairingDeviceId(-1);
-                                handler.setPairingToken(-1);
-                                handler.saveAuthToken(authTokenResp.getItem().getAuthToken());
+                                handler.saveAuthToken(authToken);
                             } else {
                                 console.println("Unable to obtain auth token!");
                             }
                         } catch (NumberFormatException nfe) {
                             console.println(
                                     "Error! Pairing code must be numeric. Check console command and try again.");
+                        } catch (IllegalStateException ise) {
+                            console.println("Error! '" + START_PAIRING + "' command must be completed first.");
+                            console.println(
+                                    "Please issue the following command and substitute the desired device name.");
+                            console.println("openhab:vizio " + handler.getThing().getUID() + " " + START_PAIRING
+                                    + " <deviceName>");
                         } catch (VizioException e) {
                             console.println("Error! Unable to complete pairing process.");
                             console.println("Exception was: " + e.getMessage());
index 1bb0975b41c08ea040fb11749263854be2534fe5..18b2856de35d22da1486a988d682bfd891306a3b 100644 (file)
@@ -19,6 +19,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
 import java.util.Optional;
+import java.util.Random;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 
@@ -569,27 +570,31 @@ public class VizioHandler extends BaseThingHandler {
     }
 
     // The remaining methods are used by the console when obtaining the auth token from the TV.
-    public void saveAuthToken(String authToken) {
-        // Store the auth token in the configuration and restart the thing
-        Configuration configuration = this.getConfig();
-        configuration.put(PROPERTY_AUTH_TOKEN, authToken);
-        this.updateConfiguration(configuration);
-        this.thingUpdated(this.getThing());
-    }
+    public int startPairing(String deviceName) throws VizioException {
+        Random rng = new Random();
+        pairingDeviceId = rng.nextInt(100000);
 
-    public int getPairingDeviceId() {
-        return pairingDeviceId;
-    }
+        pairingToken = communicator.startPairing(deviceName, pairingDeviceId).getItem().getPairingReqToken();
 
-    public void setPairingDeviceId(int pairingDeviceId) {
-        this.pairingDeviceId = pairingDeviceId;
+        return pairingToken;
     }
 
-    public int getPairingToken() {
-        return pairingToken;
+    public String submitPairingCode(String pairingCode) throws IllegalStateException, VizioException {
+        if (pairingDeviceId < 0 || pairingToken < 0) {
+            throw new IllegalStateException();
+        }
+
+        return communicator.submitPairingCode(pairingDeviceId, pairingCode, pairingToken).getItem().getAuthToken();
     }
 
-    public void setPairingToken(int pairingToken) {
-        this.pairingToken = pairingToken;
+    public void saveAuthToken(String authToken) {
+        pairingDeviceId = -1;
+        pairingToken = -1;
+
+        // Store the auth token in the configuration and restart the thing
+        Configuration configuration = this.getConfig();
+        configuration.put(PROPERTY_AUTH_TOKEN, authToken);
+        this.updateConfiguration(configuration);
+        this.thingUpdated(this.getThing());
     }
 }