2 * Copyright (c) 2010-2021 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.deutschebahn.internal;
15 import java.text.ParseException;
16 import java.text.SimpleDateFormat;
17 import java.time.ZoneId;
18 import java.time.ZonedDateTime;
19 import java.util.Arrays;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.function.BiConsumer;
23 import java.util.function.Function;
24 import java.util.stream.Collectors;
25 import java.util.stream.Stream;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.binding.deutschebahn.internal.timetable.dto.Event;
30 import org.openhab.binding.deutschebahn.internal.timetable.dto.EventStatus;
31 import org.openhab.binding.deutschebahn.internal.timetable.dto.Message;
32 import org.openhab.core.library.types.DateTimeType;
33 import org.openhab.core.library.types.DecimalType;
34 import org.openhab.core.library.types.OnOffType;
35 import org.openhab.core.library.types.StringType;
36 import org.openhab.core.types.State;
39 * Selector for the Attribute of an {@link Event}.
41 * chapter "1.2.11 Event" in Technical Interface Description for external Developers
43 * @see https://developer.deutschebahn.com/store/apis/info?name=Timetables&version=v1&provider=DBOpenData&#tab1
45 * @author Sönke Küper - initial contribution
47 * @param <VALUE_TYPE> type of value in Bean.
48 * @param <STATE_TYPE> type of state.
51 public final class EventAttribute<VALUE_TYPE, STATE_TYPE extends State>
52 extends AbstractDtoAttributeSelector<Event, @Nullable VALUE_TYPE, STATE_TYPE> {
57 public static final EventAttribute<String, StringType> PPTH = new EventAttribute<>("planned-path", Event::getPpth,
58 Event::setPpth, StringType::new, StringType.class);
63 public static final EventAttribute<String, StringType> CPTH = new EventAttribute<>("changed-path", Event::getCpth,
64 Event::setCpth, StringType::new, StringType.class);
68 public static final EventAttribute<String, StringType> PP = new EventAttribute<>("planned-platform", Event::getPp,
69 Event::setPp, StringType::new, StringType.class);
73 public static final EventAttribute<String, StringType> CP = new EventAttribute<>("changed-platform", Event::getCp,
74 Event::setCp, StringType::new, StringType.class);
78 public static final EventAttribute<Date, DateTimeType> PT = new EventAttribute<>("planned-time",
79 getDate(Event::getPt), setDate(Event::setPt), EventAttribute::createDateTimeType, DateTimeType.class);
83 public static final EventAttribute<Date, DateTimeType> CT = new EventAttribute<>("changed-time",
84 getDate(Event::getCt), setDate(Event::setCt), EventAttribute::createDateTimeType, DateTimeType.class);
88 public static final EventAttribute<EventStatus, StringType> PS = new EventAttribute<>("planned-status",
89 Event::getPs, Event::setPs, EventAttribute::fromEventStatus, StringType.class);
93 public static final EventAttribute<EventStatus, StringType> CS = new EventAttribute<>("changed-status",
94 Event::getCs, Event::setCs, EventAttribute::fromEventStatus, StringType.class);
98 public static final EventAttribute<Integer, OnOffType> HI = new EventAttribute<>("hidden", Event::getHi,
99 Event::setHi, EventAttribute::parseHidden, OnOffType.class);
103 public static final EventAttribute<Date, DateTimeType> CLT = new EventAttribute<>("cancellation-time",
104 getDate(Event::getClt), setDate(Event::setClt), EventAttribute::createDateTimeType, DateTimeType.class);
108 public static final EventAttribute<String, StringType> WINGS = new EventAttribute<>("wings", Event::getWings,
109 Event::setWings, StringType::new, StringType.class);
113 public static final EventAttribute<String, StringType> TRA = new EventAttribute<>("transition", Event::getTra,
114 Event::setTra, StringType::new, StringType.class);
116 * Planned distant endpoint.
118 public static final EventAttribute<String, StringType> PDE = new EventAttribute<>("planned-distant-endpoint",
119 Event::getPde, Event::setPde, StringType::new, StringType.class);
121 * Changed distant endpoint.
123 public static final EventAttribute<String, StringType> CDE = new EventAttribute<>("changed-distant-endpoint",
124 Event::getCde, Event::setCde, StringType::new, StringType.class);
128 public static final EventAttribute<Integer, DecimalType> DC = new EventAttribute<>("distant-change", Event::getDc,
129 Event::setDc, DecimalType::new, DecimalType.class);
133 public static final EventAttribute<String, StringType> L = new EventAttribute<>("line", Event::getL, Event::setL,
134 StringType::new, StringType.class);
139 public static final EventAttribute<List<Message>, StringType> MESSAGES = new EventAttribute<>("messages",
140 EventAttribute.getMessages(), EventAttribute::setMessages, EventAttribute::mapMessages, StringType.class);
143 * Planned Start station.
145 public static final EventAttribute<String, StringType> PLANNED_START_STATION = new EventAttribute<>(
146 "planned-start-station", EventAttribute.getSingleStationFromPath(Event::getPpth, true),
147 EventAttribute.voidSetter(), StringType::new, StringType.class);
150 * Planned Previous stations.
152 public static final EventAttribute<String, StringType> PLANNED_PREVIOUS_STATIONS = new EventAttribute<>(
153 "planned-previous-stations", EventAttribute.getIntermediateStationsFromPath(Event::getPpth, true),
154 EventAttribute.voidSetter(), StringType::new, StringType.class);
157 * Planned Target station.
159 public static final EventAttribute<String, StringType> PLANNED_TARGET_STATION = new EventAttribute<>(
160 "planned-target-station", EventAttribute.getSingleStationFromPath(Event::getPpth, false),
161 EventAttribute.voidSetter(), StringType::new, StringType.class);
164 * Planned Following stations.
166 public static final EventAttribute<String, StringType> PLANNED_FOLLOWING_STATIONS = new EventAttribute<>(
167 "planned-following-stations", EventAttribute.getIntermediateStationsFromPath(Event::getPpth, false),
168 EventAttribute.voidSetter(), StringType::new, StringType.class);
171 * Changed Start station.
173 public static final EventAttribute<String, StringType> CHANGED_START_STATION = new EventAttribute<>(
174 "changed-start-station", EventAttribute.getSingleStationFromPath(Event::getCpth, true),
175 EventAttribute.voidSetter(), StringType::new, StringType.class);
178 * Changed Previous stations.
180 public static final EventAttribute<String, StringType> CHANGED_PREVIOUS_STATIONS = new EventAttribute<>(
181 "changed-previous-stations", EventAttribute.getIntermediateStationsFromPath(Event::getCpth, true),
182 EventAttribute.voidSetter(), StringType::new, StringType.class);
185 * Changed Target station.
187 public static final EventAttribute<String, StringType> CHANGED_TARGET_STATION = new EventAttribute<>(
188 "changed-target-station", EventAttribute.getSingleStationFromPath(Event::getCpth, false),
189 EventAttribute.voidSetter(), StringType::new, StringType.class);
192 * Changed Following stations.
194 public static final EventAttribute<String, StringType> CHANGED_FOLLOWING_STATIONS = new EventAttribute<>(
195 "changed-following-stations", EventAttribute.getIntermediateStationsFromPath(Event::getCpth, false),
196 EventAttribute.voidSetter(), StringType::new, StringType.class);
199 * List containing all known {@link EventAttribute}.
201 public static final List<EventAttribute<?, ?>> ALL_ATTRIBUTES = Arrays.asList(PPTH, CPTH, PP, CP, PT, CT, PS, CS,
202 HI, CLT, WINGS, TRA, PDE, CDE, DC, L, MESSAGES);
204 private static final SimpleDateFormat DATETIME_FORMAT = new SimpleDateFormat("yyMMddHHmm");
207 * Creates an new {@link EventAttribute}.
209 * @param getter Function to get the raw value.
210 * @param setter Function to set the raw value.
211 * @param getState Function to get the Value as {@link State}.
213 private EventAttribute(final String channelTypeName, //
214 final Function<Event, @Nullable VALUE_TYPE> getter, //
215 final BiConsumer<Event, VALUE_TYPE> setter, //
216 final Function<VALUE_TYPE, @Nullable STATE_TYPE> getState, //
217 final Class<STATE_TYPE> stateType) {
218 super(channelTypeName, getter, setter, getState, stateType);
221 private static StringType fromEventStatus(final EventStatus value) {
222 return new StringType(value.value());
225 private static OnOffType parseHidden(@Nullable Integer value) {
226 return OnOffType.from(value != null && value == 1);
229 private static Function<Event, @Nullable Date> getDate(final Function<Event, @Nullable String> getValue) {
230 return (final Event event) -> {
231 return parseDate(getValue.apply(event));
235 private static BiConsumer<Event, Date> setDate(final BiConsumer<Event, String> setter) {
236 return (final Event event, final Date value) -> {
237 synchronized (DATETIME_FORMAT) {
238 String formattedDate = DATETIME_FORMAT.format(value);
239 setter.accept(event, formattedDate);
244 private static void setMessages(Event event, List<Message> messages) {
245 event.getM().clear();
246 event.getM().addAll(messages);
250 private static synchronized Date parseDate(@Nullable final String dateValue) {
251 if ((dateValue == null) || dateValue.isEmpty()) {
255 synchronized (DATETIME_FORMAT) {
256 return DATETIME_FORMAT.parse(dateValue);
258 } catch (final ParseException e) {
264 private static DateTimeType createDateTimeType(final @Nullable Date value) {
268 final ZonedDateTime d = ZonedDateTime.ofInstant(value.toInstant(), ZoneId.systemDefault());
269 return new DateTimeType(d);
274 * Maps the status codes from the messages into status texts.
277 private static StringType mapMessages(final @Nullable List<Message> messages) {
278 if (messages == null || messages.isEmpty()) {
279 return StringType.EMPTY;
281 final String messageTexts = messages //
283 .filter((Message message) -> message.getC() != null) //
284 .map(Message::getC) //
286 .map(MessageCodes::getMessage) //
287 .filter((String messageText) -> !messageText.isEmpty()) //
288 .collect(Collectors.joining(" - "));
290 return new StringType(messageTexts);
294 private static Function<Event, @Nullable List<Message>> getMessages() {
295 return new Function<Event, @Nullable List<Message>>() {
298 public @Nullable List<Message> apply(Event t) {
299 if (t.getM().isEmpty()) {
309 * Returns an single station from an path value (i.e. pipe separated value of stations).
311 * @param getPath Getter for the path.
312 * @param returnFirst if <code>true</code> the first value will be returned, <code>false</code> will return the last
315 private static Function<Event, @Nullable String> getSingleStationFromPath(
316 final Function<Event, @Nullable String> getPath, boolean returnFirst) {
317 return (final Event event) -> {
318 String path = getPath.apply(event);
319 if (path == null || path.isEmpty()) {
323 final String[] stations = splitPath(path);
327 return stations[stations.length - 1];
333 * Returns all intermediate stations from an path. The first or last station will be omitted. The values will be
334 * separated by an single dash -.
336 * @param getPath Getter for the path.
337 * @param removeFirst if <code>true</code> the first value will be removed, <code>false</code> will remove the last
340 private static Function<Event, @Nullable String> getIntermediateStationsFromPath(
341 final Function<Event, @Nullable String> getPath, boolean removeFirst) {
342 return (final Event event) -> {
343 final String path = getPath.apply(event);
344 if (path == null || path.isEmpty()) {
347 final String[] stationValues = splitPath(path);
348 Stream<String> stations = Arrays.stream(stationValues);
350 stations = stations.skip(1);
352 stations = stations.limit(stationValues.length - 1);
354 return stations.collect(Collectors.joining(" - "));
359 * Setter that does nothing.
360 * Used for derived attributes that can't be set.
362 private static <VALUE_TYPE> BiConsumer<Event, VALUE_TYPE> voidSetter() {
363 return new BiConsumer<Event, VALUE_TYPE>() {
366 public void accept(Event t, VALUE_TYPE u) {
371 private static String[] splitPath(final String path) {
372 return path.split("\\|");
376 * Returns an {@link EventAttribute} for the given channel-type and {@link EventType}.
379 public static EventAttribute<?, ?> getByChannelName(final String channelName, EventType eventType) {
380 switch (channelName) {
385 case "planned-platform":
387 case "changed-platform":
393 case "planned-status":
395 case "changed-status":
399 case "cancellation-time":
405 case "planned-distant-endpoint":
407 case "changed-distant-endpoint":
409 case "distant-change":
415 case "planned-final-station":
416 return eventType == EventType.ARRIVAL ? PLANNED_START_STATION : PLANNED_TARGET_STATION;
417 case "planned-intermediate-stations":
418 return eventType == EventType.ARRIVAL ? PLANNED_PREVIOUS_STATIONS : PLANNED_FOLLOWING_STATIONS;
419 case "changed-final-station":
420 return eventType == EventType.ARRIVAL ? CHANGED_START_STATION : CHANGED_TARGET_STATION;
421 case "changed-intermediate-stations":
422 return eventType == EventType.ARRIVAL ? CHANGED_PREVIOUS_STATIONS : CHANGED_FOLLOWING_STATIONS;