Device jobs
IoT Platform device jobs let you initiate tasks across multiple devices by product, device, or group. Configure Android Link SDK to receive custom tasks, execute jobs, and report task status.
Prerequisites
-
Products and devices are created, with device authentication credentials and access endpoints obtained. Create products and devices, View instance endpoint information.
Background information
-
The Device jobs overview covers device job configuration and workflow on IoT Platform.
-
The Alink protocol for device jobs describes the message topics and Alink data format.
Step 1: Initialize task management
LinkKit.getInstance().getTask().TaskBootsUp();
Step 2: Receive device job notifications
IoT Platform pushes job notifications to devices via the topic: /sys/{productKey}/{deviceName}/thing/job/notify.
Register the following listener to capture task notifications (requires Authentication and connection).
IConnectNotifyListener notifyListener = new IConnectNotifyListener() {
@Override
public void onNotify(String connectId, String topic, AMessage aMessage) {
// Callback for mobile terminated data.
// connectId: connection type. topic: mobile terminated topic. aMessage: mobile terminated data.
// Data parsing is as follows:
//String pushData = new String((byte[]) aMessage.data);
// pushData example: {"method":"thing.service.test_service","id":"123374967","params":{"vv":60},"version":"1.0.0"}
// method: service type. params: pushed data content.
}
@Override
public boolean shouldHandle(String connectId, String topic) {
// Select whether to skip processing mobile terminated data for a specific topic.
// If you skip a topic, onNotify will not receive mobile terminated data for that topic.
return true; //TODO: Configure this based on your needs.
}
@Override
public void onConnectStateChange(String connectId, ConnectState connectState) {
// The callback for connection state changes of the corresponding connection type. For specific connection states, see ConnectState in the SDK.
AppLog.d(TAG, "onConnectStateChange() called with: connectId = [" + connectId + "], connectState = [" + connectState + "]");
// The first connection to the cloud may fail. If the first connection fails, the SDK reports the ConnectState.CONNECTFAIL state. In this scenario, you can try to connect several times and then exit, or you can keep retrying until the connection is successful.
// TODO: The following code provides a reference implementation for active retries when the first connection fails. You can uncomment the following code to enable it.
// if(connectState == ConnectState.CONNECTFAIL){
// try{
// Thread.sleep(5000);
// PersistentNet.getInstance().reconnect();
// }catch (Exception e){
// AppLog.d(TAG, "exception is " + e);
// };
// AppLog.d(TAG, "onConnectStateChange() try to reconnect when connect failed");
// }
// After the SDK successfully connects to the cloud, if the connection is interrupted due to network fluctuations, the SDK reports the ConnectState.DISCONNECTED state. In this case, the SDK automatically retries the connection. The retry interval increases from 1s, 2s, 4s, 8s, up to 128s. After reaching the maximum interval of 128s, the SDK continues to retry at 128s intervals until the connection is successful.
}
}
// Register a listener for mobile terminated messages, including the persistent connection status and mobile terminated data.
LinkKit.getInstance().registerOnPushListener(notifyListener);
// Unregister the listener for mobile terminated messages. Make sure that the object is the same as the one used for registration.
// LinkKit.getInstance().unRegisterOnPushListener(notifyListener);
By default, the onNotify callback for mobile terminated data runs on the UI thread. Starting from version 1.7.3 of lp-iot-linkkit, you can use PersistentConnect.mNotifyReceivedMsgOnMainThread = false; to process mobile terminated messages on a non-UI thread. In scenarios with a high volume of mobile terminated messages or a busy UI thread, set this configuration item to false.
Step 3: Get tasks
Get the next task
LinkKit.getInstance().getTask().TaskGetNext(mConnectSendListener);
// Callback function for the request sent from the device to the cloud to determine if the send was successful
IConnectSendListener mConnectSendListener = new IConnectSendListener() {
@Override
public void onResponse(ARequest aRequest, AResponse aResponse) {
AppLog.d(TAG, "onResponse() called with: aRequest = [" + aRequest + "], aResponse = [" + (aResponse == null ? null : aResponse.data) + "]");
}
@Override
public void onFailure(ARequest aRequest, AError aError) {
AppLog.d(TAG, "onFailure() called with: aRequest = [" + aRequest + "], aError = [" + aError + "]");
}
};
Get task list
LinkKit.getInstance().getTask().TaskGetList(IConnectSendListener mConnectSendListener);
Get details of a specified task
LinkKit.getInstance().getTask().TaskGetDetail(String taskId, IConnectSendListener mConnectSendListener);
Step 4: Update task status
After executing the task locally, update IoT Platform with the execution status (success, failure, or abandonment) and progress percentage.
ITask.TaskDesc taskDesc = new ITask.TaskDesc();
taskDesc.setTaskId(taskId); // Must be set
taskDesc.setProgress(50); // Progress percentage, optional
taskDesc.setStatus(ITask.TASK_STATUS.IN_PROGRESS); // Must be set, task status
LinkKit.getInstance().getTask().TaskUpdateProgress(taskDesc);