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.volvooncall.internal.dto;
15 import static org.openhab.binding.volvooncall.internal.VolvoOnCallBindingConstants.UNDEFINED;
17 import java.time.Duration;
18 import java.time.ZoneId;
19 import java.time.ZonedDateTime;
20 import java.time.temporal.Temporal;
21 import java.util.Optional;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.library.types.DateTimeType;
26 import org.openhab.core.library.types.PointType;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.UnDefType;
31 * The {@link TripDetail} is responsible for storing
32 * trip details returned by trip rest answer
34 * @author Gaƫl L'hopital - Initial contribution
37 public class TripDetail {
38 private @Nullable Integer fuelConsumption;
39 private @Nullable Integer electricalConsumption;
40 private @Nullable Integer electricalRegeneration;
41 public int distance = UNDEFINED;
42 public int startOdometer = UNDEFINED;
43 public int endOdometer = UNDEFINED;
44 private @Nullable ZonedDateTime endTime;
45 private @Nullable ZonedDateTime startTime;
46 private @NonNullByDefault({}) PositionData startPosition;
47 private @NonNullByDefault({}) PositionData endPosition;
49 private State ZonedDateTimeToState(@Nullable ZonedDateTime datetime) {
50 return datetime != null ? new DateTimeType(datetime.withZoneSameInstant(ZoneId.systemDefault()))
54 private State getPositionAsState(PositionData details) {
55 if (details.latitude != null && details.longitude != null) {
56 return new PointType(details.latitude + "," + details.longitude);
58 return UnDefType.NULL;
61 public State getStartTime() {
62 return ZonedDateTimeToState(startTime);
65 public State getStartPosition() {
66 return getPositionAsState(startPosition);
69 public State getEndTime() {
70 return ZonedDateTimeToState(endTime);
73 public State getEndPosition() {
74 return getPositionAsState(endPosition);
77 public Optional<Long> getDurationInMinutes() {
78 Temporal start = startTime;
79 Temporal end = endTime;
80 if (start == null || end == null) {
81 return Optional.empty();
83 return Optional.of(Duration.between(start, end).toMinutes());
87 public Optional<Integer> getFuelConsumption() {
88 Integer fuelConsumption = this.fuelConsumption;
89 if (fuelConsumption != null) {
90 return Optional.of(fuelConsumption);
92 return Optional.empty();
95 public Optional<Integer> getElectricalConsumption() {
96 Integer electricalConsumption = this.electricalConsumption;
97 if (electricalConsumption != null) {
98 return Optional.of(electricalConsumption);
100 return Optional.empty();
103 public Optional<Integer> getElectricalRegeneration() {
104 Integer electricalRegeneration = this.electricalRegeneration;
105 if (electricalRegeneration != null) {
106 return Optional.of(electricalRegeneration);
108 return Optional.empty();