import org.openhab.binding.enturno.internal.connection.EnturCommunicationException;
import org.openhab.binding.enturno.internal.connection.EnturConfigurationException;
import org.openhab.binding.enturno.internal.connection.EnturNoConnection;
-import org.openhab.binding.enturno.internal.model.simplified.DisplayData;
+import org.openhab.binding.enturno.internal.dto.simplified.DisplayData;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.thing.Channel;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
-import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.http.HttpHeader;
import org.openhab.binding.enturno.internal.EnturNoConfiguration;
import org.openhab.binding.enturno.internal.EnturNoHandler;
-import org.openhab.binding.enturno.internal.model.EnturJsonData;
-import org.openhab.binding.enturno.internal.model.estimated.EstimatedCalls;
-import org.openhab.binding.enturno.internal.model.simplified.DisplayData;
-import org.openhab.binding.enturno.internal.model.stopplace.StopPlace;
+import org.openhab.binding.enturno.internal.dto.EnturJsonData;
+import org.openhab.binding.enturno.internal.dto.estimated.EstimatedCalls;
+import org.openhab.binding.enturno.internal.dto.simplified.DisplayData;
+import org.openhab.binding.enturno.internal.dto.stopplace.StopPlace;
+import org.openhab.binding.enturno.internal.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
}
private List<DisplayData> processData(StopPlace stopPlace, String lineCode) {
- Map<String, List<EstimatedCalls>> departures = stopPlace.estimatedCalls.stream()
- .filter(call -> StringUtils.equalsIgnoreCase(
- StringUtils.trimToEmpty(call.serviceJourney.journeyPattern.line.publicCode),
- StringUtils.trimToEmpty(lineCode)))
+ Map<String, List<EstimatedCalls>> departures = stopPlace.estimatedCalls.stream().filter(
+ call -> call.serviceJourney.journeyPattern.line.publicCode.strip().equalsIgnoreCase(lineCode.strip()))
.collect(groupingBy(call -> call.quay.id));
List<DisplayData> processedData = new ArrayList<>();
List<String> keys = new ArrayList<>(departures.keySet());
DisplayData processedData = new DisplayData();
List<EstimatedCalls> quayCalls = departures.get(keys.get(quayIndex));
- List<String> departureTimes = quayCalls.stream().map(eq -> eq.expectedDepartureTime).map(this::getIsoDateTime)
- .collect(Collectors.toList());
+ List<String> departureTimes = quayCalls.stream().map(eq -> eq.expectedDepartureTime)
+ .map(DateUtil::getIsoDateTime).collect(Collectors.toList());
List<String> estimatedFlags = quayCalls.stream().map(es -> es.realtime).collect(Collectors.toList());
processedData.transportMode = stopPlace.transportMode;
return processedData;
}
-
- private String getIsoDateTime(String dateTimeWithoutColonInZone) {
- String dateTime = StringUtils.substringBeforeLast(dateTimeWithoutColonInZone, "+");
- String offset = StringUtils.substringAfterLast(dateTimeWithoutColonInZone, "+");
-
- StringBuilder builder = new StringBuilder();
- return builder.append(dateTime).append("+").append(StringUtils.substring(offset, 0, 2)).append(":00")
- .toString();
- }
}
--- /dev/null
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.enturno.internal.dto;
+
+import org.openhab.binding.enturno.internal.dto.stopplace.StopPlace;
+
+/**
+ * Generated Plain Old Java Objects class for {@link Data} from JSON.
+ *
+ * @author Michal Kloc - Initial contribution
+ */
+public class Data {
+ public StopPlace stopPlace;
+}
--- /dev/null
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.enturno.internal.dto;
+
+/**
+ * Generated Plain Old Java Objects class for {@link Data} from JSON.
+ *
+ * @author Michal Kloc - Initial contribution
+ */
+public class EnturData {
+ public Data data;
+}
--- /dev/null
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.enturno.internal.dto;
+
+/**
+ * {@link EnturJsonData} is a root level class to holding reference of data generated from JSON.
+ *
+ * @author Michal Kloc - Initial contribution
+ */
+public class EnturJsonData {
+ public Data data;
+}
--- /dev/null
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.enturno.internal.dto.estimated;
+
+/**
+ * Generated Plain Old Java Objects class for {@link DestinationDisplay} from JSON.
+ *
+ * @author Michal Kloc - Initial contribution
+ */
+public class DestinationDisplay {
+ public String frontText;
+}
--- /dev/null
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.enturno.internal.dto.estimated;
+
+/**
+ * Generated Plain Old Java Objects class for {@link EstimatedCalls} from JSON.
+ *
+ * @author Michal Kloc - Initial contribution
+ */
+public class EstimatedCalls {
+ public String aimedArrivalTime;
+
+ public String date;
+
+ public String realtime;
+
+ public String expectedDepartureTime;
+
+ public String forAlighting;
+
+ public DestinationDisplay destinationDisplay;
+
+ public String forBoarding;
+
+ public ServiceJourney serviceJourney;
+
+ public Quay quay;
+
+ public String aimedDepartureTime;
+
+ public String expectedArrivalTime;
+}
--- /dev/null
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.enturno.internal.dto.estimated;
+
+/**
+ * Generated Plain Old Java Objects class for {@link JourneyPattern} from JSON.
+ *
+ * @author Michal Kloc - Initial contribution
+ */
+public class JourneyPattern {
+ public Line line;
+}
--- /dev/null
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.enturno.internal.dto.estimated;
+
+/**
+ * Generated Plain Old Java Objects class for {@link Line} from JSON.
+ *
+ * @author Michal Kloc - Initial contribution
+ */
+public class Line {
+ public String name;
+
+ public String transportMode;
+
+ public String publicCode;
+
+ public String id;
+}
--- /dev/null
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.enturno.internal.dto.estimated;
+
+/**
+ * Generated Plain Old Java Objects class for {@link Quay} from JSON.
+ *
+ * @author Michal Kloc - Initial contribution
+ */
+public class Quay {
+ public String publicCode;
+
+ public String id;
+}
--- /dev/null
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.enturno.internal.dto.estimated;
+
+/**
+ * Generated Plain Old Java Objects class for {@link ServiceJourney} from JSON.
+ *
+ * @author Michal Kloc - Initial contribution
+ */
+public class ServiceJourney {
+ public JourneyPattern journeyPattern;
+}
--- /dev/null
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.enturno.internal.dto.simplified;
+
+import java.util.List;
+
+/**
+ * {@link DisplayData} is a Plain Old Java Objects class to wrap only needed data after processing API call results.
+ *
+ * @author Michal Kloc - Initial contribution
+ */
+public class DisplayData {
+ public String stopPlaceId;
+
+ public String stopName;
+
+ public String transportMode;
+
+ public String lineCode;
+
+ public String frontText;
+
+ public List<String> departures;
+
+ public List<String> estimatedFlags;
+}
--- /dev/null
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.enturno.internal.dto.stopplace;
+
+import org.openhab.binding.enturno.internal.dto.estimated.EstimatedCalls;
+
+/**
+ * Generated Plain Old Java Objects class for {@link StopPlace} from JSON.
+ *
+ * @author Michal Kloc - Initial contribution
+ */
+public class StopPlace {
+ public java.util.List<EstimatedCalls> estimatedCalls;
+
+ public String name;
+
+ public String id;
+
+ public String transportMode;
+}
+++ /dev/null
-/**
- * Copyright (c) 2010-2023 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.binding.enturno.internal.model;
-
-import org.openhab.binding.enturno.internal.model.stopplace.StopPlace;
-
-/**
- * Generated Plain Old Java Objects class for {@link Data} from JSON.
- *
- * @author Michal Kloc - Initial contribution
- */
-public class Data {
- public StopPlace stopPlace;
-}
+++ /dev/null
-/**
- * Copyright (c) 2010-2023 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.binding.enturno.internal.model;
-
-/**
- * Generated Plain Old Java Objects class for {@link Data} from JSON.
- *
- * @author Michal Kloc - Initial contribution
- */
-public class EnturData {
- public Data data;
-}
+++ /dev/null
-/**
- * Copyright (c) 2010-2023 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.binding.enturno.internal.model;
-
-/**
- * {@link EnturJsonData} is a root level class to holding reference of data generated from JSON.
- *
- * @author Michal Kloc - Initial contribution
- */
-public class EnturJsonData {
- public Data data;
-}
+++ /dev/null
-/**
- * Copyright (c) 2010-2023 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.binding.enturno.internal.model.estimated;
-
-/**
- * Generated Plain Old Java Objects class for {@link DestinationDisplay} from JSON.
- *
- * @author Michal Kloc - Initial contribution
- */
-public class DestinationDisplay {
- public String frontText;
-}
+++ /dev/null
-/**
- * Copyright (c) 2010-2023 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.binding.enturno.internal.model.estimated;
-
-/**
- * Generated Plain Old Java Objects class for {@link EstimatedCalls} from JSON.
- *
- * @author Michal Kloc - Initial contribution
- */
-public class EstimatedCalls {
- public String aimedArrivalTime;
-
- public String date;
-
- public String realtime;
-
- public String expectedDepartureTime;
-
- public String forAlighting;
-
- public DestinationDisplay destinationDisplay;
-
- public String forBoarding;
-
- public ServiceJourney serviceJourney;
-
- public Quay quay;
-
- public String aimedDepartureTime;
-
- public String expectedArrivalTime;
-}
+++ /dev/null
-/**
- * Copyright (c) 2010-2023 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.binding.enturno.internal.model.estimated;
-
-/**
- * Generated Plain Old Java Objects class for {@link JourneyPattern} from JSON.
- *
- * @author Michal Kloc - Initial contribution
- */
-public class JourneyPattern {
- public Line line;
-}
+++ /dev/null
-/**
- * Copyright (c) 2010-2023 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.binding.enturno.internal.model.estimated;
-
-/**
- * Generated Plain Old Java Objects class for {@link Line} from JSON.
- *
- * @author Michal Kloc - Initial contribution
- */
-public class Line {
- public String name;
-
- public String transportMode;
-
- public String publicCode;
-
- public String id;
-}
+++ /dev/null
-/**
- * Copyright (c) 2010-2023 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.binding.enturno.internal.model.estimated;
-
-/**
- * Generated Plain Old Java Objects class for {@link Quay} from JSON.
- *
- * @author Michal Kloc - Initial contribution
- */
-public class Quay {
- public String publicCode;
-
- public String id;
-}
+++ /dev/null
-/**
- * Copyright (c) 2010-2023 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.binding.enturno.internal.model.estimated;
-
-/**
- * Generated Plain Old Java Objects class for {@link ServiceJourney} from JSON.
- *
- * @author Michal Kloc - Initial contribution
- */
-public class ServiceJourney {
- public JourneyPattern journeyPattern;
-}
+++ /dev/null
-/**
- * Copyright (c) 2010-2023 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.binding.enturno.internal.model.simplified;
-
-import java.util.List;
-
-/**
- * {@link DisplayData} is a Plain Old Java Objects class to wrap only needed data after processing API call results.
- *
- * @author Michal Kloc - Initial contribution
- */
-public class DisplayData {
- public String stopPlaceId;
-
- public String stopName;
-
- public String transportMode;
-
- public String lineCode;
-
- public String frontText;
-
- public List<String> departures;
-
- public List<String> estimatedFlags;
-}
+++ /dev/null
-/**
- * Copyright (c) 2010-2023 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.binding.enturno.internal.model.stopplace;
-
-import org.openhab.binding.enturno.internal.model.estimated.EstimatedCalls;
-
-/**
- * Generated Plain Old Java Objects class for {@link StopPlace} from JSON.
- *
- * @author Michal Kloc - Initial contribution
- */
-public class StopPlace {
- public java.util.List<EstimatedCalls> estimatedCalls;
-
- public String name;
-
- public String id;
-
- public String transportMode;
-}
--- /dev/null
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.enturno.internal.util;
+
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
+/**
+ * EnturNo date utility methods.
+ *
+ * @author Jacob Laursen - Initial contribution
+ */
+@NonNullByDefault
+public class DateUtil {
+ /**
+ * Converts a zoned date time string that lacks a colon in the zone to an ISO-8601 formatted string.
+ *
+ * @param dateTimeWithoutColonInZone
+ * @return ISO-8601 formatted string
+ */
+ public static String getIsoDateTime(String dateTimeWithoutColonInZone) {
+ ZonedDateTime zonedDateTime = null;
+ try {
+ zonedDateTime = ZonedDateTime.parse(dateTimeWithoutColonInZone);
+ } catch (DateTimeParseException e) {
+ // Skip
+ }
+ try {
+ zonedDateTime = ZonedDateTime.parse(dateTimeWithoutColonInZone.replaceAll("(\\d{2})(\\d{2})$", "$1:$2"));
+ } catch (DateTimeParseException e) {
+ // Skip
+ }
+ if (zonedDateTime != null) {
+ return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(zonedDateTime);
+ }
+ return dateTimeWithoutColonInZone;
+ }
+}
--- /dev/null
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.enturno.internal.util;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import java.util.stream.Stream;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+/**
+ * Tests for {@link DateUtil}.
+ *
+ * @author Jacob Laursen - Initial contribution
+ */
+@NonNullByDefault
+public class DateUtilTest {
+ @ParameterizedTest
+ @MethodSource("provideTestCasesForGetIsoDateTime")
+ void getIsoDateTime(String value, String expected) {
+ assertThat(DateUtil.getIsoDateTime(value), is(expected));
+ }
+
+ private static Stream<Arguments> provideTestCasesForGetIsoDateTime() {
+ return Stream.of( //
+ Arguments.of("2023-10-25T09:01:00+0200", "2023-10-25T09:01:00+02:00"),
+ Arguments.of("2023-10-25T09:01:00+02:00", "2023-10-25T09:01:00+02:00"),
+ Arguments.of("2023-10-25T09:01:00-0300", "2023-10-25T09:01:00-03:00"),
+ Arguments.of("2023-10-25T09:01:00+02:30", "2023-10-25T09:01:00+02:30"),
+ Arguments.of("2023-10-25T09:01:00", "2023-10-25T09:01:00"));
+ }
+}