]> git.basschouten.com Git - openhab-addons.git/blob
d87a56eedc0e721181119bbab191924b38dec366
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mercedesme.internal;
14
15 import static org.openhab.binding.mercedesme.internal.Constants.*;
16
17 import java.util.Set;
18
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;
41
42 /**
43  * The {@link MercedesMeHandlerFactory} is responsible for creating things and thing
44  * handlers.
45  *
46  * @author Bernd Weymann - Initial contribution
47  */
48 @NonNullByDefault
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);
53
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;
61
62     @Activate
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;
68         mmcop = cop;
69         mmsop = sop;
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);
74         try {
75             httpClient.start();
76         } catch (Exception e) {
77             logger.warn("HTTP client not started: {} - no web access possible!", e.getLocalizedMessage());
78         }
79     }
80
81     @Override
82     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
83         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
84     }
85
86     @Override
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);
91         }
92         return new VehicleHandler(thing, httpClient, thingTypeUID.getId(), storageService, mmcop, mmsop,
93                 timeZoneProvider);
94     }
95
96     @Override
97     protected void deactivate(ComponentContext componentContext) {
98         super.deactivate(componentContext);
99         try {
100             httpClient.stop();
101         } catch (Exception e) {
102             logger.debug("HTTP client not stopped: {}", e.getLocalizedMessage());
103         }
104     }
105 }