2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mielecloud.internal.config.servlet;
15 import java.util.concurrent.TimeUnit;
16 import java.util.function.BooleanSupplier;
18 import javax.servlet.http.HttpServletRequest;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants;
23 import org.openhab.binding.mielecloud.internal.config.exception.BridgeCreationFailedException;
24 import org.openhab.binding.mielecloud.internal.config.exception.BridgeReconfigurationFailedException;
25 import org.openhab.binding.mielecloud.internal.handler.MieleBridgeHandler;
26 import org.openhab.binding.mielecloud.internal.util.LocaleValidator;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.config.discovery.inbox.Inbox;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingRegistry;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Servlet that automatically creates a bridge and then redirects the browser to the account overview page.
41 * @author Björn Lange - Initial Contribution
44 public final class CreateBridgeServlet extends AbstractRedirectionServlet {
45 private static final String MIELE_CLOUD_BRIDGE_NAME = "Cloud Connector";
46 private static final String MIELE_CLOUD_BRIDGE_LABEL = "Miele@home Account";
48 private static final String LOCALE_PARAMETER_NAME = "locale";
49 public static final String BRIDGE_UID_PARAMETER_NAME = "bridgeUid";
50 public static final String EMAIL_PARAMETER_NAME = "email";
52 private static final long serialVersionUID = -2912042079128722887L;
54 private static final String DEFAULT_LOCALE = "en";
56 private static final long DISCOVERY_COMPLETION_TIMEOUT_IN_MILLISECONDS = 5000;
57 private static final long CHECK_INTERVAL_IN_MILLISECONDS = 100;
59 private final Logger logger = LoggerFactory.getLogger(CreateBridgeServlet.class);
61 private final Inbox inbox;
62 private final ThingRegistry thingRegistry;
64 private long onlineWaitTimeoutInMilliseconds = 5000;
67 * Creates a new {@link CreateBridgeServlet}.
69 * @param inbox openHAB inbox for discovery results.
70 * @param thingRegistry openHAB thing registry.
72 public CreateBridgeServlet(Inbox inbox, ThingRegistry thingRegistry) {
74 this.thingRegistry = thingRegistry;
77 public void setOnlineWaitTimeoutInMilliseconds(long onlineWaitTimeoutInMilliseconds) {
78 this.onlineWaitTimeoutInMilliseconds = onlineWaitTimeoutInMilliseconds;
82 protected String getRedirectionDestination(HttpServletRequest request) {
83 String bridgeUidString = request.getParameter(BRIDGE_UID_PARAMETER_NAME);
84 if (bridgeUidString == null || bridgeUidString.isEmpty()) {
85 logger.warn("Cannot create bridge: Bridge UID is missing.");
86 return "/mielecloud/failure?" + FailureServlet.MISSING_BRIDGE_UID_PARAMETER_NAME + "=true";
89 String email = request.getParameter(EMAIL_PARAMETER_NAME);
90 if (email == null || email.isEmpty()) {
91 logger.warn("Cannot create bridge: E-mail address is missing.");
92 return "/mielecloud/failure?" + FailureServlet.MISSING_EMAIL_PARAMETER_NAME + "=true";
95 ThingUID bridgeUid = null;
97 bridgeUid = new ThingUID(bridgeUidString);
98 } catch (IllegalArgumentException e) {
99 logger.warn("Cannot create bridge: Bridge UID '{}' is malformed.", bridgeUid);
100 return "/mielecloud/failure?" + FailureServlet.MALFORMED_BRIDGE_UID_PARAMETER_NAME + "=true";
103 String locale = getValidLocale(request.getParameter(LOCALE_PARAMETER_NAME));
105 logger.debug("Auto configuring Miele account using locale '{}' (requested locale was '{}')", locale,
106 request.getParameter(LOCALE_PARAMETER_NAME));
108 Thing bridge = pairOrReconfigureBridge(locale, bridgeUid, email);
109 waitForBridgeToComeOnline(bridge);
110 return "/mielecloud";
111 } catch (BridgeReconfigurationFailedException e) {
112 logger.warn("{}", e.getMessage());
113 return "/mielecloud/success?" + SuccessServlet.BRIDGE_RECONFIGURATION_FAILED_PARAMETER_NAME + "=true&"
114 + SuccessServlet.BRIDGE_UID_PARAMETER_NAME + "=" + bridgeUidString + "&"
115 + SuccessServlet.EMAIL_PARAMETER_NAME + "=" + email;
116 } catch (BridgeCreationFailedException e) {
117 logger.warn("Thing creation failed because there was no binding available that supports the thing.");
118 return "/mielecloud/success?" + SuccessServlet.BRIDGE_CREATION_FAILED_PARAMETER_NAME + "=true&"
119 + SuccessServlet.BRIDGE_UID_PARAMETER_NAME + "=" + bridgeUidString + "&"
120 + SuccessServlet.EMAIL_PARAMETER_NAME + "=" + email;
124 private Thing pairOrReconfigureBridge(String locale, ThingUID bridgeUid, String email) {
125 DiscoveryResult result = DiscoveryResultBuilder.create(bridgeUid)
126 .withRepresentationProperty(Thing.PROPERTY_MODEL_ID).withLabel(MIELE_CLOUD_BRIDGE_LABEL)
127 .withProperty(Thing.PROPERTY_MODEL_ID, MIELE_CLOUD_BRIDGE_NAME)
128 .withProperty(MieleCloudBindingConstants.CONFIG_PARAM_LOCALE, locale)
129 .withProperty(MieleCloudBindingConstants.CONFIG_PARAM_EMAIL, email).build();
130 if (inbox.add(result)) {
131 return pairBridge(bridgeUid);
133 return reconfigureBridge(bridgeUid);
137 private Thing pairBridge(ThingUID thingUid) {
138 Thing thing = inbox.approve(thingUid, MIELE_CLOUD_BRIDGE_LABEL, null);
140 throw new BridgeCreationFailedException();
143 logger.debug("Successfully created bridge {}", thingUid);
147 private Thing reconfigureBridge(ThingUID thingUid) {
148 logger.debug("Thing already exists. Modifying configuration.");
149 Thing thing = thingRegistry.get(thingUid);
151 throw new BridgeReconfigurationFailedException(
152 "Cannot modify non existing bridge: Could neither add bridge via inbox nor find existing bridge.");
155 ThingHandler handler = thing.getHandler();
156 if (handler == null) {
157 throw new BridgeReconfigurationFailedException("Bridge exists but has no handler.");
159 if (!(handler instanceof MieleBridgeHandler)) {
160 throw new BridgeReconfigurationFailedException("Bridge handler is of wrong type, expected '"
161 + MieleBridgeHandler.class.getSimpleName() + "' but got '" + handler.getClass().getName() + "'.");
164 MieleBridgeHandler bridgeHandler = (MieleBridgeHandler) handler;
165 bridgeHandler.disposeWebservice();
166 bridgeHandler.initializeWebservice();
171 private String getValidLocale(@Nullable String localeParameterValue) {
172 if (localeParameterValue == null || localeParameterValue.isEmpty()
173 || !LocaleValidator.isValidLanguage(localeParameterValue)) {
174 return DEFAULT_LOCALE;
176 return localeParameterValue;
180 private void waitForBridgeToComeOnline(Thing bridge) {
182 waitForConditionWithTimeout(() -> bridge.getStatus() == ThingStatus.ONLINE,
183 onlineWaitTimeoutInMilliseconds);
184 waitForConditionWithTimeout(new DiscoveryResultCountDoesNotChangeCondition(),
185 DISCOVERY_COMPLETION_TIMEOUT_IN_MILLISECONDS);
186 } catch (InterruptedException e) {
187 Thread.currentThread().interrupt();
191 private void waitForConditionWithTimeout(BooleanSupplier condition, long timeoutInMilliseconds)
192 throws InterruptedException {
193 long remainingWaitTime = timeoutInMilliseconds;
194 while (!condition.getAsBoolean() && remainingWaitTime > 0) {
195 TimeUnit.MILLISECONDS.sleep(CHECK_INTERVAL_IN_MILLISECONDS);
196 remainingWaitTime -= CHECK_INTERVAL_IN_MILLISECONDS;
200 private class DiscoveryResultCountDoesNotChangeCondition implements BooleanSupplier {
201 private long previousDiscoveryResultCount = 0;
204 public boolean getAsBoolean() {
205 var discoveryResultCount = countOwnDiscoveryResults();
206 var discoveryResultCountUnchanged = previousDiscoveryResultCount == discoveryResultCount;
207 previousDiscoveryResultCount = discoveryResultCount;
208 return discoveryResultCountUnchanged;
211 private long countOwnDiscoveryResults() {
212 return inbox.stream().map(DiscoveryResult::getBindingId)
213 .filter(MieleCloudBindingConstants.BINDING_ID::equals).count();