]> git.basschouten.com Git - openhab-addons.git/blob
b71d55d54d07211a8832eeaa22ee6bc77728847e
[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.wrapper;
14
15 import java.time.ZoneId;
16 import java.util.Optional;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.volvooncall.internal.dto.Position;
21 import org.openhab.binding.volvooncall.internal.dto.PositionData;
22 import org.openhab.core.library.types.DateTimeType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.PointType;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.UnDefType;
27
28 /**
29  * The {@link VehiclePositionWrapper} stores provides utility functions
30  * over a {@link Position} provided by the rest API
31  *
32  * @author GaĆ«l L'hopital - Initial contribution
33  */
34 @NonNullByDefault
35 public class VehiclePositionWrapper {
36     private final Optional<PositionData> position;
37     private boolean isCalculated;
38
39     public VehiclePositionWrapper(Position vehicle) {
40         if (vehicle.calculatedPosition != null && vehicle.position.latitude != null) {
41             position = Optional.of(vehicle.position);
42             isCalculated = false;
43         } else if (vehicle.calculatedPosition != null && vehicle.calculatedPosition.latitude != null) {
44             position = Optional.of(vehicle.calculatedPosition);
45             isCalculated = true;
46         } else {
47             position = Optional.empty();
48         }
49     }
50
51     private State getPositionAsState(PositionData details) {
52         if (details.latitude != null && details.longitude != null) {
53             return new PointType(details.latitude + "," + details.longitude);
54         }
55         return UnDefType.NULL;
56     }
57
58     public State getPosition() {
59         return position.map(pos -> getPositionAsState(pos)).orElse(UnDefType.UNDEF);
60     }
61
62     public @Nullable String getPositionAsJSon() {
63         if (getPosition() != UnDefType.UNDEF) {
64             StringBuilder json = new StringBuilder("{\"clientLatitude\":");
65             json.append(position.get().latitude);
66             json.append(",\"clientLongitude\":");
67             json.append(position.get().longitude);
68             json.append(",\"clientAccuracy\":0}");
69
70             return json.toString();
71         }
72         return null;
73     }
74
75     public State isCalculated() {
76         return position.map(pos -> (State) OnOffType.from(isCalculated)).orElse(UnDefType.UNDEF);
77     }
78
79     public State isHeading() {
80         return position.map(pos -> (State) OnOffType.from(pos.isHeading())).orElse(UnDefType.UNDEF);
81     }
82
83     public State getTimestamp() {
84         return position.flatMap(pos -> pos.getTimestamp())
85                 .map(dt -> (State) new DateTimeType(dt.withZoneSameInstant(ZoneId.systemDefault())))
86                 .orElse(UnDefType.NULL);
87     }
88 }