From a7517a0e4968a9551da6a5c1b61fa7ef3f60b2b5 Mon Sep 17 00:00:00 2001
From: Paulo Dias <paulo.sousa.dias@gmail.com>
Date: Thu, 26 Oct 2023 18:18:45 +0100
Subject: [PATCH] plugins/ripples/RipplesUpdater: Protect possible null pointer
 making updater stop.

---
 .../java/pt/lsts/ripples/RipplesUpdater.java  | 53 +++++++++++--------
 1 file changed, 30 insertions(+), 23 deletions(-)

diff --git a/plugins-dev/cloud/src/java/pt/lsts/ripples/RipplesUpdater.java b/plugins-dev/cloud/src/java/pt/lsts/ripples/RipplesUpdater.java
index 4dc1123935..6df4b59117 100644
--- a/plugins-dev/cloud/src/java/pt/lsts/ripples/RipplesUpdater.java
+++ b/plugins-dev/cloud/src/java/pt/lsts/ripples/RipplesUpdater.java
@@ -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();