2 * Copyright (c) 2010-2021 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.xmltv.internal;
15 import static org.openhab.binding.xmltv.internal.XmlTVBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.Hashtable;
21 import javax.xml.bind.JAXBException;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.xmltv.internal.discovery.XmlTVDiscoveryService;
26 import org.openhab.binding.xmltv.internal.handler.ChannelHandler;
27 import org.openhab.binding.xmltv.internal.handler.XmlTVHandler;
28 import org.openhab.core.config.core.Configuration;
29 import org.openhab.core.config.discovery.DiscoveryService;
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.Component;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * The {@link XmlTVHandlerFactory} is responsible for creating things and thing
46 * @author Gaƫl L'hopital - Initial contribution
49 @Component(configurationPid = "binding.xmltv", service = ThingHandlerFactory.class)
50 public class XmlTVHandlerFactory extends BaseThingHandlerFactory {
51 private final Logger logger = LoggerFactory.getLogger(XmlTVHandlerFactory.class);
52 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
55 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
56 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
60 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
61 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
62 if (XMLTV_FILE_BRIDGE_TYPE.equals(thingTypeUID)) {
63 return super.createThing(thingTypeUID, configuration, thingUID, null);
64 } else if (XMLTV_CHANNEL_THING_TYPE.equals(thingTypeUID)) {
66 if (bridgeUID != null && thingUID != null) {
67 newThingUID = new ThingUID(thingTypeUID, bridgeUID, thingUID.getId());
69 newThingUID = thingUID;
71 return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
73 throw new IllegalArgumentException(
74 "The thing type " + thingTypeUID + " is not supported by the XmlTV binding.");
78 protected @Nullable ThingHandler createHandler(Thing thing) {
79 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
81 if (XMLTV_FILE_BRIDGE_TYPE.equals(thingTypeUID)) {
83 XmlTVHandler bridgeHandler = new XmlTVHandler((Bridge) thing);
84 registerDeviceDiscoveryService(bridgeHandler);
86 } catch (JAXBException e) {
87 logger.error("Unable to create XmlTVHandler : {}", e.getMessage());
89 } else if (XMLTV_CHANNEL_THING_TYPE.equals(thingTypeUID)) {
90 return new ChannelHandler(thing);
97 protected void removeHandler(ThingHandler thingHandler) {
98 if (thingHandler instanceof XmlTVHandler) {
99 Thing thing = thingHandler.getThing();
100 unregisterDeviceDiscoveryService(thing);
102 super.removeHandler(thingHandler);
105 private synchronized void registerDeviceDiscoveryService(XmlTVHandler bridgeHandler) {
106 XmlTVDiscoveryService discoveryService = new XmlTVDiscoveryService(bridgeHandler);
107 discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
108 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
111 private synchronized void unregisterDeviceDiscoveryService(Thing thing) {
112 ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(thing.getUID());
113 serviceReg.unregister();