]> git.basschouten.com Git - openhab-addons.git/blob
e14371827da2783ee7eb029b7392f02d4a8aaa39
[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.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;
41
42 /**
43  * The {@link GeneracMobileLinkHandlerFactory} is responsible for creating things and thing
44  * handlers.
45  *
46  * @author Dan Cunningham - Initial contribution
47  */
48 @NonNullByDefault
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;
55
56     @Activate
57     public GeneracMobileLinkHandlerFactory(final @Reference HttpClientFactory httpClientFactory) {
58         this.httpClient = httpClientFactory.getCommonHttpClient();
59     }
60
61     @Override
62     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
63         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
64     }
65
66     @Override
67     protected @Nullable ThingHandler createHandler(Thing thing) {
68         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
69
70         if (THING_TYPE_GENERATOR.equals(thingTypeUID)) {
71             return new GeneracMobileLinkGeneratorHandler(thing);
72         }
73
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;
81         }
82
83         return null;
84     }
85
86     @Override
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();
92             }
93         }
94     }
95 }