English
English
简体中文
Contact Us
Register
Log In
layoutIndex

Integrate with Message Service

Last Updated on : 2021-10-21 10:19:21download

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 demo source code

Click Pulsar SDK to enter the 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.
    Integrate with Message Service

  2. Modify parameters in open-mq-sdk/src/main/java/com.tuya.open.sdk/mq/MqConsumer.java. Modify the Java project as a test environment.

    private MqEnv	env = MqEnv.TEST;
    

    Integrate with Message Service

    Note: MqEnv.TEST represents the test environment. MqEnv.PROD represents the formal 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.

  • accessKey: Enter the value of Access Secret in the Authorization Key section.

  • url: Select an option for the area where the API operation is called. For more information about the parameters, see mq/MqConfigs.java.

    • 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/

    Integrate with Message Service

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

  1. Configure a test device.

    1. On the page of Message Service, after your subscription is ready, Subscribed appears in the Status column. Click the Message Test tab.

      Integrate with Message Service

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

    3. If an associated device is set as a test device, the message reported by it will be sent to the test channel for you to debug device functions.

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

    Integrate with Message Service

  3. Control the device with a mobile app.

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

    Integrate with Message Service

Data attribute and example

For more information about message types, see Message Types.

Parameter name Data type Description
protocol Integer The protocol number.
pv String The version number of the 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 practices