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.powermax.internal;
15 import static org.openhab.binding.powermax.internal.PowermaxBindingConstants.*;
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.powermax.internal.discovery.PowermaxDiscoveryService;
24 import org.openhab.binding.powermax.internal.handler.PowermaxBridgeHandler;
25 import org.openhab.binding.powermax.internal.handler.PowermaxThingHandler;
26 import org.openhab.core.config.core.Configuration;
27 import org.openhab.core.config.discovery.DiscoveryService;
28 import org.openhab.core.i18n.TimeZoneProvider;
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 PowermaxHandlerFactory} is responsible for creating things and thing
46 * @author Laurent Garnier - Initial contribution
49 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.powermax")
50 public class PowermaxHandlerFactory extends BaseThingHandlerFactory {
52 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
54 private final SerialPortManager serialPortManager;
55 private final TimeZoneProvider timeZoneProvider;
58 public PowermaxHandlerFactory(final @Reference SerialPortManager serialPortManager,
59 final @Reference TimeZoneProvider timeZoneProvider) {
60 this.serialPortManager = serialPortManager;
61 this.timeZoneProvider = timeZoneProvider;
65 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
66 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID) || SUPPORTED_BRIDGE_TYPES_UIDS.contains(thingTypeUID);
70 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
71 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
72 if (SUPPORTED_BRIDGE_TYPES_UIDS.contains(thingTypeUID)) {
73 return super.createThing(thingTypeUID, configuration, thingUID, null);
74 } else if (SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
76 if (bridgeUID != null && thingUID != null) {
77 newThingUID = new ThingUID(thingTypeUID, bridgeUID, thingUID.getId());
79 newThingUID = thingUID;
81 return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
83 throw new IllegalArgumentException(
84 "The thing type " + thingTypeUID + " is not supported by the Powermax binding.");
88 protected @Nullable ThingHandler createHandler(Thing thing) {
89 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
91 if (SUPPORTED_BRIDGE_TYPES_UIDS.contains(thingTypeUID)) {
92 PowermaxBridgeHandler handler = new PowermaxBridgeHandler((Bridge) thing, serialPortManager);
93 registerDiscoveryService(handler);
95 } else if (SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
96 return new PowermaxThingHandler(thing, timeZoneProvider);
103 protected void removeHandler(ThingHandler thingHandler) {
104 if (thingHandler instanceof PowermaxBridgeHandler) {
105 // remove discovery service, if bridge handler is removed
106 unregisterDiscoveryService((PowermaxBridgeHandler) thingHandler);
110 private synchronized void registerDiscoveryService(PowermaxBridgeHandler bridgeHandler) {
111 PowermaxDiscoveryService discoveryService = new PowermaxDiscoveryService(bridgeHandler);
112 discoveryService.activate();
113 discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
114 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
117 private synchronized void unregisterDiscoveryService(PowermaxBridgeHandler bridgeHandler) {
118 ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(bridgeHandler.getThing().getUID());
119 if (serviceReg != null) {
120 PowermaxDiscoveryService service = (PowermaxDiscoveryService) bundleContext
121 .getService(serviceReg.getReference());
122 serviceReg.unregister();
123 if (service != null) {
124 service.deactivate();