2

I am trying to set child device to a device in Cumulocity, using the inventory api, unfortunately without success.

I have already tried the following:

            GId gid = new GId(deviceId);
            ManagedObjectRepresentation deviceRepresentation = inventoryApi.get(gid);
            ManagedObjectReferenceCollectionRepresentation childDevices = new ManagedObjectReferenceCollectionRepresentation();
            ManagedObjectReferenceRepresentation morr = new ManagedObjectReferenceRepresentation();
            morr.setManagedObject(mo);
            List<ManagedObjectReferenceRepresentation> references = new ArrayList<ManagedObjectReferenceRepresentation>();
            references.add(morr);
            childDevices.setReferences(references);
            deviceRepresentation.setChildDevices(childDevices);
            deviceRepresentation.setLastUpdatedDateTime(null);
            inventoryApi.update(deviceRepresentation);

There is no error thrown, but after that, when I look in the inventory, neither the device has childDevices set, neither the managed object has deviceParents set. What I am doing wrong?

Ivajlo Iliev
  • 303
  • 1
  • 3
  • 19

1 Answers1

2

Child devices and assets are set with a dedicated endpoint, not by changing the children lists in the managed object. See https://cumulocity.com/guides/reference/inventory/#managed-object-reference-collection for details.

In your case using java-client you can do it like this:

GId parentID = new GId(deviceId);
GId childId = new GId(childDeviceId);
ManagedObject managedObjectApi = inventoryApi.getManagedObjectApi(parentID);
managedObjectApi.addChildDevice(childId);
l2p
  • 420
  • 3
  • 9
  • 1
    Thank you very much - this code worked. I have additional question - the "deviceParents" section of the child device is empty. Is this the expected behavior? – Ivajlo Iliev Aug 05 '19 at 15:37
  • 1
    When querying devices you have to include url parameter "withParents=true". By default the parents are not populated. – socona Aug 05 '19 at 16:34