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.openhab.binding.generacmobilelink.internal.discovery.GeneracMobileLinkDiscoveryService;
25 import org.openhab.binding.generacmobilelink.internal.handler.GeneracMobileLinkAccountHandler;
26 import org.openhab.binding.generacmobilelink.internal.handler.GeneracMobileLinkGeneratorHandler;
27 import org.openhab.core.config.discovery.DiscoveryService;
28 import org.openhab.core.io.net.http.HttpClientFactory;
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.ThingUID;
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.framework.ServiceRegistration;
37 import org.osgi.service.component.annotations.Activate;
38 import org.osgi.service.component.annotations.Component;
39 import org.osgi.service.component.annotations.Reference;
42 * The {@link GeneracMobileLinkHandlerFactory} is responsible for creating things and thing
45 * @author Dan Cunningham - Initial contribution
48 @Component(configurationPid = "binding.generacmobilelink", service = ThingHandlerFactory.class)
49 public class GeneracMobileLinkHandlerFactory extends BaseThingHandlerFactory {
50 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_ACCOUNT,
51 THING_TYPE_GENERATOR);
52 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new ConcurrentHashMap<>();
53 private final HttpClientFactory httpClientFactory;
56 public GeneracMobileLinkHandlerFactory(final @Reference HttpClientFactory httpClientFactory) {
57 this.httpClientFactory = httpClientFactory;
61 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
66 protected @Nullable ThingHandler createHandler(Thing thing) {
67 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
69 if (THING_TYPE_GENERATOR.equals(thingTypeUID)) {
70 return new GeneracMobileLinkGeneratorHandler(thing);
73 if (THING_TYPE_ACCOUNT.equals(thingTypeUID)) {
74 GeneracMobileLinkDiscoveryService discoveryService = new GeneracMobileLinkDiscoveryService();
75 GeneracMobileLinkAccountHandler accountHandler = new GeneracMobileLinkAccountHandler((Bridge) thing,
76 httpClientFactory, discoveryService);
77 discoveryServiceRegs.put(accountHandler.getThing().getUID(), bundleContext
78 .registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
79 return accountHandler;
86 protected synchronized void removeHandler(ThingHandler thingHandler) {
87 if (thingHandler instanceof GeneracMobileLinkAccountHandler) {
88 ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(thingHandler.getThing().getUID());
89 if (serviceReg != null) {
90 serviceReg.unregister();