2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.plugwise.internal;
15 import static org.openhab.binding.plugwise.internal.PlugwiseBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.Hashtable;
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;
43 * The {@link PlugwiseHandlerFactory} is responsible for creating Plugwise things and thing handlers.
45 * @author Wouter Born - Initial contribution
48 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.plugwise")
49 public class PlugwiseHandlerFactory extends BaseThingHandlerFactory {
51 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegistrations = new HashMap<>();
53 private final SerialPortManager serialPortManager;
56 public PlugwiseHandlerFactory(final @Reference SerialPortManager serialPortManager) {
57 this.serialPortManager = serialPortManager;
61 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
66 protected @Nullable ThingHandler createHandler(Thing thing) {
67 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
69 if (thingTypeUID.equals(THING_TYPE_STICK)) {
70 PlugwiseStickHandler handler = new PlugwiseStickHandler((Bridge) thing, serialPortManager);
71 registerDiscoveryService(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);
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<>()));
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());