]> git.basschouten.com Git - openhab-addons.git/blob
05ad029c4183d186e45616bb37f54b6df046baca
[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.gpstracker.internal.message.dto;
14
15 import java.math.BigDecimal;
16 import java.time.ZoneId;
17 import java.time.ZonedDateTime;
18 import java.util.Date;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.library.types.DateTimeType;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.library.types.PointType;
25 import org.openhab.core.library.types.QuantityType;
26 import org.openhab.core.library.unit.SIUnits;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.UnDefType;
29
30 import com.google.gson.annotations.SerializedName;
31
32 /**
33  * The {@link LocationMessage} is a POJO for location messages sent bz trackers.
34  *
35  * @author Gabor Bicskei - Initial contribution
36  */
37 @NonNullByDefault
38 public class LocationMessage {
39
40     /**
41      * Message type
42      */
43     @SerializedName("_type")
44     private String type = "";
45
46     /**
47      * Tracker ID used to display the initials of a user (iOS,Android/string/optional) required for http mode
48      */
49     @SerializedName("tid")
50     private String trackerId = "";
51
52     /**
53      * Altitude (iOS, Android/integer/meters/optional)
54      */
55     @SerializedName("alt")
56     private Integer altitude = Integer.MIN_VALUE;
57
58     /**
59      * Latitude (iOS, Android/float/meters/required)
60      */
61     @SerializedName("lat")
62     private BigDecimal latitude = BigDecimal.ZERO;
63
64     /**
65      * Longitude (iOS,Android/float/meters/required)
66      */
67     @SerializedName("lon")
68     private BigDecimal longitude = BigDecimal.ZERO;
69
70     /**
71      * GPS accuracy
72      */
73     @SerializedName("acc")
74     private @Nullable BigDecimal gpsAccuracy;
75
76     /**
77      * Battery level (iOS,Android/integer/percent/optional)
78      */
79     @SerializedName("batt")
80     private Integer batteryLevel = Integer.MIN_VALUE;
81
82     /**
83      * Timestamp at which the event occurred (iOS,Android/integer/epoch/required)
84      */
85     @SerializedName("tst")
86     private Long timestampMillis = Long.MIN_VALUE;
87
88     public String getTrackerId() {
89         return trackerId.replaceAll("[^a-zA-Z0-9_]", "");
90     }
91
92     /**
93      * Converts event timestamp onto DateTimeType
94      *
95      * @return Conversion result
96      */
97     public State getTimestamp() {
98         if (timestampMillis != Long.MIN_VALUE) {
99             ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(new Date(timestampMillis * 1000).toInstant(),
100                     ZoneId.systemDefault());
101             return new DateTimeType(zonedDateTime);
102         }
103         return UnDefType.UNDEF;
104     }
105
106     /**
107      * Converts tracker coordinates into PointType
108      *
109      * @return Conversion result
110      */
111     public State getTrackerLocation() {
112         if (!BigDecimal.ZERO.equals(latitude) && !BigDecimal.ZERO.equals(longitude) && Integer.MIN_VALUE != altitude) {
113             return new PointType(new DecimalType(latitude), new DecimalType(longitude), new DecimalType(altitude));
114         } else if (!BigDecimal.ZERO.equals(latitude) && !BigDecimal.ZERO.equals(longitude)) {
115             return new PointType(new DecimalType(latitude), new DecimalType(longitude));
116         }
117         return UnDefType.UNDEF;
118     }
119
120     /**
121      * Converts battery level into DecimalType
122      *
123      * @return Conversion result
124      */
125     public State getBatteryLevel() {
126         if (batteryLevel != Integer.MIN_VALUE) {
127             return new DecimalType(batteryLevel);
128         }
129         return UnDefType.UNDEF;
130     }
131
132     public State getGpsAccuracy() {
133         BigDecimal gpsAccuracyLocal = gpsAccuracy;
134         if (gpsAccuracyLocal != null) {
135             return new QuantityType<>(gpsAccuracyLocal.intValue(), SIUnits.METRE);
136         }
137         return UnDefType.UNDEF;
138     }
139
140     @Override
141     public String toString() {
142         return "LocationMessage [" + ("type=" + type + ", ") + ("trackerId=" + trackerId + ", ")
143                 + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("altitude=" + altitude + ", ")
144                 + (gpsAccuracy != null ? "gpsAccuracy=" + gpsAccuracy + ", " : "")
145                 + ("batteryLevel=" + batteryLevel + ", ") + ("timestampMillis=" + timestampMillis) + "]";
146     }
147 }