]> git.basschouten.com Git - openhab-addons.git/blob
3011b5c851661a8a3c8063997ce48dc228c22425
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.tado.internal.handler;
14
15 import static org.openhab.binding.tado.internal.TadoBindingConstants.*;
16
17 import java.util.Arrays;
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Hashtable;
22 import java.util.Map;
23 import java.util.Set;
24
25 import org.openhab.binding.tado.internal.discovery.TadoDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryService;
27 import org.openhab.core.thing.Bridge;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerFactory;
34 import org.osgi.framework.ServiceRegistration;
35 import org.osgi.service.component.annotations.Component;
36
37 /**
38  * The {@link TadoHandlerFactory} is responsible for creating things and thing
39  * handlers.
40  *
41  * @author Dennis Frommknecht - Initial contribution
42  */
43 @Component(configurationPid = "binding.tado", service = ThingHandlerFactory.class)
44 public class TadoHandlerFactory extends BaseThingHandlerFactory {
45
46     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
47             .unmodifiableSet(new HashSet<>(Arrays.asList(THING_TYPE_HOME, THING_TYPE_ZONE, THING_TYPE_MOBILE_DEVICE)));
48
49     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
50
51     @Override
52     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
53         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
54     }
55
56     @Override
57     protected ThingHandler createHandler(Thing thing) {
58         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
59
60         if (thingTypeUID.equals(THING_TYPE_HOME)) {
61             TadoHomeHandler tadoHomeHandler = new TadoHomeHandler((Bridge) thing);
62             registerTadoDiscoveryService(tadoHomeHandler);
63             return tadoHomeHandler;
64         } else if (thingTypeUID.equals(THING_TYPE_ZONE)) {
65             return new TadoZoneHandler(thing);
66         } else if (thingTypeUID.equals(THING_TYPE_MOBILE_DEVICE)) {
67             return new TadoMobileDeviceHandler(thing);
68         }
69
70         return null;
71     }
72
73     private synchronized void registerTadoDiscoveryService(TadoHomeHandler tadoHomeHandler) {
74         TadoDiscoveryService discoveryService = new TadoDiscoveryService(tadoHomeHandler);
75         ServiceRegistration<?> serviceRegistration = bundleContext.registerService(DiscoveryService.class.getName(),
76                 discoveryService, new Hashtable<>());
77         discoveryService.activate();
78         this.discoveryServiceRegs.put(tadoHomeHandler.getThing().getUID(), serviceRegistration);
79     }
80
81     @Override
82     protected synchronized void removeHandler(ThingHandler thingHandler) {
83         if (thingHandler instanceof TadoHomeHandler) {
84             ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
85             if (serviceReg != null) {
86                 TadoDiscoveryService service = (TadoDiscoveryService) bundleContext
87                         .getService(serviceReg.getReference());
88                 serviceReg.unregister();
89                 if (service != null) {
90                     service.deactivate();
91                 }
92             }
93         }
94     }
95 }