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