Get Push Messages by Pulsar (Java SDK)

Last Updated on : 2024-06-20 06:56:06download

Tuya has customized and improved the message service based on the open source Pulsar. When a device has status changes, such as device registration, data reporting, and going offline, Tuya will send notifications of these changes. You can timely get device messages to analyze or store them. This topic describes how to use Tuya’s Pulsar SDK to receive messages.

Preparations

  • Download the GitHub demo: Pulsar SDK.
  • Install the development environment: IntelliJ IDEA.
  • At least one cloud project is created. For more information, see Manage Projects.
  • Enable the message service. For more information, see Manage Message Service.
  • Download the Smart Life app to receive the test messages.
  • 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.

    Get Push Messages by Pulsar (Java SDK)
  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.
    Get Push Messages by Pulsar (Java SDK)
  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";
    

    The parameters are described as follows.

    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 Developer Platform.
    accessKey Enter the value of Access Secret in the Authorization Key section of the cloud project on the Tuya Developer Platform.
    Get Push Messages by Pulsar (Java SDK)

Step 2: Receive messages in the test environment

  1. Configure a test device.

    1. In the list of My Cloud Projects, click Open Project in the Operation column.

    2. Click the Message Service tab and enable the message service.

      Get Push Messages by Pulsar (Java SDK)
    3. After the subscription status shows Enabled, click the Test Environment tab.

      Get Push Messages by Pulsar (Java SDK)
    4. Enter the linked device ID in the search box, click the magnifier icon, and then double-click the selected device.

      Get Push Messages by Pulsar (Java SDK)
    5. 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.

    Get Push Messages by Pulsar (Java SDK)
  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.

    Get Push Messages by Pulsar (Java SDK)

Step 3: Parse the messages

Example 1: Turn on a light

  • Received message

    Message received: {
        "data": "mT9eqNz5MCgibKjEC+zsikNpoukkmad6K/uS18FozRETIriMEApJBXXm5o37QmMFzQtf9wFhkSQ6IBE2P3yYPrgKoKYnbLa+mN1TUCda/JYJWG9dqUPOlAUjyey1/PKjWu8doYvgN8fCe0HIMZBTI1eVfe4RzVkEHvaaFLVF+lpfqGdvTWhlLNbh6092SG9MsR8Aq2sXHt2naIfIT36xYU9xPHy0C65K3HDuQgwi69fRWhXdA1LWvsKPavTOappD",
        "protocol": 4,
        "pv": "2.0",
        "sign": "d6be290bd0ffa196c313ecd315e6ff72",
        "t": 1622701486365
    }, seq = 0, time = 1622701486373, consumed time = 1622701486652
    

    The following parameters are concatenated:

    • seq: the sequence ID of this message.
    • time: the timestamp when the client published this message.
    • consumed time: the timestamp when the system printed this message.
  • Parse the data

    the real message data: {
        "dataId": "642769b3-c434-11eb-87b5-02425b0322e7",
        "devId": "vdevo162201920765824",
        "productKey": "atxdggtv9ddjt8xx",
        "status": [{
            "code": "switch_led",
            "t": 1622701486258,
            "value": true,
            "20": "true"
        }]
    }
    

    For more information about the data parsing method, see Data Signature. The data decryption method has been encapsulated in the SDK. For more information, see mq.AESBase64Utils.java.

Example 2: Adjust the brightness of the light

  • Received message

    Message received: {
        "data": "UoHAZ1lTSAZRr0HFrePEw/rsMDlKAOIDeSjFQbf35K4TIriMEApJBXXm5o37QmMFzQtf9wFhkSQ6IBE2P3yYPrgKoKYnbLa+mN1TUCda/JYJWG9dqUPOlAUjyey1/PKjWu8doYvgN8fCe0HIMZBTI1eVfe4RzVkEHvaaFLVF+loWkfytt13z5nHmD9EPdmOZOxGcbyvjGOOp9iQOA3Om2XPur4DMxX0M+V+fFkyD9OXFRR9d0DA6J4LHmvDBtfO3U4HBd5MyELCdYJR7B+jCuVAcflFEGZKfv+5vINIuDZu0q1Ono+G6AcEyfmhIjrrY8EpmfMW/mWvfgGAreIOThH5WroVQpgbzIDgcGsXAsW1zzZYe27YlWKXleLLBEBcQx6Y2gSutLqfsu0MA59GNtg==",
        "protocol": 4,
        "pv": "2.0",
        "sign": "f055d71c2caae3549a53adddde16aaec",
        "t": 1622701494099
    }, seq = 1, time = 1622701494099, consumed time = 1622701494059
    
  • Parse the data

    the real message data: {
        "dataId": "68ce83d2-c434-11eb-87b5-02425b0322e7",
        "devId": "vdevo162201920765824",
        "productKey": "atxdggtv9ddjt8xx",
        "status": [{
            "code": "control_data",
            "t": 1622701494064,
            "28": "100b00193020800000000",
            "value": "{\"bright\":0,
                          \"change_mode\":\"gradient\",
                          \"h\":176,
                          \"s\":403,
                          \"temperature\":0,
                          \"v\":520
                          }"
        }]
    }
    

FAQs

Can specific types of messages be forwarded and received?

After the message service is enabled, all types of messages can be received.

Why device messages cannot be received?

Perform the following troubleshooting steps:

  1. Check whether the message service is enabled.
  2. Check the environment configurations of the Pulsar client.
  3. Check whether the device is added to the test device, but Pulsar is in the production environment.

Q: How is the message service billed?

The service is billed based on the number of forwarded messages. For more information, see Pricing.