English
English
简体中文
Contact Us
Register
Log In
Language
English
English
简体中文
Contact Us
Log In
Register
Go to main website
App Develop

App Development Platform

App development platform provides multiple development methods such as no-code or IoT App SDK development to maximize the monetization of IoT apps.
layoutIndex

Message Queue

Last Updated on : 2021-07-20 02:55:13download

Message Service is used to actively push the event data to external partners through Pulsar, which meets their demands for real-time and persistent messages.

Pulsar

Overview

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 modified based on Apache Pulsar. Message Service integration can be achieved with the Pulsar SDK provided by Tuya.
As a message broker, Pulsar is built on the Pub/Sub (publish and subscribe) 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 the same topic. After messages are processed, consumers need to send acknowledgments to brokers, 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.

Secured

  • Authentication security:
    Tuya Smart 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 Smart Pulsar Message Service transmits data based on the SSL.
    Business security: Business data is encrypted with AES-128 and requests are signed

Request method

For more information, see ConsumerExample. The following three parts of data are required for integration:

  • username: Enter the Access ID of the Authorization Key generated in the Cloud Development Platform.
  • password: Enter the Access Secret of the Authorization Key generated in the Cloud Development Platform.
  • url: Choose the endpoints according to your location.
    • China: pulsar+ssl://mqe.tuyacn.com:7285/
    • America: pulsar+ssl://mqe.tuyaus.com:7285/
    • Europe: pulsar+ssl://mqe.tuyaeu.com:7285/
    • India: pulsar+ssl://mqe.tuyain.com:7285/

Procedure

For more information, see Message Service.

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 format

Parameter Data type Description
protocol Integer Protocol number.
pv String The version of communication protocol.
t Long Timestamp.
sign String Signature.
data String Data.
{
   "protocol": 4,
   "pv": "2.0",
   "t": 146052438362,
   "data":"4FDEE3FE59FCD7****D1B638BB633A478EB2C2004EF4289276****",
   "sign": "58285279b5b5790c7d917de88b3e****"
}

Data signing

  1. Before you get the real data, you can set the tamper-proof MD5 signature verification. The signature algorithm is processed in the following steps:

    1. Format the received JSON parameters (except for sign and the parameters with an empty value) into key=val.
    2. Assemble (in ascending order of key). 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. Otherwise, the data has been tampered.
  2. 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.

    The decoded data is in the following format:

    {
        "devId": "002dj00118fe34d9****",
        "productKey": "The product key defined in the Cloud Development Platform",
        "dataId":"1459168450ddfdfoiopiopi****",// Global unique ID for data reporting
        "status": [
        {
          "code":"switch",
          "value":false,
          "mode":"rw",
          "t":146052438362
        },
        {
          "code":"work_mode",
          "value":"colour",
          "mode":"rw",
          "t":146052438362
        }
      ]
    }
    

Note: You can get all the current status information of the device by RESTful API /v1.0/devices/{device_id}/status.

Business data

Protocol number

Different protocol numbers stand for different functions.

Protocol number Description
4 Device data reporting event.
20 Go online.
20 Go offline.
20 Modify device name.
20 Modify data point name.
20 Bind the device with users’ accounts.
20 Remove a device.
20 Device update status.
22 Automate an external action.
25 Scenes.

Device data reporting event

{
  "devId": "002dj00118fe34d9****",
  "productKey": "The product key defined in the Cloud Development Platform",
  "dataId": "1459168450ddfdfoiopiopi****",
  "status": [
    {
      "Data point 1 (the data point code in product definition)": "The value of data point 1",
      "t": 1540615024283  // The time when the status of the data point changed.
    },
    {
      "Data point 2 (the data point code in product definition)": "The value of data point 2",
      "t": 1540615024283 // The time when the status of the data point changed.
    }
  ]
}

Go online

  • bizData description

    code Data type Description
    time Long Timestamp.
  • Example

    {
      "devId": "002dj00118fe34d9****",
      "productKey": "The product key defined in the Cloud Development Platform",
      "bizCode": "online",
      "bizData": {
        "time": 146052438362
      }
    }
    

Go offline

  • bizData description

    code Data type Description
    time Long Timestamp
  • Example

      {
        "devId": "002dj00118fe34d9****",
        "productKey": "The product key defined in the Cloud Development Platform",
        "bizCode": "offline",
        "bizData": {
          "time": 146052438362
        }
      }
    

