DeviceTwinClient调用getTags()方法时返回java.lang.IllegalArgumentException

时间:2019-05-17 09:54:34

标签: azure azure-iot-hub azure-iot-sdk azure-iot-hub-device-management

当调用iotHub查询以获取多个设备的DevicetwinDevice时:

SqlQuery sqlQuery = SqlQuery.createSqlQuery("*", SqlQuery.FromType.DEVICES, generateWhere(listDeviceIds), null);
            Query twinQueryCollection = twinClient.queryTwin(sqlQuery.getQuery());
            QueryOptions options = new QueryOptions();
            while (twinClient.hasNextDeviceTwin(twinQueryCollection)) {
                    deviceTwinList.add(twinClient.getNextDeviceTwin(twinQueryCollection));
            }

稍后在从元素中调用getTags()时,引发了异常:

throw new IllegalArgumentException("Key cannot contain illegal unicode control characters '.', '$', ' '");

问题在于标记内的元素包含带有$的键:

{"Session":"Sessio1",
"ManufacturingDate":"2019-0517T08:57:20.260+02:00[Europe/Madrid]",
"IV":"01.00",
"**$version**":1.0,
"Subtype":"-----",
"**$metadata**":{}
}

标签上的那些元素不是我的。是SDK错误吗?我的代码包含错误?

1 个答案:

答案 0 :(得分:0)

我找到了可能的问题的解决方案,执行查询后,我得到了DeviceTwinDevice的列表。要毫无例外地获取标签,我需要执行 twinClient.getTwin(deviceTwinDevice); 。像这样:

SqlQuery sqlQuery = SqlQuery.createSqlQuery("*", SqlQuery.FromType.DEVICES, generateWhere(listDeviceIds), null);
            Query twinQueryCollection = twinClient.queryTwin(sqlQuery.getQuery());
            QueryOptions options = new QueryOptions();
            while (twinClient.hasNextDeviceTwin(twinQueryCollection)) {
                    deviceTwinList.add(twinClient.getNextDeviceTwin(twinQueryCollection));
            }
            for (DeviceTwinDevice deviceTwinDevice : deviceTwinList) {
                **twinClient.getTwin(deviceTwinDevice);**
                if(!deviceTwinDevice.getTags().isEmpty()) {
                    -------------------------------------------------------
                }

getTwin()方法删除$ keys。在这种情况下,我向Azure IotHub抛出了多条消息。单个设备的查询和getTwin。我想在Azure IotHub的唯一消息中执行相同的操作。有可能吗?