]> git.basschouten.com Git - openhab-addons.git/blob
43e36336cbcea0044ab450ac700b13a342a505f0
[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.volvooncall.internal;
14
15 import static org.openhab.binding.volvooncall.internal.VolvoOnCallBindingConstants.*;
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.volvooncall.internal.discovery.VolvoOnCallDiscoveryService;
24 import org.openhab.binding.volvooncall.internal.handler.VehicleHandler;
25 import org.openhab.binding.volvooncall.internal.handler.VehicleStateDescriptionProvider;
26 import org.openhab.binding.volvooncall.internal.handler.VolvoOnCallBridgeHandler;
27 import org.openhab.core.config.discovery.DiscoveryService;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerFactory;
35 import org.osgi.framework.ServiceRegistration;
36 import org.osgi.service.component.annotations.Activate;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.Reference;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * The {@link VolvoOnCallHandlerFactory} is responsible for creating things and thing
44  * handlers.
45  *
46  * @author GaĆ«l L'hopital - Initial contribution
47  */
48 @NonNullByDefault
49 @Component(configurationPid = "binding.volvooncall", service = ThingHandlerFactory.class)
50 public class VolvoOnCallHandlerFactory extends BaseThingHandlerFactory {
51     private final Logger logger = LoggerFactory.getLogger(VolvoOnCallHandlerFactory.class);
52     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
53     private final VehicleStateDescriptionProvider stateDescriptionProvider;
54
55     @Activate
56     public VolvoOnCallHandlerFactory(@Reference VehicleStateDescriptionProvider provider) {
57         this.stateDescriptionProvider = provider;
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         if (APIBRIDGE_THING_TYPE.equals(thingTypeUID)) {
69             VolvoOnCallBridgeHandler bridgeHandler = new VolvoOnCallBridgeHandler((Bridge) thing);
70             registerDeviceDiscoveryService(bridgeHandler);
71             return bridgeHandler;
72         } else if (VEHICLE_THING_TYPE.equals(thingTypeUID)) {
73             return new VehicleHandler(thing, stateDescriptionProvider);
74         }
75         logger.warn("ThingHandler not found for {}", thing.getThingTypeUID());
76         return null;
77     }
78
79     @Override
80     protected void removeHandler(ThingHandler thingHandler) {
81         if (thingHandler instanceof VolvoOnCallBridgeHandler) {
82             ThingUID thingUID = thingHandler.getThing().getUID();
83             unregisterDeviceDiscoveryService(thingUID);
84         }
85         super.removeHandler(thingHandler);
86     }
87
88     private void registerDeviceDiscoveryService(VolvoOnCallBridgeHandler bridgeHandler) {
89         VolvoOnCallDiscoveryService discoveryService = new VolvoOnCallDiscoveryService(bridgeHandler);
90         discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
91                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
92     }
93
94     private void unregisterDeviceDiscoveryService(ThingUID thingUID) {
95         if (discoveryServiceRegs.containsKey(thingUID)) {
96             ServiceRegistration<?> serviceReg = discoveryServiceRegs.get(thingUID);
97             serviceReg.unregister();
98             discoveryServiceRegs.remove(thingUID);
99         }
100     }
101 }