Message Queue

Last Updated on : 2024-06-20 07:20:05download

The Smart Business Platform actively pushes event data to external partners through Pulsar, which meets their demands for real-time and persistent messages.

Note:If you need to use the device message subscription, you should develop the client through the OEM App or App SDK. Devices that use the Light+Space App to pair the network do not support push messages to developers at present.

Pulsar

Pulsar is a multi-tenant, high-performance solution for server-to-server messaging. Pulsar was originally developed by Yahoo and now is under the stewardship of the Apache Software Foundation. Tuya Smart Message Service is customized and built based on Apache Pulsar. Message Service integration can be achieved by the Pulsar SDK.

Publish-subscribe (Pub/Sub)

As a message broker, Pulsar is built on the Pub/Sub pattern. In this pattern, producers publish messages to topics. Consumers subscribe to those topics, process incoming messages, and send an acknowledgment when processing is completed.

When a subscription is created, Pulsar retains all messages, even if the consumer is disconnected. Retained messages are discarded only when a consumer acknowledges that those messages are processed successfully.
Moreover, multiple consumers can subscribe to one topic. When messages are processed successfully, consumers need to send acknowledgments, so brokers can discard these messages. The Pulsar brokers provided by Tuya Smart allocate multiple partitions for each topic. Pulsar brokers send messages according to partitions and consumers.

Security

  • Authentication security:
    Tuya Smart Pulsar Message Service adopts a highly customized authentication algorithm and dynamic tokens to safeguard your operation. You can skip the implementation details and accomplish the authentication based on the provided SDK.

  • Data security:
    Transmission security: Tuya Smart Pulsar Message Service transmits data based on the SSL.
    Business security: Business data is encrypted with AES-128 and attached with a signature.

Procedure

You can submit your account and clientId (accessId) to the technical support of the Smart Business Platform to subscribe to messages.

Request method

For more information, see ConsumerExample. The following data must be provided:

  • Username: Enter the Access ID of cloud API authorization that you get from the Tuya Developer Platform.
  • Password: Enter the Access Secret of cloud API authorization that you get from the Tuya Developer Platform.
  • URL (Partners choose the URL to be accessed according to their needs):
    • China: pulsar+ssl://mqe.tuyacn.com:7285/
    • America: pulsar+ssl://mqe.tuyaus.com:7285/
    • Europe: pulsar+ssl://mqe.tuyaeu.com:7285/

Sample code

String url = "";
String accessId = "";
String accessKey = "";
MqConsumer mqConsumer = MqConsumer.build()
    .serviceUrl(url)
    .accessId(accessId)
    .accessKey(accessKey)
    .maxRedeliverCount(3)
    .messageListener(new MqConsumer.IMessageListener() {
     @Override
     public void onMessageArrived(Message message) throws Exception {
       // Write your own message processing logic
     }
    });
mqConsumer.start();

Data signature

Before you get the real data, you can set the tamper-proof MD5 signature verification. The signature algorithm is processed as below:

  1. Format the received JSON parameters (except for sign and the parameters with an empty value) into key=val.
  2. Assemble according to key in ascending order. Format example of the assembled string: k1=v1||k2=v2.
  3. Add the key. For example: k1=v1||k2=v2...kn=vn||key.
  4. Generate the MD5 signature of the final string.
  5. If the MD5 signature value is the same as the sign value, it means the data has not been tampered with. Otherwise, the data has been tampered with.

The data is decoded after the signature is checked.

  1. Decode the Base64 encoded data.
  2. Decrypt the middle 16-bit code of the accessKey through the AES (ECB mode) to get the real device status data.

Data format

Message format

Name Type Description
encryptPayload String Ciphertext
sign String The signature that is generated by a specified signature algorithm
encryptType String Encryption type
t Long A 13-bit standard timestamp
v String Version number

Sample message

