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