Skip to content

Commit

Permalink
feat: Made PositionService dependency optional for CloudServices (#5638)
Browse files Browse the repository at this point in the history
* Made PositionService optional for eclipseiot provider

Signed-off-by: pierantoniomerlino <[email protected]>

* Made PositionService optional for kapua provider

Signed-off-by: pierantoniomerlino <[email protected]>

* Fixed lon and lat units in birth message

Signed-off-by: pierantoniomerlino <[email protected]>

* Used toDegrees method; added tests

Signed-off-by: pierantoniomerlino <[email protected]>

* Fixed sonar complains

Signed-off-by: pierantoniomerlino <[email protected]>

* Updated copyright

Signed-off-by: pierantoniomerlino <[email protected]>

---------

Signed-off-by: pierantoniomerlino <[email protected]>
  • Loading branch information
pierantoniomerlino authored Jan 9, 2025
1 parent 255c556 commit 80b4b9e
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,7 @@ Import-Package: com.eclipsesource.json;version="0.9.5",
org.osgi.framework;version="1.5.0",
org.osgi.service.component;version="1.2.0",
org.osgi.service.event;version="1.3.0",
org.osgi.util.measurement;version="[1.0,2.0)",
org.osgi.util.position;version="[1.0,2.0)",
org.osgi.util.tracker;version="1.5.1",
org.slf4j;version="1.6.4"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2018, 2024 Eurotech and/or its affiliates and others
Copyright (c) 2018, 2025 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 @@ -51,7 +51,7 @@
cardinality="0..1"
bind="setPositionService"
interface="org.eclipse.kura.position.PositionService"
policy="static"
policy="dynamic"
unbind="unsetPositionService"/>
<reference name="EventAdmin"
cardinality="1..1"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2024 Eurotech and/or its affiliates and others
* Copyright (c) 2011, 2025 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 @@ -120,7 +120,7 @@ public class CloudConnectionManagerImpl
private SystemService systemService;
private SystemAdminService systemAdminService;
private NetworkService networkService;
private PositionService positionService;
private Optional<PositionService> positionService = Optional.empty();
private EventAdmin eventAdmin;
private CertificatesService certificatesService;
private Unmarshaller jsonUnmarshaller;
Expand Down Expand Up @@ -220,16 +220,16 @@ public NetworkService getNetworkService() {
}

public void setPositionService(PositionService positionService) {
this.positionService = positionService;
this.positionService = Optional.of(positionService);
}

public void unsetPositionService(PositionService positionService) {
if (this.positionService.equals(positionService)) {
this.positionService = null;
if (this.positionService.isPresent() && this.positionService.get().equals(positionService)) {
this.positionService = Optional.empty();
}
}

public PositionService getPositionService() {
public Optional<PositionService> getPositionService() {
return this.positionService;
}

Expand Down Expand Up @@ -346,7 +346,7 @@ protected void deactivate(ComponentContext componentContext) {
this.systemService = null;
this.systemAdminService = null;
this.networkService = null;
this.positionService = null;
this.positionService = Optional.empty();
this.eventAdmin = null;

this.cloudServiceRegistration.unregister();
Expand Down Expand Up @@ -623,7 +623,7 @@ private void publishBirthCertificate(boolean isNewConnection) throws KuraExcepti
logger.info("framework is stopping.. not republishing birth certificate");
return;
}

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

Expand Down Expand Up @@ -829,7 +829,7 @@ public void registerCloudDeliveryListener(CloudDeliveryListener cloudDeliveryLis
public void unregisterCloudDeliveryListener(CloudDeliveryListener cloudDeliveryListener) {
this.registeredCloudDeliveryListeners.remove(cloudDeliveryListener);
}

private boolean isFrameworkStopping() {
try {
final Bundle ownBundle = FrameworkUtil.getBundle(CloudConnectionManagerImpl.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2021 Eurotech and/or its affiliates and others
* Copyright (c) 2011, 2025 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 All @@ -13,6 +13,7 @@
package org.eclipse.kura.internal.cloudconnection.eclipseiot.mqtt.cloud;

import java.util.List;
import java.util.Optional;

import org.eclipse.kura.core.util.NetUtil;
import org.eclipse.kura.message.KuraBirthPayload;
Expand All @@ -23,10 +24,10 @@
import org.eclipse.kura.net.NetInterface;
import org.eclipse.kura.net.NetInterfaceAddress;
import org.eclipse.kura.net.NetworkService;
import org.eclipse.kura.position.NmeaPosition;
import org.eclipse.kura.position.PositionService;
import org.eclipse.kura.system.SystemAdminService;
import org.eclipse.kura.system.SystemService;
import org.osgi.util.position.Position;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -80,8 +81,7 @@ public KuraBirthPayload buildBirthPayload() {
.withTotalMemory(deviceProfile.getTotalMemory()).withOsArch(deviceProfile.getOsArch())
.withOsgiFramework(deviceProfile.getOsgiFramework())
.withOsgiFrameworkVersion(deviceProfile.getOsgiFrameworkVersion()).withPayloadEncoding(payloadEncoding)
.withJvmVendor(deviceProfile.getJvmVendor())
.withJdkVendorVersion(deviceProfile.getJdkVendorVersion());
.withJvmVendor(deviceProfile.getJvmVendor()).withJdkVendorVersion(deviceProfile.getJdkVendorVersion());

if (this.cloudConnectionManagerImpl.imei != null && this.cloudConnectionManagerImpl.imei.length() > 0
&& !this.cloudConnectionManagerImpl.imei.equals(ERROR)) {
Expand Down Expand Up @@ -136,7 +136,7 @@ public KuraDeviceProfile buildDeviceProfile() {
SystemService systemService = this.cloudConnectionManagerImpl.getSystemService();
SystemAdminService sysAdminService = this.cloudConnectionManagerImpl.getSystemAdminService();
NetworkService networkService = this.cloudConnectionManagerImpl.getNetworkService();
PositionService positionService = this.cloudConnectionManagerImpl.getPositionService();
Optional<PositionService> positionService = this.cloudConnectionManagerImpl.getPositionService();

//
// get the network information
Expand Down Expand Up @@ -172,12 +172,12 @@ public KuraDeviceProfile buildDeviceProfile() {
double latitude = 0.0;
double longitude = 0.0;
double altitude = 0.0;
if (positionService != null) {
NmeaPosition position = positionService.getNmeaPosition();
if (positionService.isPresent()) {
Position position = positionService.get().getPosition();
if (position != null) {
latitude = position.getLatitude();
longitude = position.getLongitude();
altitude = position.getAltitude();
latitude = Math.toDegrees(position.getLatitude().getValue());
longitude = Math.toDegrees(position.getLongitude().getValue());
altitude = position.getAltitude().getValue();
} else {
logger.warn("Unresolved PositionService reference.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ Import-Package: com.eclipsesource.json;version="0.9.5",
org.osgi.framework;version="1.5.0",
org.osgi.service.component;version="1.2.0",
org.osgi.service.event;version="1.3.0",
org.osgi.util.measurement;version="[1.0,2.0)",
org.osgi.util.position;version="[1.0,2.0)",
org.osgi.util.tracker;version="1.5.1",
org.slf4j;version="1.6.4"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2011, 2024 Eurotech and/or its affiliates and others
Copyright (c) 2011, 2025 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 @@ -49,10 +49,10 @@
unbind="unsetNetworkService"
interface="org.eclipse.kura.net.NetworkService"/>
<reference name="PositionService"
cardinality="1..1"
cardinality="0..1"
bind="setPositionService"
interface="org.eclipse.kura.position.PositionService"
policy="static"
policy="dynamic"
unbind="unsetPositionService"/>
<reference name="EventAdmin"
cardinality="1..1"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2024 Eurotech and/or its affiliates and others
* Copyright (c) 2011, 2025 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 @@ -138,7 +138,7 @@ public class CloudServiceImpl
private SystemService systemService;
private SystemAdminService systemAdminService;
private NetworkService networkService;
private PositionService positionService;
private Optional<PositionService> positionService = Optional.empty();
private EventAdmin eventAdmin;
private CertificatesService certificatesService;
private Unmarshaller jsonUnmarshaller;
Expand Down Expand Up @@ -252,16 +252,16 @@ public NetworkService getNetworkService() {
}

public void setPositionService(PositionService positionService) {
this.positionService = positionService;
this.positionService = Optional.of(positionService);
}

public void unsetPositionService(PositionService positionService) {
if (this.positionService != null && this.positionService.equals(positionService)) {
this.positionService = null;
if (this.positionService.isPresent() && this.positionService.get().equals(positionService)) {
this.positionService = Optional.empty();
}
}

public PositionService getPositionService() {
public Optional<PositionService> getPositionService() {
return this.positionService;
}

Expand Down Expand Up @@ -402,7 +402,7 @@ protected void deactivate(ComponentContext componentContext) {
this.systemService = null;
this.systemAdminService = null;
this.networkService = null;
this.positionService = null;
this.positionService = Optional.empty();
this.eventAdmin = null;
this.certificatesService = null;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2023 Eurotech and/or its affiliates and others
* Copyright (c) 2011, 2025 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 All @@ -26,13 +26,13 @@
import org.eclipse.kura.net.NetInterface;
import org.eclipse.kura.net.NetInterfaceAddress;
import org.eclipse.kura.net.NetworkService;
import org.eclipse.kura.position.NmeaPosition;
import org.eclipse.kura.position.PositionService;
import org.eclipse.kura.security.tamper.detection.TamperDetectionService;
import org.eclipse.kura.system.ExtendedProperties;
import org.eclipse.kura.system.ExtendedPropertyGroup;
import org.eclipse.kura.system.SystemAdminService;
import org.eclipse.kura.system.SystemService;
import org.osgi.util.position.Position;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -92,8 +92,7 @@ public KuraBirthPayload buildBirthPayload() {
.withTotalMemory(deviceProfile.getTotalMemory()).withOsArch(deviceProfile.getOsArch())
.withOsgiFramework(deviceProfile.getOsgiFramework())
.withOsgiFrameworkVersion(deviceProfile.getOsgiFrameworkVersion()).withPayloadEncoding(payloadEncoding)
.withJvmVendor(deviceProfile.getJvmVendor())
.withJdkVendorVersion(deviceProfile.getJdkVendorVersion());
.withJvmVendor(deviceProfile.getJvmVendor()).withJdkVendorVersion(deviceProfile.getJdkVendorVersion());

tryAddTamperStatus(birthPayloadBuilder);

Expand Down Expand Up @@ -185,7 +184,7 @@ public KuraDeviceProfile buildDeviceProfile() {
SystemService systemService = this.cloudServiceImpl.getSystemService();
SystemAdminService sysAdminService = this.cloudServiceImpl.getSystemAdminService();
NetworkService networkService = this.cloudServiceImpl.getNetworkService();
PositionService positionService = this.cloudServiceImpl.getPositionService();
Optional<PositionService> positionService = this.cloudServiceImpl.getPositionService();

//
// get the network information
Expand Down Expand Up @@ -221,12 +220,12 @@ public KuraDeviceProfile buildDeviceProfile() {
double latitude = 0.0;
double longitude = 0.0;
double altitude = 0.0;
if (positionService != null) {
NmeaPosition position = positionService.getNmeaPosition();
if (positionService.isPresent()) {
Position position = positionService.get().getPosition();
if (position != null) {
latitude = position.getLatitude();
longitude = position.getLongitude();
altitude = position.getAltitude();
latitude = Math.toDegrees(position.getLatitude().getValue());
longitude = Math.toDegrees(position.getLongitude().getValue());
altitude = position.getAltitude().getValue();
} else {
logger.warn("Unresolved PositionService reference.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,8 @@ public void shouldPublishBirthMessageWithConnectedModemInfoWhenMultipleModems()

}

private JsonObject publishBirthAndGetMetrics() throws InterruptedException, ExecutionException, TimeoutException,
KuraException, InvalidSyntaxException {
private JsonObject publishBirthAndGetMetrics()
throws InterruptedException, ExecutionException, TimeoutException, KuraException, InvalidSyntaxException {
final CompletableFuture<Void> disconnected = underTestInspector.disconnected();

cloudServiceImpl.disconnect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,7 @@ private void thenNoBirthIsPublished() throws KuraStoreException {
}

private void thenBirthIsPublishedAfter(long delayMillis, String expectedTopic) throws KuraException {
verify(this.dataService, after(delayMillis).never()).publish(eq(expectedTopic), any(), eq(0), eq(false),
eq(0));
verify(this.dataService, after(delayMillis).never()).publish(eq(expectedTopic), any(), eq(0), eq(false), eq(0));
verify(this.dataService, after(delayMillis + SLACK_DELAY).times(1)).publish(eq(expectedTopic), any(), eq(1),
eq(false), eq(0));
}
Expand Down
Loading

0 comments on commit 80b4b9e

Please sign in to comment.