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;
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 * Latitude (iOS, Android/float/meters/required)
55 @SerializedName("lat")
56 private BigDecimal latitude = BigDecimal.ZERO;
59 * Longitude (iOS,Android/float/meters/required)
61 @SerializedName("lon")
62 private BigDecimal longitude = BigDecimal.ZERO;
67 @SerializedName("acc")
68 private @Nullable BigDecimal gpsAccuracy;
71 * Battery level (iOS,Android/integer/percent/optional)
73 @SerializedName("batt")
74 private Integer batteryLevel = Integer.MIN_VALUE;
77 * Timestamp at which the event occurred (iOS,Android/integer/epoch/required)
79 @SerializedName("tst")
80 private Long timestampMillis = Long.MIN_VALUE;
82 public String getTrackerId() {
83 return trackerId.replaceAll("[^a-zA-Z0-9_]", "");
87 * Converts event timestamp onto DateTimeType
89 * @return Conversion result
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);
97 return UnDefType.UNDEF;
101 * Converts tracker coordinates into PointType
103 * @return Conversion result
105 public State getTrackerLocation() {
106 if (!BigDecimal.ZERO.equals(latitude) && !BigDecimal.ZERO.equals(longitude)) {
107 return new PointType(new DecimalType(latitude), new DecimalType(longitude));
109 return UnDefType.UNDEF;
113 * Converts battery level into DecimalType
115 * @return Conversion result
117 public State getBatteryLevel() {
118 if (batteryLevel != Integer.MIN_VALUE) {
119 return new DecimalType(batteryLevel);
121 return UnDefType.UNDEF;
124 public State getGpsAccuracy() {
125 if (gpsAccuracy != null) {
126 return new QuantityType<>(gpsAccuracy.intValue(), SIUnits.METRE);
128 return UnDefType.UNDEF;
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) + "]";