Refactor pairing process to use handler httpClient (#14640)

Signed-off-by: Michael Lobstein <michael.lobstein@gmail.com>
This commit is contained in:
mlobstein 2023-03-20 01:54:31 -05:00 committed by GitHub
parent 6444964bf6
commit dae33c405e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 56 deletions

View 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());

View 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 int startPairing(String deviceName) throws VizioException {
Random rng = new Random();
pairingDeviceId = rng.nextInt(100000);
pairingToken = communicator.startPairing(deviceName, pairingDeviceId).getItem().getPairingReqToken();
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 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());
}
public int getPairingDeviceId() {
return pairingDeviceId;
}
public void setPairingDeviceId(int pairingDeviceId) {
this.pairingDeviceId = pairingDeviceId;
}
public int getPairingToken() {
return pairingToken;
}
public void setPairingToken(int pairingToken) {
this.pairingToken = pairingToken;
}
}