]> git.basschouten.com Git - openhab-addons.git/blob
2bdd12332dab20e29a039f30b510481b613ba458
[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.lutron.internal;
14
15 import static org.openhab.binding.lutron.internal.LutronBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.eclipse.jetty.client.HttpClient;
27 import org.openhab.binding.lutron.internal.discovery.LutronDeviceDiscoveryService;
28 import org.openhab.binding.lutron.internal.grxprg.GrafikEyeHandler;
29 import org.openhab.binding.lutron.internal.grxprg.PrgBridgeHandler;
30 import org.openhab.binding.lutron.internal.grxprg.PrgConstants;
31 import org.openhab.binding.lutron.internal.handler.BlindHandler;
32 import org.openhab.binding.lutron.internal.handler.CcoHandler;
33 import org.openhab.binding.lutron.internal.handler.DimmerHandler;
34 import org.openhab.binding.lutron.internal.handler.GrafikEyeKeypadHandler;
35 import org.openhab.binding.lutron.internal.handler.GreenModeHandler;
36 import org.openhab.binding.lutron.internal.handler.IPBridgeHandler;
37 import org.openhab.binding.lutron.internal.handler.IntlKeypadHandler;
38 import org.openhab.binding.lutron.internal.handler.KeypadHandler;
39 import org.openhab.binding.lutron.internal.handler.MaintainedCcoHandler;
40 import org.openhab.binding.lutron.internal.handler.OccupancySensorHandler;
41 import org.openhab.binding.lutron.internal.handler.PalladiomKeypadHandler;
42 import org.openhab.binding.lutron.internal.handler.PicoKeypadHandler;
43 import org.openhab.binding.lutron.internal.handler.PulsedCcoHandler;
44 import org.openhab.binding.lutron.internal.handler.QSIOHandler;
45 import org.openhab.binding.lutron.internal.handler.ShadeHandler;
46 import org.openhab.binding.lutron.internal.handler.SwitchHandler;
47 import org.openhab.binding.lutron.internal.handler.SysvarHandler;
48 import org.openhab.binding.lutron.internal.handler.TabletopKeypadHandler;
49 import org.openhab.binding.lutron.internal.handler.TimeclockHandler;
50 import org.openhab.binding.lutron.internal.handler.VcrxHandler;
51 import org.openhab.binding.lutron.internal.handler.VirtualKeypadHandler;
52 import org.openhab.binding.lutron.internal.handler.WciHandler;
53 import org.openhab.binding.lutron.internal.hw.HwConstants;
54 import org.openhab.binding.lutron.internal.hw.HwDimmerHandler;
55 import org.openhab.binding.lutron.internal.hw.HwSerialBridgeHandler;
56 import org.openhab.binding.lutron.internal.radiora.RadioRAConstants;
57 import org.openhab.binding.lutron.internal.radiora.handler.PhantomButtonHandler;
58 import org.openhab.binding.lutron.internal.radiora.handler.RS232Handler;
59 import org.openhab.core.config.discovery.DiscoveryService;
60 import org.openhab.core.io.net.http.HttpClientFactory;
61 import org.openhab.core.io.transport.serial.SerialPortManager;
62 import org.openhab.core.thing.Bridge;
63 import org.openhab.core.thing.Thing;
64 import org.openhab.core.thing.ThingTypeUID;
65 import org.openhab.core.thing.ThingUID;
66 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
67 import org.openhab.core.thing.binding.ThingHandler;
68 import org.openhab.core.thing.binding.ThingHandlerFactory;
69 import org.osgi.framework.ServiceRegistration;
70 import org.osgi.service.component.annotations.Activate;
71 import org.osgi.service.component.annotations.Component;
72 import org.osgi.service.component.annotations.Reference;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
75
76 /**
77  * The {@link LutronHandlerFactory} is responsible for creating things and thing
78  * handlers.
79  *
80  * @author Allan Tong - Initial contribution
81  * @author Bob Adair - Added bridge discovery service registration/removal
82  */
83
84 @NonNullByDefault
85 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.lutron")
86 public class LutronHandlerFactory extends BaseThingHandlerFactory {
87
88     // Used by LutronDeviceDiscoveryService to discover these types
89     public static final Set<ThingTypeUID> DISCOVERABLE_DEVICE_TYPES_UIDS = Collections
90             .unmodifiableSet(Stream.of(THING_TYPE_DIMMER, THING_TYPE_SWITCH, THING_TYPE_OCCUPANCYSENSOR,
91                     THING_TYPE_KEYPAD, THING_TYPE_TTKEYPAD, THING_TYPE_INTLKEYPAD, THING_TYPE_PICO,
92                     THING_TYPE_VIRTUALKEYPAD, THING_TYPE_VCRX, THING_TYPE_CCO, THING_TYPE_SHADE, THING_TYPE_TIMECLOCK,
93                     THING_TYPE_GREENMODE, THING_TYPE_QSIO, THING_TYPE_GRAFIKEYEKEYPAD, THING_TYPE_BLIND,
94                     THING_TYPE_PALLADIOMKEYPAD, THING_TYPE_WCI).collect(Collectors.toSet()));
95
96     // Used by the HwDiscoveryService
97     public static final Set<ThingTypeUID> HW_DISCOVERABLE_DEVICE_TYPES_UIDS = Collections
98             .unmodifiableSet(Collections.singleton(HwConstants.THING_TYPE_HWDIMMER));
99
100     // Other types that can be initiated but not discovered
101     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
102             .unmodifiableSet(Stream.of(THING_TYPE_IPBRIDGE, PrgConstants.THING_TYPE_PRGBRIDGE,
103                     PrgConstants.THING_TYPE_GRAFIKEYE, RadioRAConstants.THING_TYPE_RS232,
104                     RadioRAConstants.THING_TYPE_DIMMER, RadioRAConstants.THING_TYPE_SWITCH,
105                     RadioRAConstants.THING_TYPE_PHANTOM, HwConstants.THING_TYPE_HWSERIALBRIDGE, THING_TYPE_CCO_PULSED,
106                     THING_TYPE_CCO_MAINTAINED, THING_TYPE_SYSVAR).collect(Collectors.toSet()));
107
108     private final Logger logger = LoggerFactory.getLogger(LutronHandlerFactory.class);
109
110     private final SerialPortManager serialPortManager;
111     private final HttpClient httpClient;
112
113     @Activate
114     public LutronHandlerFactory(final @Reference SerialPortManager serialPortManager,
115             @Reference HttpClientFactory httpClientFactory) {
116         this.serialPortManager = serialPortManager;
117         this.httpClient = httpClientFactory.getCommonHttpClient();
118     }
119
120     @Override
121     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
122         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)
123                 || DISCOVERABLE_DEVICE_TYPES_UIDS.contains(thingTypeUID)
124                 || HW_DISCOVERABLE_DEVICE_TYPES_UIDS.contains(thingTypeUID);
125     }
126
127     private final Map<ThingUID, @Nullable ServiceRegistration<?>> discoveryServiceRegMap = new HashMap<>();
128     // Marked as Nullable only to fix incorrect redundant null check complaints after adding null annotations
129
130     @Override
131     protected @Nullable ThingHandler createHandler(Thing thing) {
132         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
133
134         if (thingTypeUID.equals(THING_TYPE_IPBRIDGE)) {
135             IPBridgeHandler bridgeHandler = new IPBridgeHandler((Bridge) thing);
136             registerDiscoveryService(bridgeHandler);
137             return bridgeHandler;
138         } else if (thingTypeUID.equals(THING_TYPE_DIMMER)) {
139             return new DimmerHandler(thing);
140         } else if (thingTypeUID.equals(THING_TYPE_SHADE)) {
141             return new ShadeHandler(thing);
142         } else if (thingTypeUID.equals(THING_TYPE_SWITCH)) {
143             return new SwitchHandler(thing);
144         } else if (thingTypeUID.equals(THING_TYPE_CCO)) {
145             return new CcoHandler(thing);
146         } else if (thingTypeUID.equals(THING_TYPE_CCO_PULSED)) {
147             return new PulsedCcoHandler(thing);
148         } else if (thingTypeUID.equals(THING_TYPE_CCO_MAINTAINED)) {
149             return new MaintainedCcoHandler(thing);
150         } else if (thingTypeUID.equals(THING_TYPE_OCCUPANCYSENSOR)) {
151             return new OccupancySensorHandler(thing);
152         } else if (thingTypeUID.equals(THING_TYPE_KEYPAD)) {
153             return new KeypadHandler(thing);
154         } else if (thingTypeUID.equals(THING_TYPE_TTKEYPAD)) {
155             return new TabletopKeypadHandler(thing);
156         } else if (thingTypeUID.equals(THING_TYPE_INTLKEYPAD)) {
157             return new IntlKeypadHandler(thing);
158         } else if (thingTypeUID.equals(THING_TYPE_PICO)) {
159             return new PicoKeypadHandler(thing);
160         } else if (thingTypeUID.equals(THING_TYPE_GRAFIKEYEKEYPAD)) {
161             return new GrafikEyeKeypadHandler(thing);
162         } else if (thingTypeUID.equals(THING_TYPE_PALLADIOMKEYPAD)) {
163             return new PalladiomKeypadHandler(thing);
164         } else if (thingTypeUID.equals(THING_TYPE_VIRTUALKEYPAD)) {
165             return new VirtualKeypadHandler(thing);
166         } else if (thingTypeUID.equals(THING_TYPE_VCRX)) {
167             return new VcrxHandler(thing);
168         } else if (thingTypeUID.equals(THING_TYPE_WCI)) {
169             return new WciHandler(thing);
170         } else if (thingTypeUID.equals(THING_TYPE_TIMECLOCK)) {
171             return new TimeclockHandler(thing);
172         } else if (thingTypeUID.equals(THING_TYPE_GREENMODE)) {
173             return new GreenModeHandler(thing);
174         } else if (thingTypeUID.equals(THING_TYPE_QSIO)) {
175             return new QSIOHandler(thing);
176         } else if (thingTypeUID.equals(THING_TYPE_BLIND)) {
177             return new BlindHandler(thing);
178         } else if (thingTypeUID.equals(THING_TYPE_SYSVAR)) {
179             return new SysvarHandler(thing);
180         } else if (thingTypeUID.equals(PrgConstants.THING_TYPE_PRGBRIDGE)) {
181             return new PrgBridgeHandler((Bridge) thing);
182         } else if (thingTypeUID.equals(PrgConstants.THING_TYPE_GRAFIKEYE)) {
183             return new GrafikEyeHandler(thing);
184         } else if (thingTypeUID.equals(RadioRAConstants.THING_TYPE_RS232)) {
185             return new RS232Handler((Bridge) thing, serialPortManager);
186         } else if (thingTypeUID.equals(RadioRAConstants.THING_TYPE_DIMMER)) {
187             return new org.openhab.binding.lutron.internal.radiora.handler.DimmerHandler(thing);
188         } else if (thingTypeUID.equals(RadioRAConstants.THING_TYPE_SWITCH)) {
189             return new org.openhab.binding.lutron.internal.radiora.handler.SwitchHandler(thing);
190         } else if (thingTypeUID.equals(RadioRAConstants.THING_TYPE_PHANTOM)) {
191             return new PhantomButtonHandler(thing);
192         } else if (thingTypeUID.equals(HwConstants.THING_TYPE_HWSERIALBRIDGE)) {
193             return new HwSerialBridgeHandler((Bridge) thing, serialPortManager);
194         } else if (thingTypeUID.equals(HwConstants.THING_TYPE_HWDIMMER)) {
195             return new HwDimmerHandler(thing);
196         }
197
198         return null;
199     }
200
201     @Override
202     protected synchronized void removeHandler(ThingHandler thingHandler) {
203         if (thingHandler instanceof IPBridgeHandler) {
204             ServiceRegistration<?> serviceReg = discoveryServiceRegMap.remove(thingHandler.getThing().getUID());
205             if (serviceReg != null) {
206                 logger.debug("Unregistering discovery service.");
207                 serviceReg.unregister();
208             }
209         }
210     }
211
212     /**
213      * Register a discovery service for an IP bridge handler.
214      *
215      * @param bridgeHandler bridge handler for which to register the discovery service
216      */
217     private synchronized void registerDiscoveryService(IPBridgeHandler bridgeHandler) {
218         logger.debug("Registering discovery service.");
219         LutronDeviceDiscoveryService discoveryService = new LutronDeviceDiscoveryService(bridgeHandler, httpClient);
220         bridgeHandler.setDiscoveryService(discoveryService);
221         discoveryServiceRegMap.put(bridgeHandler.getThing().getUID(),
222                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, null));
223     }
224 }