]> git.basschouten.com Git - openhab-addons.git/commitdiff
[gpstracker] Add (optional) altitude to location channel (#14396)
authorlsiepel <leosiepel@gmail.com>
Tue, 7 Mar 2023 22:23:30 +0000 (23:23 +0100)
committerGitHub <noreply@github.com>
Tue, 7 Mar 2023 22:23:30 +0000 (23:23 +0100)
* Add (optional) altitude to location channel
* Add null annotations
* Extend toString with altitude

Signed-off-by: lsiepel <leosiepel@gmail.com>
13 files changed:
bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/GPSTrackerBindingConstants.java
bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/config/ConfigHelper.java
bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/handler/TrackerHandler.java
bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/message/LocationMessage.java [deleted file]
bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/message/MessageUtil.java
bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/message/NotificationBroker.java
bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/message/NotificationHandler.java
bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/message/TransitionMessage.java [deleted file]
bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/message/dto/LocationMessage.java [new file with mode: 0644]
bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/message/dto/TransitionMessage.java [new file with mode: 0644]
bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/provider/AbstractCallbackServlet.java
bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/provider/gpslogger/GPSLoggerCallbackServlet.java
bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/provider/owntracks/OwnTracksCallbackServlet.java

index dccc80b91abd865aea792f641249674b66ff38db..a9579f08271a7a7aa5058663fbbed7751a9f9a9d 100644 (file)
@@ -16,6 +16,7 @@ import java.util.Set;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.openhab.core.thing.ThingTypeUID;
 import org.openhab.core.thing.type.ChannelTypeUID;
 
@@ -24,6 +25,7 @@ import org.openhab.core.thing.type.ChannelTypeUID;
  *
  * @author Gabor Bicskei - Initial contribution
  */
+@NonNullByDefault
 public abstract class GPSTrackerBindingConstants {
     public static final String BINDING_ID = "gpstracker";
     static final String CONFIG_PID = "binding." + BINDING_ID;
index 0f436e1941847209dd08cbe7c5e0d645892ab9cc..908680085298091c1cbb65ef7767af097039a6f1 100644 (file)
@@ -14,6 +14,8 @@ package org.openhab.binding.gpstracker.internal.config;
 
 import java.math.BigDecimal;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.core.config.core.Configuration;
 import org.openhab.core.library.types.PointType;
 
@@ -22,6 +24,7 @@ import org.openhab.core.library.types.PointType;
  *
  * @author Gabor Bicskei - Initial contribution
  */
+@NonNullByDefault
 public class ConfigHelper {
     // configuration constants
     public static final String CONFIG_TRACKER_ID = "trackerId";
@@ -53,7 +56,7 @@ public class ConfigHelper {
         return (String) config.get(CONFIG_TRACKER_ID);
     }
 
-    public static PointType getRegionCenterLocation(Configuration config) {
+    public static @Nullable PointType getRegionCenterLocation(Configuration config) {
         String location = (String) config.get(CONFIG_REGION_CENTER_LOCATION);
         return location != null ? new PointType(location) : null;
     }
index 0f12ab127e4da19931eee2c4d6421b6b687d321e..136dc48219634b1753dfb25d0ad1a9843c006c07 100644 (file)
@@ -26,13 +26,13 @@ import java.util.stream.Collectors;
 import javax.measure.Unit;
 import javax.measure.quantity.Length;
 
-import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.gpstracker.internal.config.ConfigHelper;
-import org.openhab.binding.gpstracker.internal.message.LocationMessage;
 import org.openhab.binding.gpstracker.internal.message.NotificationBroker;
 import org.openhab.binding.gpstracker.internal.message.NotificationHandler;
-import org.openhab.binding.gpstracker.internal.message.TransitionMessage;
+import org.openhab.binding.gpstracker.internal.message.dto.LocationMessage;
+import org.openhab.binding.gpstracker.internal.message.dto.TransitionMessage;
 import org.openhab.core.config.core.Configuration;
 import org.openhab.core.i18n.UnitProvider;
 import org.openhab.core.library.types.PointType;
@@ -60,6 +60,7 @@ import org.slf4j.LoggerFactory;
  *
  * @author Gabor Bicskei - Initial contribution
  */
+@NonNullByDefault
 public class TrackerHandler extends BaseThingHandler {
     /**
      * Trigger events
@@ -105,17 +106,17 @@ public class TrackerHandler extends BaseThingHandler {
     /**
      * System location
      */
-    private PointType sysLocation;
+    private @Nullable PointType sysLocation;
 
     /**
      * Unit provider
      */
-    private UnitProvider unitProvider;
+    private @Nullable UnitProvider unitProvider;
 
     /**
      * Last message received from the tracker
      */
-    private LocationMessage lastMessage;
+    private @Nullable LocationMessage lastMessage;
 
     /**
      * Constructor.
@@ -127,7 +128,7 @@ public class TrackerHandler extends BaseThingHandler {
      * @param unitProvider Unit provider
      */
     public TrackerHandler(Thing thing, NotificationBroker notificationBroker, Set<String> regions,
-            PointType sysLocation, UnitProvider unitProvider) {
+            @Nullable PointType sysLocation, @Nullable UnitProvider unitProvider) {
         super(thing);
 
         this.notificationBroker = notificationBroker;
@@ -174,15 +175,15 @@ public class TrackerHandler extends BaseThingHandler {
             ChannelUID systemDistanceChannelUID = new ChannelUID(thing.getUID(), CHANNEL_DISTANCE_SYSTEM_ID);
             Channel systemDistance = thing.getChannel(CHANNEL_DISTANCE_SYSTEM_ID);
             ChannelBuilder channelBuilder = null;
+            String sysLocationString = sysLocation == null ? "unknown" : sysLocation.toFullString();
             if (systemDistance != null) {
-                if (!systemDistance.getConfiguration().get(CONFIG_REGION_CENTER_LOCATION)
-                        .equals(sysLocation.toFullString())) {
+                if (!systemDistance.getConfiguration().get(CONFIG_REGION_CENTER_LOCATION).equals(sysLocationString)) {
                     logger.trace("Existing distance channel for system. Changing system location config parameter: {}",
-                            sysLocation.toFullString());
+                            sysLocationString);
 
                     channelBuilder = callback.editChannel(thing, systemDistanceChannelUID);
                     Configuration configToUpdate = systemDistance.getConfiguration();
-                    configToUpdate.put(CONFIG_REGION_CENTER_LOCATION, sysLocation.toFullString());
+                    configToUpdate.put(CONFIG_REGION_CENTER_LOCATION, sysLocationString);
                     channelBuilder.withConfiguration(configToUpdate);
                 } else {
                     logger.trace("Existing distance channel for system. No change.");
@@ -192,7 +193,7 @@ public class TrackerHandler extends BaseThingHandler {
 
                 Configuration config = new Configuration();
                 config.put(ConfigHelper.CONFIG_REGION_NAME, CHANNEL_DISTANCE_SYSTEM_NAME);
-                config.put(CONFIG_REGION_CENTER_LOCATION, sysLocation.toFullString());
+                config.put(CONFIG_REGION_CENTER_LOCATION, sysLocationString);
                 config.put(ConfigHelper.CONFIG_REGION_RADIUS, CHANNEL_DISTANCE_SYSTEM_RADIUS);
                 config.put(ConfigHelper.CONFIG_ACCURACY_THRESHOLD, 0);
 
@@ -230,26 +231,27 @@ public class TrackerHandler extends BaseThingHandler {
 
     @Override
     public void handleCommand(ChannelUID channelUID, Command command) {
-        if (command instanceof RefreshType && lastMessage != null) {
+        LocationMessage lastMessageLocal = lastMessage;
+        if (command instanceof RefreshType && lastMessageLocal != null) {
             String channelId = channelUID.getId();
             switch (channelId) {
                 case CHANNEL_LAST_REPORT:
-                    updateBaseChannels(lastMessage, CHANNEL_LAST_REPORT);
+                    updateBaseChannels(lastMessageLocal, CHANNEL_LAST_REPORT);
                     break;
                 case CHANNEL_LAST_LOCATION:
-                    updateBaseChannels(lastMessage, CHANNEL_LAST_LOCATION);
+                    updateBaseChannels(lastMessageLocal, CHANNEL_LAST_LOCATION);
                     break;
                 case CHANNEL_BATTERY_LEVEL:
-                    updateBaseChannels(lastMessage, CHANNEL_BATTERY_LEVEL);
+                    updateBaseChannels(lastMessageLocal, CHANNEL_BATTERY_LEVEL);
                     break;
                 case CHANNEL_GPS_ACCURACY:
-                    updateBaseChannels(lastMessage, CHANNEL_GPS_ACCURACY);
+                    updateBaseChannels(lastMessageLocal, CHANNEL_GPS_ACCURACY);
                     break;
                 default: // distance channels
                     @Nullable
                     Channel channel = thing.getChannel(channelId);
                     if (channel != null) {
-                        updateDistanceChannelFromMessage(lastMessage, channel);
+                        updateDistanceChannelFromMessage(lastMessageLocal, channel);
                     }
             }
         }
@@ -272,7 +274,7 @@ public class TrackerHandler extends BaseThingHandler {
      * @param event Occurred event
      * @param forced Force channel triggering in case the transition event is received from the mobile application.
      */
-    private void triggerRegionChannel(@NonNull String regionName, @NonNull String event, boolean forced) {
+    private void triggerRegionChannel(String regionName, String event, boolean forced) {
         Boolean lastState = lastTriggeredStates.get(regionName);
         Boolean newState = EVENT_ENTER.equals(event);
         if (!newState.equals(lastState) || forced) {
@@ -341,9 +343,10 @@ public class TrackerHandler extends BaseThingHandler {
     }
 
     private double convertToMeters(double valueToConvert) {
-        if (unitProvider != null) {
+        UnitProvider unitProviderLocal = unitProvider;
+        if (unitProviderLocal != null) {
             @Nullable
-            Unit<Length> unit = unitProvider.getUnit(Length.class);
+            Unit<Length> unit = unitProviderLocal.getUnit(Length.class);
             if (unit != null && !SIUnits.METRE.equals(unit)) {
                 double value = ImperialUnits.YARD.getConverterTo(SIUnits.METRE).convert(valueToConvert);
                 logger.trace("Value converted: {}yd->{}m", valueToConvert, value);
diff --git a/bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/message/LocationMessage.java b/bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/message/LocationMessage.java
deleted file mode 100644 (file)
index a3c1baa..0000000
+++ /dev/null
@@ -1,138 +0,0 @@
-/**
- * Copyright (c) 2010-2023 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.binding.gpstracker.internal.message;
-
-import java.math.BigDecimal;
-import java.time.ZoneId;
-import java.time.ZonedDateTime;
-import java.util.Date;
-
-import org.eclipse.jdt.annotation.NonNullByDefault;
-import org.eclipse.jdt.annotation.Nullable;
-import org.openhab.core.library.types.DateTimeType;
-import org.openhab.core.library.types.DecimalType;
-import org.openhab.core.library.types.PointType;
-import org.openhab.core.library.types.QuantityType;
-import org.openhab.core.library.unit.SIUnits;
-import org.openhab.core.types.State;
-import org.openhab.core.types.UnDefType;
-
-import com.google.gson.annotations.SerializedName;
-
-/**
- * The {@link LocationMessage} is a POJO for location messages sent bz trackers.
- *
- * @author Gabor Bicskei - Initial contribution
- */
-@NonNullByDefault
-public class LocationMessage {
-
-    /**
-     * Message type
-     */
-    @SerializedName("_type")
-    private String type = "";
-
-    /**
-     * Tracker ID used to display the initials of a user (iOS,Android/string/optional) required for http mode
-     */
-    @SerializedName("tid")
-    private String trackerId = "";
-
-    /**
-     * Latitude (iOS, Android/float/meters/required)
-     */
-    @SerializedName("lat")
-    private BigDecimal latitude = BigDecimal.ZERO;
-
-    /**
-     * Longitude (iOS,Android/float/meters/required)
-     */
-    @SerializedName("lon")
-    private BigDecimal longitude = BigDecimal.ZERO;
-
-    /**
-     * GPS accuracy
-     */
-    @SerializedName("acc")
-    private @Nullable BigDecimal gpsAccuracy;
-
-    /**
-     * Battery level (iOS,Android/integer/percent/optional)
-     */
-    @SerializedName("batt")
-    private Integer batteryLevel = Integer.MIN_VALUE;
-
-    /**
-     * Timestamp at which the event occurred (iOS,Android/integer/epoch/required)
-     */
-    @SerializedName("tst")
-    private Long timestampMillis = Long.MIN_VALUE;
-
-    public String getTrackerId() {
-        return trackerId.replaceAll("[^a-zA-Z0-9_]", "");
-    }
-
-    /**
-     * Converts event timestamp onto DateTimeType
-     *
-     * @return Conversion result
-     */
-    public State getTimestamp() {
-        if (timestampMillis != Long.MIN_VALUE) {
-            ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(new Date(timestampMillis * 1000).toInstant(),
-                    ZoneId.systemDefault());
-            return new DateTimeType(zonedDateTime);
-        }
-        return UnDefType.UNDEF;
-    }
-
-    /**
-     * Converts tracker coordinates into PointType
-     *
-     * @return Conversion result
-     */
-    public State getTrackerLocation() {
-        if (!BigDecimal.ZERO.equals(latitude) && !BigDecimal.ZERO.equals(longitude)) {
-            return new PointType(new DecimalType(latitude), new DecimalType(longitude));
-        }
-        return UnDefType.UNDEF;
-    }
-
-    /**
-     * Converts battery level into DecimalType
-     *
-     * @return Conversion result
-     */
-    public State getBatteryLevel() {
-        if (batteryLevel != Integer.MIN_VALUE) {
-            return new DecimalType(batteryLevel);
-        }
-        return UnDefType.UNDEF;
-    }
-
-    public State getGpsAccuracy() {
-        if (gpsAccuracy != null) {
-            return new QuantityType<>(gpsAccuracy.intValue(), SIUnits.METRE);
-        }
-        return UnDefType.UNDEF;
-    }
-
-    @Override
-    public String toString() {
-        return "LocationMessage [" + ("type=" + type + ", ") + ("trackerId=" + trackerId + ", ")
-                + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ")
-                + (gpsAccuracy != null ? "gpsAccuracy=" + gpsAccuracy + ", " : "")
-                + ("batteryLevel=" + batteryLevel + ", ") + ("timestampMillis=" + timestampMillis) + "]";
-    }
-}
index eaf849083b32134b9d80ddbe969c7cc5c1436118..ad2d894259ca99a2cf848bb4398b25950807b7b4 100644 (file)
@@ -15,6 +15,11 @@ package org.openhab.binding.gpstracker.internal.message;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.openhab.binding.gpstracker.internal.message.dto.LocationMessage;
+import org.openhab.binding.gpstracker.internal.message.dto.TransitionMessage;
+
 import com.google.gson.Gson;
 
 /**
@@ -22,6 +27,7 @@ import com.google.gson.Gson;
  *
  * @author Gabor Bicskei - Initial contribution
  */
+@NonNullByDefault
 public class MessageUtil {
     /**
      * Patterns to identify incoming JSON payload.
@@ -48,7 +54,7 @@ public class MessageUtil {
      * @param json JSON string.
      * @return Parsed message POJO or null without pattern match
      */
-    public LocationMessage fromJson(String json) {
+    public @Nullable LocationMessage fromJson(String json) {
         for (String pattern : PATTERNS) {
             Class<? extends LocationMessage> c = MESSAGE_TYPES.get(pattern);
             if (c != null && json.matches(pattern)) {
index cfb3b5900ee205d4f4c28ca516a5cd0f919fc328..b92a5772a507295483a6dceed3fe34de7012e58d 100644 (file)
@@ -15,11 +15,15 @@ package org.openhab.binding.gpstracker.internal.message;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.openhab.binding.gpstracker.internal.message.dto.LocationMessage;
+
 /**
  * Notification broker.
  *
  * @author Gabor Bicskei - Initial contribution
  */
+@NonNullByDefault
 public class NotificationBroker {
     /**
      * Handlers
index bc7e1eab66998f2e64daf95fb400994397b3845c..446ebd626e60573ab0844022c258ab660c754be2 100644 (file)
@@ -17,11 +17,16 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.openhab.binding.gpstracker.internal.message.dto.LocationMessage;
+import org.openhab.binding.gpstracker.internal.message.dto.TransitionMessage;
+
 /**
  * Handler for notification messages between trackers.
  *
  * @author Gabor Bicskei - Initial contribution
  */
+@NonNullByDefault
 public class NotificationHandler {
     /**
      * Location notifications need to be sent to the own tracker. Only the last location is saved for each tracker
@@ -46,7 +51,9 @@ public class NotificationHandler {
             if (msg instanceof TransitionMessage) {
                 List<TransitionMessage> transitionMessages = transitionNotifications.computeIfAbsent(trackerId,
                         k -> new ArrayList<>());
-                transitionMessages.add((TransitionMessage) msg);
+                if (transitionMessages != null) {
+                    transitionMessages.add((TransitionMessage) msg);
+                }
             } else {
                 locationNotifications.put(trackerId, msg);
             }
diff --git a/bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/message/TransitionMessage.java b/bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/message/TransitionMessage.java
deleted file mode 100644 (file)
index 21ba879..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-/**
- * Copyright (c) 2010-2023 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.binding.gpstracker.internal.message;
-
-import com.google.gson.annotations.SerializedName;
-
-/**
- * TransitionMessage message POJO
- *
- * @author Gabor Bicskei - Initial contribution
- */
-public class TransitionMessage extends LocationMessage {
-
-    /**
-     * Event that triggered the transition (iOS,Android/string/required)
-     * enter The tracker entered the defined geographical region or BLE Beacon range (iOS)
-     * leave The tracker left the defined geographical region or BLE Beacon range (iOS)
-     */
-    @SerializedName("event")
-    String event;
-
-    /**
-     * Name of the waypoint (iOS,Android/string/optional)
-     */
-    @SerializedName("desc")
-    String regionName;
-
-    public String getRegionName() {
-        return regionName;
-    }
-
-    public String getEvent() {
-        return event;
-    }
-}
diff --git a/bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/message/dto/LocationMessage.java b/bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/message/dto/LocationMessage.java
new file mode 100644 (file)
index 0000000..05ad029
--- /dev/null
@@ -0,0 +1,147 @@
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.gpstracker.internal.message.dto;
+
+import java.math.BigDecimal;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+import java.util.Date;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.openhab.core.library.types.DateTimeType;
+import org.openhab.core.library.types.DecimalType;
+import org.openhab.core.library.types.PointType;
+import org.openhab.core.library.types.QuantityType;
+import org.openhab.core.library.unit.SIUnits;
+import org.openhab.core.types.State;
+import org.openhab.core.types.UnDefType;
+
+import com.google.gson.annotations.SerializedName;
+
+/**
+ * The {@link LocationMessage} is a POJO for location messages sent bz trackers.
+ *
+ * @author Gabor Bicskei - Initial contribution
+ */
+@NonNullByDefault
+public class LocationMessage {
+
+    /**
+     * Message type
+     */
+    @SerializedName("_type")
+    private String type = "";
+
+    /**
+     * Tracker ID used to display the initials of a user (iOS,Android/string/optional) required for http mode
+     */
+    @SerializedName("tid")
+    private String trackerId = "";
+
+    /**
+     * Altitude (iOS, Android/integer/meters/optional)
+     */
+    @SerializedName("alt")
+    private Integer altitude = Integer.MIN_VALUE;
+
+    /**
+     * Latitude (iOS, Android/float/meters/required)
+     */
+    @SerializedName("lat")
+    private BigDecimal latitude = BigDecimal.ZERO;
+
+    /**
+     * Longitude (iOS,Android/float/meters/required)
+     */
+    @SerializedName("lon")
+    private BigDecimal longitude = BigDecimal.ZERO;
+
+    /**
+     * GPS accuracy
+     */
+    @SerializedName("acc")
+    private @Nullable BigDecimal gpsAccuracy;
+
+    /**
+     * Battery level (iOS,Android/integer/percent/optional)
+     */
+    @SerializedName("batt")
+    private Integer batteryLevel = Integer.MIN_VALUE;
+
+    /**
+     * Timestamp at which the event occurred (iOS,Android/integer/epoch/required)
+     */
+    @SerializedName("tst")
+    private Long timestampMillis = Long.MIN_VALUE;
+
+    public String getTrackerId() {
+        return trackerId.replaceAll("[^a-zA-Z0-9_]", "");
+    }
+
+    /**
+     * Converts event timestamp onto DateTimeType
+     *
+     * @return Conversion result
+     */
+    public State getTimestamp() {
+        if (timestampMillis != Long.MIN_VALUE) {
+            ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(new Date(timestampMillis * 1000).toInstant(),
+                    ZoneId.systemDefault());
+            return new DateTimeType(zonedDateTime);
+        }
+        return UnDefType.UNDEF;
+    }
+
+    /**
+     * Converts tracker coordinates into PointType
+     *
+     * @return Conversion result
+     */
+    public State getTrackerLocation() {
+        if (!BigDecimal.ZERO.equals(latitude) && !BigDecimal.ZERO.equals(longitude) && Integer.MIN_VALUE != altitude) {
+            return new PointType(new DecimalType(latitude), new DecimalType(longitude), new DecimalType(altitude));
+        } else if (!BigDecimal.ZERO.equals(latitude) && !BigDecimal.ZERO.equals(longitude)) {
+            return new PointType(new DecimalType(latitude), new DecimalType(longitude));
+        }
+        return UnDefType.UNDEF;
+    }
+
+    /**
+     * Converts battery level into DecimalType
+     *
+     * @return Conversion result
+     */
+    public State getBatteryLevel() {
+        if (batteryLevel != Integer.MIN_VALUE) {
+            return new DecimalType(batteryLevel);
+        }
+        return UnDefType.UNDEF;
+    }
+
+    public State getGpsAccuracy() {
+        BigDecimal gpsAccuracyLocal = gpsAccuracy;
+        if (gpsAccuracyLocal != null) {
+            return new QuantityType<>(gpsAccuracyLocal.intValue(), SIUnits.METRE);
+        }
+        return UnDefType.UNDEF;
+    }
+
+    @Override
+    public String toString() {
+        return "LocationMessage [" + ("type=" + type + ", ") + ("trackerId=" + trackerId + ", ")
+                + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("altitude=" + altitude + ", ")
+                + (gpsAccuracy != null ? "gpsAccuracy=" + gpsAccuracy + ", " : "")
+                + ("batteryLevel=" + batteryLevel + ", ") + ("timestampMillis=" + timestampMillis) + "]";
+    }
+}
diff --git a/bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/message/dto/TransitionMessage.java b/bundles/org.openhab.binding.gpstracker/src/main/java/org/openhab/binding/gpstracker/internal/message/dto/TransitionMessage.java
new file mode 100644 (file)
index 0000000..e482ae8
--- /dev/null
@@ -0,0 +1,45 @@
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.gpstracker.internal.message.dto;
+
+import com.google.gson.annotations.SerializedName;
+
+/**
+ * TransitionMessage message POJO
+ *
+ * @author Gabor Bicskei - Initial contribution
+ */
+public class TransitionMessage extends LocationMessage {
+
+    /**
+     * Event that triggered the transition (iOS,Android/string/required)
+     * enter The tracker entered the defined geographical region or BLE Beacon range (iOS)
+     * leave The tracker left the defined geographical region or BLE Beacon range (iOS)
+     */
+    @SerializedName("event")
+    String event;
+
+    /**
+     * Name of the waypoint (iOS,Android/string/optional)
+     */
+    @SerializedName("desc")
+    String regionName;
+
+    public String getRegionName() {
+        return regionName;
+    }
+
+    public String getEvent() {
+        return event;
+    }
+}
index b6bc065987aea11ae406de1dc9c741c20fa3a02a..c92b75fca0a92bdc137e47459e330dce46b0906f 100644 (file)
@@ -20,11 +20,13 @@ import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.gpstracker.internal.discovery.TrackerDiscoveryService;
 import org.openhab.binding.gpstracker.internal.handler.TrackerHandler;
-import org.openhab.binding.gpstracker.internal.message.LocationMessage;
 import org.openhab.binding.gpstracker.internal.message.MessageUtil;
-import org.openhab.binding.gpstracker.internal.message.TransitionMessage;
+import org.openhab.binding.gpstracker.internal.message.dto.LocationMessage;
+import org.openhab.binding.gpstracker.internal.message.dto.TransitionMessage;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -33,6 +35,7 @@ import org.slf4j.LoggerFactory;
  *
  * @author Gabor Bicskei - Initial contribution
  */
+@NonNullByDefault
 public abstract class AbstractCallbackServlet extends HttpServlet {
 
     private static final long serialVersionUID = -2725161358635927815L;
@@ -139,16 +142,15 @@ public abstract class AbstractCallbackServlet extends HttpServlet {
      * @param trackerId Tracker id.
      * @return Handler for tracker.
      */
-    private TrackerHandler getHandlerById(String trackerId) {
-        if (trackerId != null) {
-            TrackerHandler handler = trackerRegistry.getTrackerHandler(trackerId);
-            if (handler == null) {
-                // handler was not found - adding the tracker to discovery service.
-                discoveryService.addTracker(trackerId);
-            } else {
-                return handler;
-            }
+    private @Nullable TrackerHandler getHandlerById(String trackerId) {
+        TrackerHandler handler = trackerRegistry.getTrackerHandler(trackerId);
+        if (handler == null) {
+            // handler was not found - adding the tracker to discovery service.
+            discoveryService.addTracker(trackerId);
+        } else {
+            return handler;
         }
+
         return null;
     }
 
index b044fa90ee2ff8e611e0b23f3ecd10f5bd5e9efd..7a9339bfe0e5c4f4c99f40c0795051a969c040bc 100644 (file)
@@ -12,6 +12,7 @@
  */
 package org.openhab.binding.gpstracker.internal.provider.gpslogger;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.openhab.binding.gpstracker.internal.discovery.TrackerDiscoveryService;
 import org.openhab.binding.gpstracker.internal.provider.AbstractCallbackServlet;
 import org.openhab.binding.gpstracker.internal.provider.TrackerRegistry;
@@ -21,6 +22,7 @@ import org.openhab.binding.gpstracker.internal.provider.TrackerRegistry;
  *
  * @author Gabor Bicskei - Initial contribution
  */
+@NonNullByDefault
 public class GPSLoggerCallbackServlet extends AbstractCallbackServlet {
 
     private static final long serialVersionUID = -6992472786850682196L;
index 649d18b498ee39d89d7f90e3daa0ff010790e1cb..fcfe94f49cd16454b0a9db3b24767c667b202114 100644 (file)
@@ -12,6 +12,7 @@
  */
 package org.openhab.binding.gpstracker.internal.provider.owntracks;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.openhab.binding.gpstracker.internal.discovery.TrackerDiscoveryService;
 import org.openhab.binding.gpstracker.internal.provider.AbstractCallbackServlet;
 import org.openhab.binding.gpstracker.internal.provider.TrackerRegistry;
@@ -21,6 +22,7 @@ import org.openhab.binding.gpstracker.internal.provider.TrackerRegistry;
  *
  * @author Gabor Bicskei - Initial contribution
  */
+@NonNullByDefault
 public class OwnTracksCallbackServlet extends AbstractCallbackServlet {
 
     private static final long serialVersionUID = -4053305903339688036L;