Last Updated on : 2024-06-20 06:23:34download
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.
Open the source code project with IntelliJ IDEA. Click Open to find the downloaded and decompressed file in the previous step.
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.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.
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. |
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.
On the IntelliJ IDEA, right-click the ConsumerExample.java
file and select Run to run the program.
Control the device with the Smart Life app.
Automatically get the push messages on IntelliJ IDEA.
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***"
}
After you use Tuya’s Pulsar SDK for Java to receive messages, you can further explore:
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.
Cause analysis: This error occurs because the subscription information does not exist.
Solution: Go to the Tuya Developer 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 Developer Platform to create a custom subscription name. For more information about specific steps, see Enable Message Service.
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.
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback