2 * Copyright (c) 2010-2023 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.mercedesme.internal;
15 import static org.openhab.binding.mercedesme.internal.Constants.*;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.eclipse.jetty.client.WWWAuthenticationProtocolHandler;
23 import org.openhab.binding.mercedesme.internal.handler.AccountHandler;
24 import org.openhab.binding.mercedesme.internal.handler.VehicleHandler;
25 import org.openhab.core.auth.client.oauth2.OAuthFactory;
26 import org.openhab.core.i18n.TimeZoneProvider;
27 import org.openhab.core.io.net.http.HttpClientFactory;
28 import org.openhab.core.storage.StorageService;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerFactory;
35 import org.osgi.service.component.ComponentContext;
36 import org.osgi.service.component.annotations.Activate;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.Reference;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * The {@link MercedesMeHandlerFactory} is responsible for creating things and thing
46 * @author Bernd Weymann - Initial contribution
49 @Component(configurationPid = "binding.mercedesme", service = ThingHandlerFactory.class)
50 public class MercedesMeHandlerFactory extends BaseThingHandlerFactory {
51 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_BEV, THING_TYPE_COMB,
52 THING_TYPE_HYBRID, THING_TYPE_ACCOUNT);
54 private final Logger logger = LoggerFactory.getLogger(MercedesMeHandlerFactory.class);
55 private final OAuthFactory oAuthFactory;
56 private final HttpClient httpClient;
57 private final MercedesMeCommandOptionProvider mmcop;
58 private final MercedesMeStateOptionProvider mmsop;
59 private final StorageService storageService;
60 private final TimeZoneProvider timeZoneProvider;
63 public MercedesMeHandlerFactory(@Reference OAuthFactory oAuthFactory, @Reference HttpClientFactory hcf,
64 @Reference StorageService storageService, final @Reference MercedesMeCommandOptionProvider cop,
65 final @Reference MercedesMeStateOptionProvider sop, final @Reference TimeZoneProvider tzp) {
66 this.oAuthFactory = oAuthFactory;
67 this.storageService = storageService;
70 timeZoneProvider = tzp;
71 httpClient = hcf.createHttpClient(Constants.BINDING_ID);
72 // https://github.com/jetty-project/jetty-reactive-httpclient/issues/33
73 httpClient.getProtocolHandlers().remove(WWWAuthenticationProtocolHandler.NAME);
76 } catch (Exception e) {
77 logger.warn("HTTP client not started: {} - no web access possible!", e.getLocalizedMessage());
82 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
83 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
87 protected @Nullable ThingHandler createHandler(Thing thing) {
88 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
89 if (THING_TYPE_ACCOUNT.equals(thingTypeUID)) {
90 return new AccountHandler((Bridge) thing, httpClient, oAuthFactory);
92 return new VehicleHandler(thing, httpClient, thingTypeUID.getId(), storageService, mmcop, mmsop,
97 protected void deactivate(ComponentContext componentContext) {
98 super.deactivate(componentContext);
101 } catch (Exception e) {
102 logger.debug("HTTP client not stopped: {}", e.getLocalizedMessage());