]> git.basschouten.com Git - openhab-addons.git/blob
a3c1baa87cba2df67064c838b88e60db25a07b51
[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;
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      * Latitude (iOS, Android/float/meters/required)
54      */
55     @SerializedName("lat")
56     private BigDecimal latitude = BigDecimal.ZERO;
57
58     /**
59      * Longitude (iOS,Android/float/meters/required)
60      */
61     @SerializedName("lon")
62     private BigDecimal longitude = BigDecimal.ZERO;
63
64     /**
65      * GPS accuracy
66      */
67     @SerializedName("acc")
68     private @Nullable BigDecimal gpsAccuracy;
69
70     /**
71      * Battery level (iOS,Android/integer/percent/optional)
72      */
73     @SerializedName("batt")
74     private Integer batteryLevel = Integer.MIN_VALUE;
75
76     /**
77      * Timestamp at which the event occurred (iOS,Android/integer/epoch/required)
78      */
79     @SerializedName("tst")
80     private Long timestampMillis = Long.MIN_VALUE;
81
82     public String getTrackerId() {
83         return trackerId.replaceAll("[^a-zA-Z0-9_]", "");
84     }
85
86     /**
87      * Converts event timestamp onto DateTimeType
88      *
89      * @return Conversion result
90      */
91     public State getTimestamp() {
92         if (timestampMillis != Long.MIN_VALUE) {
93             ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(new Date(timestampMillis * 1000).toInstant(),
94                     ZoneId.systemDefault());
95             return new DateTimeType(zonedDateTime);
96         }
97         return UnDefType.UNDEF;
98     }
99
100     /**
101      * Converts tracker coordinates into PointType
102      *
103      * @return Conversion result
104      */
105     public State getTrackerLocation() {
106         if (!BigDecimal.ZERO.equals(latitude) && !BigDecimal.ZERO.equals(longitude)) {
107             return new PointType(new DecimalType(latitude), new DecimalType(longitude));
108         }
109         return UnDefType.UNDEF;
110     }
111
112     /**
113      * Converts battery level into DecimalType
114      *
115      * @return Conversion result
116      */
117     public State getBatteryLevel() {
118         if (batteryLevel != Integer.MIN_VALUE) {
119             return new DecimalType(batteryLevel);
120         }
121         return UnDefType.UNDEF;
122     }
123
124     public State getGpsAccuracy() {
125         if (gpsAccuracy != null) {
126             return new QuantityType<>(gpsAccuracy.intValue(), SIUnits.METRE);
127         }
128         return UnDefType.UNDEF;
129     }
130
131     @Override
132     public String toString() {
133         return "LocationMessage [" + ("type=" + type + ", ") + ("trackerId=" + trackerId + ", ")
134                 + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ")
135                 + (gpsAccuracy != null ? "gpsAccuracy=" + gpsAccuracy + ", " : "")
136                 + ("batteryLevel=" + batteryLevel + ", ") + ("timestampMillis=" + timestampMillis) + "]";
137     }
138 }