Updated on 2024-11-06 GMT+08:00

Examples

Scenario 1: Allowing or Denying the Message Publishing Through a Specific Topic

  1. Create a product and device.
    • Create a product. Access the IoTDA service page and click Access Console. Click the target instance card. Choose Products in the navigation pane and click Create Product. Set the parameters as prompted and click OK. For details, see Creating a Product.
    • Create a device. On the IoTDA console, choose Devices > All Devices in the navigation pane, and click Register Device. Set the parameters as prompted and click OK. For details, see Registering an Individual Device.
  2. Create a policy.
    • Access the IoTDA service page and click Access Console. Click the target instance card.
    • Choose Devices > Policies in the navigation pane.
      Figure 1 Device policy - Access page
    • Click Create Policy, set policy parameters, and click Generate. The application scope of the policy is the resource space (appId). Resources starting with topic: indicate topics in MQTT communications and are used for publishing and subscription. In this example, the topic that can be published and subscribed to is /v1/test/hello.
      Figure 2 Device policy - Creating a policy
    • Bind the policy. In this example, set Target Type to Devices and select the devices to which the policy is to be bound.
      Figure 3 Device policy - Binding a device
    • Verify the policy.
      1. Obtain connection parameters. In the navigation pane, choose Devices > All Devices, find the devices bound to the policy in the preceding step, go to the device details page, and view the connection parameters.
        Figure 4 Device - Device details
        Figure 5 Device - Device details - MQTT connection parameters
      2. Use the MQTT.fx tool to connect to the cloud platform. Open the MQTT.fx tool, set authentication parameters for the devices bound to the policy in the preceding step, click Apply, and click Connect for connection authentication.
      3. Use the device to subscribe to the allowed topic /v1/test/hello. The subscription is successful.
        Figure 6 Successful subscription
      4. Use the device to subscribe to another topic /v2/test/hello. The subscription failed.
        Figure 7 Failed subscription

Scenario 2: Using Policy in E2E (M2M) Communications

In this example, you can enable device A under product A and all devices under product B to communicate with each other, and only allow them to subscribe to or publish messages through topics starting with /test/M2M/.

  1. Create a product and device.
    • Create a product. Access the IoTDA service page and click Access Console. Click the target instance card. Choose Products in the navigation pane and click Create Product. Set the parameters as prompted and click OK. For details, see Creating a Product.
    • Create a device. On the IoTDA console, choose Devices > All Devices in the navigation pane, and click Register Device. Set the parameters as prompted and click OK. For details, see Registering an Individual Device.
  2. Configure a data forwarding rule.
    • Access the IoTDA service page and click Access Console. Click the target instance card. In the navigation pane, choose Rules > Data Forwarding.
      Figure 8 Data forwarding - List
    • Click Create Rule, set the parameters as required, and click Create Rule. Set the SQL filter statement to STARTS_WITH(notify_data.body.topic,'/test/M2M/').
      Figure 9 Creating a forwarding rule - M2M

      For details about how to use SQL filter statements, see SQL Statements.

      STARTS_WITH(notify_data.body.topic,'/test/M2M/') indicates that data with topics starting with /test/M2M/ is filtered.

    • Set the forwarding target. Set Forwarding Target to Device, set Topic to $topic() (indicating that the forwarded topic remains unchanged and the original topic is delivered), and click OK.
      Figure 10 Creating a forwarding target - to a device
    • Click Enable Rule in the middle of the page.
      Figure 11 Enabling a rule - Forwarding data to a device
  3. Set a policy.
    • Choose Devices > Policies in the navigation pane.
      Figure 12 Device policy - Access page
    • Click Create Policy, set policy parameters, and click Generate, as shown in the following figure.
      Figure 13 Device policy - Creating a policy (M2M)
    • Bind the policy to the target products and devices. Set Target Type to Products and select the products to which the policy is to be bound. You can later modify the policy on the policy details page to add the devices to be bound.
      Figure 14 Device policy - Binding a product
      Figure 15 Device policy - Encoding a policy

  4. Verify the policy.
    1. Open two MQTT.fx tools to simulate device A (test111) under product A and device B (test222) under product B.
    2. On the Subscribe page of device B, enter the topic /test/M2M/# and click Subscribe.
      Figure 16 Entering a topic on the Subscribe page of device B
    3. Let device A send a message to device B. On the Publish page of device A, enter the topic /test/M2M/${Any word}. Enter the message to be sent (for example, hello) in the text box, and click Publish.
      Figure 17 Entering a message on the Publish page of device A

      On the Subscribe page of device B, you can see the received message, as shown in the following figure.

      Figure 18 Subscribe page of device B
    4. For devices not belonging to product B, they cannot subscribe to or publish messages through the topic /test/M2M/#.
      Figure 19 Failed subscription