]> git.basschouten.com Git - openhab-addons.git/blob
dc6f8f3a495f9e18fb8bf6d14e3d2b6486d35d68
[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.enphase.internal;
14
15 import static org.openhab.binding.enphase.internal.EnphaseBindingConstants.THING_TYPE_ENPHASE_ENVOY;
16 import static org.openhab.binding.enphase.internal.EnphaseBindingConstants.THING_TYPE_ENPHASE_INVERTER;
17 import static org.openhab.binding.enphase.internal.EnphaseBindingConstants.THING_TYPE_ENPHASE_RELAY;
18
19 import java.util.Set;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.eclipse.jetty.util.ssl.SslContextFactory;
25 import org.openhab.binding.enphase.internal.handler.EnphaseInverterHandler;
26 import org.openhab.binding.enphase.internal.handler.EnphaseRelayHandler;
27 import org.openhab.binding.enphase.internal.handler.EnvoyBridgeHandler;
28 import org.openhab.core.i18n.LocaleProvider;
29 import org.openhab.core.i18n.TranslationProvider;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerFactory;
36 import org.osgi.service.component.annotations.Activate;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.Deactivate;
39 import org.osgi.service.component.annotations.Reference;
40
41 /**
42  * The {@link EnphaseHandlerFactory} is responsible for creating things and thing handlers.
43  *
44  * @author Hilbrand Bouwkamp - Initial contribution
45  */
46 @NonNullByDefault
47 @Component(configurationPid = "binding.enphase", service = ThingHandlerFactory.class)
48 public class EnphaseHandlerFactory extends BaseThingHandlerFactory {
49
50     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_ENPHASE_ENVOY,
51             THING_TYPE_ENPHASE_INVERTER, THING_TYPE_ENPHASE_RELAY);
52
53     private final MessageTranslator messageTranslator;
54     private final EnvoyHostAddressCache envoyHostAddressCache;
55     private final HttpClient httpClient;
56
57     @Activate
58     public EnphaseHandlerFactory(final @Reference LocaleProvider localeProvider,
59             final @Reference TranslationProvider i18nProvider,
60             final @Reference EnvoyHostAddressCache envoyHostAddressCache) {
61         messageTranslator = new MessageTranslator(localeProvider, i18nProvider);
62         this.envoyHostAddressCache = envoyHostAddressCache;
63         // Note: Had to switch to using a locally generated httpClient as
64         // the Envoy server went to a self-signed SSL connection and this
65         // was the only way to set the client to ignore SSL errors
66         this.httpClient = new HttpClient(new SslContextFactory.Client(true));
67         startHttpClient();
68     }
69
70     private void startHttpClient() {
71         try {
72             httpClient.start();
73         } catch (final Exception ex) {
74             throw new IllegalStateException("Could not start HttpClient.", ex);
75         }
76     }
77
78     @Deactivate
79     public void deactivate() {
80         httpClient.destroy();
81     }
82
83     @Override
84     public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
85         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
86     }
87
88     @Override
89     protected @Nullable ThingHandler createHandler(final Thing thing) {
90         final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
91
92         if (THING_TYPE_ENPHASE_ENVOY.equals(thingTypeUID)) {
93             return new EnvoyBridgeHandler((Bridge) thing, httpClient, envoyHostAddressCache);
94         } else if (THING_TYPE_ENPHASE_INVERTER.equals(thingTypeUID)) {
95             return new EnphaseInverterHandler(thing, messageTranslator);
96         } else if (THING_TYPE_ENPHASE_RELAY.equals(thingTypeUID)) {
97             return new EnphaseRelayHandler(thing, messageTranslator);
98         }
99
100         return null;
101     }
102 }