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.vdr.internal.svdrp;
15 import java.time.Instant;
16 import java.time.temporal.ChronoUnit;
17 import java.util.NoSuchElementException;
18 import java.util.StringTokenizer;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
23 * The {@link SVDRPEpgEvent} contains SVDRP Response Data for an EPG Event
25 * @author Matthias Klocke - Initial contribution
28 public class SVDRPEpgEvent {
35 private String title = "";
36 private String subtitle = "";
37 private Instant begin = Instant.now();
38 private Instant end = Instant.now();
41 private SVDRPEpgEvent() {
45 * parse object from SVDRP Client Response
47 * @param message SVDRP Client Response
48 * @return SVDRPEpgEvent Object
49 * @throws SVDRPParseResponseException thrown if response data is not parseable
51 public static SVDRPEpgEvent parse(String message) throws SVDRPParseResponseException {
52 SVDRPEpgEvent entry = new SVDRPEpgEvent();
53 StringTokenizer st = new StringTokenizer(message, System.lineSeparator());
55 while (st.hasMoreTokens()) {
56 String line = st.nextToken();
57 if (line.length() >= 1 && !line.startsWith("End")) {
58 switch (line.charAt(0)) {
60 entry.setTitle(line.substring(1).trim());
63 entry.setSubtitle(line.substring(1).trim());
66 StringTokenizer lt = new StringTokenizer(line.substring(1).trim(), " ");
67 lt.nextToken(); // event id
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);
75 entry.setDuration(Integer.parseInt(lt.nextToken()) / 60);
76 } catch (NumberFormatException | NoSuchElementException e) {
77 throw new SVDRPParseResponseException("Duration: " + e.getMessage(), e);
79 entry.setEnd(entry.getBegin().plus(entry.getDuration(), ChronoUnit.MINUTES));
83 } else if (!line.startsWith("End")) {
84 throw new SVDRPParseResponseException("EPG Event Line corrupt: " + line);
92 * Get Title of EPG Event
96 public String getTitle() {
101 * Set Title of EPG Event
103 * @param title Event Title
105 public void setTitle(String title) {
110 * Get Subtitle of EPG Event
112 * @return Event Subtitle
114 public String getSubtitle() {
119 * Set Subtitle of EPG Event
121 * @param subtitle Event Subtitle
123 public void setSubtitle(String subtitle) {
124 this.subtitle = subtitle;
128 * Get Begin of EPG Event
130 * @return Event Begin
132 public Instant getBegin() {
137 * Set Begin of EPG Event
139 * @param begin Event Begin
141 public void setBegin(Instant begin) {
146 * Get End of EPG Event
150 public Instant getEnd() {
155 * Set End of EPG Event
157 * @param end Event End
159 public void setEnd(Instant end) {
164 * Get Duration of EPG Event in Minutes
166 * @return Event Duration in Minutes
168 public int getDuration() {
173 * Set Duration of EPG Event in Minutes
175 * @param duration Event Duration in Minutes
177 public void setDuration(int duration) {
178 this.duration = duration;
182 * String Representation of SVDRPDiskStatus Object
185 public String toString() {
186 StringBuilder sb = new StringBuilder();
188 sb.append("Title: ");
190 sb.append(System.lineSeparator());
192 sb.append("Subtitle: ");
194 sb.append(System.lineSeparator());
196 sb.append("Begin: ");
198 sb.append(System.lineSeparator());
202 sb.append(System.lineSeparator());
205 sb.append("Duration: ");
207 sb.append(System.lineSeparator());
209 return sb.toString();