我正在用 Java 为 Azure IotCentral 编写一个设备客户端。我无法让设备孪生属性在 IotCentral 中显示为“已同步”。具体来说,我有一个名为“test”的“toggle”属性。
我在我的设备客户端中收到对所需属性的更改。所需属性的值为 HashMap{"value":true}。
当我将更改报告为已报告的属性时,该属性不会在 IotCentral 界面中显示为已同步,但会显示通知“n 分钟内无更新”。
我怀疑我报告的属性格式不正确?
我试过了
- 键:“测试”,值:Boolean.true
- 键:“测试”,值:“真”
- 键:“测试”,值:HashMap{“值”:Boolean.true}
这些都不起作用。
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
那么报告属性的预期格式是什么?有这方面的文件吗?