]> git.basschouten.com Git - openhab-addons.git/blob
ef9ac10cd2ebacffa61f4ba89eac0e4ad0aae627
[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.plugwise.internal;
14
15 import static org.openhab.binding.plugwise.internal.PlugwiseBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.plugwise.internal.handler.PlugwiseRelayDeviceHandler;
24 import org.openhab.binding.plugwise.internal.handler.PlugwiseScanHandler;
25 import org.openhab.binding.plugwise.internal.handler.PlugwiseSenseHandler;
26 import org.openhab.binding.plugwise.internal.handler.PlugwiseStickHandler;
27 import org.openhab.binding.plugwise.internal.handler.PlugwiseSwitchHandler;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.io.transport.serial.SerialPortManager;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerFactory;
37 import org.osgi.framework.ServiceRegistration;
38 import org.osgi.service.component.annotations.Activate;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Reference;
41
42 /**
43  * The {@link PlugwiseHandlerFactory} is responsible for creating Plugwise things and thing handlers.
44  *
45  * @author Wouter Born - Initial contribution
46  */
47 @NonNullByDefault
48 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.plugwise")
49 public class PlugwiseHandlerFactory extends BaseThingHandlerFactory {
50
51     private final Map<ThingUID, @Nullable ServiceRegistration<?>> discoveryServiceRegistrations = new HashMap<>();
52
53     private final SerialPortManager serialPortManager;
54
55     @Activate
56     public PlugwiseHandlerFactory(final @Reference SerialPortManager serialPortManager) {
57         this.serialPortManager = serialPortManager;
58     }
59
60     @Override
61     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
63     }
64
65     @Override
66     protected @Nullable ThingHandler createHandler(Thing thing) {
67         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
68
69         if (thingTypeUID.equals(THING_TYPE_STICK)) {
70             PlugwiseStickHandler handler = new PlugwiseStickHandler((Bridge) thing, serialPortManager);
71             registerDiscoveryService(handler);
72             return handler;
73         } else if (thingTypeUID.equals(THING_TYPE_CIRCLE) || thingTypeUID.equals(THING_TYPE_CIRCLE_PLUS)
74                 || thingTypeUID.equals(THING_TYPE_STEALTH)) {
75             return new PlugwiseRelayDeviceHandler(thing);
76         } else if (thingTypeUID.equals(THING_TYPE_SCAN)) {
77             return new PlugwiseScanHandler(thing);
78         } else if (thingTypeUID.equals(THING_TYPE_SENSE)) {
79             return new PlugwiseSenseHandler(thing);
80         } else if (thingTypeUID.equals(THING_TYPE_SWITCH)) {
81             return new PlugwiseSwitchHandler(thing);
82         }
83
84         return null;
85     }
86
87     private void registerDiscoveryService(PlugwiseStickHandler handler) {
88         PlugwiseThingDiscoveryService discoveryService = new PlugwiseThingDiscoveryService(handler);
89         discoveryService.activate();
90         this.discoveryServiceRegistrations.put(handler.getThing().getUID(),
91                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
92     }
93
94     @Override
95     protected void removeHandler(ThingHandler thingHandler) {
96         ServiceRegistration<?> registration = this.discoveryServiceRegistrations.get(thingHandler.getThing().getUID());
97         if (registration != null) {
98             PlugwiseThingDiscoveryService discoveryService = (PlugwiseThingDiscoveryService) bundleContext
99                     .getService(registration.getReference());
100             discoveryService.deactivate();
101             registration.unregister();
102             discoveryServiceRegistrations.remove(thingHandler.getThing().getUID());
103         }
104     }
105 }