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.enphase.internal;
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;
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;
42 * The {@link EnphaseHandlerFactory} is responsible for creating things and thing handlers.
44 * @author Hilbrand Bouwkamp - Initial contribution
47 @Component(configurationPid = "binding.enphase", service = ThingHandlerFactory.class)
48 public class EnphaseHandlerFactory extends BaseThingHandlerFactory {
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);
53 private final MessageTranslator messageTranslator;
54 private final EnvoyHostAddressCache envoyHostAddressCache;
55 private final HttpClient httpClient;
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));
70 private void startHttpClient() {
73 } catch (final Exception ex) {
74 throw new IllegalStateException("Could not start HttpClient.", ex);
79 public void deactivate() {
84 public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
85 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
89 protected @Nullable ThingHandler createHandler(final Thing thing) {
90 final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
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);