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.handler;
16 import java.io.FileInputStream;
17 import java.io.FileNotFoundException;
18 import java.time.Instant;
19 import java.util.Collections;
20 import java.util.Comparator;
21 import java.util.concurrent.ScheduledFuture;
22 import java.util.concurrent.TimeUnit;
24 import javax.xml.bind.JAXBContext;
25 import javax.xml.bind.JAXBException;
26 import javax.xml.bind.Unmarshaller;
27 import javax.xml.stream.XMLInputFactory;
28 import javax.xml.stream.XMLStreamException;
29 import javax.xml.stream.XMLStreamReader;
31 import org.eclipse.jdt.annotation.NonNullByDefault;
32 import org.eclipse.jdt.annotation.Nullable;
33 import org.openhab.binding.xmltv.internal.configuration.XmlTVConfiguration;
34 import org.openhab.binding.xmltv.internal.jaxb.Programme;
35 import org.openhab.binding.xmltv.internal.jaxb.Tv;
36 import org.openhab.core.thing.Bridge;
37 import org.openhab.core.thing.ChannelUID;
38 import org.openhab.core.thing.ThingStatus;
39 import org.openhab.core.thing.ThingStatusDetail;
40 import org.openhab.core.thing.binding.BaseBridgeHandler;
41 import org.openhab.core.types.Command;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * The {@link XmlTVHandler} is responsible for handling XMLTV file and dispatch
47 * information made available to according Media Channels
49 * @author Gaƫl L'hopital - Initial contribution
52 public class XmlTVHandler extends BaseBridgeHandler {
53 private final Logger logger = LoggerFactory.getLogger(XmlTVHandler.class);
54 private final XMLInputFactory xif = XMLInputFactory.newFactory();
55 private final JAXBContext jc;
57 private @Nullable Tv currentXmlFile;
58 private @NonNullByDefault({}) ScheduledFuture<?> reloadJob;
60 public XmlTVHandler(Bridge thing) throws JAXBException {
62 xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
63 jc = JAXBContext.newInstance(Tv.class);
67 public void initialize() {
68 XmlTVConfiguration config = getConfigAs(XmlTVConfiguration.class);
69 logger.debug("Initializing {} for input file '{}'", getClass(), config.filePath);
71 reloadJob = scheduler.scheduleWithFixedDelay(() -> {
72 currentXmlFile = null;
73 XMLStreamReader xsr = null;
75 // This can take some seconds depending upon weight of the XmlTV source file
76 xsr = xif.createXMLStreamReader(new FileInputStream(new File(config.filePath)), config.encoding);
79 Unmarshaller unmarshaller = jc.createUnmarshaller();
80 Tv xmlFile = (Tv) unmarshaller.unmarshal(xsr);
81 // Remove all finished programmes
82 xmlFile.getProgrammes().removeIf(programme -> Instant.now().isAfter(programme.getProgrammeStop()));
84 if (!xmlFile.getProgrammes().isEmpty()) {
85 // Sort programmes by starting instant
86 Collections.sort(xmlFile.getProgrammes(), Comparator.comparing(Programme::getProgrammeStart));
87 // Ready to deliver data to ChannelHandlers
88 currentXmlFile = xmlFile;
89 updateStatus(ThingStatus.ONLINE);
91 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.DISABLED, "XMLTV file seems outdated");
94 } catch (JAXBException e) {
95 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR, e.getMessage());
97 } catch (XMLStreamException | FileNotFoundException e) {
98 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
104 } catch (XMLStreamException e) {
105 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
108 }, 0, config.refresh, TimeUnit.HOURS);
112 public void dispose() {
113 logger.debug("Running dispose");
114 if (reloadJob != null && !reloadJob.isCancelled()) {
115 reloadJob.cancel(true);
121 public void handleCommand(ChannelUID channelUID, Command command) {
126 public Tv getXmlFile() {
127 return currentXmlFile;