]> git.basschouten.com Git - openhab-addons.git/blob
adf7146b0375251af2afaecd054975665f20f33d
[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.sensibo.internal;
14
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Hashtable;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.eclipse.jetty.client.HttpClient;
26 import org.openhab.binding.sensibo.internal.discovery.SensiboDiscoveryService;
27 import org.openhab.binding.sensibo.internal.handler.SensiboAccountHandler;
28 import org.openhab.binding.sensibo.internal.handler.SensiboSkyHandler;
29 import org.openhab.core.config.discovery.DiscoveryService;
30 import org.openhab.core.io.net.http.HttpClientFactory;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.openhab.core.thing.binding.ThingHandlerFactory;
38 import org.osgi.framework.ServiceRegistration;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Reference;
42
43 /**
44  * The {@link SensiboHandlerFactory} is responsible for creating things and thing
45  * handlers.
46  *
47  * @author Arne Seime - Initial contribution
48  */
49 @NonNullByDefault
50 @Component(configurationPid = "binding.sensibo", service = ThingHandlerFactory.class)
51 public class SensiboHandlerFactory extends BaseThingHandlerFactory {
52     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.unmodifiableSet(
53             Stream.of(SensiboBindingConstants.THING_TYPE_ACCOUNT, SensiboBindingConstants.THING_TYPE_SENSIBOSKY)
54                     .collect(Collectors.toSet()));
55     private final HttpClient httpClient;
56     private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
57
58     @Activate
59     public SensiboHandlerFactory(@Reference HttpClientFactory httpClientFactory) {
60         this.httpClient = httpClientFactory.getCommonHttpClient();
61     }
62
63     @Override
64     protected @Nullable ThingHandler createHandler(final Thing thing) {
65         final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
66         if (SensiboBindingConstants.THING_TYPE_SENSIBOSKY.equals(thingTypeUID)) {
67             return new SensiboSkyHandler(thing);
68         } else if (SensiboBindingConstants.THING_TYPE_ACCOUNT.equals(thingTypeUID)) {
69             final SensiboAccountHandler handler = new SensiboAccountHandler((Bridge) thing, httpClient);
70             registerDeviceDiscoveryService(handler);
71             return handler;
72         }
73         return null;
74     }
75
76     private void registerDeviceDiscoveryService(SensiboAccountHandler bridgeHandler) {
77         SensiboDiscoveryService discoveryService = new SensiboDiscoveryService(bridgeHandler);
78         discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
79                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
80     }
81
82     private void unregisterDeviceDiscoveryService(ThingUID thingUID) {
83         if (discoveryServiceRegs.containsKey(thingUID)) {
84             ServiceRegistration<?> serviceReg = discoveryServiceRegs.get(thingUID);
85             serviceReg.unregister();
86             discoveryServiceRegs.remove(thingUID);
87         }
88     }
89
90     @Override
91     protected void removeHandler(ThingHandler thingHandler) {
92         if (thingHandler instanceof SensiboAccountHandler) {
93             ThingUID thingUID = thingHandler.getThing().getUID();
94             unregisterDeviceDiscoveryService(thingUID);
95         }
96         super.removeHandler(thingHandler);
97     }
98
99     @Override
100     public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
101         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
102     }
103 }