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.discovery;
15 import static org.openhab.binding.xmltv.internal.XmlTVBindingConstants.XMLTV_CHANNEL_THING_TYPE;
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;
31 * The {@link XmlTVDiscoveryService} is responsible for discovering all channels
32 * declared in the XmlTV file
34 * @author Gaƫl L'hopital - Initial contribution
37 public class XmlTVDiscoveryService extends AbstractDiscoveryService {
38 private final Logger logger = LoggerFactory.getLogger(XmlTVDiscoveryService.class);
40 private static final int SEARCH_TIME = 10;
42 private XmlTVHandler bridgeHandler;
45 * Creates a XmlTVDiscoveryService with background discovery disabled.
47 public XmlTVDiscoveryService(XmlTVHandler bridgeHandler) {
48 super(XmlTVBindingConstants.SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME);
49 this.bridgeHandler = bridgeHandler;
53 protected void startScan() {
54 logger.debug("Starting XmlTV discovery scan");
55 if (bridgeHandler.getThing().getStatus() == ThingStatus.ONLINE) {
56 Tv tv = bridgeHandler.getXmlFile();
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);
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();
68 thingDiscovered(discoveryResult);