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.
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
.
Code: Select all
private MqEnv env = MqEnv.TEST;
:::info
MqEnv.TEST
represents the test environment.MqEnv.PROD
represents the production environment.
3. Modify parameters in open-mq-sdk/src/main/java/com.tuya.open.sdk/example/ConsumerExample.java
.
Code: Select all
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.<ul><li> CN_SERVER_URL : China, pulsar+ssl://mqe.tuyacn.com:7285/ </li><li> US_SERVER_URL : America, pulsar+ssl://mqe.tuyaus.com:7285/ </li><li> EU_SERVER_URL : Europe, pulsar+ssl://mqe.tuyaeu.com:7285/ </li><li> IND_SERVER_URL : India, pulsar+ssl://mqe.tuyain.com:7285/ </li></ul>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. |
Step 2: Receive messages in the test environment
1. Configure a test device.
On the Message Service page of the cloud project, enable the message service.
After the subscription status shows Enabled, click the Test Environment tab.
Enter the linked device ID in the search box, click the magnifier icon, and then double-click the selected device.
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.
3. Control the device with the Smart Life app.
- Turn on the test light.
- Adjust the brightness of the light.
4. Automatically get the push messages on IntelliJ IDEA.
Step 3: Configure the production environment message push rule
For details, please visit Manage Message Service.
Step 4: 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. |
Code: Select all
{
"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
orACCESS_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.
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.
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.