{
  "encryptType": "aes_ecb",
  "v": "1.0",
  "t": 1588918073598,
  "encryptPayload":"4FDEE3FE59FCD76E260******33A478EB2C2004EF4289276****",
  "sign": "4b7084b3b87b550c0d967f7736e9****"
}

Message body format

Name Type Description
bizCode String Service type
eventType String Event type
data JSON Message data

Description of service type

bizCode Description
device Push device events
voice Push voice-enabled services
construction Push construction services

Sample message body

{
  "bizCode": "device",
  "eventType": "dp_report",
  "data": {}
}

Device event type

eventType Description
bind Bind a device
unbind Unbind a device
reset Reset a device
online Go online
offline Go offline
dp_report Report the device status
name_update Change the device name

Sample message to bind a device

{
   "data":{
     "devId":"02200434dc4f221axxxx",
     "deviceName":"****socket****(wifi)_0",
     "gwId":"",
     "location":{
       "child":{
         "id":"1256152666585710592",
         "name":"****room where the test is carried out****"
       },
       "id":"1256148753241358336",
       "name":""
     },
     "namespace":"BgdBXzUhIg5x77770LL1OfpDxxxxx",
     "ownerId":"139960xx",
     "position":"****study room****",
     "productId":"qCY4YFq02AP0i1xx",
     "sub":false,
     "uid":"bay1588324079695kFxx",
     "uuid":"02200434dc4f221a6xxx"
   },
   "bizCode":"device",
   "eventType":"bind"
 }

Sample message to unbind a device

{
   "data":{
     "devId":"02200434dc4f221xxxxx",
     "namespace":"****Your****namespace"
   },
   "bizCode":"device",
   "eventType":"unbind"
 
}

Sample message to reset a device

{
   "data":{
     "devId":"02200434dc4f221xxxxx",
     "namespace":"****Your****namespace"
   },
   "bizCode":"device",
   "eventType":"reset"
 
}

Sample message to put a device online

{
   "data":{
     "devId":"02200434dc4f221axxxx",
     "namespace":"****Your****namespace",
     "time":1589019224062
   },
   "bizCode":"device",
   "eventType":"online"
}

Sample message to put a device offline

{
   "data":{
     "devId":"02200434dc4f221a6120",
     "namespace":"****Your****namespace",
     "time":1589019224062
   },
   "bizCode":"device",
   "eventType":"offline"
}

Sample message to report the device status

{
  "data":{
  "devId": "002dj00118fe34xxxxx",
  "productId": "The product ID defined on the Tuya Developer Platform",
  "dataId": "145xxxxxxxxxxxxxxxxxxx",// The global unique ID, indicating the reporting of single data
  "namespace":"Your namespace",
  "status": [
    {
      "code": "switch_1",
      "value": "true",
      "time": 1589767589137
    }
  ]
},
  "bizCode":"device",
  "eventType":"dp_report"
}

Sample message to change the device name

{
   "data":{
     "devId":"02200434dc4f221axxxx",
     "deviceName":"****socket****",
     "namespace":"****Your****namespace"
   },
   "bizCode":"device",
   "eventType":"name_update"
}

Voice-enabled event type

eventType Description
voice_nlu Push the speech recognition result
voice_room_bind Push the binding result of a speaker and a room

Sample message to push the speech recognition result

{
  "data":{
  "devId":"6cd40b4ed084ac264bmcvu",
  "namespace":"BgdBXzUhIg5x77770LL1OfpDwXj6b",
  "nlu":"[{\"entities\":[{\"value\":\"TV\",\"entity\":\"deviceName\"}], \"domain\":\"mediaControl\",\"confidence\":1,\"text\":\"Pause the TV\",\"intent\":\"pause\"}]",
  "productId":"fabzep46",
  "uid":"bay1588324079695kF7G",
  "uuid":"f572ecc09e19d8c1"
},
  "bizCode":"voice",
  "eventType":"voice_nlu"
}