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.handler;
15 import java.io.FileInputStream;
16 import java.io.FileNotFoundException;
17 import java.time.Instant;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Comparator;
21 import java.util.Optional;
23 import java.util.concurrent.ScheduledFuture;
24 import java.util.concurrent.TimeUnit;
26 import javax.xml.bind.JAXBException;
27 import javax.xml.bind.Unmarshaller;
28 import javax.xml.stream.XMLInputFactory;
29 import javax.xml.stream.XMLStreamException;
30 import javax.xml.stream.XMLStreamReader;
32 import org.eclipse.jdt.annotation.NonNullByDefault;
33 import org.eclipse.jdt.annotation.Nullable;
34 import org.openhab.binding.xmltv.internal.configuration.XmlTVConfiguration;
35 import org.openhab.binding.xmltv.internal.discovery.XmlTVDiscoveryService;
36 import org.openhab.binding.xmltv.internal.jaxb.Programme;
37 import org.openhab.binding.xmltv.internal.jaxb.Tv;
38 import org.openhab.core.thing.Bridge;
39 import org.openhab.core.thing.ChannelUID;
40 import org.openhab.core.thing.ThingStatus;
41 import org.openhab.core.thing.ThingStatusDetail;
42 import org.openhab.core.thing.binding.BaseBridgeHandler;
43 import org.openhab.core.thing.binding.ThingHandlerService;
44 import org.openhab.core.types.Command;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * The {@link XmlTVHandler} is responsible for handling XMLTV file and dispatch
50 * information made available to according Media Channels
52 * @author Gaƫl L'hopital - Initial contribution
55 public class XmlTVHandler extends BaseBridgeHandler {
56 private final Logger logger = LoggerFactory.getLogger(XmlTVHandler.class);
57 private final XMLInputFactory xif;
58 private final Unmarshaller unmarshaller;
60 private @Nullable Tv currentXmlFile;
61 private @NonNullByDefault({}) ScheduledFuture<?> reloadJob;
63 public XmlTVHandler(Bridge thing, XMLInputFactory xif, Unmarshaller unmarshaller) {
66 this.unmarshaller = unmarshaller;
70 public void initialize() {
71 XmlTVConfiguration config = getConfigAs(XmlTVConfiguration.class);
72 logger.debug("Initializing {} for input file '{}'", getClass(), config.filePath);
74 reloadJob = scheduler.scheduleWithFixedDelay(() -> {
75 currentXmlFile = null;
76 XMLStreamReader xsr = null;
78 // This can take some seconds depending upon weight of the XmlTV source file
79 xsr = xif.createXMLStreamReader(new FileInputStream(config.filePath), config.encoding);
81 Tv xmlFile = (Tv) unmarshaller.unmarshal(xsr);
82 // Remove all finished programmes
83 xmlFile.getProgrammes().removeIf(programme -> Instant.now().isAfter(programme.getProgrammeStop()));
85 if (!xmlFile.getProgrammes().isEmpty()) {
86 // Sort programmes by starting instant
87 Collections.sort(xmlFile.getProgrammes(), Comparator.comparing(Programme::getProgrammeStart));
88 // Ready to deliver data to ChannelHandlers
89 currentXmlFile = xmlFile;
90 updateStatus(ThingStatus.ONLINE);
92 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.DISABLED, "@text/file-outdated");
95 } catch (JAXBException e) {
96 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR, e.getMessage());
98 } catch (XMLStreamException | FileNotFoundException e) {
99 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
105 } catch (XMLStreamException e) {
106 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
109 }, 0, config.refresh, TimeUnit.HOURS);
113 public void dispose() {
114 logger.debug("Running dispose");
115 if (reloadJob != null && !reloadJob.isCancelled()) {
116 reloadJob.cancel(true);
122 public void handleCommand(ChannelUID channelUID, Command command) {
126 public Optional<Tv> getXmlFile() {
127 return Optional.ofNullable(currentXmlFile);
131 public Collection<Class<? extends ThingHandlerService>> getServices() {
132 return Set.of(XmlTVDiscoveryService.class);