]> git.basschouten.com Git - openhab-addons.git/blob
85e7969462259ac7cc95fa24dcc3f322df7c8161
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.xmltv.internal.handler;
14
15 import java.io.File;
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;
23
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;
30
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;
44
45 /**
46  * The {@link XmlTVHandler} is responsible for handling XMLTV file and dispatch
47  * information made available to according Media Channels
48  *
49  * @author GaĆ«l L'hopital - Initial contribution
50  */
51 @NonNullByDefault
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;
56
57     private @Nullable Tv currentXmlFile;
58     private @NonNullByDefault({}) ScheduledFuture<?> reloadJob;
59
60     public XmlTVHandler(Bridge thing) throws JAXBException {
61         super(thing);
62         xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
63         jc = JAXBContext.newInstance(Tv.class);
64     }
65
66     @Override
67     public void initialize() {
68         XmlTVConfiguration config = getConfigAs(XmlTVConfiguration.class);
69         logger.debug("Initializing {} for input file '{}'", getClass(), config.filePath);
70
71         reloadJob = scheduler.scheduleWithFixedDelay(() -> {
72             currentXmlFile = null;
73             XMLStreamReader xsr = null;
74             try {
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);
77
78                 try {
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()));
83
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);
90                     } else {
91                         updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.DISABLED, "XMLTV file seems outdated");
92                     }
93                     xsr.close();
94                 } catch (JAXBException e) {
95                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR, e.getMessage());
96                 }
97             } catch (XMLStreamException | FileNotFoundException e) {
98                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
99             } finally {
100                 try {
101                     if (xsr != null) {
102                         xsr.close();
103                     }
104                 } catch (XMLStreamException e) {
105                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
106                 }
107             }
108         }, 0, config.refresh, TimeUnit.HOURS);
109     }
110
111     @Override
112     public void dispose() {
113         logger.debug("Running dispose");
114         if (reloadJob != null && !reloadJob.isCancelled()) {
115             reloadJob.cancel(true);
116             reloadJob = null;
117         }
118     }
119
120     @Override
121     public void handleCommand(ChannelUID channelUID, Command command) {
122         // nothing to do
123     }
124
125     @Nullable
126     public Tv getXmlFile() {
127         return currentXmlFile;
128     }
129 }