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.volvooncall.internal;
15 import static org.openhab.binding.volvooncall.internal.VolvoOnCallBindingConstants.*;
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.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;
43 * The {@link VolvoOnCallHandlerFactory} is responsible for creating things and thing
46 * @author Gaƫl L'hopital - Initial contribution
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;
56 public VolvoOnCallHandlerFactory(@Reference VehicleStateDescriptionProvider provider) {
57 this.stateDescriptionProvider = provider;
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();
68 if (APIBRIDGE_THING_TYPE.equals(thingTypeUID)) {
69 VolvoOnCallBridgeHandler bridgeHandler = new VolvoOnCallBridgeHandler((Bridge) thing);
70 registerDeviceDiscoveryService(bridgeHandler);
72 } else if (VEHICLE_THING_TYPE.equals(thingTypeUID)) {
73 return new VehicleHandler(thing, stateDescriptionProvider);
75 logger.warn("ThingHandler not found for {}", thing.getThingTypeUID());
80 protected void removeHandler(ThingHandler thingHandler) {
81 if (thingHandler instanceof VolvoOnCallBridgeHandler) {
82 ThingUID thingUID = thingHandler.getThing().getUID();
83 unregisterDeviceDiscoveryService(thingUID);
85 super.removeHandler(thingHandler);
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<>()));
94 private void unregisterDeviceDiscoveryService(ThingUID thingUID) {
95 if (discoveryServiceRegs.containsKey(thingUID)) {
96 ServiceRegistration<?> serviceReg = discoveryServiceRegs.get(thingUID);
97 serviceReg.unregister();
98 discoveryServiceRegs.remove(thingUID);