Skip to content

Commit

Permalink
feat(api): added gnss type data to PositionService (#5381)
Browse files Browse the repository at this point in the history
* feat(api): added gnss type data to PositionService

Signed-off-by: SimoneFiorani <simone.fiorani@abinsula.com>

* test: added tests

Signed-off-by: SimoneFiorani <simone.fiorani@abinsula.com>

* refactor: moved GNSSType enum to api bundle

Signed-off-by: SimoneFiorani <simone.fiorani@abinsula.com>

* refactor: removed testing logger

Signed-off-by: SimoneFiorani <simone.fiorani@abinsula.com>

* refactor: refactored gnsstype

Signed-off-by: SimoneFiorani <simone.fiorani@abinsula.com>

* refactor: updated with suggestions

Signed-off-by: SimoneFiorani <simone.fiorani@abinsula.com>

* fix: fixed line length

Signed-off-by: SimoneFiorani <simone.fiorani@abinsula.com>

* Update kura/org.eclipse.kura.api/src/main/java/org/eclipse/kura/position/PositionService.java

Co-authored-by: Mattia Dal Ben <mattdibi@users.noreply.github.com>

* Update GNSSType.java

---------

Signed-off-by: SimoneFiorani <simone.fiorani@abinsula.com>
Co-authored-by: Mattia Dal Ben <mattdibi@users.noreply.github.com>
  • Loading branch information
sfiorani and mattdibi authored Oct 3, 2024
1 parent d713b88 commit 51fcd57
Show file tree
Hide file tree
Showing 17 changed files with 249 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Import-Package: javax.xml.parsers,
javax.xml.stream,
org.eclipse.kura;version="[1.0,2.0)",
org.eclipse.kura.configuration;version="[1.1,2.0)",
org.eclipse.kura.position;version="[1.3,1.4)",
org.eclipse.kura.position;version="[1.4,1.5)",
org.osgi.framework;version="1.7.0",
org.osgi.service.component;version="1.2.0",
org.osgi.service.event;version="1.3.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
import java.net.URL;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
Expand All @@ -28,6 +31,7 @@
import javax.xml.parsers.SAXParserFactory;

import org.eclipse.kura.configuration.ConfigurableComponent;
import org.eclipse.kura.position.GNSSType;
import org.eclipse.kura.position.NmeaPosition;
import org.eclipse.kura.position.PositionListener;
import org.eclipse.kura.position.PositionLockedEvent;
Expand Down Expand Up @@ -240,4 +244,9 @@ public void unregisterListener(String listenerId) {
public LocalDateTime getDateTime() {
return LocalDateTime.ofInstant(this.currentTime.toInstant(), ZoneId.systemDefault());
}

@Override
public Set<GNSSType> getGnssType() {
return new HashSet<>(Arrays.asList(GNSSType.GPS));
}
}
2 changes: 1 addition & 1 deletion kura/org.eclipse.kura.api/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Export-Package: org.eclipse.kura;version="1.7.0",
org.eclipse.kura.net.status.wifi;version="1.0.0",
org.eclipse.kura.net.vlan;version="1.0.0",
org.eclipse.kura.net.wifi;version="2.5.0",
org.eclipse.kura.position;version="1.3.0",
org.eclipse.kura.position;version="1.4.0",
org.eclipse.kura.security;version="1.3.0",
org.eclipse.kura.security.keystore;version="1.1.0",
org.eclipse.kura.security.tamper.detection;version="1.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.eclipse.kura.position;

import java.util.HashMap;
import java.util.Map;

public enum GNSSType {
UNKNOWN("Unknown"),
OTHER("Other"),
BEIDOU("Beidou"),
GALILEO("Galileo"),
GLONASS("Glonass"),
GPS("Gps"),
IRNSS("IRNSS"),
QZSS("QZSS");

private String value;

private static Map<String, GNSSType> valuesMap = new HashMap<>();

static {
for (GNSSType type : GNSSType.values()) {
valuesMap.put(type.getValue(), type);
}
}

private GNSSType(String value) {
this.value = value;
}

public String getValue() {
return this.value;
}

public static GNSSType fromValue(String value) {
return valuesMap.get(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.eclipse.kura.position;

import java.time.LocalDateTime;
import java.util.Set;

import org.osgi.annotation.versioning.ProviderType;
import org.osgi.util.position.Position;
Expand Down Expand Up @@ -112,6 +113,15 @@ public interface PositionService {
@Deprecated
public String getLastSentence();

/**
* Returns the GNSS System used to get the position information @reference GNSSType. Could be one or more system (eg: GPS + GLONASS).
*
* If empty, no recognized GNSS System Type is available.
*
* @since 2.8
*/
public Set<GNSSType> getGnssType();

/**
* Registers position listener
*
Expand Down
2 changes: 1 addition & 1 deletion kura/org.eclipse.kura.linux.position/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Import-Package: de.taimos.gpsd4java.api;version="[1.0,2.0)",
org.eclipse.kura.comm;version="[1.0,2.0)",
org.eclipse.kura.configuration;version="[1.1,2.0)",
org.eclipse.kura.net.modem;version="[2.0,3.0)",
org.eclipse.kura.position;version="[1.3,1.4)",
org.eclipse.kura.position;version="[1.4,1.5)",
org.eclipse.kura.usb;version="[1.0,2.0)",
org.osgi.framework,
org.osgi.service.component;version="1.2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.Set;

import org.eclipse.kura.comm.CommConnection;
import org.eclipse.kura.comm.CommURI;
import org.eclipse.kura.linux.position.NMEAParser.Code;
import org.eclipse.kura.linux.position.NMEAParser.ParseException;
import org.eclipse.kura.position.GNSSType;
import org.eclipse.kura.position.NmeaPosition;
import org.eclipse.kura.position.PositionException;
import org.eclipse.kura.position.PositionListener;
Expand Down Expand Up @@ -81,6 +83,10 @@ public synchronized String getTimeNmea() {
return this.nmeaParser.getTimeNmea();
}

public synchronized Set<GNSSType> getGnssType() {
return this.nmeaParser.getGnssType();
}

public void disconnect() {
this.listener = null;
this.commThread.disconnect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashSet;
import java.util.List;
import java.util.OptionalInt;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
Expand All @@ -27,6 +30,7 @@
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.kura.linux.position.GpsDevice.Listener;
import org.eclipse.kura.position.GNSSType;
import org.eclipse.kura.position.NmeaPosition;
import org.osgi.util.measurement.Measurement;
import org.osgi.util.measurement.Unit;
Expand All @@ -40,6 +44,7 @@
import de.taimos.gpsd4java.types.DeviceObject;
import de.taimos.gpsd4java.types.DevicesObject;
import de.taimos.gpsd4java.types.ENMEAMode;
import de.taimos.gpsd4java.types.SATObject;
import de.taimos.gpsd4java.types.SKYObject;
import de.taimos.gpsd4java.types.TPVObject;
import de.taimos.gpsd4java.types.subframes.SUBFRAMEObject;
Expand All @@ -49,6 +54,7 @@ public class GpsdPositionProvider implements PositionProvider, IObjectListener {
private static final Logger logger = LoggerFactory.getLogger(GpsdPositionProvider.class);
private final AtomicReference<GpsdInternalState> internalStateReference = new AtomicReference<>(
new GpsdInternalState());
private final AtomicReference<Set<GNSSType>> gnssType = new AtomicReference<>(new HashSet<>());

private GPSdEndpoint gpsEndpoint;
private PositionServiceOptions configuration;
Expand Down Expand Up @@ -164,6 +170,11 @@ public PositionProviderType getType() {
return PositionProviderType.GPSD;
}

@Override
public Set<GNSSType> getGnssType() {
return this.gnssType.get();
}

@Override
public void handleATT(ATTObject att) {
// Noting to do.
Expand All @@ -181,7 +192,18 @@ public void handleDevices(DevicesObject devices) {

@Override
public void handleSKY(SKYObject sky) {
// Noting to do.

List<SATObject> satellites = sky.getSatellites();
Set<GNSSType> newGnssTypeSet = new HashSet<>();

for (SATObject object : satellites) {
if (object.getUsed()) {
newGnssTypeSet.add(getGnssTypeFromPrn(object.getPRN()));
}
}

this.gnssType.set(newGnssTypeSet);

}

@Override
Expand Down Expand Up @@ -262,6 +284,21 @@ private Measurement toMetersPerSecondMeasurement(double value, double error) {
return new Measurement(Double.isNaN(value) ? 0.0d : value, Double.isNaN(error) ? 0.0d : error, Unit.m_s);
}

/*
* GNSS Type PRNs ranges retrieved from
* {@link https://github.com/taimos/GPSd4Java/blob/6c92bac30d98121bb212bcc7f2426c48ce592433/src/main/java/de/taimos/
* gpsd4java/types/SATObject.java#L44}
*/
private GNSSType getGnssTypeFromPrn(int prnId) {
if (prnId >= 1 && prnId <= 63) {
return GNSSType.GPS;
} else if (prnId >= 64 && prnId <= 96) {
return GNSSType.GLONASS;
} else {
return GNSSType.OTHER;
}
}

private class GpsdInternalState {

private final long creationInstantNanos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
import static java.lang.Math.toRadians;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.kura.position.GNSSType;
import org.eclipse.kura.position.NmeaPosition;
import org.osgi.util.measurement.Measurement;
import org.osgi.util.measurement.Unit;
Expand Down Expand Up @@ -47,6 +50,10 @@ public class NMEAParser {
private char latitudeHemisphere = 0;
private char longitudeHemisphere = 0;

private Set<GNSSType> gnssType = new HashSet<>();
private int gnssTypeUpdateCounter = 0;
private static final int GNSSTYPE_RESET_COUNTER = 50;

/**
* Fill the fields of GPS position depending of the type of the sentence
*
Expand All @@ -70,6 +77,9 @@ public boolean parseSentence(String sentence) throws ParseException {
* $GS = Glonass
* $GN = GNSS, that is GPS + Glonass + possibly others
*/

parseGnssType(tokens.get(0));

if (!tokens.get(0).startsWith("$G")) {
// Not a valid token. Return.
throw new ParseException(Code.INVALID);
Expand All @@ -92,6 +102,65 @@ public boolean parseSentence(String sentence) throws ParseException {
return this.validPosition;
}

private void parseGnssType(String token) {

if (this.gnssTypeUpdateCounter > GNSSTYPE_RESET_COUNTER) {
this.gnssType.clear();
this.gnssTypeUpdateCounter = 0;
}

String id = token.substring(1, 3);

GNSSType type = getGnssTypeFromSentenceId(id);

if (!type.equals(GNSSType.UNKNOWN)) {
this.gnssType.add(type);
}

this.gnssTypeUpdateCounter++;
}

/*
* Also 'GN' is a possible GNSSType, representing the Mixed GNSS System (GPS+GALILEO or GPS+GLONASS for example).
*
* But, if the device is capable to emit GN sentences, it must emit also the single-id ones. So we are still able to
* extract the specific GNSS System. See {@link
* https://receiverhelp.trimble.com/alloy-gnss/en-us/NMEA-0183messages_GNS.html}
*
* As example, if the device emits GN messages due to a GP/GA combination, it will emit three sentences: GN, GP, GA.
*
* Info about the correlation GNSS System / NMEA Sentence at
* {@link https://en.wikipedia.org/wiki/NMEA_0183#NMEA_sentence_format}
*
*/
private GNSSType getGnssTypeFromSentenceId(String type) {

switch (type) {

case "GP":
return GNSSType.GPS;

case "BD":
case "GB":
return GNSSType.BEIDOU;

case "GA":
return GNSSType.GALILEO;

case "GL":
return GNSSType.GLONASS;

case "GI":
return GNSSType.IRNSS;

case "GQ":
return GNSSType.QZSS;

default:
return GNSSType.UNKNOWN;
}
}

private void parseVTGSentence(List<String> tokens) {
if (tokens.size() > 7 && !tokens.get(7).isEmpty()) {
// conversion km/h in m/s : 1 km/h -> 0,277777778 m/s
Expand Down Expand Up @@ -261,7 +330,6 @@ boolean computeNMEACksum(String nmeaMessageIn) {
final int starpos = nmeaMessageIn.indexOf('*');
final String strChecksum = nmeaMessageIn.substring(starpos + 1, nmeaMessageIn.length() - 1);
final int parsedChecksum = Integer.parseInt(strChecksum, 16); // Check sum is coded in hex string

int actualChecksum = 0;
for (int i = 1; i < starpos; i++) {
actualChecksum ^= nmeaMessageIn.charAt(i);
Expand Down Expand Up @@ -372,6 +440,10 @@ public char getLongitudeHemisphere() {
return this.longitudeHemisphere;
}

public Set<GNSSType> getGnssType() {
return this.gnssType;
}

public enum Code {
INVALID,
BAD_CHECKSUM,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
package org.eclipse.kura.linux.position;

import java.time.LocalDateTime;
import java.util.Set;

import org.eclipse.kura.linux.position.GpsDevice.Listener;
import org.eclipse.kura.position.GNSSType;
import org.eclipse.kura.position.NmeaPosition;
import org.osgi.util.position.Position;

Expand Down Expand Up @@ -44,4 +46,6 @@ public void init(PositionServiceOptions configuration, Listener gpsDeviceListene

public PositionProviderType getType();

public Set<GNSSType> getGnssType();

}
Loading

0 comments on commit 51fcd57

Please sign in to comment.