我正在用Java编写Azure IotCentral的设备客户端。我在获取设备孪生属性以在IotCentral中显示为“已同步”时遇到问题。 具体来说,我有一个名为“ test”的“ toggle”属性。
我在设备客户端中收到对所需属性的更改。 所需的属性的值为HashMap {“ value”:true}。
当我将更改作为报告的属性报告回来时,该属性在IotCentral界面中未显示为已同步,而是显示“ n分钟未更新”通知。
我怀疑我报告的财产格式不正确吗?
我尝试了
这些都不起作用。
public static void main(String[] args) throws Exception {
String connectString = "HostName=iotc-xxx.azure-devices.net;DeviceId=showcase1;SharedAccessKey=yyy=";
DeviceClient client = new DeviceClient(connectString, IotHubClientProtocol.MQTT);
client.open();
client.startDeviceTwin(
(status, ctx)->System.out.println("IoT Hub responded to device twin operation with status " + status.name()),
null,
(prop,ctx)->System.out.println("onProperty callback for " + (prop.getIsReported() ? "reported" : "desired") +
" property " + prop.getKey() + " to " + prop.getValue() + ", Properties version:" + prop.getVersion()),
null);
Map<Property, Pair<TwinPropertyCallBack, Object>> desiredProperties = new HashMap<Property, Pair<TwinPropertyCallBack, Object>>()
{
{
put(new Property("test", null), new Pair<TwinPropertyCallBack, Object>((p,o)-> {
HashMap<String,Object> hm = (HashMap)p.getValue();
Boolean v = (Boolean)hm.get("value");
System.out.println("Received value for test: " + v);
try {
var hs = new HashSet<Property>();
hs.add(new Property("test", v));
client.sendReportedProperties(hs);
System.out.println("Sent reported property map: " + hs);
} catch(IOException e) {
System.out.println("Could not report property: "+ e);
}
}, null));
}
};
client.subscribeToTwinDesiredProperties(desiredProperties);
client.getDeviceTwin(); // For each desired property in the Service, the SDK will call the appropriate callback with the value and version.
}
当我执行程序并更改切换UI元素时,程序将输出以下内容:
10:50:12.471 [MQTT Call: pumpshowcase1] INFO c.m.a.s.i.d.t.mqtt.MqttDeviceTwin - Message received on DT DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE
10:50:12.473 [MQTT Call: pumpshowcase1] INFO c.m.a.s.i.d.t.IotHubTransport - Message with hashcode 1740719682 is received from IotHub on Tue Mar 26 10:50:12 CET 2019, method name is onMessageReceived
Received value for test: true
10:50:12.486 [azure-iot-sdk-IotHubReceiveTask] INFO c.m.azure.sdk.iot.device.DeviceIO - Message with messageid 7d034cd6-2eb3-4c9e-9946-9d14c7bc11a4 along with callback and callbackcontext is added to the queue, method name is sendEventAsync
Sent reported property map: [test - true]
10:50:12.493 [azure-iot-sdk-IotHubSendTask] INFO c.m.a.s.i.d.t.IotHubTransport - Get the message from waiting message queue to be sent to IoT Hub, method name is sendMessages
10:50:12.983 [azure-iot-sdk-IotHubSendTask] INFO c.m.a.s.i.d.t.IotHubTransport - Invoking the callback function for sent message, IoT Hub responded to message with status OK_EMPTY, method name is invokeCallbacks
10:50:13.022 [MQTT Call: pumpshowcase1] INFO c.m.a.s.i.d.t.IotHubTransport - Message with hashcode 506690950 is received from IotHub on Tue Mar 26 10:50:13 CET 2019, method name is onMessageReceived
IoT Hub responded to device twin operation with status OK_EMPTY
那么报告的属性的预期格式是什么? 是否有相关文件?