Get Push Messages by Pulsar (Java SDK)

Last Updated on : 2024-09-11 08:40:11

A cloud project provides the message subscription feature. If the device status in the project changes, such as registration, data reporting, and offline events, Message Service is used to actively push event data to external partners with Pulsar. This helps to implement real-time and persistent messages.

Prerequisites

  • You have created a project. For more information about project creation, see Quick Start.

Procedure

Currently, Tuya Smart Message Service is customized and modified based on open source Pulsar. The Pulsar SDK for Java is provided.
In this topic, the Java development tool IntelliJ IDEA is used to receive messages.

Step 1: Download the demo source code

Visit the Pulsar SDK download page, click Code, download the package to the local computer, and then decompress the package.

Step 2: Set up the test environment

  1. Use IntelliJ IDEA to open the source code project: 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. Modify the Java project to a test environment.

    private MqEnv	env = MqEnv.TEST;
    

    Get Push Messages by Pulsar (Java SDK)

    MqEnv.TEST represents the test environment. MqEnv.PROD represents the production environment. For more information about the parameters, see mq/MqEnv.java.

Step 3: Configure project parameters

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.

  • accessId: Enter the value of Access ID in the Authorization Key section on the Tuya IoT Development Platform.

  • accessKey: Enter the value of Access Secret in the Authorization Key section on the Tuya IoT Development Platform.

  • url: For more information about the parameters, see mq/MqConfigs.java. Be sure to use the URL of the specified domain name where the private cloud is deployed.

    Get Push Messages by Pulsar (Java SDK)

Step 4: Operate a device on the app and receive messages in the test environment

  1. The messages reported by the test device will be sent through the test channel to accelerate your device function debugging.

  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 a mobile 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)

  5. Display the data. For more information about field descriptions, see Message Types.

Parse push messages

Turn on the 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.

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

  1. Q: What is the application scenario of message subscription?
    A: When the device status changes, Tuya sends a notification of the change. You can timely get, analyze, or store device messages.
  2. Q: Can I forward and receive specific types of messages?
    A: After the message subscription is enabled, you will receive all types of messages.
  3. Q: Why cannot I receive device information?
    A: Perform the following troubleshooting steps:
    1. Check whether the message subscription is enabled.
    2. Check the environment configuration of the Pulsar client.
    3. Check whether the device is added to the test device and whether Pulsar runs in the production environment.