IOT-3234 fix discovery request creating 55/27555/2 iotivity-cloud
authorPeter Rafaj <peter.rafaj@kistler.com>
Mon, 12 Nov 2018 15:22:03 +0000 (16:22 +0100)
committerJozef Kralik <jojo.lwin@gmail.com>
Fri, 23 Nov 2018 12:21:39 +0000 (12:21 +0000)
Change-Id: I1a2c2ce90555fa8c7049d0986ac8806c29013f48
Signed-off-by: Peter Rafaj <peter.rafaj@kistler.com>
cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/directory/RDManager.java

index 7a01402..4212a83 100644 (file)
@@ -247,7 +247,7 @@ public class RDManager {
         ArrayList<HashMap<String, Object>> records = DBManager.getInstance()
                 .selectRecord(Constants.RD_TABLE, condition);
         if (!records.isEmpty()) {
-            response.add(makeDiscoverResponseSegment(records));
+            response.addAll(makeDiscoverResponseSegment(records));
         }
     }
 
@@ -263,28 +263,32 @@ public class RDManager {
         return records.stream().map(record -> (String)record.get("di")).collect(Collectors.toList());
     }
 
-    private HashMap<String, Object> makeDiscoverResponseSegment(
+    private Collection<HashMap<String, Object>> makeDiscoverResponseSegment(
             ArrayList<HashMap<String, Object>> records) {
 
-        HashMap<String, Object> responseSegment = new HashMap<>();
-
-        // make Tags
-        HashMap<String, Object> discoverTag = mPayloadManager
-                .setPayloadData(records.get(0), mPayloadManager.discoverTagKey);
-        responseSegment.putAll(discoverTag);
-
-        ArrayList<Object> links = new ArrayList<>();
-        // make links
-        for (HashMap<String, Object> record : records) {
-            HashMap<String, Object> link = mPayloadManager
+        final Map<String, HashMap<String, Object>> result = new HashMap<>();
+        for (final HashMap<String, Object> record : records) {
+            final String di = (String) record.get(Constants.DEVICE_ID);
+            ArrayList<Object> links;
+            if (!result.containsKey(record.get(Constants.DEVICE_ID))) {
+                final HashMap<String, Object> discovered = createDevice(record);
+                result.put(di, discovered);
+                links = (ArrayList<Object>) discovered.get(Constants.LINKS);
+            } else {
+                links = (ArrayList<Object>) result.get(di).get(Constants.LINKS);
+            }
+            HashMap<String, Object> discoveredLinks = mPayloadManager
                     .setPayloadData(record, mPayloadManager.discoverLinkKey);
-            mPayloadManager.changePolicyTypeToDiscover(link);
-            links.add(link);
+            mPayloadManager.changePolicyTypeToDiscover(discoveredLinks);
+            links.add(discoveredLinks);
         }
-        responseSegment.put(Constants.LINKS, links);
-
-        return responseSegment;
+        return result.values();
+    }
 
+    private HashMap<String,Object> createDevice(final  HashMap<String, Object> record){
+        HashMap<String, Object> discoverTag = mPayloadManager.setPayloadData(record, mPayloadManager.discoverTagKey);
+        discoverTag.put(Constants.LINKS, new ArrayList<>());
+        return discoverTag;
     }
 
     private void setmResourcePresence(