1. Functions

You can integrate the IoTDA server SDKs provided by Huawei Cloud to call IoTDA APIs and use IoTDA smoothly. This example shows and demonstrates how to use the Python SDK to query and configure a device shadow as well as configure device shadows in batches.

Device Shadow: IoTDA supports the creation of device shadows. A device shadow is a JSON file that stores the device status, latest device properties reported, and device configurations to deliver. Each device has only one shadow. A device can retrieve and set its shadow to synchronize properties, either from the shadow to the device or from the device to the shadow. For details, see Device Shadow

What You Will Learn

The ways to use the Python SDK to configure and query a device shadow.

2. Prerequisites

    1. You have created a Huawei Cloud account and obtained the access key (AK) and secret access key (SK) of your account. To create and view an AK/SK, go to the My Credentials > Access Keys page of the Huawei Cloud console. For details, see Access Keys.
    1. You have set up the development environment with Python 3 and higher versions.
    1. You have obtained the project ID of the target region. View the project ID on the My Credentials > API Credentials page of the Huawei Cloud console. For details, see Obtaining a Project ID.
    1. You have enabled IoTDA. For details about how to obtain the ID of the current instance, see Viewing Instance Details.
    1. You have created a device and connected it to the platform for viewing interconnection details.

3. Obtaining and Installing the SDK

Execute the following commands to install Huawei Cloud Python SDK core library and the related service library.

# Install the core library
pip install huaweicloudsdkcore

# Install IoTDA service library
pip install huaweicloudsdkiotda

4. API Parameters

For details about API parameters, see:

Query a Device Shadow

Configure Desired Properties in a Device Shadow

Create a Batch Task

Query a Batch Task

5. Key Code Snippets

5.1 Configure Desired Values in a Device Shadow

    @staticmethod
    def __update_shadow(iotda_client, device_id, body):
        """
        Configure Desired Values in a Device Shadow

        @param iotda_client iotda client
        @param device_id    ID of the device whose device shadow needs to be configured
        @param body        Structure for configuring the device shadow
        """
        request = UpdateDeviceShadowDesiredDataRequest()
        request.device_id = device_id
        request.body = body
        request.instance_id = DeviceShadow.__INSTANCE_ID
        response = iotda_client.update_device_shadow_desired_data(request)
        print("The device shadow configuration result is:", response)

5.2 Query the Details About a Device Shadow

    @staticmethod
    def __query_shadow_detail(iotda_client, device_id):
        """
        Query the Details About a Device Shadow

        @param iotda_client iotda client
        @param device_id    Device ID
        """
        request = ShowDeviceShadowRequest()
        request.instance_id = DeviceShadow.__INSTANCE_ID
        request.device_id = device_id
        message_response = iotda_client.show_device_shadow(request)
        print("The device shadow query result is:", message_response)

5.3 Create a Batch Task

    @staticmethod
    def __create_task(iotda_client, create_batch_task):
        """
        Create a Batch Task

        @param iotda_client     iotda client
        @param createBatchTask Structure for a batch task creation
        @return The task obtained after the creation
        """
        request = CreateBatchTaskRequest()
        request.instance_id = DeviceShadow.__INSTANCE_ID
        request.body = create_batch_task
        batch_task = iotda_client.create_batch_task(request)
        print("The batch task creation result is:", batch_task)
        return batch_task

5.4 Query the Details About a Batch Task

    @staticmethod
    def __query_task(iotda_client, task_id):
        """
        Query the Details About a Batch Task

        @param iotda_client iotda client
        @param taskId      Task ID
        @return The details of the task
        """
        request = ShowBatchTaskRequest()
        request.instance_id = DeviceShadow.__INSTANCE_ID
        request.task_id = task_id
        show_batch_task_response = iotda_client.show_batch_task(request)
        print("The batch task details query result is:", show_batch_task_response)
        return show_batch_task_response.batchtask

6. Execution Results

Replace the parameters and execute the code in the sequence of the main method. The program demonstrates how to query and configure a device shadow as well as configure device shadows in batches. The execution results of each step of the main method are shown as follows.

6.1 Query the Details About a Device Shadow(Before the Device Shadow Configuration)

{
  "device_id": "64111bcd06ca5933b28a058b_7758dsfdsfsd",
  "shadow": [
    {
      "service_id": "BasicData",
      "desired": {
        "properties": {},
        "event_time": "20230829T083942Z"
      },
      "reported": {
        "properties": {
          "luminance": "2"
        },
        "event_time": "20230904T021317Z"
      },
      "version": 22
    }
  ]
}

