Updated on 2024-04-29 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. 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.
    • Choose Devices > Policies in the navigation pane.
      Figure 1 Policy 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 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 Binding a policy to devices
    • 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 Viewing device 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 the button for connection authentication.
      3. Use the device to subscribe to the allowed topic /v1/test/hello. The subscription is successful.
        Figure 5 Successful subscription
      4. Use the device to subscribe to another topic /v2/test/hello. The subscription failed.
        Figure 6 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. 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. In the navigation pane, choose Rules > Data Forwarding.
      Figure 7 Data forwarding page
    • 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 8 Creating a data forwarding rule

      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 9 Setting the forwarding target
    • Click the button for enabling the rule.
      Figure 10 Enabling the rule
  3. Set a policy.
    • Choose Devices > Policies in the navigation pane.
      Figure 11 Policy page
    • Click Create Policy, set policy parameters, and click Generate, as shown in the following figure.
      Figure 12 Creating a policy
    • 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 13 Binding products
      Figure 14 Binding devices

  4. Verify the policy.
    1. Open two MQTT.fx tools to simulate device A (test111) and device B (test222) under product B (a product that is not selected in the preceding step).
    2. On the Subscribe page of device B, enter the topic /test/M2M/# and click Subscribe.
      Figure 15 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 16 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 17 Subscribe page of device B
    4. For devices (not device A) not belonging to product B, they cannot subscribe to or publish messages through the topic /test/M2M/#.
      Figure 18 Failed subscription