]> git.basschouten.com Git - openhab-addons.git/blob
d7cf754cfb9fb015bb744cc8b885dc13c12e5f4b
[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.nest.internal;
14
15 import static java.util.stream.Collectors.toSet;
16 import static org.openhab.binding.nest.internal.NestBindingConstants.*;
17
18 import java.util.HashMap;
19 import java.util.Hashtable;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.stream.Stream;
23
24 import javax.ws.rs.client.ClientBuilder;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.nest.internal.discovery.NestDiscoveryService;
29 import org.openhab.binding.nest.internal.handler.NestBridgeHandler;
30 import org.openhab.binding.nest.internal.handler.NestCameraHandler;
31 import org.openhab.binding.nest.internal.handler.NestSmokeDetectorHandler;
32 import org.openhab.binding.nest.internal.handler.NestStructureHandler;
33 import org.openhab.binding.nest.internal.handler.NestThermostatHandler;
34 import org.openhab.core.config.discovery.DiscoveryService;
35 import org.openhab.core.thing.Bridge;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingTypeUID;
38 import org.openhab.core.thing.ThingUID;
39 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
40 import org.openhab.core.thing.binding.ThingHandler;
41 import org.openhab.core.thing.binding.ThingHandlerFactory;
42 import org.osgi.framework.ServiceRegistration;
43 import org.osgi.service.component.annotations.Activate;
44 import org.osgi.service.component.annotations.Component;
45 import org.osgi.service.component.annotations.Reference;
46 import org.osgi.service.jaxrs.client.SseEventSourceFactory;
47
48 /**
49  * The {@link NestHandlerFactory} is responsible for creating things and thing
50  * handlers. It also sets up the discovery service to track things from the bridge
51  * when the bridge is created.
52  *
53  * @author David Bennett - Initial contribution
54  */
55 @NonNullByDefault
56 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.nest")
57 public class NestHandlerFactory extends BaseThingHandlerFactory {
58     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream.of(THING_TYPE_THERMOSTAT,
59             THING_TYPE_CAMERA, THING_TYPE_BRIDGE, THING_TYPE_STRUCTURE, THING_TYPE_SMOKE_DETECTOR).collect(toSet());
60
61     private final ClientBuilder clientBuilder;
62     private final SseEventSourceFactory eventSourceFactory;
63     private final Map<ThingUID, ServiceRegistration<?>> discoveryService = new HashMap<>();
64
65     @Activate
66     public NestHandlerFactory(@Reference ClientBuilder clientBuilder,
67             @Reference SseEventSourceFactory eventSourceFactory) {
68         this.clientBuilder = clientBuilder;
69         this.eventSourceFactory = eventSourceFactory;
70     }
71
72     /**
73      * The things this factory supports creating.
74      */
75     @Override
76     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
77         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
78     }
79
80     /**
81      * Creates a handler for the specific thing. THis also creates the discovery service
82      * when the bridge is created.
83      */
84     @Override
85     protected @Nullable ThingHandler createHandler(Thing thing) {
86         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
87
88         if (THING_TYPE_THERMOSTAT.equals(thingTypeUID)) {
89             return new NestThermostatHandler(thing);
90         }
91
92         if (THING_TYPE_CAMERA.equals(thingTypeUID)) {
93             return new NestCameraHandler(thing);
94         }
95
96         if (THING_TYPE_STRUCTURE.equals(thingTypeUID)) {
97             return new NestStructureHandler(thing);
98         }
99
100         if (THING_TYPE_SMOKE_DETECTOR.equals(thingTypeUID)) {
101             return new NestSmokeDetectorHandler(thing);
102         }
103
104         if (THING_TYPE_BRIDGE.equals(thingTypeUID)) {
105             NestBridgeHandler handler = new NestBridgeHandler((Bridge) thing, clientBuilder, eventSourceFactory);
106             NestDiscoveryService service = new NestDiscoveryService(handler);
107             service.activate();
108             // Register the discovery service.
109             discoveryService.put(handler.getThing().getUID(),
110                     bundleContext.registerService(DiscoveryService.class.getName(), service, new Hashtable<>()));
111             return handler;
112         }
113
114         return null;
115     }
116
117     /**
118      * Removes the handler for the specific thing. This also handles disabling the discovery
119      * service when the bridge is removed.
120      */
121     @Override
122     protected void removeHandler(ThingHandler thingHandler) {
123         if (thingHandler instanceof NestBridgeHandler) {
124             ServiceRegistration<?> reg = discoveryService.get(thingHandler.getThing().getUID());
125             if (reg != null) {
126                 // Unregister the discovery service.
127                 NestDiscoveryService service = (NestDiscoveryService) bundleContext.getService(reg.getReference());
128                 service.deactivate();
129                 reg.unregister();
130                 discoveryService.remove(thingHandler.getThing().getUID());
131             }
132         }
133         super.removeHandler(thingHandler);
134     }
135 }