Device Message Subscription

Last Updated on : 2023-01-12 07:13:33download

The Smart Industry Platform actively pushes event data to you through Pulsar, which meets your demands for real-time and persistent messages.

If you need to subscribe to device messages, you need to develop the client through the OEM app or App SDK. Devices that are paired by the Jizhi Lighting Control app do not support pushing messages.

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’s message service is customized and modified based on Apache Pulsar. Message service integration can be achieved with the Pulsar SDK provided by Tuya.

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 to brokers, so brokers can discard these messages. The Pulsar brokers provided by Tuya allocate multiple partitions for each topic and send messages to the specified partitions and consumers.

Security

  • Authentication security:
    Tuya’s Pulsar message service adopts a highly customized authentication algorithm and dynamic tokens to enhance security. You can ignore the implementation details and accomplish the authentication based on the provided SDK.

  • Data security:

    • Transmission security: Tuya’s Pulsar message service transmits data based on SSL.
    • Business security: Business data is encrypted with AES-128 and attached with a signature.

Subscription

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

Request method

The following data must be provided:

  • username: Enter the Access ID of cloud API authorization that you get from the Tuya IoT Development Platform.
  • password: Enter the Access Secret of cloud API authorization that you get from the Tuya IoT Development Platform.
  • url (You can choose the desired URL to be accessed):
    • China: pulsar+ssl://mqe.tuyacn.com:7285/
    • The U.S.A.: 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 in the following procedure:

  1. Format the received JSON parameters (except sign and the parameters with an empty value) into key=val.
  2. Assemble them in ascending order of key. Format example of the assembled string: k1=v1||k2=v2.
  3. Add the key. 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 is not tampered. Otherwise, the data is tampered.

The data is decrypted 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

Parameter Type Description
encryptPayload String The ciphertext of the message body.
sign String The signature that is generated by the specified signature algorithm.
encryptType String The encryption type.
t Long The 13-digit standard timestamp.
v String The version number.

Sample message

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

Message body format

Parameter Type Description
bizCode String The business type.
eventType String The event type.
data JSON The message data.

Description of business 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 Put a device online.
offline Put a device offline.
dp_report Report the device status.
name_update Rename a device.

Sample message to bind a device

{
   "data":{
     "devId":"02200434dc4f221a****",
     "deviceName":"****socket****(wifi)_0",
     "gwId":"",
     "location":{
       "child":{
         "id":"1256152666585710592",
         "name":"****room where the test is carried out****"
       },
       "id":"1256148753241358***",
       "name":""
     },
     "namespace":"BgdBXzUhIg5x77770LL1OfpD****",
     "ownerId":"139960xx",
     "position":"****study room****",
     "productId":"qCY4YFq02AP0i***",
     "sub":false,
     "uid":"bay1588324079695k***",
     "uuid":"02200434dc4f221a6***"
   },
   "bizCode":"device",
   "eventType":"bind"
 }

Sample message to unbind a device

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

}

Sample message to reset a device

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

}

Sample message to put a device online

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

Sample message to put a device offline

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

Sample message to report the device status

{
  "data":{
  "devId": "002dj00118fe34***",
  "productId": "The product ID defined on the Tuya IoT Development Platform",
  "dataId": "145************",// 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 rename the device

{
   "data":{
     "devId":"02200434dc4f221a***",
     "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":"6cd40b4ed084ac264bm***",
  "namespace":"BgdBXzUhIg5x77770LL1OfpDw*****",
  "nlu":"[{\"entities\":[{\"value\":\"TV\",\"entity\":\"deviceName\"}], \"domain\":\"mediaControl\",\"confidence\":1,\"text\":\"Pause the TV\",\"intent\":\"pause\"}]",
  "productId":"fabze***",
  "uid":"bay1588324079695k***",
  "uuid":"f572ecc09e19d***"
},
  "bizCode":"voice",
  "eventType":"voice_nlu"
}