6.2 Configure a Device Shadow

{
  "device_id": "64111bcd06ca5933b28a058b_7758dsfdsfsd",
  "shadow": [
    {
      "service_id": "BasicData",
      "desired": {
        "properties": {
          "luminance": 99
        },
        "event_time": "20230904T095758Z"
      },
      "reported": {
        "properties": {
          "luminance": "2"
        },
        "event_time": "20230904T021317Z"
      },
      "version": 23
    }
  ]
}

6.3 Query a Device Shadow(After the Device Shadow Configuration)

{
  "device_id": "64111bcd06ca5933b28a058b_7758dsfdsfsd",
  "shadow": [
    {
      "service_id": "BasicData",
      "desired": {
        "properties": {
          "luminance": 99
        },
        "event_time": "20230904T095758Z"
      },
      "reported": {
        "properties": {
          "luminance": "2"
        },
        "event_time": "20230904T021317Z"
      },
      "version": 23
    }
  ]
}

6.4 Create a Batch Task

{
  "task_id": "64f5bf901a98d00c657d2886",
  "task_name": "batchShadows_1",
  "task_type": "updateDeviceShadows",
  "targets": [
    "64111bcd06ca5933b28a058b_7758dsfdsfsd"
  ],
  "targets_filter": {},
  "document": {
    "shadow": [
      {
        "service_id": "BasicData",
        "desired": {
          "luminance": 100
        }
      }
    ]
  },
  "task_policy": {},
  "status": "Initializing",
  "task_progress": {
    "total": 0,
    "processing": 0,
    "success": 0,
    "fail": 0,
    "waitting": 0,
    "fail_wait_retry": 0,
    "stopped": 0,
    "removed": 0
  },
  "create_time": "20230904T112920Z"
}

6.5 Query the Details About a Batch Task

{
  "batchtask": {
    "task_id": "64f5bf901a98d00c657d2886",
    "task_name": "batchShadows_1",
    "task_type": "updateDeviceShadows",
    "targets": [
      "64111bcd06ca5933b28a058b_7758dsfdsfsd"
    ],
    "targets_filter": {},
    "document": {
      "shadow": [
        {
          "service_id": "BasicData",
          "desired": {
            "luminance": 100
          }
        }
      ]
    },
    "task_policy": {},
    "status": "Processing",
    "status_desc": "",
    "task_progress": {
      "total": 1,
      "processing": 1,
      "success": 0,
      "fail": 0,
      "waitting": 0,
      "fail_wait_retry": 0,
      "stopped": 0,
      "removed": 0
    },
    "create_time": "20230904T112920Z"
  },
  "task_details": [
    {
      "target": "64111bcd06ca5933b28a058b_7758dsfdsfsd",
      "status": "Processing",
      "error": {}
    }
  ],
  "page": {
    "count": 1,
    "marker": "64f5bf901a98d00c657d2888"
  }
}

6.6 Query the Details About a Device Shadow(After the Batch Task)

{
  "device_id": "64111bcd06ca5933b28a058b_7758dsfdsfsd",
  "shadow": [
    {
      "service_id": "BasicData",
      "desired": {
        "properties": {
          "luminance": 100
        },
        "event_time": "20230904T112930Z"
      },
      "reported": {
        "properties": {
          "luminance": "2"
        },
        "event_time": "20230904T021317Z"
      },
      "version": 24
    }
  ]
}

6.7 Delete the Desired Value of a Device Shadow

{
  "device_id": "64111bcd06ca5933b28a058b_7758dsfdsfsd",
  "shadow": [
    {
      "service_id": "BasicData",
      "desired": {
        "properties": {},
        "event_time": "20230904T112931Z"
      },
      "reported": {
        "properties": {
          "luminance": "2"
        },
        "event_time": "20230904T021317Z"
      },
      "version": 25
    }
  ]
}

6.8 Device Shadow Details Queried(After the Device Shadow Deletion)

{
  "device_id": "64111bcd06ca5933b28a058b_7758dsfdsfsd",
  "shadow": [
    {
      "service_id": "BasicData",
      "desired": {
        "properties": {},
        "event_time": "20230904T112931Z"
      },
      "reported": {
        "properties": {
          "luminance": "2"
        },
        "event_time": "20230904T021317Z"
      },
      "version": 25
    }
  ]
}