]> git.basschouten.com Git - openhab-addons.git/blob
b9f3e0d5e2ca6ce5770231dee170868279af4deb
[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.powermax.internal;
14
15 import static org.openhab.binding.powermax.internal.PowermaxBindingConstants.*;
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.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;
41
42 /**
43  * The {@link PowermaxHandlerFactory} is responsible for creating things and thing
44  * handlers.
45  *
46  * @author Laurent Garnier - Initial contribution
47  */
48 @NonNullByDefault
49 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.powermax")
50 public class PowermaxHandlerFactory extends BaseThingHandlerFactory {
51
52     private final Map<ThingUID, @Nullable ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
53
54     private final SerialPortManager serialPortManager;
55     private final TimeZoneProvider timeZoneProvider;
56
57     @Activate
58     public PowermaxHandlerFactory(final @Reference SerialPortManager serialPortManager,
59             final @Reference TimeZoneProvider timeZoneProvider) {
60         this.serialPortManager = serialPortManager;
61         this.timeZoneProvider = timeZoneProvider;
62     }
63
64     @Override
65     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
66         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID) || SUPPORTED_BRIDGE_TYPES_UIDS.contains(thingTypeUID);
67     }
68
69     @Override
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)) {
75             ThingUID newThingUID;
76             if (bridgeUID != null && thingUID != null) {
77                 newThingUID = new ThingUID(thingTypeUID, bridgeUID, thingUID.getId());
78             } else {
79                 newThingUID = thingUID;
80             }
81             return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
82         }
83         throw new IllegalArgumentException(
84                 "The thing type " + thingTypeUID + " is not supported by the Powermax binding.");
85     }
86
87     @Override
88     protected @Nullable ThingHandler createHandler(Thing thing) {
89         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
90
91         if (SUPPORTED_BRIDGE_TYPES_UIDS.contains(thingTypeUID)) {
92             PowermaxBridgeHandler handler = new PowermaxBridgeHandler((Bridge) thing, serialPortManager);
93             registerDiscoveryService(handler);
94             return handler;
95         } else if (SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
96             return new PowermaxThingHandler(thing, timeZoneProvider);
97         }
98
99         return null;
100     }
101
102     @Override
103     protected void removeHandler(ThingHandler thingHandler) {
104         if (thingHandler instanceof PowermaxBridgeHandler) {
105             // remove discovery service, if bridge handler is removed
106             unregisterDiscoveryService((PowermaxBridgeHandler) thingHandler);
107         }
108     }
109
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<>()));
115     }
116
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();
125             }
126         }
127     }
128 }