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