Refactor pairing process to use handler httpClient (#14640)
Signed-off-by: Michael Lobstein <michael.lobstein@gmail.com>pull/14645/head
parent
6444964bf6
commit
dae33c405e
|
@ -16,18 +16,13 @@ import static org.openhab.binding.vizio.internal.VizioBindingConstants.*;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
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.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.binding.vizio.internal.handler.VizioHandler;
|
||||||
import org.openhab.core.io.console.Console;
|
import org.openhab.core.io.console.Console;
|
||||||
import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
|
import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
|
||||||
import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
|
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.Thing;
|
||||||
import org.openhab.core.thing.ThingRegistry;
|
import org.openhab.core.thing.ThingRegistry;
|
||||||
import org.openhab.core.thing.ThingUID;
|
import org.openhab.core.thing.ThingUID;
|
||||||
|
@ -49,14 +44,11 @@ public class VizioCommandExtension extends AbstractConsoleCommandExtension {
|
||||||
private static final String SUBMIT_CODE = "submit_code";
|
private static final String SUBMIT_CODE = "submit_code";
|
||||||
|
|
||||||
private final ThingRegistry thingRegistry;
|
private final ThingRegistry thingRegistry;
|
||||||
private final HttpClient httpClient;
|
|
||||||
|
|
||||||
@Activate
|
@Activate
|
||||||
public VizioCommandExtension(final @Reference ThingRegistry thingRegistry,
|
public VizioCommandExtension(final @Reference ThingRegistry thingRegistry) {
|
||||||
final @Reference HttpClientFactory httpClientFactory) {
|
|
||||||
super("vizio", "Interact with the Vizio binding to get an authentication token from the TV.");
|
super("vizio", "Interact with the Vizio binding to get an authentication token from the TV.");
|
||||||
this.thingRegistry = thingRegistry;
|
this.thingRegistry = thingRegistry;
|
||||||
this.httpClient = httpClientFactory.getCommonHttpClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -94,25 +86,14 @@ public class VizioCommandExtension extends AbstractConsoleCommandExtension {
|
||||||
console.println(
|
console.println(
|
||||||
"Error! Host Name and Port must be specified in thing configuration before paring.");
|
"Error! Host Name and Port must be specified in thing configuration before paring.");
|
||||||
return;
|
return;
|
||||||
} else if (host.contains(":")) {
|
|
||||||
// format for ipv6
|
|
||||||
host = "[" + host + "]";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
VizioCommunicator communicator = new VizioCommunicator(httpClient, host, port.intValue(), EMPTY);
|
|
||||||
|
|
||||||
switch (args[1]) {
|
switch (args[1]) {
|
||||||
case START_PAIRING:
|
case START_PAIRING:
|
||||||
try {
|
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) {
|
if (pairingToken != -1) {
|
||||||
handler.setPairingDeviceId(pairingDeviceId);
|
|
||||||
handler.setPairingToken(pairingToken);
|
|
||||||
|
|
||||||
console.println("Pairing has been started!");
|
console.println("Pairing has been started!");
|
||||||
console.println(
|
console.println(
|
||||||
"Please note the 4 digit code displayed on the TV and substitute it into the following console command:");
|
"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;
|
break;
|
||||||
case SUBMIT_CODE:
|
case SUBMIT_CODE:
|
||||||
try {
|
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]);
|
Integer.valueOf(args[2]);
|
||||||
PairingComplete authTokenResp = communicator.submitPairingCode(pairingDeviceId, args[2],
|
String authToken = handler.submitPairingCode(args[2]);
|
||||||
pairingToken);
|
|
||||||
if (authTokenResp.getItem().getAuthToken() != EMPTY) {
|
if (authToken != EMPTY) {
|
||||||
console.println("Pairing complete!");
|
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.");
|
+ " was received and will be added to the thing configuration.");
|
||||||
console.println(
|
console.println(
|
||||||
"If the thing is provisioned via a file, the token must be manually added to the thing configuration.");
|
"If the thing is provisioned via a file, the token must be manually added to the thing configuration.");
|
||||||
|
|
||||||
handler.setPairingDeviceId(-1);
|
handler.saveAuthToken(authToken);
|
||||||
handler.setPairingToken(-1);
|
|
||||||
handler.saveAuthToken(authTokenResp.getItem().getAuthToken());
|
|
||||||
} else {
|
} else {
|
||||||
console.println("Unable to obtain auth token!");
|
console.println("Unable to obtain auth token!");
|
||||||
}
|
}
|
||||||
} catch (NumberFormatException nfe) {
|
} catch (NumberFormatException nfe) {
|
||||||
console.println(
|
console.println(
|
||||||
"Error! Pairing code must be numeric. Check console command and try again.");
|
"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) {
|
} catch (VizioException e) {
|
||||||
console.println("Error! Unable to complete pairing process.");
|
console.println("Error! Unable to complete pairing process.");
|
||||||
console.println("Exception was: " + e.getMessage());
|
console.println("Exception was: " + e.getMessage());
|
||||||
|
|
|
@ -19,6 +19,7 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.Random;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
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.
|
// 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) {
|
public void saveAuthToken(String authToken) {
|
||||||
|
pairingDeviceId = -1;
|
||||||
|
pairingToken = -1;
|
||||||
|
|
||||||
// Store the auth token in the configuration and restart the thing
|
// Store the auth token in the configuration and restart the thing
|
||||||
Configuration configuration = this.getConfig();
|
Configuration configuration = this.getConfig();
|
||||||
configuration.put(PROPERTY_AUTH_TOKEN, authToken);
|
configuration.put(PROPERTY_AUTH_TOKEN, authToken);
|
||||||
this.updateConfiguration(configuration);
|
this.updateConfiguration(configuration);
|
||||||
this.thingUpdated(this.getThing());
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue