Skip to content

Commit

Permalink
Merge branch 'feature/iridium-messaging-expand' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
paulosousadias committed Dec 19, 2024
2 parents 220db57 + 6523afb commit 077a129
Show file tree
Hide file tree
Showing 11 changed files with 631 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.TimeZone;

import javax.swing.table.AbstractTableModel;
Expand All @@ -55,6 +56,7 @@
import pt.lsts.neptus.comm.iridium.ImcIridiumMessage;
import pt.lsts.neptus.comm.iridium.IridiumCommand;
import pt.lsts.neptus.comm.iridium.IridiumMessage;
import pt.lsts.neptus.comm.manager.imc.ImcId16;
import pt.lsts.neptus.comm.manager.imc.ImcMsgManager;
import pt.lsts.neptus.i18n.I18n;
import pt.lsts.neptus.messages.TypedMessageFilter;
Expand Down Expand Up @@ -91,8 +93,18 @@ public void onMessage(MessageInfo info, IMCMessage msg) {
if (msg.getMgid() == IridiumMsgRx.ID_STATIC) {
IridiumMessage m;
try {
Date now = new Date();
m = IridiumMessage.deserialize(msg.getRawData("data"));
//msgs.addElement(m); // m.source == ImcMsgManager.getManager().getLocalId().intValue()
if (!new Date(m.timestampMillis).before(now) && !Objects.equals(msg.getDate(), new Date(0))) {
m.timestampMillis = msg.getDate().getTime();
}
if (m.source == ImcId16.NULL_ID.intValue()) {
m.source = msg.getSrc();
}
if (m.destination == ImcId16.NULL_ID.intValue()) {
m.destination = msg.getDst();
}
msgs.add(m);
fireTableRowsInserted(msgs.size()-1, msgs.size()-1);
}
Expand All @@ -112,7 +124,17 @@ public void onMessage(MessageInfo info, IMCMessage msg) {
else if (msg.getMgid() == IridiumMsgTx.ID_STATIC) {
IridiumMessage m;
try {
Date now = new Date();
m = IridiumMessage.deserialize(msg.getRawData("data"));
if (!new Date(m.timestampMillis).before(now) && !Objects.equals(msg.getDate(), new Date(0))) {
m.timestampMillis = msg.getDate().getTime();
}
if (m.source == ImcId16.NULL_ID.intValue()) {
m.source = msg.getSrc();
}
if (m.destination == ImcId16.NULL_ID.intValue()) {
m.destination = msg.getDst();
}
synchronized (msgs) {
msgs.add(m);
if (msg.getSrc() == ImcMsgManager.getManager().getLocalId().intValue()) { // Only keeps local
Expand Down
64 changes: 41 additions & 23 deletions src/java/pt/lsts/neptus/comm/iridium/DuneIridiumMessenger.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Vector;
import java.util.List;
import java.util.Set;

import pt.lsts.imc.IMCDefinition;
import pt.lsts.imc.IMCInputStream;
Expand Down Expand Up @@ -67,9 +68,7 @@ public class DuneIridiumMessenger implements IridiumMessenger, MessageListener<M

protected int req_id = (int) (Math.random() * 65535);

protected Vector<IridiumMessage> messagesReceived = new Vector<>();

protected HashSet<IridiumMessageListener> listeners = new HashSet<>();
protected Set<IridiumMessageListener> listeners = new HashSet<>();

@Override
public void addListener(IridiumMessageListener listener) {
Expand All @@ -91,7 +90,6 @@ public void onMessage(MessageInfo info, IMCMessage msg) {
if (msg.getMgid() == IridiumMsgRx.ID_STATIC) {
try {
IridiumMessage m = IridiumMessage.deserialize(msg.getRawData("data"));
messagesReceived.add(m);
NeptusLog.pub().info("Received a " + m.getClass().getSimpleName() + " from " + msg.getSourceName());
for (IridiumMessageListener listener : listeners)
listener.messageReceived(m);
Expand Down Expand Up @@ -119,21 +117,15 @@ else if (msg.getMgid() == IridiumTxStatus.ID_STATIC) {
}
}

private static void sendToProvider(IridiumMsgTx tx, ImcSystem system) throws Exception {
if (!ImcMsgManager.getManager().sendMessageToSystem(tx, system.getName()))
throw new Exception("Error while sending message to " + system.getName() + " via IMC.");
}

@Override
public void sendMessage(IridiumMessage msg) throws Exception {
ImcSystem system = getProviderToUse();

ArrayList<String> providers = new ArrayList<String>();
providers.addAll(getIridiumServiceProviders());

if (providers.isEmpty()) {
throw new Exception("No Iridium service providers are available");
}

providers.sort(Collections.reverseOrder());
ImcSystem system = ImcSystemsHolder.lookupSystemByName(providers.iterator().next());

System.out.println("Subscribed to Iridium Device Updates through "+system.getName());

// Activate and deactivate subscriptions should use the id of the used gateway
//if (msg instanceof ActivateSubscription || msg instanceof DeactivateSubscription) {
// ImcSystem system = ImcSystemsHolder.lookupSystemByName(messengerName);
Expand All @@ -145,10 +137,38 @@ public void sendMessage(IridiumMessage msg) throws Exception {
tx.setReqId((++req_id % 65535));
tx.setTtl(3600);
tx.setData(msg.serialize());
if (!ImcMsgManager.getManager().sendMessageToSystem(tx, system.getName()))
throw new Exception("Error while sending message to " + system.getName() + " via IMC.");
sendToProvider(tx, system);
}


@Override
public void sendMessageRaw(String destinationName, String destinationAddr, byte[] data) throws Exception {
ImcSystem system = getProviderToUse();

// Activate and deactivate subscriptions should use the id of the used gateway
//if (msg instanceof ActivateSubscription || msg instanceof DeactivateSubscription) {
// ImcSystem system = ImcSystemsHolder.lookupSystemByName(messengerName);
// if (system != null)
// msg.setSource(system.getId().intValue());

IridiumMsgTx tx = new IridiumMsgTx();
tx.setReqId((++req_id % 65535));
tx.setTtl(3600);
tx.setData(data);
sendToProvider(tx, system);
}

private ImcSystem getProviderToUse() throws Exception {
List<String> providers = new ArrayList<>(getIridiumServiceProviders());
if (providers.isEmpty()) {
throw new Exception("No Iridium service providers are available");
}

providers.sort(Collections.reverseOrder());
ImcSystem system = ImcSystemsHolder.lookupSystemByName(providers.iterator().next());
NeptusLog.pub().info("Subscribed to Iridium Device Updates through {}", system.getName());
return system;
}

public Collection<String> getIridiumServiceProviders() {
ArrayList<String> names = new ArrayList<>();
ImcSystem[] providers = ImcSystemsHolder.lookupSystemByService("iridium", SystemTypeEnum.ALL, true);
Expand All @@ -162,7 +182,7 @@ public Collection<String> getIridiumServiceProviders() {

@Override
public Collection<IridiumMessage> pollMessages(Date timeSince) throws Exception {
return new Vector<>();
return Collections.emptyList();
}

@Override
Expand All @@ -178,12 +198,10 @@ public boolean isAvailable() {
@Override
public void cleanup() {
listeners.clear();
messagesReceived.clear();
}

@Override
public String toString() {
return getName();
}

}
Loading

0 comments on commit 077a129

Please sign in to comment.