]> git.basschouten.com Git - openhab-addons.git/blob
d88bef01c9eaed1f8ced08b24e38e22d58b2bdd8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.xmltv.internal;
14
15 import static org.openhab.binding.xmltv.internal.XmlTVBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Map;
20
21 import javax.xml.bind.JAXBException;
22
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;
41
42 /**
43  * The {@link XmlTVHandlerFactory} 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.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<>();
53
54     @Override
55     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
56         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
57     }
58
59     @Override
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)) {
65             ThingUID newThingUID;
66             if (bridgeUID != null && thingUID != null) {
67                 newThingUID = new ThingUID(thingTypeUID, bridgeUID, thingUID.getId());
68             } else {
69                 newThingUID = thingUID;
70             }
71             return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
72         }
73         throw new IllegalArgumentException(
74                 "The thing type " + thingTypeUID + " is not supported by the XmlTV binding.");
75     }
76
77     @Override
78     protected @Nullable ThingHandler createHandler(Thing thing) {
79         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
80
81         if (XMLTV_FILE_BRIDGE_TYPE.equals(thingTypeUID)) {
82             try {
83                 XmlTVHandler bridgeHandler = new XmlTVHandler((Bridge) thing);
84                 registerDeviceDiscoveryService(bridgeHandler);
85                 return bridgeHandler;
86             } catch (JAXBException e) {
87                 logger.error("Unable to create XmlTVHandler : {}", e.getMessage());
88             }
89         } else if (XMLTV_CHANNEL_THING_TYPE.equals(thingTypeUID)) {
90             return new ChannelHandler(thing);
91         }
92
93         return null;
94     }
95
96     @Override
97     protected void removeHandler(ThingHandler thingHandler) {
98         if (thingHandler instanceof XmlTVHandler) {
99             Thing thing = thingHandler.getThing();
100             unregisterDeviceDiscoveryService(thing);
101         }
102         super.removeHandler(thingHandler);
103     }
104
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<>()));
109     }
110
111     private synchronized void unregisterDeviceDiscoveryService(Thing thing) {
112         ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(thing.getUID());
113         serviceReg.unregister();
114     }
115 }