]> git.basschouten.com Git - openhab-addons.git/blob
2faf4e1f4c4e8cb49bdc4533603c41f371b29b9c
[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.generacmobilelink.internal.factory;
14
15 import static org.openhab.binding.generacmobilelink.internal.GeneracMobileLinkBindingConstants.*;
16
17 import java.util.Hashtable;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.concurrent.ConcurrentHashMap;
21
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;
40
41 /**
42  * The {@link GeneracMobileLinkHandlerFactory} is responsible for creating things and thing
43  * handlers.
44  *
45  * @author Dan Cunningham - Initial contribution
46  */
47 @NonNullByDefault
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;
54
55     @Activate
56     public GeneracMobileLinkHandlerFactory(final @Reference HttpClientFactory httpClientFactory) {
57         this.httpClientFactory = httpClientFactory;
58     }
59
60     @Override
61     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
63     }
64
65     @Override
66     protected @Nullable ThingHandler createHandler(Thing thing) {
67         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
68
69         if (THING_TYPE_GENERATOR.equals(thingTypeUID)) {
70             return new GeneracMobileLinkGeneratorHandler(thing);
71         }
72
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;
80         }
81
82         return null;
83     }
84
85     @Override
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();
91             }
92         }
93     }
94 }