2 * Copyright (c) 2010-2020 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.util.Optional;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.library.types.DateTimeType;
25 import org.openhab.core.library.types.PointType;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
30 * The {@link TripDetail} is responsible for storing
31 * trip details returned by trip rest answer
33 * @author Gaƫl L'hopital - Initial contribution
36 public class TripDetail {
37 private @Nullable Integer fuelConsumption;
38 private @Nullable Integer electricalConsumption;
39 private @Nullable Integer electricalRegeneration;
40 public int distance = UNDEFINED;
41 public int startOdometer = UNDEFINED;
42 public int endOdometer = UNDEFINED;
43 private @Nullable ZonedDateTime endTime;
44 private @Nullable ZonedDateTime startTime;
45 private @NonNullByDefault({}) PositionData startPosition;
46 private @NonNullByDefault({}) PositionData endPosition;
48 private State ZonedDateTimeToState(@Nullable ZonedDateTime datetime) {
49 return datetime != null ? new DateTimeType(datetime.withZoneSameInstant(ZoneId.systemDefault()))
53 private State getPositionAsState(PositionData details) {
54 if (details.latitude != null && details.longitude != null) {
55 return new PointType(details.latitude + "," + details.longitude);
57 return UnDefType.NULL;
60 public State getStartTime() {
61 return ZonedDateTimeToState(startTime);
64 public State getStartPosition() {
65 return getPositionAsState(startPosition);
68 public State getEndTime() {
69 return ZonedDateTimeToState(endTime);
72 public State getEndPosition() {
73 return getPositionAsState(endPosition);
76 public long getDurationInMinutes() {
77 return Duration.between(startTime, endTime).toMinutes();
80 public Optional<Integer> getFuelConsumption() {
81 Integer fuelConsumption = this.fuelConsumption;
82 if (fuelConsumption != null) {
83 return Optional.of(fuelConsumption);
85 return Optional.empty();
88 public Optional<Integer> getElectricalConsumption() {
89 Integer electricalConsumption = this.electricalConsumption;
90 if (electricalConsumption != null) {
91 return Optional.of(electricalConsumption);
93 return Optional.empty();
96 public Optional<Integer> getElectricalRegeneration() {
97 Integer electricalRegeneration = this.electricalRegeneration;
98 if (electricalRegeneration != null) {
99 return Optional.of(electricalRegeneration);
101 return Optional.empty();