Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into heyoulin-kura
Browse files Browse the repository at this point in the history
  • Loading branch information
spnettec committed Nov 14, 2024
2 parents 62f3bec + d32a0a5 commit 0eadd01
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
* <li>Latitude in degrees
* <li>Track in degrees
* <li>Altitude in meters
* <li>Speed in km/h
* <li>Speed in mph
* <li>Speed in m/s (this field has different getters to retrieved value in m/s, km/h or mph)
* </ul>
* It adds to the OSGI Position class the following fields:<br>
* <ul>
Expand All @@ -47,11 +46,14 @@
@ProviderType
public class NmeaPosition {

private double latitude;
private double longitude;
private double altitude;
private double speed;
private double track;
private static final double MS_TO_KMH = 3.6;
private static final double MS_TO_MPH = 2.24;

private double latitudeDegrees;
private double longitudeDegrees;
private double altitudeMeters;
private double speedMetersPerSecond;
private double trackDegrees;
private int fixQuality;
private int nrSatellites;
private double mDOP;
Expand All @@ -63,28 +65,29 @@ public class NmeaPosition {
private char latitudeHemisphere;
private char longitudeHemisphere;

public NmeaPosition(double lat, double lon, double alt, double speed, double track) {
this(lat, lon, alt, speed, track, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, '0', '0', '0');
public NmeaPosition(double latDegrees, double lonDegrees, double altDegrees, double speedMps, double trackDegrees) {
this(latDegrees, lonDegrees, altDegrees, speedMps, trackDegrees, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, '0', '0', '0');
}

@SuppressWarnings("checkstyle:parameterNumber")
public NmeaPosition(double lat, double lon, double alt, double speed, double track, int fixQuality,
int nrSatellites, double dop, double pdop, double hdop, double vdop, int fix3D) {
this(lat, lon, alt, speed, track, fixQuality, nrSatellites, dop, pdop, hdop, vdop, fix3D, '0', '0', '0');
public NmeaPosition(double latDegrees, double lonDegrees, double altDegrees, double speedMps, double trackDegrees,
int fixQuality, int nrSatellites, double dop, double pdop, double hdop, double vdop, int fix3D) {
this(latDegrees, lonDegrees, altDegrees, speedMps, trackDegrees, fixQuality, nrSatellites, dop, pdop, hdop,
vdop, fix3D, '0', '0', '0');
}

/**
* @since 2.0
*/
@SuppressWarnings("checkstyle:parameterNumber")
public NmeaPosition(double lat, double lon, double alt, double speed, double track, int fixQuality,
int nrSatellites, double dop, double pdop, double hdop, double vdop, int fix3D, char validF, char hemiLat,
char hemiLon) {
this.latitude = lat;
this.longitude = lon;
this.altitude = alt;
this.speed = speed;
this.track = track;
public NmeaPosition(double latDegrees, double lonDegrees, double altDegrees, double speedMps, double trackDegrees,
int fixQuality, int nrSatellites, double dop, double pdop, double hdop, double vdop, int fix3D, char validF,
char hemiLat, char hemiLon) {
this.latitudeDegrees = latDegrees;
this.longitudeDegrees = lonDegrees;
this.altitudeMeters = altDegrees;
this.speedMetersPerSecond = speedMps;
this.trackDegrees = trackDegrees;
this.fixQuality = fixQuality;
this.nrSatellites = nrSatellites;
this.mDOP = dop;
Expand All @@ -101,69 +104,69 @@ public NmeaPosition(double lat, double lon, double alt, double speed, double tra
* Return the latitude in degrees
*/
public double getLatitude() {
return this.latitude;
return this.latitudeDegrees;
}

public void setLatitude(double latitude) {
this.latitude = latitude;
this.latitudeDegrees = latitude;
}

/**
* Return the longitude in degrees
*/
public double getLongitude() {
return this.longitude;
return this.longitudeDegrees;
}

public void setLongitude(double longitude) {
this.longitude = longitude;
this.longitudeDegrees = longitude;
}

/**
* Return the altitude in meters
*/
public double getAltitude() {
return this.altitude;
return this.altitudeMeters;
}

public void setAltitude(double altitude) {
this.altitude = altitude;
this.altitudeMeters = altitude;
}

/**
* Return the speed in km/h
*/
public double getSpeedKmh() {
return this.speed * 3.6;
return this.speedMetersPerSecond * MS_TO_KMH;
}

/**
* Return the speed in mph
*/
public double getSpeedMph() {
return this.speed * 2.24;
return this.speedMetersPerSecond * MS_TO_MPH;
}

/**
* Return the speed in m/s
*/
public double getSpeed() {
return this.speed;
return this.speedMetersPerSecond;
}

public void setSpeed(double speed) {
this.speed = speed;
this.speedMetersPerSecond = speed;
}

/**
* Return the track in degrees
*/
public double getTrack() {
return this.track;
return this.trackDegrees;
}

public void setTrack(double track) {
this.track = track;
this.trackDegrees = track;
}

public int getFixQuality() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -174,9 +175,8 @@ public class CloudServiceImpl
private String ownPid;

private ScheduledFuture<?> scheduledBirthPublisherFuture;
private final ScheduledExecutorService scheduledBirthPublisher = Executors.newScheduledThreadPool(1);
private LifecycleMessage lastBirthMessage;
private LifecycleMessage lastAppMessage;
private final ScheduledExecutorService scheduledBirthPublisher = Executors.newSingleThreadScheduledExecutor();
private final AtomicBoolean shouldPublishDelayedBirth = new AtomicBoolean();

public CloudServiceImpl() {
this.cloudClients = new CopyOnWriteArrayList<>();
Expand Down Expand Up @@ -825,12 +825,11 @@ private void publishBirthCertificate(boolean isNewConnection) throws KuraExcepti
}

readModemProfile();
LifecycleMessage birthToPublish = new LifecycleMessage(this.options, this).asBirthCertificateMessage();

if (isNewConnection) {
publishLifeCycleMessage(birthToPublish);
publishLifeCycleMessage(new LifecycleMessage(this.options, this).asBirthCertificateMessage());
} else {
publishWithDelay(birthToPublish);
publishWithDelay(false);
}
}

Expand All @@ -843,42 +842,26 @@ private void publishAppCertificate() {
logger.info("framework is stopping.. not republishing app certificate");
return;
}
publishWithDelay(new LifecycleMessage(this.options, this).asAppCertificateMessage());
publishWithDelay(true);
}

private void publishWithDelay(LifecycleMessage message) {
if (Objects.nonNull(this.scheduledBirthPublisherFuture)) {
this.scheduledBirthPublisherFuture.cancel(false);
logger.debug("CloudServiceImpl: BIRTH message cache timer restarted.");
}
private void publishWithDelay(boolean isAppUpdate) {
synchronized (this.shouldPublishDelayedBirth) {
if (!isAppUpdate) {
this.shouldPublishDelayedBirth.set(true);
}

logger.debug("CloudServiceImpl: BIRTH message cached for 30s.");
if (Objects.nonNull(this.scheduledBirthPublisherFuture)) {
this.scheduledBirthPublisherFuture.cancel(false);
logger.debug("CloudServiceImpl: BIRTH message cache timer restarted.");
}
logger.info("isAppUpdate? {}", isAppUpdate, new RuntimeException());

if (message.isBirthCertificateMessage()) {
this.lastBirthMessage = message;
}
logger.debug("CloudServiceImpl: BIRTH message cached for 30s.");

if (message.isAppCertificateMessage()) {
this.lastAppMessage = message;
this.scheduledBirthPublisherFuture = this.scheduledBirthPublisher.schedule(this::publishDelayedMessage, 30L,
TimeUnit.SECONDS);
}

this.scheduledBirthPublisherFuture = this.scheduledBirthPublisher.schedule(() -> {
try {

if (Objects.nonNull(this.lastBirthMessage)) {
logger.debug("CloudServiceImpl: publishing cached BIRTH message.");
publishLifeCycleMessage(this.lastBirthMessage);
}

if (Objects.nonNull(this.lastAppMessage)) {
logger.debug("CloudServiceImpl: publishing cached APP message.");
publishLifeCycleMessage(this.lastAppMessage);
}

} catch (KuraException e) {
logger.error("Error sending cached BIRTH/APP certificate.", e);
}
}, 30L, TimeUnit.SECONDS);
}

private void publishLifeCycleMessage(LifecycleMessage message) throws KuraException {
Expand All @@ -902,6 +885,23 @@ private void publishLifeCycleMessage(LifecycleMessage message) throws KuraExcept
}
}

private void publishDelayedMessage() {
synchronized (this.shouldPublishDelayedBirth) {
try {

if (this.shouldPublishDelayedBirth.get()) {
publishLifeCycleMessage(new LifecycleMessage(this.options, this).asBirthCertificateMessage());
} else {
publishLifeCycleMessage(new LifecycleMessage(this.options, this).asAppCertificateMessage());
}
this.shouldPublishDelayedBirth.set(false);

} catch (KuraException e) {
logger.error("Error sending cached BIRTH/APP certificate.", e);
}
}
}

private byte[] encodeProtobufPayload(KuraPayload payload) throws KuraException {
byte[] bytes = new byte[0];
if (payload == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 Eurotech and/or its affiliates and others
* Copyright (c) 2023, 2024 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -40,7 +40,7 @@ public class DnsmasqTool implements DhcpLinuxTool {
private static final Logger logger = LoggerFactory.getLogger(DnsmasqTool.class);

private String globalConfigFilename = "/etc/dnsmasq.d/dnsmasq-globals.conf";
private static final String GLOBAL_CONFIGURATION = "port=0\nbind-dynamic\ndhcp-leasefile=/var/lib/dhcp/dnsmasq.leases\n";
private static final String GLOBAL_CONFIGURATION = "port=0\nbind-dynamic\n";

static final Command IS_ACTIVE_COMMAND = new Command(new String[] { "systemctl", "is-active", "--quiet",
DhcpServerTool.DNSMASQ.getValue() });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ public class MMLocationParser {

private static final Logger logger = LoggerFactory.getLogger(MMLocationParser.class);

private static final double KNOTS_TO_M_S = 1 / 1.94384449;
private static final double KNOTS_TO_MS = 1 / 1.94384449;

private int gnssTypeUpdateCounter = 0;
private static final int GNSSTYPE_RESET_COUNTER = 50;

private Double lat = 0.0;
private Double lon = 0.0;
private Double alt = 0.0;
private Double speed = 0.0;
private Double track = 0.0;
private Double latitudeDegrees = 0.0;
private Double longitudeDegrees = 0.0;
private Double altitudeMeters = 0.0;
private Double speedMetersPerSecond = 0.0;
private Double trackDegrees = 0.0;

private int fixQuality;
private int nrSatellites;
Expand All @@ -68,10 +69,15 @@ public LocalDateTime getLocalDateTime() {
return Objects.requireNonNull(LocalDateTime.of(date, time));
}

/*
* Conversions are required since org.osgi.util.position.Position requires values in radians for latitude, longitude
* and track.
*/
public Position getPosition() {
return Objects.requireNonNull(new Position(new Measurement(this.lat, Unit.rad),
new Measurement(this.lon, Unit.rad), new Measurement(this.alt, Unit.m),
new Measurement(this.speed, Unit.m_s), new Measurement(this.track, Unit.rad)));
return Objects.requireNonNull(new Position(new Measurement(Math.toRadians(this.latitudeDegrees), Unit.rad),
new Measurement(Math.toRadians(this.longitudeDegrees), Unit.rad),
new Measurement(this.altitudeMeters, Unit.m), new Measurement(this.speedMetersPerSecond, Unit.m_s),
new Measurement(Math.toRadians(this.trackDegrees), Unit.rad)));
}

public Set<GNSSType> getGnssTypes() {
Expand All @@ -83,9 +89,9 @@ public boolean isFixed() {
}

public NmeaPosition getNmeaPosition() {
return new NmeaPosition(this.lat, this.lon, this.alt, this.speed, this.track, this.fixQuality,
this.nrSatellites, this.mDOP, this.mPDOP, this.mHDOP, this.mVDOP, this.m3Dfix, this.validFix,
this.latitudeHemisphere, this.longitudeHemisphere);
return new NmeaPosition(this.latitudeDegrees, this.longitudeDegrees, this.altitudeMeters,
this.speedMetersPerSecond, this.trackDegrees, this.fixQuality, this.nrSatellites, this.mDOP, this.mPDOP,
this.mHDOP, this.mVDOP, this.m3Dfix, this.validFix, this.latitudeHemisphere, this.longitudeHemisphere);
}

public String getNmeaTime() {
Expand All @@ -102,15 +108,15 @@ public void parseRawLocation(Variant<?> rawLocationVariant) {

switch (rawEntry.getKey()) {
case "latitude":
this.lat = Math.toRadians((Double) rawEntry.getValue().getValue());
this.latitudeDegrees = (Double) rawEntry.getValue().getValue();
break;

case "longitude":
this.lon = Math.toRadians((Double) rawEntry.getValue().getValue());
this.longitudeDegrees = (Double) rawEntry.getValue().getValue();
break;

case "altitude":
this.alt = (Double) rawEntry.getValue().getValue();
this.altitudeMeters = (Double) rawEntry.getValue().getValue();
break;

// time comes in format HHmmss.SS, so we cut the string after the dot to extract only the util information
Expand Down Expand Up @@ -210,10 +216,10 @@ private void parseRmcSentence(List<String> rmcTokens) {
this.date = LocalDate.parse(rmcTokens.get(9), DateTimeFormatter.ofPattern("ddMyy"));
}
if (!rmcTokens.get(7).isEmpty()) {
this.speed = Double.parseDouble(rmcTokens.get(7)) * KNOTS_TO_M_S;
this.speedMetersPerSecond = Double.parseDouble(rmcTokens.get(7)) * KNOTS_TO_MS;
}
if (!rmcTokens.get(8).isEmpty()) {
this.track = Math.toRadians(Double.parseDouble(rmcTokens.get(8)));
this.trackDegrees = Double.parseDouble(rmcTokens.get(8));
}
if (!rmcTokens.get(4).isEmpty()) {
this.latitudeHemisphere = rmcTokens.get(4).charAt(0);
Expand Down Expand Up @@ -287,7 +293,8 @@ private GNSSType sentenceIdToGnssType(String type) {

@Override
public String toString() {
return "ModemManagerProvider [latitude=" + lat + ", longitude=" + lon + ", altitude=" + alt + ", speed=" + speed
+ ", timestamp=" + time + ", date=" + date + ", gnssType=" + gnssTypes + "]";
return "ModemManagerProvider [latitude=" + latitudeDegrees + ", longitude=" + longitudeDegrees + ", altitude="
+ altitudeMeters + ", speed=" + speedMetersPerSecond + ", timestamp=" + time + ", date=" + date
+ ", gnssType=" + gnssTypes + "]";
}
}
Loading

0 comments on commit 0eadd01

Please sign in to comment.