]> git.basschouten.com Git - openhab-addons.git/blob
e66ec738f3f9ba249ceb6e863f12b556415e2f2b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.discovery;
14
15 import static org.openhab.binding.xmltv.internal.XmlTVBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.xmltv.internal.configuration.XmlChannelConfiguration;
20 import org.openhab.binding.xmltv.internal.handler.XmlTVHandler;
21 import org.openhab.core.config.discovery.AbstractDiscoveryService;
22 import org.openhab.core.config.discovery.DiscoveryResult;
23 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.thing.ThingUID;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.openhab.core.thing.binding.ThingHandlerService;
28 import org.osgi.service.component.annotations.Activate;
29 import org.osgi.service.component.annotations.Component;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * The {@link XmlTVDiscoveryService} is responsible for discovering all channels
35  * declared in the XmlTV file
36  *
37  * @author GaĆ«l L'hopital - Initial contribution
38  */
39 @Component(service = ThingHandlerService.class)
40 @NonNullByDefault
41 public class XmlTVDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
42     private static final int SEARCH_TIME = 5;
43
44     private final Logger logger = LoggerFactory.getLogger(XmlTVDiscoveryService.class);
45     private @Nullable XmlTVHandler handler;
46
47     /**
48      * Creates a XmlTVDiscoveryService with background discovery disabled.
49      */
50     @Activate
51     public XmlTVDiscoveryService() {
52         super(SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME);
53     }
54
55     @Override
56     public void deactivate() {
57         super.deactivate();
58     }
59
60     @Override
61     protected void startScan() {
62         logger.debug("Starting XmlTV discovery scan");
63         XmlTVHandler bridgeHandler = handler;
64         if (bridgeHandler != null && bridgeHandler.getThing().getStatus() == ThingStatus.ONLINE) {
65             bridgeHandler.getXmlFile().ifPresent(tv -> {
66                 tv.getMediaChannels().stream().forEach(channel -> {
67                     String channelId = channel.getId();
68                     String uid = channelId.replaceAll("[^A-Za-z0-9_]", "_");
69                     ThingUID thingUID = new ThingUID(XMLTV_CHANNEL_THING_TYPE, bridgeHandler.getThing().getUID(), uid);
70
71                     DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
72                             .withBridge(bridgeHandler.getThing().getUID())
73                             .withLabel(channel.getDisplayNames().get(0).getValue()).withRepresentationProperty(uid)
74                             .withProperty(XmlChannelConfiguration.CHANNEL_ID, channelId).build();
75
76                     thingDiscovered(discoveryResult);
77                 });
78             });
79         }
80     }
81
82     @Override
83     public void setThingHandler(ThingHandler handler) {
84         if (handler instanceof XmlTVHandler tvHandler) {
85             this.handler = tvHandler;
86         }
87     }
88
89     @Override
90     public @Nullable ThingHandler getThingHandler() {
91         return handler;
92     }
93 }