]> git.basschouten.com Git - openhab-addons.git/blob
f7ac2d6752dace4f0c58969ce55b26920d09b15f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.Activate;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Reference;
38
39 /**
40  * The {@link TadoHandlerFactory} is responsible for creating things and thing
41  * handlers.
42  *
43  * @author Dennis Frommknecht - Initial contribution
44  */
45 @Component(configurationPid = "binding.tado", service = ThingHandlerFactory.class)
46 public class TadoHandlerFactory extends BaseThingHandlerFactory {
47
48     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
49             .unmodifiableSet(new HashSet<>(Arrays.asList(THING_TYPE_HOME, THING_TYPE_ZONE, THING_TYPE_MOBILE_DEVICE)));
50
51     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
52
53     private final TadoStateDescriptionProvider stateDescriptionProvider;
54
55     @Activate
56     public TadoHandlerFactory(final @Reference TadoStateDescriptionProvider stateDescriptionProvider) {
57         this.stateDescriptionProvider = stateDescriptionProvider;
58     }
59
60     @Override
61     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
63     }
64
65     @Override
66     protected ThingHandler createHandler(Thing thing) {
67         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
68
69         if (thingTypeUID.equals(THING_TYPE_HOME)) {
70             TadoHomeHandler tadoHomeHandler = new TadoHomeHandler((Bridge) thing);
71             registerTadoDiscoveryService(tadoHomeHandler);
72             return tadoHomeHandler;
73         } else if (thingTypeUID.equals(THING_TYPE_ZONE)) {
74             return new TadoZoneHandler(thing, stateDescriptionProvider);
75         } else if (thingTypeUID.equals(THING_TYPE_MOBILE_DEVICE)) {
76             return new TadoMobileDeviceHandler(thing);
77         }
78
79         return null;
80     }
81
82     private synchronized void registerTadoDiscoveryService(TadoHomeHandler tadoHomeHandler) {
83         TadoDiscoveryService discoveryService = new TadoDiscoveryService(tadoHomeHandler);
84         ServiceRegistration<?> serviceRegistration = bundleContext.registerService(DiscoveryService.class.getName(),
85                 discoveryService, new Hashtable<>());
86         discoveryService.activate();
87         this.discoveryServiceRegs.put(tadoHomeHandler.getThing().getUID(), serviceRegistration);
88     }
89
90     @Override
91     protected synchronized void removeHandler(ThingHandler thingHandler) {
92         if (thingHandler instanceof TadoHomeHandler) {
93             ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
94             if (serviceReg != null) {
95                 TadoDiscoveryService service = (TadoDiscoveryService) bundleContext
96                         .getService(serviceReg.getReference());
97                 serviceReg.unregister();
98                 if (service != null) {
99                     service.deactivate();
100                 }
101             }
102         }
103     }
104 }