Last Updated on: 2024-09-10 07:19:13
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.
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.
Visit the Pulsar SDK download page, click Code, download the package to the local computer, and then decompress the package.
Use IntelliJ IDEA to open the source code project: 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. Modify the Java project to a
test environment.
private MqEnv env = MqEnv.TEST;
MqEnv.TEST represents the test environment. MqEnv.PROD represents the production environment. For more information about the parameters, see mq/MqEnv.java.
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.
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
}"
}]
}