2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.gpstracker.internal.message.dto;
15 import java.math.BigDecimal;
16 import java.time.ZoneId;
17 import java.time.ZonedDateTime;
18 import java.util.Date;
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;
30 import com.google.gson.annotations.SerializedName;
33 * The {@link LocationMessage} is a POJO for location messages sent bz trackers.
35 * @author Gabor Bicskei - Initial contribution
38 public class LocationMessage {
43 @SerializedName("_type")
44 private String type = "";
47 * Tracker ID used to display the initials of a user (iOS,Android/string/optional) required for http mode
49 @SerializedName("tid")
50 private String trackerId = "";
53 * Altitude (iOS, Android/float/meters/optional)
55 @SerializedName("alt")
56 private BigDecimal altitude = BigDecimal.ZERO;
59 * Latitude (iOS, Android/float/meters/required)
61 @SerializedName("lat")
62 private BigDecimal latitude = BigDecimal.ZERO;
65 * Longitude (iOS,Android/float/meters/required)
67 @SerializedName("lon")
68 private BigDecimal longitude = BigDecimal.ZERO;
73 @SerializedName("acc")
74 private @Nullable BigDecimal gpsAccuracy;
77 * Battery level (iOS,Android/integer/percent/optional)
79 @SerializedName("batt")
80 private Integer batteryLevel = Integer.MIN_VALUE;
83 * Timestamp at which the event occurred (iOS,Android/integer/epoch/required)
85 @SerializedName("tst")
86 private Long timestampMillis = Long.MIN_VALUE;
88 public String getTrackerId() {
89 return trackerId.replaceAll("[^a-zA-Z0-9_]", "");
93 * Converts event timestamp onto DateTimeType
95 * @return Conversion result
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);
103 return UnDefType.UNDEF;
107 * Converts tracker coordinates into PointType
109 * @return Conversion result
111 public State getTrackerLocation() {
112 if (!BigDecimal.ZERO.equals(latitude) && !BigDecimal.ZERO.equals(longitude)
113 && !BigDecimal.ZERO.equals(altitude)) {
114 return new PointType(new DecimalType(latitude), new DecimalType(longitude), new DecimalType(altitude));
115 } else if (!BigDecimal.ZERO.equals(latitude) && !BigDecimal.ZERO.equals(longitude)) {
116 return new PointType(new DecimalType(latitude), new DecimalType(longitude));
118 return UnDefType.UNDEF;
122 * Converts battery level into DecimalType
124 * @return Conversion result
126 public State getBatteryLevel() {
127 if (batteryLevel != Integer.MIN_VALUE) {
128 return new DecimalType(batteryLevel);
130 return UnDefType.UNDEF;
133 public State getGpsAccuracy() {
134 BigDecimal gpsAccuracyLocal = gpsAccuracy;
135 if (gpsAccuracyLocal != null) {
136 return new QuantityType<>(gpsAccuracyLocal.intValue(), SIUnits.METRE);
138 return UnDefType.UNDEF;
142 public String toString() {
143 return "LocationMessage [" + ("type=" + type + ", ") + ("trackerId=" + trackerId + ", ")
144 + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("altitude=" + altitude + ", ")
145 + (gpsAccuracy != null ? "gpsAccuracy=" + gpsAccuracy + ", " : "")
146 + ("batteryLevel=" + batteryLevel + ", ") + ("timestampMillis=" + timestampMillis) + "]";