Integrate with Message Service

Last Updated on : 2023-08-23 05:58:09download

Tuya’s message service is customized and modified based on open source Pulsar. The Pulsar SDK for Java is provided. This topic describes how to use Tuya’s Pulsar SDK to receive messages.

Prerequisite

  • Download the GitHub demo: Pulsar SDK.
  • Install the development environment: IntelliJ IDEA.
  • Download the Smart Life app to receive the test messages.
  • At least one cloud project is created. For more information, see Manage Projects.
  • Prepare a Tuya-enabled smart device, such as a smart light, and link the device with your cloud project. For more information, see Link Devices.

Step 1: Set up a development environment

  1. Open the source code project with IntelliJ IDEA. Click Open to find the downloaded and decompressed file in the previous step.

    Integrate with Message Service
  2. Modify parameters in open-mq-sdk/src/main/java/com.tuya.open.sdk/mq/MqConsumer.java and modify the Java project as a test environment. For more information about the parameters, see mq/MqEnv.java.

    private MqEnv    env = MqEnv.TEST;
    
    • MqEnv.TEST represents the test environment.
    • MqEnv.PROD represents the production environment.
    Integrate with Message Service

  3. Modify parameters in open-mq-sdk/src/main/java/com.tuya.open.sdk/example/ConsumerExample.java.

    String url = MqConfigs.CN_SERVER_URL;
    String accessId = "xt*****ff**n1****8ufo";
    String accessKey = "479bcb7345******582d9c******4ef7";
    
    Parameter Description
    url The endpoint. Choose the endpoints according to your location.
    • CN_SERVER_URL: China, pulsar+ssl://mqe.tuyacn.com:7285/
    • US_SERVER_URL: America, pulsar+ssl://mqe.tuyaus.com:7285/
    • EU_SERVER_URL: Europe, pulsar+ssl://mqe.tuyaeu.com:7285/
    • IND_SERVER_URL: India, pulsar+ssl://mqe.tuyain.com:7285/
    For more information about the parameters, see mq/MqConfigs.java.
    accessId Enter the value of Access ID in the Authorization Key section of the cloud project on the Tuya IoT Development Platform.
    accessKey Enter the value of Access Secret in the Authorization Key section of the cloud project on the Tuya IoT Development Platform.
    Integrate with Message Service

Step 2: Receive messages in the test environment

  1. Configure a test device.

    1. On the Message Service page of the cloud project, enable the message service.

      Integrate with Message Service

    2. After the subscription status shows Enabled, click the Test Environment tab.

      Integrate with Message Service
    3. Enter the linked device ID in the search box, click the magnifier icon, and then double-click the selected device.

      Integrate with Message Service
    4. If a linked device is specified as a test device, the test device will report messages to the test channel for you to debug device features.

  2. On the IntelliJ IDEA, right-click the ConsumerExample.java file and select Run to run the program.

    Integrate with Message Service
  3. Control the device with the Smart Life app.

    1. Turn on the test light.
    2. Adjust the brightness of the light.
  4. Automatically get the push messages on IntelliJ IDEA.

    Integrate with Message Service

Step 3: Parse the messages

You can parse the messages you received in the following format. For more information about message types, see Message Types.

Parameter name Data type Description
protocol Integer The protocol number.
pv String The version of the specified communication protocol.
t Long The timestamp.
sign String The signature.
data String The data body.
{
   "protocol": 4,
   "pv": "2.0",
   "t": 146052438362,
   "data":"4FDEE3FE59Fxxxx***",
   "sign": "58285279b5b5790c7d917dxxxxx***"
}

Best practice

After you use Tuya’s Pulsar SDK for Java to receive messages, you can further explore:

Troubleshooting

Failed to authenticate

  • Cause analysis: This error occurs because the subscriber cannot be identified.

  • Solution:

    • If the ACCESS_ID or ACCESS_KEY is incorrect, you can check whether the ACCESS_ID key pair configured for the cloud project above is correct.

    • If you have not subscribed to the message service for the region the client is connected to, see Message Service.

      Integrate with Message Service

Subscription does not exist

  • Cause analysis: This error occurs because the subscription information does not exist.

  • Solution: Go to the Tuya IoT Development Platform to check whether the subscription name exists. Tuya provides a subscription name by default, and the naming rule is {AccessId}-sub. For example, 57e9trwr********s38-sub. However, you need to go to the Tuya IoT Development Platform to create a custom subscription name. For more information about specific steps, see Enable Message Service.

    Integrate with Message Service

Subscription is of different type

  • Full error message: Failed to subscribe 57e9trwr********s38/out/event with 11 partitions Subscription is of different type.

  • Cause analysis: This error occurs because a consumer of another subscription type already exists. Consumers of multiple subscription types cannot coexist.

  • Solution: To modify the subscription type of a consumer, you must close all consumers and start them again, or change a subscription name.

    Integrate with Message Service