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