]> git.basschouten.com Git - openhab-addons.git/blob
63ad225a8457cb992457596c43982ea42d6f9b18
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.Collection;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.Optional;
23 import java.util.Set;
24 import java.util.concurrent.ScheduledFuture;
25 import java.util.concurrent.TimeUnit;
26
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;
32
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;
48
49 /**
50  * The {@link XmlTVHandler} is responsible for handling XMLTV file and dispatch
51  * information made available to according Media Channels
52  *
53  * @author GaĆ«l L'hopital - Initial contribution
54  */
55 @NonNullByDefault
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;
60
61     private @Nullable Tv currentXmlFile;
62     private @NonNullByDefault({}) ScheduledFuture<?> reloadJob;
63
64     public XmlTVHandler(Bridge thing, XMLInputFactory xif, Unmarshaller unmarshaller) {
65         super(thing);
66         this.xif = xif;
67         this.unmarshaller = unmarshaller;
68     }
69
70     @Override
71     public void initialize() {
72         XmlTVConfiguration config = getConfigAs(XmlTVConfiguration.class);
73         logger.debug("Initializing {} for input file '{}'", getClass(), config.filePath);
74
75         reloadJob = scheduler.scheduleWithFixedDelay(() -> {
76             currentXmlFile = null;
77             XMLStreamReader xsr = null;
78             try {
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);
81                 try {
82                     Tv xmlFile = (Tv) unmarshaller.unmarshal(xsr);
83                     // Remove all finished programmes
84                     xmlFile.getProgrammes().removeIf(programme -> Instant.now().isAfter(programme.getProgrammeStop()));
85
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);
92                     } else {
93                         updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.DISABLED, "@text/file-outdated");
94                     }
95                     xsr.close();
96                 } catch (JAXBException e) {
97                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR, e.getMessage());
98                 }
99             } catch (XMLStreamException | FileNotFoundException e) {
100                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
101             } finally {
102                 try {
103                     if (xsr != null) {
104                         xsr.close();
105                     }
106                 } catch (XMLStreamException e) {
107                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
108                 }
109             }
110         }, 0, config.refresh, TimeUnit.HOURS);
111     }
112
113     @Override
114     public void dispose() {
115         logger.debug("Running dispose");
116         if (reloadJob != null && !reloadJob.isCancelled()) {
117             reloadJob.cancel(true);
118             reloadJob = null;
119         }
120     }
121
122     @Override
123     public void handleCommand(ChannelUID channelUID, Command command) {
124         // nothing to do
125     }
126
127     public Optional<Tv> getXmlFile() {
128         return Optional.ofNullable(currentXmlFile);
129     }
130
131     @Override
132     public Collection<Class<? extends ThingHandlerService>> getServices() {
133         return Set.of(XmlTVDiscoveryService.class);
134     }
135 }