]> git.basschouten.com Git - openhab-addons.git/blob
3d05c6f21a101ad5c01a6f596b3a34a42ed0425e
[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.*;
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.openhab.binding.enphase.internal.handler.EnphaseInverterHandler;
23 import org.openhab.binding.enphase.internal.handler.EnphaseRelayHandler;
24 import org.openhab.binding.enphase.internal.handler.EnvoyBridgeHandler;
25 import org.openhab.core.i18n.LocaleProvider;
26 import org.openhab.core.i18n.TranslationProvider;
27 import org.openhab.core.io.net.http.HttpClientFactory;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerFactory;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
37
38 /**
39  * The {@link EnphaseHandlerFactory} is responsible for creating things and thing
40  * handlers.
41  *
42  * @author Hilbrand Bouwkamp - Initial contribution
43  */
44 @NonNullByDefault
45 @Component(configurationPid = "binding.enphase", service = ThingHandlerFactory.class)
46 public class EnphaseHandlerFactory extends BaseThingHandlerFactory {
47
48     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_ENPHASE_ENVOY,
49             THING_TYPE_ENPHASE_INVERTER, THING_TYPE_ENPHASE_RELAY);
50
51     private final MessageTranslator messageTranslator;
52     private final HttpClient commonHttpClient;
53     private final EnvoyHostAddressCache envoyHostAddressCache;
54
55     @Activate
56     public EnphaseHandlerFactory(final @Reference LocaleProvider localeProvider,
57             final @Reference TranslationProvider i18nProvider, final @Reference HttpClientFactory httpClientFactory,
58             @Reference final EnvoyHostAddressCache envoyHostAddressCache) {
59         messageTranslator = new MessageTranslator(localeProvider, i18nProvider);
60         commonHttpClient = httpClientFactory.getCommonHttpClient();
61         this.envoyHostAddressCache = envoyHostAddressCache;
62     }
63
64     @Override
65     public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
66         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
67     }
68
69     @Override
70     protected @Nullable ThingHandler createHandler(final Thing thing) {
71         final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
72
73         if (THING_TYPE_ENPHASE_ENVOY.equals(thingTypeUID)) {
74             return new EnvoyBridgeHandler((Bridge) thing, commonHttpClient, envoyHostAddressCache);
75         } else if (THING_TYPE_ENPHASE_INVERTER.equals(thingTypeUID)) {
76             return new EnphaseInverterHandler(thing, messageTranslator);
77         } else if (THING_TYPE_ENPHASE_RELAY.equals(thingTypeUID)) {
78             return new EnphaseRelayHandler(thing, messageTranslator);
79         }
80
81         return null;
82     }
83 }