[gpstracker] Add (optional) altitude to location channel (#14396)

* Add (optional) altitude to location channel
* Add null annotations
* Extend toString with altitude

Signed-off-by: lsiepel <leosiepel@gmail.com>
This commit is contained in:
lsiepel 2023-03-07 23:23:30 +01:00 committed by GitHub
parent c9d5784d36
commit 5c0b5711a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 81 additions and 41 deletions

View File

@ -16,6 +16,7 @@ import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.ThingTypeUID; import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.type.ChannelTypeUID; import org.openhab.core.thing.type.ChannelTypeUID;
@ -24,6 +25,7 @@ import org.openhab.core.thing.type.ChannelTypeUID;
* *
* @author Gabor Bicskei - Initial contribution * @author Gabor Bicskei - Initial contribution
*/ */
@NonNullByDefault
public abstract class GPSTrackerBindingConstants { public abstract class GPSTrackerBindingConstants {
public static final String BINDING_ID = "gpstracker"; public static final String BINDING_ID = "gpstracker";
static final String CONFIG_PID = "binding." + BINDING_ID; static final String CONFIG_PID = "binding." + BINDING_ID;

View File

@ -14,6 +14,8 @@ package org.openhab.binding.gpstracker.internal.config;
import java.math.BigDecimal; 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.config.core.Configuration;
import org.openhab.core.library.types.PointType; import org.openhab.core.library.types.PointType;
@ -22,6 +24,7 @@ import org.openhab.core.library.types.PointType;
* *
* @author Gabor Bicskei - Initial contribution * @author Gabor Bicskei - Initial contribution
*/ */
@NonNullByDefault
public class ConfigHelper { public class ConfigHelper {
// configuration constants // configuration constants
public static final String CONFIG_TRACKER_ID = "trackerId"; public static final String CONFIG_TRACKER_ID = "trackerId";
@ -53,7 +56,7 @@ public class ConfigHelper {
return (String) config.get(CONFIG_TRACKER_ID); 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); String location = (String) config.get(CONFIG_REGION_CENTER_LOCATION);
return location != null ? new PointType(location) : null; return location != null ? new PointType(location) : null;
} }

View File

@ -26,13 +26,13 @@ import java.util.stream.Collectors;
import javax.measure.Unit; import javax.measure.Unit;
import javax.measure.quantity.Length; 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.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.gpstracker.internal.config.ConfigHelper; 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.NotificationBroker;
import org.openhab.binding.gpstracker.internal.message.NotificationHandler; 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.config.core.Configuration;
import org.openhab.core.i18n.UnitProvider; import org.openhab.core.i18n.UnitProvider;
import org.openhab.core.library.types.PointType; import org.openhab.core.library.types.PointType;
@ -60,6 +60,7 @@ import org.slf4j.LoggerFactory;
* *
* @author Gabor Bicskei - Initial contribution * @author Gabor Bicskei - Initial contribution
*/ */
@NonNullByDefault
public class TrackerHandler extends BaseThingHandler { public class TrackerHandler extends BaseThingHandler {
/** /**
* Trigger events * Trigger events
@ -105,17 +106,17 @@ public class TrackerHandler extends BaseThingHandler {
/** /**
* System location * System location
*/ */
private PointType sysLocation; private @Nullable PointType sysLocation;
/** /**
* Unit provider * Unit provider
*/ */
private UnitProvider unitProvider; private @Nullable UnitProvider unitProvider;
/** /**
* Last message received from the tracker * Last message received from the tracker
*/ */
private LocationMessage lastMessage; private @Nullable LocationMessage lastMessage;
/** /**
* Constructor. * Constructor.
@ -127,7 +128,7 @@ public class TrackerHandler extends BaseThingHandler {
* @param unitProvider Unit provider * @param unitProvider Unit provider
*/ */
public TrackerHandler(Thing thing, NotificationBroker notificationBroker, Set<String> regions, public TrackerHandler(Thing thing, NotificationBroker notificationBroker, Set<String> regions,
PointType sysLocation, UnitProvider unitProvider) { @Nullable PointType sysLocation, @Nullable UnitProvider unitProvider) {
super(thing); super(thing);
this.notificationBroker = notificationBroker; this.notificationBroker = notificationBroker;
@ -174,15 +175,15 @@ public class TrackerHandler extends BaseThingHandler {
ChannelUID systemDistanceChannelUID = new ChannelUID(thing.getUID(), CHANNEL_DISTANCE_SYSTEM_ID); ChannelUID systemDistanceChannelUID = new ChannelUID(thing.getUID(), CHANNEL_DISTANCE_SYSTEM_ID);
Channel systemDistance = thing.getChannel(CHANNEL_DISTANCE_SYSTEM_ID); Channel systemDistance = thing.getChannel(CHANNEL_DISTANCE_SYSTEM_ID);
ChannelBuilder channelBuilder = null; ChannelBuilder channelBuilder = null;
String sysLocationString = sysLocation == null ? "unknown" : sysLocation.toFullString();
if (systemDistance != null) { if (systemDistance != null) {
if (!systemDistance.getConfiguration().get(CONFIG_REGION_CENTER_LOCATION) if (!systemDistance.getConfiguration().get(CONFIG_REGION_CENTER_LOCATION).equals(sysLocationString)) {
.equals(sysLocation.toFullString())) {
logger.trace("Existing distance channel for system. Changing system location config parameter: {}", logger.trace("Existing distance channel for system. Changing system location config parameter: {}",
sysLocation.toFullString()); sysLocationString);
channelBuilder = callback.editChannel(thing, systemDistanceChannelUID); channelBuilder = callback.editChannel(thing, systemDistanceChannelUID);
Configuration configToUpdate = systemDistance.getConfiguration(); Configuration configToUpdate = systemDistance.getConfiguration();
configToUpdate.put(CONFIG_REGION_CENTER_LOCATION, sysLocation.toFullString()); configToUpdate.put(CONFIG_REGION_CENTER_LOCATION, sysLocationString);
channelBuilder.withConfiguration(configToUpdate); channelBuilder.withConfiguration(configToUpdate);
} else { } else {
logger.trace("Existing distance channel for system. No change."); logger.trace("Existing distance channel for system. No change.");
@ -192,7 +193,7 @@ public class TrackerHandler extends BaseThingHandler {
Configuration config = new Configuration(); Configuration config = new Configuration();
config.put(ConfigHelper.CONFIG_REGION_NAME, CHANNEL_DISTANCE_SYSTEM_NAME); 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_REGION_RADIUS, CHANNEL_DISTANCE_SYSTEM_RADIUS);
config.put(ConfigHelper.CONFIG_ACCURACY_THRESHOLD, 0); config.put(ConfigHelper.CONFIG_ACCURACY_THRESHOLD, 0);
@ -230,26 +231,27 @@ public class TrackerHandler extends BaseThingHandler {
@Override @Override
public void handleCommand(ChannelUID channelUID, Command command) { 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(); String channelId = channelUID.getId();
switch (channelId) { switch (channelId) {
case CHANNEL_LAST_REPORT: case CHANNEL_LAST_REPORT:
updateBaseChannels(lastMessage, CHANNEL_LAST_REPORT); updateBaseChannels(lastMessageLocal, CHANNEL_LAST_REPORT);
break; break;
case CHANNEL_LAST_LOCATION: case CHANNEL_LAST_LOCATION:
updateBaseChannels(lastMessage, CHANNEL_LAST_LOCATION); updateBaseChannels(lastMessageLocal, CHANNEL_LAST_LOCATION);
break; break;
case CHANNEL_BATTERY_LEVEL: case CHANNEL_BATTERY_LEVEL:
updateBaseChannels(lastMessage, CHANNEL_BATTERY_LEVEL); updateBaseChannels(lastMessageLocal, CHANNEL_BATTERY_LEVEL);
break; break;
case CHANNEL_GPS_ACCURACY: case CHANNEL_GPS_ACCURACY:
updateBaseChannels(lastMessage, CHANNEL_GPS_ACCURACY); updateBaseChannels(lastMessageLocal, CHANNEL_GPS_ACCURACY);
break; break;
default: // distance channels default: // distance channels
@Nullable @Nullable
Channel channel = thing.getChannel(channelId); Channel channel = thing.getChannel(channelId);
if (channel != null) { if (channel != null) {
updateDistanceChannelFromMessage(lastMessage, channel); updateDistanceChannelFromMessage(lastMessageLocal, channel);
} }
} }
} }
@ -272,7 +274,7 @@ public class TrackerHandler extends BaseThingHandler {
* @param event Occurred event * @param event Occurred event
* @param forced Force channel triggering in case the transition event is received from the mobile application. * @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 lastState = lastTriggeredStates.get(regionName);
Boolean newState = EVENT_ENTER.equals(event); Boolean newState = EVENT_ENTER.equals(event);
if (!newState.equals(lastState) || forced) { if (!newState.equals(lastState) || forced) {
@ -341,9 +343,10 @@ public class TrackerHandler extends BaseThingHandler {
} }
private double convertToMeters(double valueToConvert) { private double convertToMeters(double valueToConvert) {
if (unitProvider != null) { UnitProvider unitProviderLocal = unitProvider;
if (unitProviderLocal != null) {
@Nullable @Nullable
Unit<Length> unit = unitProvider.getUnit(Length.class); Unit<Length> unit = unitProviderLocal.getUnit(Length.class);
if (unit != null && !SIUnits.METRE.equals(unit)) { if (unit != null && !SIUnits.METRE.equals(unit)) {
double value = ImperialUnits.YARD.getConverterTo(SIUnits.METRE).convert(valueToConvert); double value = ImperialUnits.YARD.getConverterTo(SIUnits.METRE).convert(valueToConvert);
logger.trace("Value converted: {}yd->{}m", valueToConvert, value); logger.trace("Value converted: {}yd->{}m", valueToConvert, value);

View File

@ -15,6 +15,11 @@ package org.openhab.binding.gpstracker.internal.message;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; 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; import com.google.gson.Gson;
/** /**
@ -22,6 +27,7 @@ import com.google.gson.Gson;
* *
* @author Gabor Bicskei - Initial contribution * @author Gabor Bicskei - Initial contribution
*/ */
@NonNullByDefault
public class MessageUtil { public class MessageUtil {
/** /**
* Patterns to identify incoming JSON payload. * Patterns to identify incoming JSON payload.
@ -48,7 +54,7 @@ public class MessageUtil {
* @param json JSON string. * @param json JSON string.
* @return Parsed message POJO or null without pattern match * @return Parsed message POJO or null without pattern match
*/ */
public LocationMessage fromJson(String json) { public @Nullable LocationMessage fromJson(String json) {
for (String pattern : PATTERNS) { for (String pattern : PATTERNS) {
Class<? extends LocationMessage> c = MESSAGE_TYPES.get(pattern); Class<? extends LocationMessage> c = MESSAGE_TYPES.get(pattern);
if (c != null && json.matches(pattern)) { if (c != null && json.matches(pattern)) {

View File

@ -15,11 +15,15 @@ package org.openhab.binding.gpstracker.internal.message;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.gpstracker.internal.message.dto.LocationMessage;
/** /**
* Notification broker. * Notification broker.
* *
* @author Gabor Bicskei - Initial contribution * @author Gabor Bicskei - Initial contribution
*/ */
@NonNullByDefault
public class NotificationBroker { public class NotificationBroker {
/** /**
* Handlers * Handlers

View File

@ -17,11 +17,16 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; 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. * Handler for notification messages between trackers.
* *
* @author Gabor Bicskei - Initial contribution * @author Gabor Bicskei - Initial contribution
*/ */
@NonNullByDefault
public class NotificationHandler { public class NotificationHandler {
/** /**
* Location notifications need to be sent to the own tracker. Only the last location is saved for each tracker * 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) { if (msg instanceof TransitionMessage) {
List<TransitionMessage> transitionMessages = transitionNotifications.computeIfAbsent(trackerId, List<TransitionMessage> transitionMessages = transitionNotifications.computeIfAbsent(trackerId,
k -> new ArrayList<>()); k -> new ArrayList<>());
transitionMessages.add((TransitionMessage) msg); if (transitionMessages != null) {
transitionMessages.add((TransitionMessage) msg);
}
} else { } else {
locationNotifications.put(trackerId, msg); locationNotifications.put(trackerId, msg);
} }

View File

@ -10,7 +10,7 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.binding.gpstracker.internal.message; package org.openhab.binding.gpstracker.internal.message.dto;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.ZoneId; import java.time.ZoneId;
@ -49,6 +49,12 @@ public class LocationMessage {
@SerializedName("tid") @SerializedName("tid")
private String trackerId = ""; private String trackerId = "";
/**
* Altitude (iOS, Android/integer/meters/optional)
*/
@SerializedName("alt")
private Integer altitude = Integer.MIN_VALUE;
/** /**
* Latitude (iOS, Android/float/meters/required) * Latitude (iOS, Android/float/meters/required)
*/ */
@ -103,7 +109,9 @@ public class LocationMessage {
* @return Conversion result * @return Conversion result
*/ */
public State getTrackerLocation() { public State getTrackerLocation() {
if (!BigDecimal.ZERO.equals(latitude) && !BigDecimal.ZERO.equals(longitude)) { 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 new PointType(new DecimalType(latitude), new DecimalType(longitude));
} }
return UnDefType.UNDEF; return UnDefType.UNDEF;
@ -122,8 +130,9 @@ public class LocationMessage {
} }
public State getGpsAccuracy() { public State getGpsAccuracy() {
if (gpsAccuracy != null) { BigDecimal gpsAccuracyLocal = gpsAccuracy;
return new QuantityType<>(gpsAccuracy.intValue(), SIUnits.METRE); if (gpsAccuracyLocal != null) {
return new QuantityType<>(gpsAccuracyLocal.intValue(), SIUnits.METRE);
} }
return UnDefType.UNDEF; return UnDefType.UNDEF;
} }
@ -131,7 +140,7 @@ public class LocationMessage {
@Override @Override
public String toString() { public String toString() {
return "LocationMessage [" + ("type=" + type + ", ") + ("trackerId=" + trackerId + ", ") return "LocationMessage [" + ("type=" + type + ", ") + ("trackerId=" + trackerId + ", ")
+ ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("altitude=" + altitude + ", ")
+ (gpsAccuracy != null ? "gpsAccuracy=" + gpsAccuracy + ", " : "") + (gpsAccuracy != null ? "gpsAccuracy=" + gpsAccuracy + ", " : "")
+ ("batteryLevel=" + batteryLevel + ", ") + ("timestampMillis=" + timestampMillis) + "]"; + ("batteryLevel=" + batteryLevel + ", ") + ("timestampMillis=" + timestampMillis) + "]";
} }

View File

@ -10,7 +10,7 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.binding.gpstracker.internal.message; package org.openhab.binding.gpstracker.internal.message.dto;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;

View File

@ -20,11 +20,13 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 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.discovery.TrackerDiscoveryService;
import org.openhab.binding.gpstracker.internal.handler.TrackerHandler; 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.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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -33,6 +35,7 @@ import org.slf4j.LoggerFactory;
* *
* @author Gabor Bicskei - Initial contribution * @author Gabor Bicskei - Initial contribution
*/ */
@NonNullByDefault
public abstract class AbstractCallbackServlet extends HttpServlet { public abstract class AbstractCallbackServlet extends HttpServlet {
private static final long serialVersionUID = -2725161358635927815L; private static final long serialVersionUID = -2725161358635927815L;
@ -139,16 +142,15 @@ public abstract class AbstractCallbackServlet extends HttpServlet {
* @param trackerId Tracker id. * @param trackerId Tracker id.
* @return Handler for tracker. * @return Handler for tracker.
*/ */
private TrackerHandler getHandlerById(String trackerId) { private @Nullable TrackerHandler getHandlerById(String trackerId) {
if (trackerId != null) { TrackerHandler handler = trackerRegistry.getTrackerHandler(trackerId);
TrackerHandler handler = trackerRegistry.getTrackerHandler(trackerId); if (handler == null) {
if (handler == null) { // handler was not found - adding the tracker to discovery service.
// handler was not found - adding the tracker to discovery service. discoveryService.addTracker(trackerId);
discoveryService.addTracker(trackerId); } else {
} else { return handler;
return handler;
}
} }
return null; return null;
} }

View File

@ -12,6 +12,7 @@
*/ */
package org.openhab.binding.gpstracker.internal.provider.gpslogger; 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.discovery.TrackerDiscoveryService;
import org.openhab.binding.gpstracker.internal.provider.AbstractCallbackServlet; import org.openhab.binding.gpstracker.internal.provider.AbstractCallbackServlet;
import org.openhab.binding.gpstracker.internal.provider.TrackerRegistry; 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 * @author Gabor Bicskei - Initial contribution
*/ */
@NonNullByDefault
public class GPSLoggerCallbackServlet extends AbstractCallbackServlet { public class GPSLoggerCallbackServlet extends AbstractCallbackServlet {
private static final long serialVersionUID = -6992472786850682196L; private static final long serialVersionUID = -6992472786850682196L;

View File

@ -12,6 +12,7 @@
*/ */
package org.openhab.binding.gpstracker.internal.provider.owntracks; 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.discovery.TrackerDiscoveryService;
import org.openhab.binding.gpstracker.internal.provider.AbstractCallbackServlet; import org.openhab.binding.gpstracker.internal.provider.AbstractCallbackServlet;
import org.openhab.binding.gpstracker.internal.provider.TrackerRegistry; 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 * @author Gabor Bicskei - Initial contribution
*/ */
@NonNullByDefault
public class OwnTracksCallbackServlet extends AbstractCallbackServlet { public class OwnTracksCallbackServlet extends AbstractCallbackServlet {
private static final long serialVersionUID = -4053305903339688036L; private static final long serialVersionUID = -4053305903339688036L;