Get Push Messages by Pulsar (Python SDK)

Last Updated on : 2023-08-23 05:58:09

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.

Prerequisites

Procedure

Currently, Tuya Smart Message Service is customized and modified based on open source Pulsar. The Pulsar SDK for Python is provided.
In this topic, the Python development tool PyCharm is used to receive messages.

Step 1: Download demo source code

Click Pulsar SDK to download the package to the local computer and decompress the package.

Step 2: Set up the test environment

  1. Use PyCharm to open the source code project: Click Open to find the downloaded and decompressed file in the previous step.

    Get Push Messages by Pulsar (Python SDK)

  2. Add dependency environment.

    1. py2 version dependency
      • websocket-client version 0.59.0
      • pycrypto version 2.6.1
    2. py3 version dependency
      • websocket-client version 1.0.1
      • pycrypto version 2.6.1

    Note: The Python version for PC used in this topic is 3.8.

    Get Push Messages by Pulsar (Python SDK)

  3. View dependency package.

    Click PyCharm > Preferences on the top navigation bar to view the project environment at Project:python > Python Interpreter.

    Get Push Messages by Pulsar (Python SDK)

Step 3: Configure project parameters

Modify the parameters in the example/index.js file. This topic takes the test channel as an example.

# accessId, accessKey, serverUrl, MQ_ENV
ACCESS_ID = "xt*****ff**n1****8ufo"
ACCESS_KEY = "479bcb7345******582d9c******4ef7"
WSS_SERVER_URL = "wss://mqe.tuyacn.com:8285/"
MQ_ENV = MQ_ENV_PROD

The parameters are described as follows.

  • ACCESS_ID: Enter the value of Access ID in the Authorization Key section.

  • ACCESS_KEY: Enter the value of Access Secret in the Authorization Key section.

  • WSS_SERVER_URL: Choose the endpoints according to your location.

    • China Data Center: wss://mqe.tuyacn.com:8285/
    • Western America Data Center: wss://mqe.tuyaus.com:8285/
    • Central Europe Data Center: wss://mqe.tuyaeu.com:8285/
    • India Data Center: wss://mqe.tuyain.com:8285/
  • MQ_ENV: message subscription channel.

    • MQ_ENV_PROD represents the formal environment.
    • MQ_ENV_TEST represents the test environment.

    Get Push Messages by Pulsar (Python SDK)

Step 4: Operate a device on the app and receive messages in the test environment

  1. Configure a test device.

    1. On the page of Message Service, after your subscription is ready, Subscribed appears in the Status column. Click the Message Test tab.
      Get Push Messages by Pulsar (Python SDK)

    2. Enter the associated device ID in the search box, click the magnifier icon, and then double-click the selected device.

      Get Push Messages by Pulsar (Python SDK)

    3. If an associated device is set as a test device, the message reported by it will be sent to the test channel for you to debug device functions.

  2. On the development tool PyCharm, right-click the pythonWssConsumerDemo.py file and select RUN to run it.

    Get Push Messages by Pulsar (Python SDK)

  3. Control the device with a mobile app.

    1. Turn on the switch of the test light.
    2. Adjust the brightness of the light.
  4. Automatically get the push messages on PyCharm.

    Get Push Messages by Pulsar (Python SDK)

  5. For more information about field descriptions, see Data format description and usage notice.

Parse push messages

Turn on the light

  • Received message

    received message origin payload: {"data":"5dBU8WiRS1+zydoFTtaVKuL+DnRGSSj1VvrRH4pN1kvawIq2mG/fZTd9TCimqpGPzQtf9wFhkSQ6IBE2P3yYPj6CrsqFEQvL9PFeHpD9Z9KadznFwSQX6sgRa2f2ANFHDTC8pZ0+fLnWge7AqPG4LdxobOPC7kNpk92hmXjB0PEOZ2Du4MbP4NSN49QcJ4MPoKVOkbMxkxYUbynUW5fQ6FgHYZYcTUp+a0vF/oS3VL3jGH5tallpI10RZxofJ72f","protocol":4,"pv":"2.0","sign":"b8a548131304b49b524fca193f9ffd63","t":1630979005761}
    
  • Parse payload data

    decryptContentData={"dataId":"fe3a7d80-0f7c-11ec-8511-024283c73485","devId":"vdev******86991","productKey":"na****jz","status":[{"1":"true","code":"switch_led","t":1630979005725,"value":true}]}
    

    Note: For more information about the data parsing method, see Data signing. The payload decryption method has been encapsulated in the SDK.

Adjust the brightness of the light

  • Received message

    received message origin payload: {"data":"z/xrNsO26LmxprqP0OaMSXdSXy2jAEiz0blUCnlUZe7awIq2mG/fZTd9TCimqpGPzQtf9wFhkSQ6IBE2P3yYPj6CrsqFEQvL9PFeHpD9Z9KadznFwSQX6sgRa2f2ANFHDTC8pZ0+fLnWge7AqPG4LSlV4VEv7NtZ0T9wsM0xsQe+Gd3lcDRjtoWkeHTaIhvNzw8Y01IymwbZVRlVvpO3bLeIRohJoYtnI0lpflt6tcoKE7H3tlNNyLhx/y903ldy7z0o3iD5KROUiGtsAvdO0WfAsVxl87x4EbH6A7FLPOR2vyfiUckMP3+KlQR/CA6UtuuExvaaSNCeSx8NL2Rpmw==","protocol":4,"pv":"2.0","sign":"7ce23e6fb11386020cc4a58524cf2a4e","t":1630979195061}
    
  • Parse the data

    decryptContentData={"dataId":"6f0f12a7-0f7d-11ec-8511-024283c73485","devId":"vdev******86991","productKey":"na****jz","status":[{"2":"white","code":"work_mode","t":1630979195023,"value":"white"},{"3":"764","code":"bright_value_v2","t":1630979195023,"value":764}]}