Skip to content

Commit

Permalink
fix: Use dynamic policy for PositionProvider reference in PositionSer…
Browse files Browse the repository at this point in the history
…vice [backport release-5.6.0] (#5545)

fix: Use dynamic policy for PositionProvider reference in PositionService (#5529)

fix: Use dynamic policy for PositonProvider reference in PositionService

Signed-off-by: Nicola Timeus <nicola.timeus@eurotech.com>
(cherry picked from commit 72a2834)

Co-authored-by: nicolatimeus <nicola.timeus@eurotech.com>
  • Loading branch information
eclipse-kura-bot and nicolatimeus authored Nov 13, 2024
1 parent fdced81 commit de03964
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
4 changes: 2 additions & 2 deletions kura/org.eclipse.kura.linux.position/OSGI-INF/position.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<provide interface="org.eclipse.kura.configuration.ConfigurableComponent"/>
</service>

<reference bind="setPositionProviders" cardinality="1..n" interface="org.eclipse.kura.linux.position.provider.PositionProvider" name="PositionProviders" policy-option="greedy" policy="static" unbind="unsetPositionProviders"/>
<reference bind="setPositionProviders" cardinality="1..n" interface="org.eclipse.kura.linux.position.provider.PositionProvider" name="PositionProviders" policy="dynamic" unbind="unsetPositionProviders"/>

<property name="service.pid" value="org.eclipse.kura.position.PositionService"/>
<reference bind="setEventAdmin" cardinality="1..1" interface="org.osgi.service.event.EventAdmin" name="EventAdmin" policy="static" unbind="unsetEventAdmin"/>
<reference bind="setEventAdmin" cardinality="1..1" interface="org.osgi.service.event.EventAdmin" name="EventAdmin" policy="static"/>
</scr:component>
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.kura.KuraErrorCode;
import org.eclipse.kura.KuraException;
import org.eclipse.kura.configuration.ConfigurableComponent;
import org.eclipse.kura.linux.position.options.PositionServiceOptions;
import org.eclipse.kura.linux.position.provider.GpsDeviceAvailabilityListener;
Expand Down Expand Up @@ -73,16 +72,20 @@ public void setEventAdmin(final EventAdmin eventAdmin) {
this.eventAdmin = eventAdmin;
}

public void unsetEventAdmin(final EventAdmin eventAdmin) {
this.eventAdmin = null;
}

public void setPositionProviders(PositionProvider positionProvider) {
public synchronized void setPositionProviders(PositionProvider positionProvider) {
this.positionProviders.add(positionProvider); // ADD NAME TO PROVIDERS

if (this.options != null && isSelectedProvider(positionProvider, this.options)) {
updateInternal();
}
}

public void unsetPositionProviders(PositionProvider positionProvider) {
public synchronized void unsetPositionProviders(PositionProvider positionProvider) {
this.positionProviders.remove(positionProvider);

if (this.options != null && isSelectedProvider(positionProvider, this.options)) {
updateInternal();
}
}

// ----------------------------------------------------------------
Expand All @@ -108,13 +111,15 @@ protected void deactivate() {
logger.info("Deactivating... Done.");
}

public void updated(final Map<String, Object> properties) {
public synchronized void updated(final Map<String, Object> properties) {

logger.debug("Updating...");

final PositionServiceOptions newOptions = new PositionServiceOptions(properties);

if (newOptions.equals(this.options)) {
final boolean isProviderBound = this.currentProvider != null && isSelectedProvider(currentProvider, newOptions);

if (newOptions.equals(this.options) && (newOptions.isStatic() || isProviderBound)) {
logger.debug("same configuration, no need ot reconfigure GPS device");
return;
}
Expand All @@ -141,7 +146,7 @@ private synchronized void updateInternal() {
} else {
try {
startPositionProvider();
} catch (KuraException e) {
} catch (Exception e) {
logger.error("Unable to start the chosen Position Provider", e);
}
}
Expand All @@ -156,7 +161,8 @@ private synchronized void updateInternal() {
@Override
public Position getPosition() {
if (this.options.isEnabled() && !this.options.isStatic()) {
return this.currentProvider.getPosition();
return Optional.ofNullable(this.currentProvider).map(PositionProvider::getPosition)
.orElseGet(() -> staticPosition(0, 0, 0));
} else {
return this.staticPosition;
}
Expand All @@ -166,7 +172,8 @@ public Position getPosition() {
@Override
public NmeaPosition getNmeaPosition() {
if (this.options.isEnabled() && !this.options.isStatic()) {
return this.currentProvider.getNmeaPosition();
return Optional.ofNullable(this.currentProvider).map(PositionProvider::getNmeaPosition)
.orElseGet(() -> staticNMEAPosition(0, 0, 0));
} else {
return this.staticNmeaPosition;
}
Expand All @@ -180,7 +187,7 @@ public boolean isLocked() {
if (this.options.isStatic()) {
return true;
}
return this.currentProvider.isLocked();
return Optional.ofNullable(this.currentProvider).map(PositionProvider::isLocked).orElse(false);
}

@Override
Expand Down Expand Up @@ -255,19 +262,31 @@ protected PositionServiceOptions getPositionServiceOptions() {
return this.options;
}

private void startPositionProvider() throws KuraException {
private void startPositionProvider() {
stopPositionProvider();

this.currentProvider = this.positionProviders.stream()
.filter(pp -> pp.getType() == this.options.getPositionProvider()).findAny()
.orElseThrow(() -> new KuraException(KuraErrorCode.CONFIGURATION_ATTRIBUTE_INVALID, " provider",
this.options.getPositionProvider()));
final Optional<PositionProvider> provider = getSelectedProvider(this.options);

if (!provider.isPresent()) {
logger.info("PositionProvider {} is not bound", this.options.getPositionProvider());
return;
}

this.currentProvider = provider.get();

this.currentProvider.init(options, this, this);
this.currentProvider.start();

}

private Optional<PositionProvider> getSelectedProvider(final PositionServiceOptions options) {
return this.positionProviders.stream().filter(pp -> isSelectedProvider(pp, options)).findAny();
}

private boolean isSelectedProvider(final PositionProvider provider, final PositionServiceOptions options) {
return provider.getType() == options.getPositionProvider();
}

private void stopPositionProvider() {
if (this.currentProvider != null) {
this.currentProvider.stop();
Expand All @@ -291,6 +310,12 @@ private void setLock(boolean hasLock) {

private void setStaticPosition(double latitudeDeg, double longitudeDeg, double altitudeNmea, String gnssType) {

this.staticPosition = staticPosition(latitudeDeg, longitudeDeg, altitudeNmea);
this.staticNmeaPosition = staticNMEAPosition(latitudeDeg, longitudeDeg, altitudeNmea);
this.staticGnssType = GNSSType.fromValue(gnssType);
}

private static Position staticPosition(double latitudeDeg, double longitudeDeg, double altitudeNmea) {
final double latitudeRad = Math.toRadians(latitudeDeg);
final double longitudeRad = Math.toRadians(longitudeDeg);

Expand All @@ -301,10 +326,12 @@ private void setStaticPosition(double latitudeDeg, double longitudeDeg, double a
// knots
final Measurement track = new Measurement(java.lang.Math.toRadians(0), Unit.rad);

this.staticPosition = new Position(latitude, longitude, altitude, speed, track);
this.staticNmeaPosition = new NmeaPosition(latitudeDeg, longitudeDeg, altitudeNmea, 0, 0, 0, 0, 0, 0, 0, 0, 0,
(char) 0, (char) 0, (char) 0);
this.staticGnssType = GNSSType.fromValue(gnssType);
return new Position(latitude, longitude, altitude, speed, track);
}

private static NmeaPosition staticNMEAPosition(double latitudeDeg, double longitudeDeg, double altitudeNmea) {
return new NmeaPosition(latitudeDeg, longitudeDeg, altitudeNmea, 0, 0, 0, 0, 0, 0, 0, 0, 0, (char) 0, (char) 0,
(char) 0);
}

@Override
Expand Down

0 comments on commit de03964

Please sign in to comment.