]> git.basschouten.com Git - openhab-addons.git/blob
a75beecece0413ed38015c96f315c57e373127f7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.volvooncall.internal.dto;
14
15 import static org.openhab.binding.volvooncall.internal.VolvoOnCallBindingConstants.UNDEFINED;
16
17 import java.time.Duration;
18 import java.time.ZoneId;
19 import java.time.ZonedDateTime;
20 import java.util.Optional;
21
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;
28
29 /**
30  * The {@link TripDetail} is responsible for storing
31  * trip details returned by trip rest answer
32  *
33  * @author GaĆ«l L'hopital - Initial contribution
34  */
35 @NonNullByDefault
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;
47
48     private State ZonedDateTimeToState(@Nullable ZonedDateTime datetime) {
49         return datetime != null ? new DateTimeType(datetime.withZoneSameInstant(ZoneId.systemDefault()))
50                 : UnDefType.NULL;
51     }
52
53     private State getPositionAsState(PositionData details) {
54         if (details.latitude != null && details.longitude != null) {
55             return new PointType(details.latitude + "," + details.longitude);
56         }
57         return UnDefType.NULL;
58     }
59
60     public State getStartTime() {
61         return ZonedDateTimeToState(startTime);
62     }
63
64     public State getStartPosition() {
65         return getPositionAsState(startPosition);
66     }
67
68     public State getEndTime() {
69         return ZonedDateTimeToState(endTime);
70     }
71
72     public State getEndPosition() {
73         return getPositionAsState(endPosition);
74     }
75
76     public long getDurationInMinutes() {
77         return Duration.between(startTime, endTime).toMinutes();
78     }
79
80     public Optional<Integer> getFuelConsumption() {
81         Integer fuelConsumption = this.fuelConsumption;
82         if (fuelConsumption != null) {
83             return Optional.of(fuelConsumption);
84         }
85         return Optional.empty();
86     }
87
88     public Optional<Integer> getElectricalConsumption() {
89         Integer electricalConsumption = this.electricalConsumption;
90         if (electricalConsumption != null) {
91             return Optional.of(electricalConsumption);
92         }
93         return Optional.empty();
94     }
95
96     public Optional<Integer> getElectricalRegeneration() {
97         Integer electricalRegeneration = this.electricalRegeneration;
98         if (electricalRegeneration != null) {
99             return Optional.of(electricalRegeneration);
100         }
101         return Optional.empty();
102     }
103 }