Change the device name

  • bizData description

    code Data type Description
    devId String The device ID.
    name String The device name.
  • Example

    {
      "devId": "002dj00118fe34d9****",
      "productKey": "The product key defined in the Cloud Development Platform",
      "bizCode": "nameUpdate",
      "bizData": {
        "devId": "002dj00118fe34d9****",
        "name": "new name"
      }
    }
    

Change DP name

  • bizData description

    code Data type Description
    devId String The device ID.
    name String The DP name.
    dpId String The DP ID.
  • Example

    {
      "devId": "002dj00118fe34d9****",
      "productKey": "The product key defined in the Cloud Development Platform",
      "bizCode": "dpNameUpdate",
      "bizData": {
        "devId": "002dj00118fe34d9****",
        "name": "new name",
        "dpId": "dpId"
      }
    }
    

Bind a device

  • bizData description

    code Data type Description
    devId String The device ID.
    uuid String The universally unique identifier.
    uid String The user account bound to the device.
    token String The device pairing token.
  • Example

    {
      "devId": "002dj00118fe34d9****",
      "productKey": "The product key defined in the Cloud Development Platform",
      "bizCode": "bindUser",
      "bizData": {
        "devId": "002dj00118fe34d9****",
        "uuid": "06200043b4e618c1****",
        "uid":"ay1529485403390****",
        "token": "IIpQ****"
      }
    }
    

Remove a device

  • bizData description

    code Data type Description
    devId String The device ID.
    uid String The user account that is bound to the device.
  • Example

    {
      "devId": "002dj00118fe34d9****",
      "productKey": "The product key defined in the Cloud Development Platform",
      "bizCode": "delete",
      "bizData": {
        "devId": "002dj00118fe34d9****",
        "uid":"ay1529485403390S****"
      }
    }
    

Device update status

  • bizData description

    code Data type Description
    devId String The device ID.
    moduleType Int Module types:
    • 0: Wi-Fi
    • 1: Bluetooth Low Energy (Bluetooth LE)
    • 2: GPRS
    upgradeStatus Int Update status:
    • 1: Prepare to update
    • 2: Updating
    • 3: Update completed
    • 4: Failure
    description String Description of update.
    oldVersion String Earlier firmware.
    newVersion String Later firmware.
  • Example

    {
        "bizCode":"upgradeStatus",
        "bizData": {
            "devId":"6ca8756d*****1b4ewsdn",  
            "moduleType":0,
            "upgradeStatus":2,
            "description":"Update test",
            "oldVersion": "1.0.1",
            "newVersion": "1.0.2"         
        },
        "devId":"6ca8756d*****1b4ewsdn",
        "productKey":"vFHpaEFwu8UD****",
        "ts":1562232522192
    }
    

Run a scene

  • bizData description

    code Data type Description
    name String Scene name.
    id String Scene ID.
    gid Long Home ID.
    uid String The UID of the user who made the request.
    ts Long The timestamp of message delivery.
    actions.entityId String Entity ID.
    actions.execStatus Integer The status of the action:
    • -1: default
    • 0: failed
    • 1: succeeded
    • 2: to be executed
    • 3: no execution required
    actions.executeTime Long The timestamp of execution.
    actions.id String Action ID.
    actions.errorMsg String Error message.
  • Example

    {
       "bizData":{
        "name":"Switch-On",
        "id":"azmQ2w8kPSCdcrxxx",
        "actions":[
            {
                "entityId":"02167073ccxxxx",
                "execStatus":1,
                "executeTime":1625798044277,
                "id":"1jPxz3zaNdLxxxx"
            }
        ]
      },
     "gid":37081848,
     "uid":"ay1622715182388Nxxxx",
     "ts":1625798044298
    }
    

Automate an external action

  • bizData description

    code Data type Description
    automationId String Automation ID
    bizData JSON The custom data, to be used with POST/v1.0/homes/{home_id}/automations/{automation_id}/conditions/trigger.
  • Example

    {
        "automationId":"Rc5XAKLSmwpxHxxx",//
        "bizCode":"automationExternalAction",
        "bizData":{
            "code":"mode",
            "value":2,
            "operator":"=="
        }
    }