Skip to content

Commit

Permalink
fix: added modem infos to birth message when the NetworkStatusService…
Browse files Browse the repository at this point in the history
… is used (#5128)

* Added modem infos to birth in CloudServiceImpl

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

* Added modem infos to birth in CloudConnectionManager

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

* Added test form modem infos in CloudServiceImplTest

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

* Updated copyright; small fixes

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

* Small refactors for fixing sonar issues

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

* Fixed iccid and imsi values; updated metatypes

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

---------

Signed-off-by: pierantoniomerlino <[email protected]>
  • Loading branch information
pierantoniomerlino authored Feb 19, 2024
1 parent 1c36691 commit 574e54d
Show file tree
Hide file tree
Showing 10 changed files with 398 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Import-Package: com.eclipsesource.json;version="0.9.5",
org.eclipse.kura.message;version="[1.5,2.0)",
org.eclipse.kura.net;version="[2.0,3.0)",
org.eclipse.kura.net.modem;version="[2.0,3.0)",
org.eclipse.kura.net.status;version="[1.1,2.0)",
org.eclipse.kura.net.status.modem;version="[1.0,2.0)",
org.eclipse.kura.position;version="[1.0,2.0)",
org.eclipse.kura.system;version="[1.0,2.0)",
org.osgi.framework;version="1.5.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2018, 2020 Eurotech and/or its affiliates and others
Copyright (c) 2018, 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 @@ -73,6 +73,12 @@
policy="static"
target="(kura.service.pid=org.eclipse.kura.json.marshaller.unmarshaller.provider)"
unbind="unsetJsonMarshaller"/>
<reference bind="setNetworkStatusService"
cardinality="0..1"
interface="org.eclipse.kura.net.status.NetworkStatusService"
name="NetworkStatusService"
policy="dynamic"
unbind="unsetNetworkStatusService"/>
<property name="kura.ui.service.hide" type="Boolean" value="true"/>
<property name="kura.ui.factory.hide" type="String" value="true"/>
</scr:component>
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
cardinality="0"
required="true"
default="false"
description="Whether or not to republish the MQTT Birth Certificate on modem detection event"/>
description="Whether or not to republish the MQTT Birth Certificate on modem detection event. This functionality is currently not supported on devices configured to use NetworkManager, in this case the property value is ignored."/>"/>

<AD id="payload.encoding"
name="Payload Encoding"
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, 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 All @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.kura.internal.cloudconnection.eclipseiot.mqtt.cloud;

import static java.util.Objects.nonNull;
import static org.eclipse.kura.cloud.CloudPayloadEncoding.KURA_PROTOBUF;
import static org.eclipse.kura.cloud.CloudPayloadEncoding.SIMPLE_JSON;
import static org.eclipse.kura.internal.cloudconnection.eclipseiot.mqtt.message.MessageConstants.FULL_TOPIC;
Expand All @@ -21,13 +22,17 @@

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -68,6 +73,11 @@
import org.eclipse.kura.message.KuraPayload;
import org.eclipse.kura.net.NetworkService;
import org.eclipse.kura.net.modem.ModemReadyEvent;
import org.eclipse.kura.net.status.NetworkInterfaceStatus;
import org.eclipse.kura.net.status.NetworkInterfaceType;
import org.eclipse.kura.net.status.NetworkStatusService;
import org.eclipse.kura.net.status.modem.ModemInterfaceStatus;
import org.eclipse.kura.net.status.modem.Sim;
import org.eclipse.kura.position.PositionLockedEvent;
import org.eclipse.kura.position.PositionService;
import org.eclipse.kura.system.SystemAdminService;
Expand All @@ -86,6 +96,10 @@ public class CloudConnectionManagerImpl
implements DataServiceListener, ConfigurableComponent, EventHandler, CloudPayloadProtoBufEncoder,
CloudPayloadProtoBufDecoder, RequestHandlerRegistry, CloudConnectionManager, CloudEndpoint {

private static final String KURA_PAYLOAD = "KuraPayload";

private static final String SETUP_CLOUD_SERVICE_CONNECTION_ERROR_MESSAGE = "Cannot setup cloud service connection";

private static final String ERROR = "ERROR";

private static final Logger logger = LoggerFactory.getLogger(CloudConnectionManagerImpl.class);
Expand All @@ -112,6 +126,7 @@ public class CloudConnectionManagerImpl
private CertificatesService certificatesService;
private Unmarshaller jsonUnmarshaller;
private Marshaller jsonMarshaller;
private Optional<NetworkStatusService> networkStatusService = Optional.empty();

// package visibility for LifeCyclePayloadBuilder
String imei;
Expand Down Expand Up @@ -154,7 +169,9 @@ public void setDataService(DataService dataService) {
}

public void unsetDataService(DataService dataService) {
this.dataService = null;
if (this.dataService.equals(dataService)) {
this.dataService = null;
}
}

public DataService getDataService() {
Expand All @@ -166,7 +183,9 @@ public void setSystemAdminService(SystemAdminService systemAdminService) {
}

public void unsetSystemAdminService(SystemAdminService systemAdminService) {
this.systemAdminService = null;
if (this.systemAdminService.equals(systemAdminService)) {
this.systemAdminService = null;
}
}

public SystemAdminService getSystemAdminService() {
Expand All @@ -178,7 +197,9 @@ public void setSystemService(SystemService systemService) {
}

public void unsetSystemService(SystemService systemService) {
this.systemService = null;
if (this.systemService.equals(systemService)) {
this.systemService = null;
}
}

public SystemService getSystemService() {
Expand All @@ -190,7 +211,9 @@ public void setNetworkService(NetworkService networkService) {
}

public void unsetNetworkService(NetworkService networkService) {
this.networkService = null;
if (this.networkService.equals(networkService)) {
this.networkService = null;
}
}

public NetworkService getNetworkService() {
Expand All @@ -202,7 +225,9 @@ public void setPositionService(PositionService positionService) {
}

public void unsetPositionService(PositionService positionService) {
this.positionService = null;
if (this.positionService.equals(positionService)) {
this.positionService = null;
}
}

public PositionService getPositionService() {
Expand All @@ -214,23 +239,39 @@ public void setEventAdmin(EventAdmin eventAdmin) {
}

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

public void setJsonUnmarshaller(Unmarshaller jsonUnmarshaller) {
this.jsonUnmarshaller = jsonUnmarshaller;
}

public void unsetJsonUnmarshaller(Unmarshaller jsonUnmarshaller) {
this.jsonUnmarshaller = null;
if (this.jsonUnmarshaller.equals(jsonUnmarshaller)) {
this.jsonUnmarshaller = null;
}
}

public void setJsonMarshaller(Marshaller jsonMarshaller) {
this.jsonMarshaller = jsonMarshaller;
}

public void unsetJsonMarshaller(Marshaller jsonMarshaller) {
this.jsonMarshaller = null;
if (this.jsonMarshaller.equals(jsonMarshaller)) {
this.jsonMarshaller = null;
}
}

public void setNetworkStatusService(NetworkStatusService networkStatusService) {
this.networkStatusService = Optional.of(networkStatusService);
}

public void unsetNetworkStatusService(NetworkStatusService networkStatusService) {
if (this.networkStatusService.isPresent() && this.networkStatusService.get().equals(networkStatusService)) {
this.networkStatusService = Optional.empty();
}
}

// ----------------------------------------------------------------
Expand Down Expand Up @@ -271,7 +312,7 @@ protected void activate(ComponentContext componentContext, Map<String, Object> p
try {
setupCloudConnection(false);
} catch (KuraException e) {
logger.warn("Cannot setup cloud service connection", e);
logger.warn(SETUP_CLOUD_SERVICE_CONNECTION_ERROR_MESSAGE, e);
}
}
}
Expand All @@ -285,7 +326,7 @@ public void updated(Map<String, Object> properties) {
try {
setupCloudConnection(false);
} catch (KuraException e) {
logger.warn("Cannot setup cloud service connection");
logger.warn(SETUP_CLOUD_SERVICE_CONNECTION_ERROR_MESSAGE);
}
}
}
Expand Down Expand Up @@ -333,7 +374,7 @@ public void handleEvent(Event event) {
tryPublishBirthCertificate(false);
}
}

private void handlePositionLockedEvent() {
// if we get a position locked event,
// republish the birth certificate only if we are configured to
Expand Down Expand Up @@ -410,7 +451,7 @@ public byte[] encodePayload(KuraPayload payload) throws KuraException {
} else if (preferencesEncoding == SIMPLE_JSON) {
bytes = encodeJsonPayload(payload);
} else {
throw new KuraException(KuraErrorCode.ENCODE_ERROR, "KuraPayload");
throw new KuraException(KuraErrorCode.ENCODE_ERROR, KURA_PAYLOAD);
}
return bytes;
}
Expand All @@ -426,7 +467,7 @@ public void onConnectionEstablished() {
try {
setupCloudConnection(true);
} catch (KuraException e) {
logger.warn("Cannot setup cloud service connection");
logger.warn(SETUP_CLOUD_SERVICE_CONNECTION_ERROR_MESSAGE);
}

postConnectionStateChangeEvent(true);
Expand Down Expand Up @@ -569,7 +610,7 @@ public byte[] getBytes(KuraPayload kuraPayload, boolean gzipped) throws KuraExce
bytes = encoder.getBytes();
return bytes;
} catch (IOException e) {
throw new KuraException(KuraErrorCode.ENCODE_ERROR, "KuraPayload", e);
throw new KuraException(KuraErrorCode.ENCODE_ERROR, KURA_PAYLOAD, e);
}
}

Expand All @@ -588,7 +629,7 @@ public KuraPayload buildFromByteArray(byte[] payload) throws KuraException {
kuraPayload = encoder.buildFromByteArray();
return kuraPayload;
} catch (KuraInvalidMessageException | IOException e) {
throw new KuraException(KuraErrorCode.DECODER_ERROR, "KuraPayload", e);
throw new KuraException(KuraErrorCode.DECODER_ERROR, KURA_PAYLOAD, e);
}
}

Expand All @@ -614,6 +655,7 @@ private void setupDeviceSubscriptions() throws KuraException {
}

private void publishBirthCertificate(boolean isNewConnection) throws KuraException {
readModemProfile();
LifecycleMessage birthToPublish = new LifecycleMessage(this.options, this).asBirthCertificateMessage();

if (isNewConnection) {
Expand All @@ -634,7 +676,7 @@ private void publishWithDelay(LifecycleMessage message) {
}

logger.debug("CloudConnectionManagerImpl: BIRTH message cached for 30s.");

this.scheduledBirthPublisherFuture = this.scheduledBirthPublisher.schedule(() -> {
try {
logger.debug("CloudConnectionManagerImpl: publishing cached BIRTH message.");
Expand Down Expand Up @@ -681,7 +723,7 @@ private byte[] encodeProtobufPayload(KuraPayload payload) throws KuraException {
try {
bytes = encoder.getBytes();
} catch (IOException e) {
throw new KuraException(KuraErrorCode.ENCODE_ERROR, "KuraPayload", e);
throw new KuraException(KuraErrorCode.ENCODE_ERROR, KURA_PAYLOAD, e);
}
return bytes;
}
Expand Down Expand Up @@ -801,14 +843,10 @@ public void unregister(String id) throws KuraException {
}

public String getNotificationPublisherPid() {
// TODO: Specify a notification publisher when Hono will define apis to support long running jobs with
// notifications
// return NOTIFICATION_PUBLISHER_PID;
throw new UnsupportedOperationException();
}

public CloudNotificationPublisher getNotificationPublisher() {
// return this.notificationPublisher;
throw new UnsupportedOperationException();
}

Expand All @@ -822,4 +860,63 @@ public void registerCloudDeliveryListener(CloudDeliveryListener cloudDeliveryLis
public void unregisterCloudDeliveryListener(CloudDeliveryListener cloudDeliveryListener) {
this.registeredCloudDeliveryListeners.remove(cloudDeliveryListener);
}

private void readModemProfile() {
this.networkStatusService.ifPresent(statusService -> {
List<ModemInterfaceStatus> modemStatuses = getModemsStatuses(statusService);
if (nonNull(modemStatuses) && !modemStatuses.isEmpty()) {
readModemInfos(modemStatuses);
} else {
this.imei = null;
this.iccid = null;
this.imsi = null;
this.rssi = null;
this.modemFwVer = null;
}
});
}

private List<ModemInterfaceStatus> getModemsStatuses(NetworkStatusService networkStatusService) {
List<ModemInterfaceStatus> modemStatuses = new ArrayList<>();
try {
List<String> interfaceIds = networkStatusService.getInterfaceIds();
for (String interfaceId : interfaceIds) {
Optional<NetworkInterfaceStatus> networkInterfaceStatus = networkStatusService
.getNetworkStatus(interfaceId);
networkInterfaceStatus.ifPresent(state -> {
NetworkInterfaceType type = state.getType();
if (NetworkInterfaceType.MODEM.equals(type)) {
modemStatuses.add((ModemInterfaceStatus) state);
}
});
}
} catch (KuraException e) {
logger.error("Error reading modem profile", e);
}
return modemStatuses;
}

private void readModemInfos(List<ModemInterfaceStatus> modemStatuses) {
Collections.sort(modemStatuses, Comparator.comparing(ModemInterfaceStatus::getConnectionStatus));
ModemInterfaceStatus modemStatus = modemStatuses.get(modemStatuses.size() - 1);
Optional<Sim> activeSim = Optional.empty();

List<Sim> availableSims = modemStatus.getAvailableSims();
for (Sim sim : availableSims) {
if (sim.isActive() && sim.isPrimary()) {
activeSim = Optional.of(sim);
}
}

this.iccid = "NA";
this.imsi = "NA";
activeSim.ifPresent(sim -> {
this.iccid = sim.getIccid();
this.imsi = sim.getImsi();
});
this.imei = modemStatus.getSerialNumber();
this.rssi = String.valueOf(modemStatus.getSignalStrength());
this.modemFwVer = modemStatus.getFirmwareVersion();

}
}
2 changes: 2 additions & 0 deletions kura/org.eclipse.kura.core.cloud/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Import-Package: com.eclipsesource.json;version="0.9.5",
org.eclipse.kura.message;version="[1.5,2.0)",
org.eclipse.kura.net;version="[2.0,3.0)",
org.eclipse.kura.net.modem;version="[2.0,3.0)",
org.eclipse.kura.net.status;version="[1.1,2.0)",
org.eclipse.kura.net.status.modem;version="[1.0,2.0)",
org.eclipse.kura.position;version="[1.0,2.0)",
org.eclipse.kura.security.tamper.detection;version="[1.0,2.0)",
org.eclipse.kura.system;version="[1.5,2.0)",
Expand Down
Loading

0 comments on commit 574e54d

Please sign in to comment.