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.
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";
The parameters are described as follows.
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.
In the list of My Cloud Projects, click Open Project in the Operation column.
Click the Message Service tab and 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.
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
.
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
}"
}]
}
After the message service is enabled, all types of messages can be received.
Perform the following troubleshooting steps:
The service is billed based on the number of forwarded messages. For more information, see Pricing.
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback