If a product does not support thing specification, you can use a device shadow to cache the latest status of the device in the IoT Platform console. The data cached in the console is in JSON format. You can parse the data as needed. Thing specification provides higher device shadow capabilities, which can separately display each device property, event, and service. Thing specification also stores logs of all operations.
Note: For more information about API operations for device shadows, see IDeviceShadow.
Send data to IoT Platform
-
Obtain device shadows from IoT Platform.
-
Update device shadows in the IoT Platform console.
-
Delete device shadows from IoT Platform.
// Obtain a device shadow. For more information, see the demo.
String data = "{" + "\"method\": \"get\"" + "}";
// Update the device shadow. Read the returned version value from the obtained device shadow and replace {ver} with version+1 during the update.
// String data = "{" + "\"method\": \"update\"," + "\"state\": {" + "\"reported\": {" +
// "\"color\": \"red\"" + "}" + "}," + "\"version\": {ver}" + "}";
// Delete the device shadow. Change the values of color and {ver}.
// String data = "{" + "\"method\": \"delete\"," + "\"state\": {" + "\"reported\": {" +
// "\"color\": \"null\"" + "}" + "}," + "\"version\": {ver}" + "}";
LinkKit.getInstance().getDeviceShadow().shadowUpload(data, new IConnectSendListener() {
@Override
public void onResponse(ARequest aRequest, AResponse aResponse) {
// The device shadow is updated.
// For more information about how to parse the data, see the demo or the "Send data from IoT Platform" section.
}
@Override
public void onFailure(ARequest aRequest, AError aError) {
// The device shadow failed to be updated.
}
});
Send data from IoT Platform
Data updates of device shadows from IoT Platform are listened to when an application controls a device. When an application controls a device, IoT Platform sends device shadow update data to the device. After the device receives the update data, it starts the update based on the desired value.
// Listen to device shadow updates from IoT Platform.
LinkKit.getInstance().getDeviceShadow().setShadowChangeListener(new IShadowRRPC() {
@Override
public void onSubscribeSuccess(ARequest aRequest) {
// The subscription to device shadow data from IoT Platform is successful.
}
@Override
public void onSubscribeFailed(ARequest aRequest, AError aError) {
// The subscription to the device shadow data from IoT Platform failed.
}
@Override
public void onReceived(ARequest aRequest, AResponse aResponse, IConnectRrpcHandle iConnectRrpcHandle) {
// Data is received from IoT Platform. The downstream data is in aResponse.
try {
if (aRequest ! = null) {
String dataStr = null;
if (aResponse.data instanceof byte[]) {
dataStr = new String((byte[]) aResponse.data, "UTF-8");
} else if (aResponse.data instanceof String) {
dataStr = (String) aResponse.data;
} else {
dataStr = String.valueOf(aResponse.data);
}
// Log.d(TAG, "dataStr = " + dataStr);
// The sample response.
//{"method":"control","payload":{"state":{"desired":{"mode":2,"color":"white"},"reported":{"mode":"1","color":"red"}},"metadata":{"desired":{"mode":{"timestamp":1547642408},"color":{"timestamp":1547642408}},"reported":{"mode":{"timestamp":1547642408},"color":{"timestamp":1547642408}}}},"timestamp":1547642408,"version":12}
// The sample response is used for reference only.
ShadowResponse<String> shadowResponse = JSONObject.parseObject(dataStr, new TypeReference<ShadowResponse<String>>() {
}.getType());
if (shadowResponse ! = null && shadowResponse.version ! = null && TextUtils.isDigitsOnly(shadowResponse.version)) {
version = Long.valueOf(shadowResponse.version);
}
AResponse response = new AResponse();
// TODO: Control the device.
// Report the shadow value to IoT Platform after you control the device.
// Report the updated value to IoT Platform.
// Report the current actual value to IoT Platform.
response.data = shadowUpdate.replace("{ver}", String.valueOf(version + 1));
// The replyTopic parameter uses a default value. You do not need to set this parameter.
iConnectRrpcHandle.onRrpcResponse(null, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onResponseSuccess(ARequest aRequest) {
// The device shadow data is uploaded to IoT Platform after processing.
}
@Override
public void onResponseFailed(ARequest aRequest, AError aError) {
// The device shadow data failed to be uploaded to IoT Platform after processing.
}
});