2 * Copyright (c) 2010-2023 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.*;
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;
34 * The {@link XmlTVDiscoveryService} is responsible for discovering all channels
35 * declared in the XmlTV file
37 * @author Gaƫl L'hopital - Initial contribution
39 @Component(service = ThingHandlerService.class)
41 public class XmlTVDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
42 private static final int SEARCH_TIME = 5;
44 private final Logger logger = LoggerFactory.getLogger(XmlTVDiscoveryService.class);
45 private @Nullable XmlTVHandler handler;
48 * Creates a XmlTVDiscoveryService with background discovery disabled.
51 public XmlTVDiscoveryService() {
52 super(SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME);
56 public void deactivate() {
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);
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();
76 thingDiscovered(discoveryResult);
83 public void setThingHandler(ThingHandler handler) {
84 if (handler instanceof XmlTVHandler tvHandler) {
85 this.handler = tvHandler;
90 public @Nullable ThingHandler getThingHandler() {