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.generacmobilelink.internal.factory;
15 import static org.openhab.binding.generacmobilelink.internal.GeneracMobileLinkBindingConstants.*;
17 import java.util.Hashtable;
20 import java.util.concurrent.ConcurrentHashMap;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.eclipse.jetty.client.HttpClient;
25 import org.openhab.binding.generacmobilelink.internal.discovery.GeneracMobileLinkDiscoveryService;
26 import org.openhab.binding.generacmobilelink.internal.handler.GeneracMobileLinkAccountHandler;
27 import org.openhab.binding.generacmobilelink.internal.handler.GeneracMobileLinkGeneratorHandler;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.io.net.http.HttpClientFactory;
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.ThingUID;
34 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerFactory;
37 import org.osgi.framework.ServiceRegistration;
38 import org.osgi.service.component.annotations.Activate;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Reference;
43 * The {@link GeneracMobileLinkHandlerFactory} is responsible for creating things and thing
46 * @author Dan Cunningham - Initial contribution
49 @Component(configurationPid = "binding.generacmobilelink", service = ThingHandlerFactory.class)
50 public class GeneracMobileLinkHandlerFactory extends BaseThingHandlerFactory {
51 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_ACCOUNT,
52 THING_TYPE_GENERATOR);
53 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new ConcurrentHashMap<>();
54 private final HttpClient httpClient;
57 public GeneracMobileLinkHandlerFactory(final @Reference HttpClientFactory httpClientFactory) {
58 this.httpClient = httpClientFactory.getCommonHttpClient();
62 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
63 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
67 protected @Nullable ThingHandler createHandler(Thing thing) {
68 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
70 if (THING_TYPE_GENERATOR.equals(thingTypeUID)) {
71 return new GeneracMobileLinkGeneratorHandler(thing);
74 if (THING_TYPE_ACCOUNT.equals(thingTypeUID)) {
75 GeneracMobileLinkDiscoveryService discoveryService = new GeneracMobileLinkDiscoveryService();
76 GeneracMobileLinkAccountHandler accountHandler = new GeneracMobileLinkAccountHandler((Bridge) thing,
77 httpClient, discoveryService);
78 discoveryServiceRegs.put(accountHandler.getThing().getUID(), bundleContext
79 .registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
80 return accountHandler;
87 protected synchronized void removeHandler(ThingHandler thingHandler) {
88 if (thingHandler instanceof GeneracMobileLinkAccountHandler) {
89 ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(thingHandler.getThing().getUID());
90 if (serviceReg != null) {
91 serviceReg.unregister();