]> git.basschouten.com Git - openhab-addons.git/blob
ddb4ac258bbe84324b39157e06e2c3b86333e011
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.time.temporal.Temporal;
21 import java.util.Optional;
22
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;
29
30 /**
31  * The {@link TripDetail} is responsible for storing
32  * trip details returned by trip rest answer
33  *
34  * @author GaĆ«l L'hopital - Initial contribution
35  */
36 @NonNullByDefault
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;
48
49     private State ZonedDateTimeToState(@Nullable ZonedDateTime datetime) {
50         return datetime != null ? new DateTimeType(datetime.withZoneSameInstant(ZoneId.systemDefault()))
51                 : UnDefType.NULL;
52     }
53
54     private State getPositionAsState(PositionData details) {
55         if (details.latitude != null && details.longitude != null) {
56             return new PointType(details.latitude + "," + details.longitude);
57         }
58         return UnDefType.NULL;
59     }
60
61     public State getStartTime() {
62         return ZonedDateTimeToState(startTime);
63     }
64
65     public State getStartPosition() {
66         return getPositionAsState(startPosition);
67     }
68
69     public State getEndTime() {
70         return ZonedDateTimeToState(endTime);
71     }
72
73     public State getEndPosition() {
74         return getPositionAsState(endPosition);
75     }
76
77     public Optional<Long> getDurationInMinutes() {
78         Temporal start = startTime;
79         Temporal end = endTime;
80         if (start == null || end == null) {
81             return Optional.empty();
82         } else {
83             return Optional.of(Duration.between(start, end).toMinutes());
84         }
85     }
86
87     public Optional<Integer> getFuelConsumption() {
88         Integer fuelConsumption = this.fuelConsumption;
89         if (fuelConsumption != null) {
90             return Optional.of(fuelConsumption);
91         }
92         return Optional.empty();
93     }
94
95     public Optional<Integer> getElectricalConsumption() {
96         Integer electricalConsumption = this.electricalConsumption;
97         if (electricalConsumption != null) {
98             return Optional.of(electricalConsumption);
99         }
100         return Optional.empty();
101     }
102
103     public Optional<Integer> getElectricalRegeneration() {
104         Integer electricalRegeneration = this.electricalRegeneration;
105         if (electricalRegeneration != null) {
106             return Optional.of(electricalRegeneration);
107         }
108         return Optional.empty();
109     }
110 }