]> git.basschouten.com Git - openhab-addons.git/blob
cc6940c33805eb8b9722cc6535b427a193886559
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.hue.internal;
14
15 import static org.openhab.binding.hue.internal.HueBindingConstants.*;
16
17 import java.util.Collections;
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.Collectors;
23 import java.util.stream.Stream;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.hue.internal.discovery.HueLightDiscoveryService;
28 import org.openhab.binding.hue.internal.handler.HueBridgeHandler;
29 import org.openhab.binding.hue.internal.handler.HueGroupHandler;
30 import org.openhab.binding.hue.internal.handler.HueLightHandler;
31 import org.openhab.binding.hue.internal.handler.HueStateDescriptionOptionProvider;
32 import org.openhab.binding.hue.internal.handler.sensors.ClipHandler;
33 import org.openhab.binding.hue.internal.handler.sensors.DimmerSwitchHandler;
34 import org.openhab.binding.hue.internal.handler.sensors.LightLevelHandler;
35 import org.openhab.binding.hue.internal.handler.sensors.PresenceHandler;
36 import org.openhab.binding.hue.internal.handler.sensors.TapSwitchHandler;
37 import org.openhab.binding.hue.internal.handler.sensors.TemperatureHandler;
38 import org.openhab.core.config.core.Configuration;
39 import org.openhab.core.config.discovery.DiscoveryService;
40 import org.openhab.core.thing.Bridge;
41 import org.openhab.core.thing.Thing;
42 import org.openhab.core.thing.ThingTypeUID;
43 import org.openhab.core.thing.ThingUID;
44 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
45 import org.openhab.core.thing.binding.ThingHandler;
46 import org.openhab.core.thing.binding.ThingHandlerFactory;
47 import org.osgi.framework.ServiceRegistration;
48 import org.osgi.service.component.annotations.Activate;
49 import org.osgi.service.component.annotations.Component;
50 import org.osgi.service.component.annotations.Reference;
51
52 /**
53  * {@link HueThingHandlerFactory} is a factory for {@link HueBridgeHandler}s.
54  *
55  * @author Dennis Nobel - Initial contribution of hue binding
56  * @author Kai Kreuzer - added supportsThingType method
57  * @author Andre Fuechsel - implemented to use one discovery service per bridge
58  * @author Samuel Leisering - Added support for sensor API
59  * @author Christoph Weitkamp - Added support for sensor API
60  * @author Laurent Garnier - Added support for groups
61  */
62 @NonNullByDefault
63 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.hue")
64 public class HueThingHandlerFactory extends BaseThingHandlerFactory {
65
66     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.unmodifiableSet(
67             Stream.of(HueBridgeHandler.SUPPORTED_THING_TYPES.stream(), HueLightHandler.SUPPORTED_THING_TYPES.stream(),
68                     DimmerSwitchHandler.SUPPORTED_THING_TYPES.stream(), TapSwitchHandler.SUPPORTED_THING_TYPES.stream(),
69                     PresenceHandler.SUPPORTED_THING_TYPES.stream(), TemperatureHandler.SUPPORTED_THING_TYPES.stream(),
70                     LightLevelHandler.SUPPORTED_THING_TYPES.stream(), ClipHandler.SUPPORTED_THING_TYPES.stream(),
71                     HueGroupHandler.SUPPORTED_THING_TYPES.stream()).flatMap(i -> i).collect(Collectors.toSet()));
72
73     private final HueStateDescriptionOptionProvider stateOptionProvider;
74
75     private final Map<ThingUID, @Nullable ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
76
77     @Activate
78     public HueThingHandlerFactory(final @Reference HueStateDescriptionOptionProvider stateOptionProvider) {
79         this.stateOptionProvider = stateOptionProvider;
80     }
81
82     @Override
83     public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
84             @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
85         if (HueBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
86             return super.createThing(thingTypeUID, configuration, thingUID, null);
87         } else if (HueLightHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
88             ThingUID hueLightUID = getLightUID(thingTypeUID, thingUID, configuration, bridgeUID);
89             return super.createThing(thingTypeUID, configuration, hueLightUID, bridgeUID);
90         } else if (DimmerSwitchHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
91                 || TapSwitchHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
92                 || PresenceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
93                 || TemperatureHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
94                 || LightLevelHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
95                 || ClipHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
96             ThingUID hueSensorUID = getSensorUID(thingTypeUID, thingUID, configuration, bridgeUID);
97             return super.createThing(thingTypeUID, configuration, hueSensorUID, bridgeUID);
98         } else if (HueGroupHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
99             ThingUID hueGroupUID = getGroupUID(thingTypeUID, thingUID, configuration, bridgeUID);
100             return super.createThing(thingTypeUID, configuration, hueGroupUID, bridgeUID);
101         }
102
103         throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the hue binding.");
104     }
105
106     @Override
107     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
108         return SUPPORTED_THING_TYPES.contains(thingTypeUID);
109     }
110
111     private ThingUID getLightUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
112             @Nullable ThingUID bridgeUID) {
113         if (thingUID != null) {
114             return thingUID;
115         } else {
116             return getThingUID(thingTypeUID, configuration.get(LIGHT_ID).toString(), bridgeUID);
117         }
118     }
119
120     private ThingUID getSensorUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
121             @Nullable ThingUID bridgeUID) {
122         if (thingUID != null) {
123             return thingUID;
124         } else {
125             return getThingUID(thingTypeUID, configuration.get(SENSOR_ID).toString(), bridgeUID);
126         }
127     }
128
129     private ThingUID getGroupUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
130             @Nullable ThingUID bridgeUID) {
131         if (thingUID != null) {
132             return thingUID;
133         } else {
134             return getThingUID(thingTypeUID, configuration.get(GROUP_ID).toString(), bridgeUID);
135         }
136     }
137
138     private ThingUID getThingUID(ThingTypeUID thingTypeUID, String id, @Nullable ThingUID bridgeUID) {
139         if (bridgeUID != null) {
140             return new ThingUID(thingTypeUID, id, bridgeUID.getId());
141         } else {
142             return new ThingUID(thingTypeUID, id);
143         }
144     }
145
146     @Override
147     protected @Nullable ThingHandler createHandler(Thing thing) {
148         if (HueBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
149             HueBridgeHandler handler = new HueBridgeHandler((Bridge) thing, stateOptionProvider);
150             registerLightDiscoveryService(handler);
151             return handler;
152         } else if (HueLightHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
153             return new HueLightHandler(thing);
154         } else if (DimmerSwitchHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
155             return new DimmerSwitchHandler(thing);
156         } else if (TapSwitchHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
157             return new TapSwitchHandler(thing);
158         } else if (PresenceHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
159             return new PresenceHandler(thing);
160         } else if (TemperatureHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
161             return new TemperatureHandler(thing);
162         } else if (LightLevelHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
163             return new LightLevelHandler(thing);
164         } else if (ClipHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
165             return new ClipHandler(thing);
166         } else if (HueGroupHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
167             return new HueGroupHandler(thing, stateOptionProvider);
168         } else {
169             return null;
170         }
171     }
172
173     private synchronized void registerLightDiscoveryService(HueBridgeHandler bridgeHandler) {
174         HueLightDiscoveryService discoveryService = new HueLightDiscoveryService(bridgeHandler);
175         discoveryService.activate();
176         this.discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
177                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
178     }
179
180     @Override
181     protected synchronized void removeHandler(ThingHandler thingHandler) {
182         if (thingHandler instanceof HueBridgeHandler) {
183             ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
184             if (serviceReg != null) {
185                 // remove discovery service, if bridge handler is removed
186                 HueLightDiscoveryService service = (HueLightDiscoveryService) bundleContext
187                         .getService(serviceReg.getReference());
188                 serviceReg.unregister();
189                 if (service != null) {
190                     service.deactivate();
191                 }
192             }
193         }
194     }
195 }