Skip to content

Commit

Permalink
plugins/ripples/RipplesUpdater: Protect possible null pointer making …
Browse files Browse the repository at this point in the history
…updater stop.
  • Loading branch information
paulosousadias committed Oct 26, 2023
1 parent ad3b7f8 commit a7517a0
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions plugins-dev/cloud/src/java/pt/lsts/ripples/RipplesUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,36 +222,43 @@ private RipplesPlan pcsToRipplesPlan(PlanControlState pcs) {

@Periodic(millisBetweenUpdates = 1000)
public void sendUpdatesToRipples() {
if (!this.connected)
if (!this.connected) {
return;
ArrayList<RipplesAsset> payload = new ArrayList<>();
assetStates.forEach((sysName, assetState) -> {
PlanControlState pcs = planStates.get(sysName);
RipplesPlan plan = new RipplesPlan();
if (pcs != null) {
plan = pcsToRipplesPlan(pcs);
}
ImcSystem imcSystem = ImcSystemsHolder.lookupSystemByName(sysName);
int imcId = (imcSystem != null && imcSystem.getId() != null) ? imcSystem.getId().intValue() : -1;
payload.add(new RipplesAsset(sysName, imcId, assetState, plan));
NeptusLog.pub().debug("Asset state: " + assetState);
});
}

String assetsAsJson = gson.toJson(payload);
try {
NeptusLog.pub().info("Sending update for " + payload.size() + " assets");
sendPost(assetsAsJson);
}
catch (IOException e) {
e.printStackTrace();
}
ArrayList<RipplesAsset> payload = new ArrayList<>();
assetStates.forEach((sysName, assetState) -> {
PlanControlState pcs = planStates.get(sysName);
RipplesPlan plan = new RipplesPlan();
if (pcs != null) {
plan = pcsToRipplesPlan(pcs);
}
ImcSystem imcSystem = ImcSystemsHolder.lookupSystemByName(sysName);
int imcId = (imcSystem != null && imcSystem.getId() != null) ? imcSystem.getId().intValue() : -1;
payload.add(new RipplesAsset(sysName, imcId, assetState, plan));
NeptusLog.pub().debug("Asset state: " + assetState);
});

try {
String assetsAsJson = gson.toJson(payload);
NeptusLog.pub().info("Sending update for " + payload.size() + " assets");
sendPost(assetsAsJson);
}
catch (Exception e) {
NeptusLog.pub().warn(e.getMessage());
}

synchronized (assetStates) {
assetStates.clear();
synchronized (assetStates) {
assetStates.clear();
}
}
catch (Exception e) {
NeptusLog.pub().warn(e.getMessage());
}
}

private String sendPost(String data) throws IOException {
private String sendPost(String data) throws Exception {
if (this.connected) {
URL url = new URL(this.ripplesPostUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
Expand Down

0 comments on commit a7517a0

Please sign in to comment.