【问题标题】:How to set child devices to a device via inventory api?如何通过库存 api 将子设备设置为设备?
【发布时间】:2019-12-13 00:05:27
【问题描述】:

我正在尝试使用库存 api 将子设备设置为 Cumulocity 中的设备,但不幸的是没有成功。

我已经尝试了以下方法:

            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);

没有抛出错误,但是在那之后,当我查看清单时,设备都没有设置 childDevices,托管对象也没有设置 deviceParents。 我做错了什么?

【问题讨论】:

    标签: children inventory cumulocity


    【解决方案1】:

    子设备和资产使用专用端点设置,而不是通过更改托管对象中的子列表。详情请见https://cumulocity.com/guides/reference/inventory/#managed-object-reference-collection

    在您使用 java-client 的情况下,您可以这样做:

    GId parentID = new GId(deviceId);
    GId childId = new GId(childDeviceId);
    ManagedObject managedObjectApi = inventoryApi.getManagedObjectApi(parentID);
    managedObjectApi.addChildDevice(childId);
    

    【讨论】:

    • 非常感谢 - 此代码有效。我还有一个问题 - 子设备的“deviceParents”部分是空的。这是预期的行为吗?
    • 查询设备时,您必须包含 url 参数“withParents=true”。默认情况下不填充父母。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 2015-06-12
    相关资源
    最近更新 更多