]> git.basschouten.com Git - openhab-addons.git/blob
d1ddc675d0c40059f2599b552bdba760e533b9be
[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.vdr.internal.svdrp;
14
15 import java.time.Instant;
16 import java.time.temporal.ChronoUnit;
17 import java.util.NoSuchElementException;
18 import java.util.StringTokenizer;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21
22 /**
23  * The {@link SVDRPEpgEvent} contains SVDRP Response Data for an EPG Event
24  *
25  * @author Matthias Klocke - Initial contribution
26  */
27 @NonNullByDefault
28 public class SVDRPEpgEvent {
29
30     public enum TYPE {
31         NOW,
32         NEXT
33     }
34
35     private String title = "";
36     private String subtitle = "";
37     private Instant begin = Instant.now();
38     private Instant end = Instant.now();
39     private int duration;
40
41     private SVDRPEpgEvent() {
42     }
43
44     /**
45      * parse object from SVDRP Client Response
46      *
47      * @param message SVDRP Client Response
48      * @return SVDRPEpgEvent Object
49      * @throws SVDRPParseResponseException thrown if response data is not parseable
50      */
51     public static SVDRPEpgEvent parse(String message) throws SVDRPParseResponseException {
52         SVDRPEpgEvent entry = new SVDRPEpgEvent();
53         StringTokenizer st = new StringTokenizer(message, System.lineSeparator());
54
55         while (st.hasMoreTokens()) {
56             String line = st.nextToken();
57             if (line.length() >= 1 && !line.startsWith("End")) {
58                 switch (line.charAt(0)) {
59                     case 'T':
60                         entry.setTitle(line.substring(1).trim());
61                         break;
62                     case 'S':
63                         entry.setSubtitle(line.substring(1).trim());
64                         break;
65                     case 'E':
66                         StringTokenizer lt = new StringTokenizer(line.substring(1).trim(), " ");
67                         lt.nextToken(); // event id
68                         try {
69                             long begin = Long.parseLong(lt.nextToken());
70                             entry.setBegin(Instant.ofEpochSecond(begin));
71                         } catch (NumberFormatException | NoSuchElementException e) {
72                             throw new SVDRPParseResponseException("Begin: " + e.getMessage(), e);
73                         }
74                         try {
75                             entry.setDuration(Integer.parseInt(lt.nextToken()) / 60);
76                         } catch (NumberFormatException | NoSuchElementException e) {
77                             throw new SVDRPParseResponseException("Duration: " + e.getMessage(), e);
78                         }
79                         entry.setEnd(entry.getBegin().plus(entry.getDuration(), ChronoUnit.MINUTES));
80                     default:
81                         break;
82                 }
83             } else if (!line.startsWith("End")) {
84                 throw new SVDRPParseResponseException("EPG Event Line corrupt: " + line);
85             }
86         }
87
88         return entry;
89     }
90
91     /**
92      * Get Title of EPG Event
93      *
94      * @return Event Title
95      */
96     public String getTitle() {
97         return title;
98     }
99
100     /**
101      * Set Title of EPG Event
102      *
103      * @param title Event Title
104      */
105     public void setTitle(String title) {
106         this.title = title;
107     }
108
109     /**
110      * Get Subtitle of EPG Event
111      *
112      * @return Event Subtitle
113      */
114     public String getSubtitle() {
115         return subtitle;
116     }
117
118     /**
119      * Set Subtitle of EPG Event
120      *
121      * @param subtitle Event Subtitle
122      */
123     public void setSubtitle(String subtitle) {
124         this.subtitle = subtitle;
125     }
126
127     /**
128      * Get Begin of EPG Event
129      *
130      * @return Event Begin
131      */
132     public Instant getBegin() {
133         return begin;
134     }
135
136     /**
137      * Set Begin of EPG Event
138      *
139      * @param begin Event Begin
140      */
141     public void setBegin(Instant begin) {
142         this.begin = begin;
143     }
144
145     /**
146      * Get End of EPG Event
147      *
148      * @return Event End
149      */
150     public Instant getEnd() {
151         return end;
152     }
153
154     /**
155      * Set End of EPG Event
156      *
157      * @param end Event End
158      */
159     public void setEnd(Instant end) {
160         this.end = end;
161     }
162
163     /**
164      * Get Duration of EPG Event in Minutes
165      *
166      * @return Event Duration in Minutes
167      */
168     public int getDuration() {
169         return duration;
170     }
171
172     /**
173      * Set Duration of EPG Event in Minutes
174      *
175      * @param duration Event Duration in Minutes
176      */
177     public void setDuration(int duration) {
178         this.duration = duration;
179     }
180
181     /**
182      * String Representation of SVDRPDiskStatus Object
183      */
184     @Override
185     public String toString() {
186         StringBuilder sb = new StringBuilder();
187
188         sb.append("Title: ");
189         sb.append(title);
190         sb.append(System.lineSeparator());
191
192         sb.append("Subtitle: ");
193         sb.append(subtitle);
194         sb.append(System.lineSeparator());
195
196         sb.append("Begin: ");
197         sb.append(begin);
198         sb.append(System.lineSeparator());
199
200         sb.append("End: ");
201         sb.append(end);
202         sb.append(System.lineSeparator());
203
204         if (duration > -1) {
205             sb.append("Duration: ");
206             sb.append(duration);
207             sb.append(System.lineSeparator());
208         }
209         return sb.toString();
210     }
211 }