|
@@ -40,6 +40,7 @@ import java.text.ParseException;
|
|
|
import java.time.Duration;
|
|
|
import java.time.Instant;
|
|
|
import java.util.Objects;
|
|
|
+import java.util.Optional;
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
import java.util.concurrent.CompletionException;
|
|
|
import java.util.concurrent.ExecutorService;
|
|
@@ -55,6 +56,7 @@ public class RegisterDeviceController implements FxController {
|
|
|
private final Stage window;
|
|
|
private final HubConfig hubConfig;
|
|
|
private final String bearerToken;
|
|
|
+ private final AtomicReference<Throwable> registerException;
|
|
|
private final Lazy<Scene> registerSuccessScene;
|
|
|
private final Lazy<Scene> registerFailedScene;
|
|
|
private final String deviceId;
|
|
@@ -62,7 +64,6 @@ public class RegisterDeviceController implements FxController {
|
|
|
private final CompletableFuture<ReceivedKey> result;
|
|
|
private final HttpClient httpClient;
|
|
|
|
|
|
- private final BooleanProperty deviceNameAlreadyExists = new SimpleBooleanProperty(false);
|
|
|
private final BooleanProperty invalidSetupCode = new SimpleBooleanProperty(false);
|
|
|
private final BooleanProperty workInProgress = new SimpleBooleanProperty(false);
|
|
|
public TextField setupCodeField;
|
|
@@ -70,13 +71,14 @@ public class RegisterDeviceController implements FxController {
|
|
|
public Button registerBtn;
|
|
|
|
|
|
@Inject
|
|
|
- public RegisterDeviceController(@KeyLoading Stage window, ExecutorService executor, HubConfig hubConfig, @Named("deviceId") String deviceId, DeviceKey deviceKey, CompletableFuture<ReceivedKey> result, @Named("bearerToken") AtomicReference<String> bearerToken, @FxmlScene(FxmlFile.HUB_REGISTER_SUCCESS) Lazy<Scene> registerSuccessScene, @FxmlScene(FxmlFile.HUB_REGISTER_FAILED) Lazy<Scene> registerFailedScene) {
|
|
|
+ public RegisterDeviceController(@KeyLoading Stage window, ExecutorService executor, HubConfig hubConfig, @Named("deviceId") String deviceId, DeviceKey deviceKey, CompletableFuture<ReceivedKey> result, @Named("bearerToken") AtomicReference<String> bearerToken, @Named("registerException") AtomicReference<Throwable> registerException, @FxmlScene(FxmlFile.HUB_REGISTER_SUCCESS) Lazy<Scene> registerSuccessScene, @FxmlScene(FxmlFile.HUB_REGISTER_FAILED) Lazy<Scene> registerFailedScene) {
|
|
|
this.window = window;
|
|
|
this.hubConfig = hubConfig;
|
|
|
this.deviceId = deviceId;
|
|
|
this.deviceKeyPair = Objects.requireNonNull(deviceKey.get());
|
|
|
this.result = result;
|
|
|
this.bearerToken = Objects.requireNonNull(bearerToken.get());
|
|
|
+ this.registerException = registerException;
|
|
|
this.registerSuccessScene = registerSuccessScene;
|
|
|
this.registerFailedScene = registerFailedScene;
|
|
|
this.window.addEventHandler(WindowEvent.WINDOW_HIDING, this::windowClosed);
|
|
@@ -85,7 +87,6 @@ public class RegisterDeviceController implements FxController {
|
|
|
|
|
|
public void initialize() {
|
|
|
deviceNameField.setText(determineHostname());
|
|
|
- deviceNameField.textProperty().addListener(observable -> deviceNameAlreadyExists.set(false));
|
|
|
deviceNameField.disableProperty().bind(workInProgress);
|
|
|
setupCodeField.textProperty().addListener(observable -> invalidSetupCode.set(false));
|
|
|
setupCodeField.disableProperty().bind(workInProgress);
|
|
@@ -146,7 +147,7 @@ public class RegisterDeviceController implements FxController {
|
|
|
return httpClient.sendAsync(putDeviceReq, HttpResponse.BodyHandlers.discarding());
|
|
|
}).whenCompleteAsync((response, throwable) -> {
|
|
|
if (response != null) {
|
|
|
- this.handleResponse(response);
|
|
|
+ this.handleRegisterDeviceResponse(response);
|
|
|
} else {
|
|
|
this.setupFailed(throwable);
|
|
|
}
|
|
@@ -170,12 +171,12 @@ public class RegisterDeviceController implements FxController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void handleResponse(HttpResponse<Void> response) {
|
|
|
+ private void handleRegisterDeviceResponse(HttpResponse<Void> response) {
|
|
|
if (response.statusCode() == 201) {
|
|
|
LOG.debug("Device registration for hub instance {} successful.", hubConfig.authSuccessUrl);
|
|
|
window.setScene(registerSuccessScene.get());
|
|
|
} else if (response.statusCode() == 409) {
|
|
|
- deviceNameAlreadyExists.set(true);
|
|
|
+ setupFailed(new DeviceAlreadyExistsException());
|
|
|
} else {
|
|
|
setupFailed(new IllegalStateException("Unexpected http status code " + response.statusCode()));
|
|
|
}
|
|
@@ -185,7 +186,12 @@ public class RegisterDeviceController implements FxController {
|
|
|
switch (cause) {
|
|
|
case CompletionException e when e.getCause() instanceof JWEHelper.InvalidJweKeyException -> invalidSetupCode.set(true);
|
|
|
default -> {
|
|
|
- LOG.warn("Device setup failed.", cause);
|
|
|
+ if(cause instanceof DeviceAlreadyExistsException) {
|
|
|
+ LOG.debug("Device already registered in hub instance {} for different user", hubConfig.authSuccessUrl);
|
|
|
+ } else {
|
|
|
+ LOG.warn("Device setup failed.", cause);
|
|
|
+ }
|
|
|
+ registerException.set(cause);
|
|
|
window.setScene(registerFailedScene.get());
|
|
|
result.completeExceptionally(cause);
|
|
|
}
|
|
@@ -202,15 +208,6 @@ public class RegisterDeviceController implements FxController {
|
|
|
}
|
|
|
|
|
|
//--- Getters & Setters
|
|
|
-
|
|
|
- public BooleanProperty deviceNameAlreadyExistsProperty() {
|
|
|
- return deviceNameAlreadyExists;
|
|
|
- }
|
|
|
-
|
|
|
- public boolean getDeviceNameAlreadyExists() {
|
|
|
- return deviceNameAlreadyExists.get();
|
|
|
- }
|
|
|
-
|
|
|
public BooleanProperty invalidSetupCodeProperty() {
|
|
|
return invalidSetupCode;
|
|
|
}
|